Post by Craig JamesPost by Haifeng Liuhash = hash * 31 + s.charAt(i)... but I got integer out of range error. How
can I avoid this? I saw java do not care overflow of int, it just make the
result negative.
hash = (hash & 67108863) * 31 + s.charAt(i);
Craig
Thank you, I believe your solution is OK for a hash function, but I am
aiming to create a hash function that is consistent with the one
applications use. I know postgresql 9.1 has a hash function called
hashtext, but I don't know what algorithm it use, and I also see that it's
not recommended to relay on it. So I am trying to create a hash function
which behaves exactly the same as java.lang.String.hashCode(). The later
one may generate negative hash value. I guess when the number is
overflowing, the part out of range will be ignored, and if the highest bit
get 1, the hash value turn to negative value.
You are probably doing something where you want the application and the
database to implement the exact same function, but if you stick to the Java
built-in function, you will only have control over one implementation of
that function. What happens if someone working on Java changes the how the
Java internals work?
A better solution would be to implement your own hash function in Postgres,
and then once you know exactly how it will work, re-implement it in Java
with your own code. That's the only way you can ensure consistency between
the two.
Craig