Skip to content

Commit f553b46

Browse files
committed
changes following code review
Signed-off-by: xbasel <[email protected]>
1 parent 028c7df commit f553b46

File tree

7 files changed

+20
-37
lines changed

7 files changed

+20
-37
lines changed

src/aof.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ int rewriteAppendOnlyFileRio(rio *aof) {
22102210

22112211
for (j = 0; j < server.dbnum; j++) {
22122212
char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
2213-
if (databaseEmpty(j)) continue;
2213+
if (dbHasNoKeys(j)) continue;
22142214
serverDb *db = server.db[j];
22152215

22162216
/* SELECT the new DB */

src/cluster_legacy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5711,7 +5711,7 @@ int verifyClusterConfigWithData(void) {
57115711

57125712
/* Make sure we only have keys in DB0. */
57135713
for (j = 1; j < server.dbnum; j++) {
5714-
if (!databaseEmpty(j)) return C_ERR;
5714+
if (!dbHasNoKeys(j)) return C_ERR;
57155715
}
57165716

57175717
/* Check that all the slots we see populated memory have a corresponding
@@ -6828,7 +6828,7 @@ int clusterCommandSpecial(client *c) {
68286828
}
68296829
} else if (!strcasecmp(c->argv[1]->ptr, "flushslots") && c->argc == 2) {
68306830
/* CLUSTER FLUSHSLOTS */
6831-
if (!databaseEmpty(0)) {
6831+
if (!dbHasNoKeys(0)) {
68326832
addReplyError(c, "DB must be empty to perform CLUSTER FLUSHSLOTS.");
68336833
return 1;
68346834
}
@@ -6969,7 +6969,7 @@ int clusterCommandSpecial(client *c) {
69696969
/* If the instance is currently a primary, it should have no assigned
69706970
* slots nor keys to accept to replicate some other node.
69716971
* Replicas can switch to another primary without issues. */
6972-
if (clusterNodeIsPrimary(myself) && (myself->numslots != 0 || !databaseEmpty(0))) {
6972+
if (clusterNodeIsPrimary(myself) && (myself->numslots != 0 || !dbHasNoKeys(0))) {
69736973
addReplyError(c, "To set a master the node must be empty and "
69746974
"without assigned slots.");
69756975
return 1;

src/db.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -679,16 +679,15 @@ void discardTempDb(serverDb **tempDb) {
679679

680680
int selectDb(client *c, int id) {
681681
if (id < 0 || id >= server.dbnum) return C_ERR;
682-
initDatabase(id);
683-
c->db = server.db[id];
682+
c->db = createDatabaseIfNeeded(id);
684683
return C_OK;
685684
}
686685

687686
long long dbTotalServerKeyCount(void) {
688687
long long total = 0;
689688
int j;
690689
for (j = 0; j < server.dbnum; j++) {
691-
if (databaseEmpty(j)) continue;
690+
if (dbHasNoKeys(j)) continue;
692691
total += kvstoreSize(server.db[j]->keys);
693692
}
694693
return total;
@@ -1641,10 +1640,9 @@ void scanDatabaseForDeletedKeys(serverDb *emptied, serverDb *replaced_with) {
16411640
int dbSwapDatabases(int id1, int id2) {
16421641
if (id1 < 0 || id1 >= server.dbnum || id2 < 0 || id2 >= server.dbnum) return C_ERR;
16431642
if (id1 == id2) return C_OK;
1644-
initDatabase(id1);
1645-
initDatabase(id2);
1646-
serverDb aux = *server.db[id1];
1647-
serverDb *db1 = server.db[id1], *db2 = server.db[id2];
1643+
serverDb *db1 = createDatabaseIfNeeded(id1);
1644+
serverDb *db2 = createDatabaseIfNeeded(id2);
1645+
serverDb aux = *db1;
16481646

16491647
/* Swapdb should make transaction fail if there is any
16501648
* client watching keys */

src/defrag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ static void beginDefragCycle(void) {
12731273
defrag.remaining_stages = listCreate();
12741274

12751275
for (int dbid = 0; dbid < server.dbnum; dbid++) {
1276-
if (databaseEmpty(dbid)) continue;
1276+
if (dbHasNoKeys(dbid)) continue;
12771277
addDefragStage(defragStageDbKeys, (void *)(uintptr_t)dbid, NULL);
12781278
addDefragStage(defragStageExpiresKvstore, (void *)(uintptr_t)dbid, NULL);
12791279
}

src/replication.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ int sendCurrentOffsetToReplica(client *replica) {
26672667
char buf[128];
26682668
int buflen;
26692669
buflen = snprintf(buf, sizeof(buf), "$ENDOFF:%lld %s %d %llu\r\n", server.primary_repl_offset, server.replid,
2670-
server.db[0]->id, (long long unsigned int)replica->id); // TODO xbasel , why is it index 0 ?
2670+
server.db[0]->id, (long long unsigned int)replica->id);
26712671
dualChannelServerLog(LL_NOTICE, "Sending to replica %s RDB end offset %lld and client-id %llu",
26722672
replicationGetReplicaName(replica), server.primary_repl_offset,
26732673
(long long unsigned int)replica->id);

src/server.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,26 +2722,10 @@ void makeThreadKillable(void) {
27222722
}
27232723

27242724
/* Return non-zero if the database is empty */
2725-
int databaseEmpty(int id) {
2726-
return id < 0 || id >= server.dbnum || !server.db[id] || kvstoreSize(server.db[id]->keys) == 0;
2727-
}
2728-
2729-
// /* Initialize temporary db on replica for use during diskless replication. */
2730-
// serverDb *initTempDb(int id) {
2731-
// int slot_count_bits = 0;
2732-
// int flags = KVSTORE_ALLOCATE_HASHTABLES_ON_DEMAND;
2733-
// if (server.cluster_enabled) {
2734-
// slot_count_bits = CLUSTER_SLOT_MASK_BITS;
2735-
// flags |= KVSTORE_FREE_EMPTY_HASHTABLES;
2736-
// }
2737-
// serverDb *tempDb = zcalloc(sizeof(serverDb) * server.dbnum);
2738-
//
2739-
// tempDb->id = id;
2740-
// tempDb->keys = kvstoreCreate(&kvstoreKeysHashtableType, slot_count_bits, flags);
2741-
// tempDb->expires = kvstoreCreate(&kvstoreExpiresHashtableType, slot_count_bits, flags);
2742-
//
2743-
// return tempDb;
2744-
// }
2725+
int dbHasNoKeys(int dbid) {
2726+
return dbid < 0 || dbid >= server.dbnum || !server.db[dbid] || kvstoreSize(server.db[dbid]->keys) == 0;
2727+
}
2728+
27452729
serverDb *createDatabase(int id) {
27462730
int slot_count_bits = 0;
27472731
int flags = KVSTORE_ALLOCATE_HASHTABLES_ON_DEMAND;
@@ -2763,10 +2747,11 @@ serverDb *createDatabase(int id) {
27632747
return db;
27642748
}
27652749

2766-
void initDatabase(int id) {
2750+
serverDb *createDatabaseIfNeeded(int id) {
27672751
if (server.db[id] == NULL) {
27682752
server.db[id] = createDatabase(id);
27692753
}
2754+
return server.db[id];
27702755
}
27712756

27722757
void initServer(void) {
@@ -2842,7 +2827,7 @@ void initServer(void) {
28422827
}
28432828

28442829
server.db = zcalloc(sizeof(serverDb *) * server.dbnum);
2845-
initDatabase(0); /* The default database should always exist */
2830+
createDatabaseIfNeeded(0); /* The default database should always exist */
28462831

28472832
evictionPoolAlloc(); /* Initialize the LRU keys pool. */
28482833
/* Note that server.pubsub_channels was chosen to be a kvstore (with only one dict, which

src/server.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,8 +3915,8 @@ void debugDelay(int usec);
39153915
void killThreads(void);
39163916
void makeThreadKillable(void);
39173917
serverDb *createDatabase(int id);
3918-
int databaseEmpty(int id);
3919-
void initDatabase(int id);
3918+
int dbHasNoKeys(int dbid);
3919+
serverDb *createDatabaseIfNeeded(int id);
39203920
void swapMainDbWithTempDb(serverDb **tempDb);
39213921
sds getVersion(void);
39223922
void debugPauseProcess(void);

0 commit comments

Comments
 (0)