Skip to content

Commit 558c536

Browse files
Jim Posenjimpo
authored andcommitted
blockfilter: Implement GCSFilter Match methods.
1 parent cf70b55 commit 558c536

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/blockfilter.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,47 @@ GCSFilter::GCSFilter(uint64_t siphash_k0, uint64_t siphash_k1, uint8_t P, uint32
149149

150150
bitwriter.Flush();
151151
}
152+
153+
bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
154+
{
155+
VectorReader stream(GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0);
156+
157+
// Seek forward by size of N
158+
uint64_t N = ReadCompactSize(stream);
159+
assert(N == m_N);
160+
161+
BitStreamReader<VectorReader> bitreader(stream);
162+
163+
uint64_t value = 0;
164+
size_t hashes_index = 0;
165+
for (uint32_t i = 0; i < m_N; ++i) {
166+
uint64_t delta = GolombRiceDecode(bitreader, m_P);
167+
value += delta;
168+
169+
while (true) {
170+
if (hashes_index == size) {
171+
return false;
172+
} else if (element_hashes[hashes_index] == value) {
173+
return true;
174+
} else if (element_hashes[hashes_index] > value) {
175+
break;
176+
}
177+
178+
hashes_index++;
179+
}
180+
}
181+
182+
return false;
183+
}
184+
185+
bool GCSFilter::Match(const Element& element) const
186+
{
187+
uint64_t query = HashToRange(element);
188+
return MatchInternal(&query, 1);
189+
}
190+
191+
bool GCSFilter::MatchAny(const ElementSet& elements) const
192+
{
193+
const std::vector<uint64_t> queries = BuildHashedSet(elements);
194+
return MatchInternal(queries.data(), queries.size());
195+
}

src/blockfilter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class GCSFilter
3636

3737
std::vector<uint64_t> BuildHashedSet(const ElementSet& elements) const;
3838

39+
/** Helper method used to implement Match and MatchAny */
40+
bool MatchInternal(const uint64_t* sorted_element_hashes, size_t size) const;
41+
3942
public:
4043

4144
/** Constructs an empty filter. */

0 commit comments

Comments
 (0)