Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_CuckooFilter.h
Go to the documentation of this file.
1
43#pragma once
44
47
70public:
71 // -------------------------------------------------------------
72 // Interface methods.
73 // -------------------------------------------------------------
74
75 bool isValid() override;
76
77 bool contains(const void* key, size_t keyLengthInBytes) override {
78 return contains(getExtendedFingerprint(key, keyLengthInBytes));
79 }
80
85 constexpr size_t size() { return size(bucketCount(), _data->nestsPerBucket); }
86
87 // -------------------------------------------------------------
88 // These might be useful for fingerprints coming from the mesh.
89 // -------------------------------------------------------------
90 bool add(cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex) {
91 return add(getExtendedFingerprint(finger, bucketIndex));
92 }
93
94 bool remove(cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex) {
95 return remove(getExtendedFingerprint(finger, bucketIndex));
96 }
97
98 bool contains(cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex) {
99 return contains(getExtendedFingerprint(finger, bucketIndex));
100 }
101
102 // -------------------------------------------------------------
103 // these might be useful for stuff that comes in from commands
104 // -------------------------------------------------------------
105
106 bool add(cuckoo_key_t key, size_t keyLengthInBytes) { return add(getExtendedFingerprint(key, keyLengthInBytes)); }
107
108 bool remove(cuckoo_key_t key, size_t keyLengthInBytes) {
109 return remove(getExtendedFingerprint(key, keyLengthInBytes));
110 }
111
117
118 // -------------------------------------------------------------
119 // Init/deinit like stuff.
120 // -------------------------------------------------------------
121
126
130 CuckooFilter() : _data(nullptr) {}
131
136 void clear();
137
147
148 // -------------------------------------------------------------
149 // Sizing helpers
150 // -------------------------------------------------------------
151
152 static constexpr size_t fingerprintCount(size_t bucketCount, cuckoo_index_t nestsPerBucket) {
153 return bucketCount * nestsPerBucket;
154 }
155
159 static constexpr size_t bufferSize(size_t bucketCount, cuckoo_index_t nestsPerBucket) {
160 return fingerprintCount(bucketCount, nestsPerBucket) * sizeof(cuckoo_fingerprint_t);
161 }
162
170 static constexpr size_t size(cuckoo_index_t bucketCount, cuckoo_index_t nestsPerBucket) {
171 return sizeof(cuckoo_filter_data_t) + bufferSize(bucketCount, nestsPerBucket);
172 }
173
177 constexpr size_t bucketCount() { return 1 << _data->bucketCountLog2; }
178
179 constexpr size_t bufferSize() { return bufferSize(bucketCount(), _data->nestsPerBucket); }
180
181private:
182 // -------------------------------------------------------------
183 // Compile time settings
184 // -------------------------------------------------------------
185
186 static constexpr size_t MAX_KICK_ATTEMPTS = 100;
187
188 // -------------------------------------------------------------
189 // Run time variables
190 // -------------------------------------------------------------
191
193
194 // -------------------------------------------------------------
195 // ----- Private methods -----
196 // -------------------------------------------------------------
197
205
213
218
223
227 cuckoo_fingerprint_t hashToBucket(cuckoo_key_t key, size_t keyLengthInBytes);
228
233 // TODO @ArrowAcrobatics: Please, check if there's no alignment bug here
234 return (cuckoo_fingerprint_t&)_data->bucketArray[(bucketIndex * _data->nestsPerBucket) + fingerIndex];
235 }
236
244
249
250 /*
251 * Puts the fingerprint in the filter, moving aside anything that is in the way unless it takes
252 * more than max_kick_attempts. return true on success, false when max kick attempts was
253 * reached. This doesn't check if the given fingerprint is already contained in the filter.
254 */
256
257 // -------------------------------------------------------------
258 // Core methods for manipulating this->data
259 // -------------------------------------------------------------
260
264};
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 02 Apr....
Definition: cs_CuckooFilter.h:69
CuckooFilter(cuckoo_filter_data_t *data)
Wraps a data struct into a CuckooFilter object.
Definition: cs_CuckooFilter.h:125
bool add(cuckoo_extended_fingerprint_t efp)
constexpr size_t size()
Total number of bytes this instance occypies.
Definition: cs_CuckooFilter.h:85
bool add(cuckoo_key_t key, size_t keyLengthInBytes)
Definition: cs_CuckooFilter.h:106
cuckoo_filter_data_t * _data
Definition: cs_CuckooFilter.h:192
void init(cuckoo_index_t bucketCount, cuckoo_index_t nestsPerBucket)
Sets the size parameters and then clear()s this object so that the bucket_array is filled with precis...
constexpr size_t bufferSize()
Definition: cs_CuckooFilter.h:179
bool removeFingerprintFromBucket(cuckoo_fingerprint_t fingerprint, cuckoo_index_t bucketIndex)
Returns true if found and removed, returns false if not found.
static constexpr size_t bufferSize(size_t bucketCount, cuckoo_index_t nestsPerBucket)
Size of the byte buffer in bytes.
Definition: cs_CuckooFilter.h:159
bool contains(const void *key, size_t keyLengthInBytes) override
Definition: cs_CuckooFilter.h:77
bool remove(cuckoo_key_t key, size_t keyLengthInBytes)
Definition: cs_CuckooFilter.h:108
bool move(cuckoo_extended_fingerprint_t entryToInsert)
bool add(cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex)
Definition: cs_CuckooFilter.h:90
bool remove(cuckoo_extended_fingerprint_t efp)
cuckoo_fingerprint_t & lookupFingerprint(cuckoo_index_t bucketIndex, cuckoo_index_t fingerIndex)
Returns a reference to the fingerprint at the given coordinates.
Definition: cs_CuckooFilter.h:232
static constexpr size_t MAX_KICK_ATTEMPTS
Definition: cs_CuckooFilter.h:186
bool contains(cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex)
Definition: cs_CuckooFilter.h:98
void clear()
Memsets the bucket_array to 0x00 and sets victim to 0.
cuckoo_extended_fingerprint_t getExtendedFingerprint(cuckoo_fingerprint_t fingerprint, cuckoo_index_t bucketIndex)
Expands a fingerprint and the index where it is placed into an extended fingerprint,...
static constexpr size_t fingerprintCount(size_t bucketCount, cuckoo_index_t nestsPerBucket)
Definition: cs_CuckooFilter.h:152
cuckoo_fingerprint_t hashToFingerprint(cuckoo_key_t key, size_t keyLengthInBytes)
Hashes the given key into a fingerprint.
cuckoo_extended_fingerprint_t getExtendedFingerprint(cuckoo_key_t key, size_t keyLengthInBytes)
Reduces a key (element) to an extended fingerprint, consisting of the fingerprint of the key and its ...
bool isValid() override
static constexpr size_t size(cuckoo_index_t bucketCount, cuckoo_index_t nestsPerBucket)
Total number of bytes a CuckooFilter with the given parameters would occupy.
Definition: cs_CuckooFilter.h:170
constexpr size_t bucketCount()
Actual bucket count value may be bigger than a cuckoo_index_t can hold.
Definition: cs_CuckooFilter.h:177
cuckoo_fingerprint_t filterHash()
A crc16 hash of this filters current contents (_data).
bool remove(cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex)
Definition: cs_CuckooFilter.h:94
cuckoo_compressed_fingerprint_t getCompressedFingerprint(cuckoo_key_t key, size_t keyLengthInBytes)
Reduces a key (element) to a compressed fingerprint, consisting of the fingerprint of the key and its...
CuckooFilter()
Use this with loads of care, _data is never checked to be non-nullptr in this class.
Definition: cs_CuckooFilter.h:130
bool contains(cuckoo_extended_fingerprint_t efp)
bool addFingerprintToBucket(cuckoo_fingerprint_t fingerprint, cuckoo_index_t bucketIndex)
Returns true if there was an empty space in the bucket and placement was successful,...
cuckoo_fingerprint_t hashToBucket(cuckoo_key_t key, size_t keyLengthInBytes)
Hashes the given key to obtain an (untruncated) bucket index.
Used in AssetFiltering as a generic way to query a filter for containment and assetId.
Definition: cs_FilterInterface.h:16
uint16_t cuckoo_fingerprint_t
Definition: cs_CuckooFilterStructs.h:14
const void * cuckoo_key_t
Definition: cs_CuckooFilterStructs.h:13
uint8_t cuckoo_index_t
Definition: cs_CuckooFilterStructs.h:15
Definition: cs_CuckooFilterStructs.h:30
Representation of an object (O) in this filter comprises of a fingerprint (F) and a the bucket index ...
Definition: cs_CuckooFilterStructs.h:24
Data content of the cuckoo filter.
Definition: cs_CuckooFilterStructs.h:38
cuckoo_fingerprint_t bucketArray[]
Definition: cs_CuckooFilterStructs.h:42
cuckoo_index_t bucketCountLog2
Definition: cs_CuckooFilterStructs.h:39
cuckoo_index_t nestsPerBucket
Definition: cs_CuckooFilterStructs.h:40