Skip to content

Commit 7a89a70

Browse files
committed
Separate hash entry implementation
Signed-off-by: Ran Shidlansik <[email protected]>
1 parent 23bb4a2 commit 7a89a70

File tree

11 files changed

+64
-398
lines changed

11 files changed

+64
-398
lines changed

cmake/Modules/SourceFiles.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ set(VALKEY_SERVER_SRCS
117117
${CMAKE_SOURCE_DIR}/src/unix.c
118118
${CMAKE_SOURCE_DIR}/src/server.c
119119
${CMAKE_SOURCE_DIR}/src/logreqres.c
120+
${CMAKE_SOURCE_DIR}/src/entry.c
120121
${CMAKE_SOURCE_DIR}/src/volatile_set.c)
121122

122123

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ ENGINE_NAME=valkey
423423
SERVER_NAME=$(ENGINE_NAME)-server$(PROG_SUFFIX)
424424
ENGINE_SENTINEL_NAME=$(ENGINE_NAME)-sentinel$(PROG_SUFFIX)
425425
ENGINE_TRACE_OBJ=trace/trace.o trace/trace_commands.o trace/trace_db.o trace/trace_bgsave.o trace/trace_cluster.o trace/trace_server.o trace/trace_aof.o
426-
ENGINE_SERVER_OBJ=threads_mngr.o adlist.o quicklist.o ae.o anet.o dict.o hashtable.o kvstore.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o memory_prefetch.o io_threads.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o cluster_legacy.o cluster_slot_stats.o crc16.o endianconv.o commandlog.o eval.o bio.o rio.o rand.o memtest.o syscheck.o crcspeed.o crccombine.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o valkey-check-rdb.o valkey-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o allocator_defrag.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o tracking.o socket.o tls.o sha256.o timeout.o setcpuaffinity.o monotonic.o mt19937-64.o resp_parser.o call_reply.o script.o functions.o commands.o strl.o connection.o unix.o logreqres.o rdma.o scripting_engine.o volatile_set.o lua/script_lua.o lua/function_lua.o lua/engine_lua.o lua/debug_lua.o
426+
ENGINE_SERVER_OBJ=threads_mngr.o adlist.o quicklist.o ae.o anet.o dict.o hashtable.o kvstore.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o memory_prefetch.o io_threads.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o cluster_legacy.o cluster_slot_stats.o crc16.o endianconv.o commandlog.o eval.o bio.o rio.o rand.o memtest.o syscheck.o crcspeed.o crccombine.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o valkey-check-rdb.o valkey-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o allocator_defrag.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o tracking.o socket.o tls.o sha256.o timeout.o setcpuaffinity.o monotonic.o mt19937-64.o resp_parser.o call_reply.o script.o functions.o commands.o strl.o connection.o unix.o logreqres.o rdma.o scripting_engine.o entry.o volatile_set.o lua/script_lua.o lua/function_lua.o lua/engine_lua.o lua/debug_lua.o
427427
ENGINE_SERVER_OBJ+=$(ENGINE_TRACE_OBJ)
428428
ENGINE_CLI_NAME=$(ENGINE_NAME)-cli$(PROG_SUFFIX)
429429
ENGINE_CLI_OBJ=anet.o adlist.o dict.o valkey-cli.o zmalloc.o release.o ae.o serverassert.o crcspeed.o crccombine.o crc64.o siphash.o crc16.o monotonic.o cli_common.o mt19937-64.o strl.o cli_commands.o sds.o util.o sha256.o

src/aof.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ int rewriteHashObject(rio *r, robj *key, robj *o) {
19691969
}
19701970
}
19711971

1972-
if (volatile_items > 0 && hashTypeEntryHasExpire(hi.next))
1972+
if (volatile_items > 0 && entryHasExpiry(hi.next))
19731973
continue;
19741974

