Skip to content

Commit 97ba4e3

Browse files
committed
Lazyfree: Hash converted to use plain SDS WIP 5.
1 parent 36be34b commit 97ba4e3

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

src/rdb.c

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -651,16 +651,17 @@ ssize_t rdbSaveObject(rio *rdb, robj *o) {
651651
nwritten += n;
652652

653653
while((de = dictNext(di)) != NULL) {
654-
robj *key = dictGetKey(de);
655-
robj *val = dictGetVal(de);
654+
sds field = dictGetKey(de);
655+
sds value = dictGetVal(de);
656656

657-
if ((n = rdbSaveStringObject(rdb,key)) == -1) return -1;
657+
if ((n = rdbSaveRawString(rdb,(unsigned char*)field,
658+
sdslen(field))) == -1) return -1;
658659
nwritten += n;
659-
if ((n = rdbSaveStringObject(rdb,val)) == -1) return -1;
660+
if ((n = rdbSaveRawString(rdb,(unsigned char*)value,
661+
sdslen(value))) == -1) return -1;
660662
nwritten += n;
661663
}
662664
dictReleaseIterator(di);
663-
664665
} else {
665666
serverPanic("Unknown hash encoding");
666667
}
@@ -1044,6 +1045,7 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
10441045
} else if (rdbtype == RDB_TYPE_HASH) {
10451046
size_t len;
10461047
int ret;
1048+
sds field, value;
10471049

10481050
len = rdbLoadLen(rdb, NULL);
10491051
if (len == RDB_LENERR) return NULL;
@@ -1056,46 +1058,40 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
10561058

10571059
/* Load every field and value into the ziplist */
10581060
while (o->encoding == OBJ_ENCODING_ZIPLIST && len > 0) {
1059-
robj *field, *value;
1060-
10611061
len--;
10621062
/* Load raw strings */
1063-
field = rdbLoadStringObject(rdb);
1064-
if (field == NULL) return NULL;
1065-
serverAssert(sdsEncodedObject(field));
1066-
value = rdbLoadStringObject(rdb);
1067-
if (value == NULL) return NULL;
1068-
serverAssert(sdsEncodedObject(value));
1063+
if ((field = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
1064+
return NULL;
1065+
if ((value = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
1066+
return NULL;
10691067

10701068
/* Add pair to ziplist */
1071-
o->ptr = ziplistPush(o->ptr, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL);
1072-
o->ptr = ziplistPush(o->ptr, value->ptr, sdslen(value->ptr), ZIPLIST_TAIL);
1069+
o->ptr = ziplistPush(o->ptr, (unsigned char*)field,
1070+
sdslen(field), ZIPLIST_TAIL);
1071+
o->ptr = ziplistPush(o->ptr, (unsigned char*)value,
1072+
sdslen(value), ZIPLIST_TAIL);
1073+
10731074
/* Convert to hash table if size threshold is exceeded */
1074-
if (sdslen(field->ptr) > server.hash_max_ziplist_value ||
1075-
sdslen(value->ptr) > server.hash_max_ziplist_value)
1075+
if (sdslen(field) > server.hash_max_ziplist_value ||
1076+
sdslen(value) > server.hash_max_ziplist_value)
10761077
{
1077-
decrRefCount(field);
1078-
decrRefCount(value);
1078+
sdsfree(field);
1079+
sdsfree(value);
10791080
hashTypeConvert(o, OBJ_ENCODING_HT);
10801081
break;
10811082
}
1082-
decrRefCount(field);
1083-
decrRefCount(value);
1083+
sdsfree(field);
1084+
sdsfree(value);
10841085
}
10851086

10861087
/* Load remaining fields and values into the hash table */
10871088
while (o->encoding == OBJ_ENCODING_HT && len > 0) {
1088-
robj *field, *value;
1089-
10901089
len--;
10911090
/* Load encoded strings */
1092-
field = rdbLoadEncodedStringObject(rdb);
1093-
if (field == NULL) return NULL;
1094-
value = rdbLoadEncodedStringObject(rdb);
1095-
if (value == NULL) return NULL;
1096-
1097-
field = tryObjectEncoding(field);
1098-
value = tryObjectEncoding(value);
1091+
if ((field = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
1092+
return NULL;
1093+
if ((value = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
1094+
return NULL;
10991095

11001096
/* Add pair to hash table */
11011097
ret = dictAdd((dict*)o->ptr, field, value);

src/t_hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ void hincrbyfloatCommand(client *c) {
567567
if (hashTypeGetValue(o,c->argv[2]->ptr,&vstr,&vlen,&ll) == C_OK) {
568568
if (vstr) {
569569
if (string2d((char*)vstr,vlen,&value) == 0) {
570-
addReplyError(c,"hash value is not an integer");
570+
addReplyError(c,"hash value is not a float");
571571
return;
572572
}
573573
} else {

src/util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,12 @@ int ld2string(char *buf, size_t len, long double value, int humanfriendly) {
521521
if (l+1 > len) return 0; /* No room. */
522522
/* Now remove trailing zeroes after the '.' */
523523
if (strchr(buf,'.') != NULL) {
524-
char *p = buf+len-1;
524+
char *p = buf+l-1;
525525
while(*p == '0') {
526526
p--;
527-
len--;
527+
l--;
528528
}
529-
if (*p == '.') len--;
529+
if (*p == '.') l--;
530530
}
531531
} else {
532532
l = snprintf(buf,len,"%.17Lg", value);

0 commit comments

Comments
 (0)