Lazyfree: Hash converted to use plain SDS WIP 5.

This commit is contained in:
antirez 2015-09-23 10:34:53 +02:00
parent 36be34bb87
commit 97ba4e3886
3 changed files with 30 additions and 34 deletions

View File

@ -651,16 +651,17 @@ ssize_t rdbSaveObject(rio *rdb, robj *o) {
nwritten += n;
while((de = dictNext(di)) != NULL) {
robj *key = dictGetKey(de);
robj *val = dictGetVal(de);
sds field = dictGetKey(de);
sds value = dictGetVal(de);
if ((n = rdbSaveStringObject(rdb,key)) == -1) return -1;
if ((n = rdbSaveRawString(rdb,(unsigned char*)field,
sdslen(field))) == -1) return -1;
nwritten += n;
if ((n = rdbSaveStringObject(rdb,val)) == -1) return -1;
if ((n = rdbSaveRawString(rdb,(unsigned char*)value,
sdslen(value))) == -1) return -1;
nwritten += n;
}
dictReleaseIterator(di);
} else {
serverPanic("Unknown hash encoding");
}
@ -1044,6 +1045,7 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
} else if (rdbtype == RDB_TYPE_HASH) {
size_t len;
int ret;
sds field, value;
len = rdbLoadLen(rdb, NULL);
if (len == RDB_LENERR) return NULL;
@ -1056,46 +1058,40 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
/* Load every field and value into the ziplist */
while (o->encoding == OBJ_ENCODING_ZIPLIST && len > 0) {
robj *field, *value;
len--;
/* Load raw strings */
field = rdbLoadStringObject(rdb);
if (field == NULL) return NULL;
serverAssert(sdsEncodedObject(field));
value = rdbLoadStringObject(rdb);
if (value == NULL) return NULL;
serverAssert(sdsEncodedObject(value));
if ((field = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
return NULL;
if ((value = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
return NULL;
/* Add pair to ziplist */
o->ptr = ziplistPush(o->ptr, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL);
o->ptr = ziplistPush(o->ptr, value->ptr, sdslen(value->ptr), ZIPLIST_TAIL);
o->ptr = ziplistPush(o->ptr, (unsigned char*)field,
sdslen(field), ZIPLIST_TAIL);
o->ptr = ziplistPush(o->ptr, (unsigned char*)value,
sdslen(value), ZIPLIST_TAIL);
/* Convert to hash table if size threshold is exceeded */
if (sdslen(field->ptr) > server.hash_max_ziplist_value ||
sdslen(value->ptr) > server.hash_max_ziplist_value)
if (sdslen(field) > server.hash_max_ziplist_value ||
sdslen(value) > server.hash_max_ziplist_value)
{
decrRefCount(field);
decrRefCount(value);
sdsfree(field);
sdsfree(value);
hashTypeConvert(o, OBJ_ENCODING_HT);
break;
}
decrRefCount(field);
decrRefCount(value);
sdsfree(field);
sdsfree(value);
}
/* Load remaining fields and values into the hash table */
while (o->encoding == OBJ_ENCODING_HT && len > 0) {
robj *field, *value;
len--;
/* Load encoded strings */
field = rdbLoadEncodedStringObject(rdb);
if (field == NULL) return NULL;
value = rdbLoadEncodedStringObject(rdb);
if (value == NULL) return NULL;
field = tryObjectEncoding(field);
value = tryObjectEncoding(value);
if ((field = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
return NULL;
if ((value = rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS)) == NULL)
return NULL;
/* Add pair to hash table */
ret = dictAdd((dict*)o->ptr, field, value);

View File

@ -567,7 +567,7 @@ void hincrbyfloatCommand(client *c) {
if (hashTypeGetValue(o,c->argv[2]->ptr,&vstr,&vlen,&ll) == C_OK) {
if (vstr) {
if (string2d((char*)vstr,vlen,&value) == 0) {
addReplyError(c,"hash value is not an integer");
addReplyError(c,"hash value is not a float");
return;
}
} else {

View File

@ -521,12 +521,12 @@ int ld2string(char *buf, size_t len, long double value, int humanfriendly) {
if (l+1 > len) return 0; /* No room. */
/* Now remove trailing zeroes after the '.' */
if (strchr(buf,'.') != NULL) {
char *p = buf+len-1;
char *p = buf+l-1;
while(*p == '0') {
p--;
len--;
l--;
}
if (*p == '.') len--;
if (*p == '.') l--;
}
} else {
l = snprintf(buf,len,"%.17Lg", value);