Commit 742c6750 authored by Vitaly Arbuzov's avatar Vitaly Arbuzov
Browse files

Add comments for db iterator functions

parent a4b155fd
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
* Redis cluster data structures, defines, exported API. * Redis cluster data structures, defines, exported API.
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
#define CLUSTER_SLOT_MASK_BITS 14 /* Number of bits used for slot id.*/ #define CLUSTER_SLOT_MASK_BITS 14 /* Number of bits used for slot id. */
#define CLUSTER_SLOTS (1<<CLUSTER_SLOT_MASK_BITS) /* Total number of slots in cluster mode. */ #define CLUSTER_SLOTS (1<<CLUSTER_SLOT_MASK_BITS) /* Total number of slots in cluster mode, which is 16384. */
#define CLUSTER_SLOT_MASK ((unsigned long long)(CLUSTER_SLOTS - 1)) /* Bit mask for slot id stored in LSB. */ #define CLUSTER_SLOT_MASK ((unsigned long long)(CLUSTER_SLOTS - 1)) /* Bit mask for slot id stored in LSB. */
#define CLUSTER_OK 0 /* Everything looks ok */ #define CLUSTER_OK 0 /* Everything looks ok */
#define CLUSTER_FAIL 1 /* The cluster can't work */ #define CLUSTER_FAIL 1 /* The cluster can't work */
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
int expireIfNeeded(redisDb *db, robj *key, int flags); int expireIfNeeded(redisDb *db, robj *key, int flags);
int keyIsExpired(redisDb *db, robj *key); int keyIsExpired(redisDb *db, robj *key);
/* Returns next dictionary from the iterator, or NULL if iteration is complete. */
dict *dbNextDict(dbIterator *iter) { dict *dbNextDict(dbIterator *iter) {
while (iter->index < iter->db->dict_count - 1) { while (iter->index < iter->db->dict_count - 1) {
iter->index++; iter->index++;
...@@ -59,11 +60,7 @@ dict *dbNextDict(dbIterator *iter) { ...@@ -59,11 +60,7 @@ dict *dbNextDict(dbIterator *iter) {
return NULL; return NULL;
} }
dbIterator *dbGetIterator(redisDb *db) { void dbInitIteratorAt(dbIterator *dbit, redisDb *db, int slot) {
return dbGetIteratorAt(db, 0);
}
dbIterator *dbGetIteratorAt(redisDb *db, int slot) {
serverAssert(slot == 0 || server.cluster_enabled); serverAssert(slot == 0 || server.cluster_enabled);
dbIterator *iter = zmalloc(sizeof(*iter)); dbIterator *iter = zmalloc(sizeof(*iter));
iter->db = db; iter->db = db;
...@@ -71,6 +68,14 @@ dbIterator *dbGetIteratorAt(redisDb *db, int slot) { ...@@ -71,6 +68,14 @@ dbIterator *dbGetIteratorAt(redisDb *db, int slot) {
return iter; return iter;
} }
/* Returns DB iterator that can be used to iterate through sub-dictionaries.
* Primary database contains only one dictionary when node runs without cluster mode,
* or 16k dictionaries (one per slot) when node runs with cluster mode enabled. */
void dbInitIterator(dbIterator *dbit, redisDb *db) {
dbInitIteratorAt(dbit, db, 0);
}
/* Returns next dictionary strictly after provided slot and updates slot id in the supplied reference. */
dict *dbGetNextUnvisitedSlot(redisDb *db, int *slot) { dict *dbGetNextUnvisitedSlot(redisDb *db, int *slot) {
if (*slot < db->dict_count - 1) { if (*slot < db->dict_count - 1) {
dbIterator *dbit = dbGetIteratorAt(db, *slot + 1); /* Scan on the current slot has already returned 0, find next non-empty dict. */ dbIterator *dbit = dbGetIteratorAt(db, *slot + 1); /* Scan on the current slot has already returned 0, find next non-empty dict. */
...@@ -1121,6 +1126,7 @@ cleanup: ...@@ -1121,6 +1126,7 @@ cleanup:
} }
void addSlotIdToCursor(int slot, unsigned long long int *cursor) { void addSlotIdToCursor(int slot, unsigned long long int *cursor) {
/* Slot id can be -1 if there are no more slots to visit. */
if (slot >= 0) { if (slot >= 0) {
*cursor = (*cursor << CLUSTER_SLOT_MASK_BITS) | slot; *cursor = (*cursor << CLUSTER_SLOT_MASK_BITS) | slot;
} }
......
...@@ -2361,12 +2361,10 @@ typedef struct dbIterator { ...@@ -2361,12 +2361,10 @@ typedef struct dbIterator {
int index; int index;
} dbIterator; } dbIterator;
/* dbIterator specific functions */ /* DB iterator specific functions */
dict *dbNextDict(dbIterator *iter); void dbInitIterator(dbIterator *dbit, redisDb *db);
dbIterator *dbGetIterator(redisDb *db); dict *dbNextDict(dbIterator *dbit);
dbIterator *dbGetIteratorAt(redisDb *db, int slot);
dict *dbGetNextUnvisitedSlot(redisDb *db, int *slot); dict *dbGetNextUnvisitedSlot(redisDb *db, int *slot);
void dbReleaseIterator(dbIterator *iter);
/* SCAN specific commands for easy cursor manipulation, shared between main code and modules. */ /* SCAN specific commands for easy cursor manipulation, shared between main code and modules. */
int getAndClearSlotIdFromCursor(unsigned long long int *cursor); int getAndClearSlotIdFromCursor(unsigned long long int *cursor);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment