Skip to content

Commit c29e59e

Browse files
committed
Xor 12: fix fingerprint hash
1 parent abbadfa commit c29e59e

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

src/xorfilter_2.h

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,8 @@ inline uint32_t reduce(uint32_t hash, uint32_t n) {
3434
}
3535

3636
size_t getHashFromHash(uint64_t hash, int index, int blockLength) {
37-
uint32_t r;
38-
switch(index) {
39-
case 0:
40-
r = (uint32_t) (hash);
41-
break;
42-
case 1:
43-
r = (uint32_t) rotl64(hash, 21);
44-
break;
45-
default:
46-
r = (uint32_t) rotl64(hash, 42);
47-
break;
48-
}
49-
r = reduce(r, blockLength);
50-
r = r + index * blockLength;
51-
return (size_t) r;
37+
uint32_t r = rotl64(hash, index * 21);
38+
return (size_t) reduce(r, blockLength) + index * blockLength;
5239
}
5340

5441
template <typename ItemType, typename FingerprintType,
@@ -64,7 +51,7 @@ class XorFilter2 {
6451
HashFamily* hasher;
6552

6653
inline FingerprintType fingerprint(const uint64_t hash) const {
67-
return (FingerprintType) fingerprints->mask(hash);
54+
return (FingerprintType) hash ^ (hash >> 32);
6855
}
6956

7057
public:
@@ -331,10 +318,10 @@ Status XorFilter2<ItemType, FingerprintType, FingerprintStorageType, HashFamily>
331318
} else {
332319
// this is different from BDZ: using xor to calculate the
333320
// fingerprint
334-
xor2 ^= fingerprints->mask(fp[h]);
321+
xor2 ^= fp[h];
335322
}
336323
}
337-
fp[change] = xor2;
324+
fp[change] = fingerprints->mask(xor2);
338325
}
339326
fingerprints->bulkSet(fp, arrayLength);
340327

@@ -351,15 +338,15 @@ template <typename ItemType, typename FingerprintType,
351338
Status XorFilter2<ItemType, FingerprintType, FingerprintStorageType, HashFamily>::Contain(
352339
const ItemType &key) const {
353340
uint64_t hash = (*hasher)(key);
354-
FingerprintType f = hash;
341+
FingerprintType f = fingerprint(hash);
355342
uint32_t r0 = (uint32_t) hash;
356343
uint32_t r1 = (uint32_t) rotl64(hash, 21);
357344
uint32_t r2 = (uint32_t) rotl64(hash, 42);
358345
uint32_t h0 = reduce(r0, blockLength);
359346
uint32_t h1 = reduce(r1, blockLength) + blockLength;
360347
uint32_t h2 = reduce(r2, blockLength) + 2 * blockLength;
361348
f ^= fingerprints->get(h0) ^ fingerprints->get(h1) ^ fingerprints->get(h2);
362-
return fingerprint(f) == 0 ? Ok : NotFound;
349+
return fingerprints->mask(f) == 0 ? Ok : NotFound;
363350
}
364351

365352
template <typename ItemType, typename FingerprintType,

0 commit comments

Comments
 (0)