MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / dictScan

Function dictScan

src/dict.cpp:1249–1333  ·  view source on GitHub ↗

dictScan() is used to iterate over the elements of a dictionary. * * Iterating works the following way: * * 1) Initially you call the function using a cursor (v) value of 0. * 2) The function performs one step of the iteration, and returns the * new cursor value you must use in the next call. * 3) When the returned cursor is 0, the iteration is complete. * * The function guarantees all

Source from the content-addressed store, hash-verified

1247 * comment is supposed to help.
1248 */
1249unsigned long dictScan(dict *d,
1250 unsigned long v,
1251 dictScanFunction *fn,
1252 dictScanBucketFunction* bucketfn,
1253 void *privdata)
1254{
1255 dictht *t0, *t1;
1256 const dictEntry *de, *next;
1257 unsigned long m0, m1;
1258
1259 if (dictSize(d) == 0) return 0;
1260
1261 /* This is needed in case the scan callback tries to do dictFind or alike. */
1262 dictPauseRehashing(d);
1263
1264 if (!dictIsRehashing(d)) {
1265 t0 = &(d->ht[0]);
1266 m0 = t0->sizemask;
1267
1268 /* Emit entries at cursor */
1269 if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);
1270 de = t0->table[v & m0];
1271 while (de) {
1272 next = de->next;
1273 fn(privdata, de);
1274 de = next;
1275 }
1276
1277 /* Set unmasked bits so incrementing the reversed cursor
1278 * operates on the masked bits */
1279 v |= ~m0;
1280
1281 /* Increment the reverse cursor */
1282 v = rev(v);
1283 v++;
1284 v = rev(v);
1285
1286 } else {
1287 t0 = &d->ht[0];
1288 t1 = &d->ht[1];
1289
1290 /* Make sure t0 is the smaller and t1 is the bigger table */
1291 if (t0->size > t1->size) {
1292 t0 = &d->ht[1];
1293 t1 = &d->ht[0];
1294 }
1295
1296 m0 = t0->sizemask;
1297 m1 = t1->sizemask;
1298
1299 /* Emit entries at cursor */
1300 if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);
1301 de = t0->table[v & m0];
1302 while (de) {
1303 next = de->next;
1304 fn(privdata, de);
1305 de = next;
1306 }

Callers 8

scanGenericCommandFunction · 0.85
scan_threadsafeMethod · 0.85
scanLaterZsetFunction · 0.85
scanLaterSetFunction · 0.85
scanLaterHashFunction · 0.85
activeDefragCycleFunction · 0.85
RM_ScanFunction · 0.85
RM_ScanKeyFunction · 0.85

Calls 1

revFunction · 0.85

Tested by

no test coverage detected