19751975
if (!rioWriteHashIteratorCursor(r, &hi, OBJ_HASH_FIELD) || !rioWriteHashIteratorCursor(r, &hi, OBJ_HASH_VALUE)) {
@@ -1986,9 +1986,9 @@ int rewriteHashObject(rio *r, robj *key, robj *o) {
19861986
if (hashTypeHasVolatileElements(o)) {
19871987
hashTypeInitVolatileIterator(o, &hi);
19881988
while (hashTypeNext(&hi) != C_ERR) {
1989-
long long expiry = hashTypeEntryGetExpiry(hi.next);
1990-
sds field = hashTypeEntryGetField(hi.next);
1991-
sds value = hashTypeEntryGetValue(hi.next);
1989+
long long expiry = entryGetExpiry(hi.next);
1990+
sds field = entryGetField(hi.next);
1991+
sds value = entryGetValue(hi.next);
19921992
if (rioWriteBulkCount(r, '*', 8) == 0) return 0;
19931993
if (rioWriteBulkString(r, "HSETEX", 6) == 0) return 0;
19941994
if (rioWriteBulkObject(r, key) == 0) return 0;

src/db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,9 +989,9 @@ void hashtableScanCallback(void *privdata, void *entry) {
989989
key = node->ele;
990990
/* zset data is copied after filtering by key */
991991
} else if (o->type == OBJ_HASH) {
992-
key = hashTypeEntryGetField(entry);
992+
key = entryGetField(entry);
993993
if (!data->only_keys) {
994-
val = hashTypeEntryGetValue(entry);
994+
val = entryGetValue(entry);
995995
}
996996
} else {
997997
serverPanic("Type not handled in hashtable SCAN callback.");

src/defrag.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,18 @@ static void scanLaterSet(robj *ob, unsigned long *cursor) {
442442
}
443443

444444
/* Hashtable scan callback for hash datatype */
445-
static void activeDefragHashTypeEntry(void *privdata, void *element_ref) {
445+
static void activeDefragentry(void *privdata, void *element_ref) {
446446
UNUSED(privdata);
447-
hashTypeEntry **entry_ref = (hashTypeEntry **)element_ref;
447+
entry **entry_ref = (entry **)element_ref;
448448

449-
hashTypeEntry *new_entry = hashTypeEntryDefrag(*entry_ref, activeDefragAlloc, activeDefragSds);
449+
entry *new_entry = entryDefrag(*entry_ref, activeDefragAlloc, activeDefragSds);
450450
if (new_entry) *entry_ref = new_entry;
451451
}
452452

453453
static void scanLaterHash(robj *ob, unsigned long *cursor) {
454454
serverAssert(ob->type == OBJ_HASH && ob->encoding == OBJ_ENCODING_HASHTABLE);
455455
hashtable *ht = ob->ptr;
456-
*cursor = hashtableScanDefrag(ht, *cursor, activeDefragHashTypeEntry, NULL, activeDefragAlloc, HASHTABLE_SCAN_EMIT_REF);
456+
*cursor = hashtableScanDefrag(ht, *cursor, activeDefragentry, NULL, activeDefragAlloc, HASHTABLE_SCAN_EMIT_REF);
457457
}
458458

459459
static void defragQuicklist(robj *ob) {
@@ -498,7 +498,7 @@ static void defragHash(robj *ob) {
498498
} else {
499499
unsigned long cursor = 0;
500500
do {
501-
cursor = hashtableScanDefrag(ht, cursor, activeDefragHashTypeEntry, NULL, activeDefragAlloc, HASHTABLE_SCAN_EMIT_REF);
501+
cursor = hashtableScanDefrag(ht, cursor, activeDefragentry, NULL, activeDefragAlloc, HASHTABLE_SCAN_EMIT_REF);
502502
} while (cursor != 0);
503503
}
504504
/* defrag the hashtable struct and tables */

src/module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11224,8 +11224,8 @@ static void moduleScanKeyHashtableCallback(void *privdata, void *entry) {
1122411224
key = node->ele;
1122511225
value = createStringObjectFromLongDouble(node->score, 0);
1122611226
} else if (o->type == OBJ_HASH) {
11227-
key = hashTypeEntryGetField(entry);
11228-
sds val = hashTypeEntryGetValue(entry);
11227+
key = entryGetField(entry);
11228+
sds val = entryGetValue(entry);
1122911229
value = createStringObject(val, sdslen(val));
1123011230
} else {
1123111231
serverPanic("unexpected object type");

src/object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ void dismissHashObject(robj *o, size_t size_hint) {
683683
hashtableInitIterator(&iter, ht, 0);
684684
void *next;
685685
while (hashtableNext(&iter, &next)) {
686-
dismissHashTypeEntry(next);
686+
dismissEntry(next);
687687
}
688688
hashtableResetIterator(&iter);
689689
}
@@ -1204,7 +1204,7 @@ size_t objectComputeSize(robj *key, robj *o, size_t sample_size, int dbid) {
12041204

12051205
asize = sizeof(*o) + hashtableMemUsage(ht);
12061206
while (hashtableNext(&iter, &next) && samples < sample_size) {
1207-
elesize += hashTypeEntryMemUsage(next);
1207+
elesize += entryMemUsage(next);
12081208
samples++;
12091209
}
12101210
hashtableResetIterator(&iter);

src/rdb.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,8 @@ ssize_t rdbSaveObject(rio *rdb, robj *o, robj *key, int dbid) {
972972
hashtableInitIterator(&iter, ht, HASHTABLE_ITER_AVOID_ACCESS);
973973
void *next;
974974
while (hashtableNext(&iter, &next)) {
975-
sds field = hashTypeEntryGetField(next);
976-
sds value = hashTypeEntryGetValue(next);
975+
sds field = entryGetField(next);
976+
sds value = entryGetValue(next);
977977

978978
if ((n = rdbSaveRawString(rdb, (unsigned char *)field, sdslen(field))) == -1) {
979979
hashtableResetIterator(&iter);
@@ -986,7 +986,7 @@ ssize_t rdbSaveObject(rio *rdb, robj *o, robj *key, int dbid) {
986986
}
987987
nwritten += n;
988988
if (add_expiry) {
989-
long long expiry = hashTypeEntryGetExpiry(next);
989+
long long expiry = entryGetExpiry(next);
990990
if ((n = rdbSaveMillisecondTime(rdb, expiry) == -1)) {
991991
hashtableResetIterator(&iter);
992992
return -1;
@@ -2143,12 +2143,12 @@ robj *rdbLoadObject(int rdbtype, rio *rdb, sds key, int dbid, int *error) {
21432143
(sdslen(field) > server.hash_max_listpack_value || sdslen(value) > server.hash_max_listpack_value ||
21442144
!lpSafeToAdd(o->ptr, sdslen(field) + sdslen(value)))) {
21452145
hashTypeConvert(o, OBJ_ENCODING_HASHTABLE);
2146-
hashTypeEntry *entry = hashTypeCreateEntry(field, value, EXPIRY_NONE);
2146+
entry *entry = entryCreate(field, value, EXPIRY_NONE);
21472147
sdsfree(field);
21482148
if (!hashtableAdd((hashtable *)o->ptr, entry)) {
21492149
rdbReportCorruptRDB("Duplicate hash fields detected");
21502150
if (dupSearchHashtable) hashtableRelease(dupSearchHashtable);
2151-
freeHashTypeEntry(entry);
2151+
freeentry(entry);
21522152
decrRefCount(o);
21532153
return NULL;
21542154
}
@@ -2201,11 +2201,11 @@ robj *rdbLoadObject(int rdbtype, rio *rdb, sds key, int dbid, int *error) {
22012201
}
22022202

22032203
/* Add pair to hash table */
2204-
hashTypeEntry *entry = hashTypeCreateEntry(field, value, itemexpiry);
2204+
entry *entry = entryCreate(field, value, itemexpiry);
22052205
sdsfree(field);
22062206
if (!hashtableAdd((hashtable *)o->ptr, entry)) {
22072207
rdbReportCorruptRDB("Duplicate hash fields detected");
2208-
freeHashTypeEntry(entry);
2208+
freeentry(entry);
22092209
decrRefCount(o);
22102210
return NULL;
22112211
}

src/server.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,11 @@ hashtableType subcommandSetType = {.entryGetKey = hashtableSubcommandGetKey,
666666

667667
/* Hash type hash table (note that small hashes are represented with listpacks) */
668668
const void *hashHashtableTypeGetKey(const void *entry) {
669-
const hashTypeEntry *hash_entry = entry;
670-
return (const void *)hashTypeEntryGetField(hash_entry);
669+
return (const void *)entryGetField(entry);
671670
}
672671

673672
void hashHashtableTypeDestructor(void *entry) {
674-
hashTypeEntry *hash_entry = entry;
675-
freeHashTypeEntry(hash_entry);
673+
freeentry(entry);
676674
}
677675

678676
size_t hashHashtableTypeMetadataSize(void) {

src/server.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ typedef long long ustime_t; /* microsecond time type. */
8484
#include "memory_prefetch.h"
8585
#include "volatile_set.h"
8686
#include "trace/trace.h"
87+
#include "entry.h"
8788

8889
#ifdef USE_LTTNG
8990
#define valkey_fork() do_fork()
@@ -3359,16 +3360,7 @@ robj *setTypeDup(robj *o);
33593360
#define HASH_SET_KEEP_EXPIRY (1 << 2)
33603361
#define HASH_SET_COPY 0
33613362

3362-
typedef void hashTypeEntry;
3363-
hashTypeEntry *hashTypeCreateEntry(sds field, sds value, long long ttl);
3364-
sds hashTypeEntryGetField(const hashTypeEntry *entry);
3365-
sds hashTypeEntryGetValue(const hashTypeEntry *entry);
3366-
long long hashTypeEntryGetExpiry(const hashTypeEntry *entry);
3367-
int hashTypeEntryHasExpire(const hashTypeEntry *entry);
3368-
size_t hashTypeEntryMemUsage(hashTypeEntry *entry);
3369-
hashTypeEntry *hashTypeEntryDefrag(hashTypeEntry *entry, void *(*defragfn)(void *), sds (*sdsdefragfn)(sds));
3370-
void dismissHashTypeEntry(hashTypeEntry *entry);
3371-
void freeHashTypeEntry(hashTypeEntry *entry);
3363+
33723364
void hashTypeFreeVolatileSet(robj *o);
33733365
void hashTypeTrackEntry(robj *o, void *entry);
33743366
void hashTypeUntrackEntry(robj *o, void *entry);

0 commit comments

Comments
 (0)