Skip to content

Commit da5d9a1

Browse files
committed
replaced temp dicts with hashtable
Signed-off-by: Shai Zarka <[email protected]>
1 parent 4e0149a commit da5d9a1

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/t_hash.c

+33-33
Original file line numberDiff line numberDiff line change
@@ -1079,50 +1079,50 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
10791079
* used into CASE 4 is highly inefficient. */
10801080
if (count * HRANDFIELD_SUB_STRATEGY_MUL > size) {
10811081
/* Hashtable encoding (generic implementation) */
1082-
dict *d = dictCreate(&sdsReplyDictType);
1083-
dictExpand(d, size);
1082+
hashtable *ht = hashtableCreate(&hashHashtableType);
1083+
hashtableExpand(ht, size);
10841084
hashTypeIterator hi;
10851085
hashTypeInitIterator(hash, &hi);
10861086

1087-
/* Add all the elements into the temporary dictionary. */
1088-
while ((hashTypeNext(&hi)) != C_ERR) {
1089-
int ret = DICT_ERR;
1087+
/* Add all the elements into the temporary hashtable. */
1088+
while (hashTypeNext(&hi) != C_ERR) {
1089+
int ret = 0;
10901090
sds field, value = NULL;
10911091

10921092
field = hashTypeCurrentObjectNewSds(&hi, OBJ_HASH_FIELD);
10931093
if (withvalues) value = hashTypeCurrentObjectNewSds(&hi, OBJ_HASH_VALUE);
1094-
ret = dictAdd(d, field, value);
1095-
1096-
serverAssert(ret == DICT_OK);
1094+
hashTypeEntry *entry = hashTypeCreateEntry(field, value);
1095+
sdsfree(field);
1096+
ret = hashtableAdd(ht, entry);
1097+
serverAssert(ret);
10971098
}
1098-
serverAssert(dictSize(d) == size);
1099+
serverAssert(hashtableSize(ht) == size);
10991100
hashTypeResetIterator(&hi);
11001101

11011102
/* Remove random elements to reach the right count. */
11021103
while (size > count) {
1103-
dictEntry *de;
1104-
de = dictGetFairRandomKey(d);
1105-
dictUnlink(d, dictGetKey(de));
1106-
sdsfree(dictGetKey(de));
1107-
sdsfree(dictGetVal(de));
1108-
dictFreeUnlinkedEntry(d, de);
1104+
void *element;
1105+
hashtableFairRandomEntry(ht, &element);
1106+
sds field = hashTypeEntryGetField((hashTypeEntry*)element);
1107+
hashtableDelete(ht, field);
11091108
size--;
11101109
}
11111110

1112-
/* Reply with what's in the dict and release memory */
1113-
dictIterator *di;
1114-
dictEntry *de;
1115-
di = dictGetIterator(d);
1116-
while ((de = dictNext(di)) != NULL) {
1117-
sds field = dictGetKey(de);
1118-
sds value = dictGetVal(de);
1111+
/* Reply with what's in the temporary hashtable and release memory */
1112+
hashtableIterator iter;
1113+
hashtableInitIterator(&iter, ht, 0);
1114+
void *next;
1115+
while (hashtableNext(&iter, &next)) {
1116+
hashTypeEntry* entry = (hashTypeEntry*)next;
1117+
sds field = hashTypeEntryGetField(entry);
1118+
sds value = hashTypeEntryGetValue(entry);
11191119
if (withvalues && c->resp > 2) addWritePreparedReplyArrayLen(wpc, 2);
1120-
addWritePreparedReplyBulkSds(wpc, field);
1121-
if (withvalues) addWritePreparedReplyBulkSds(wpc, value);
1120+
addWritePreparedReplyBulkSds(wpc, sdsdup(field));
1121+
if (withvalues) addWritePreparedReplyBulkSds(wpc, sdsdup(value));
11221122
}
1123-
1124-
dictReleaseIterator(di);
1125-
dictRelease(d);
1123+
1124+
hashtableResetIterator(&iter);
1125+
hashtableRelease(ht);
11261126
}
11271127

11281128
/* CASE 4: We have a big hash compared to the requested number of elements.
@@ -1133,16 +1133,16 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
11331133
/* Hashtable encoding (generic implementation) */
11341134
unsigned long added = 0;
11351135
listpackEntry field, value;
1136-
dict *d = dictCreate(&hashDictType);
1137-
dictExpand(d, count);
1136+
hashtable *ht = hashtableCreate(&setHashtableType);
1137+
hashtableExpand(ht, count);
11381138
while (added < count) {
11391139
hashTypeRandomElement(hash, size, &field, withvalues ? &value : NULL);
11401140

1141-
/* Try to add the object to the dictionary. If it already exists
1141+
/* Try to add the object to the hashtable. If it already exists
11421142
* free it, otherwise increment the number of objects we have
1143-
* in the result dictionary. */
1143+
* in the result hashtable. */
11441144
sds sfield = hashSdsFromListpackEntry(&field);
1145-
if (dictAdd(d, sfield, NULL) != DICT_OK) {
1145+
if (!hashtableAdd(ht, sfield)) {
11461146
sdsfree(sfield);
11471147
continue;
11481148
}
@@ -1155,7 +1155,7 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
11551155
}
11561156

11571157
/* Release memory */
1158-
dictRelease(d);
1158+
hashtableRelease(ht);
11591159
}
11601160
}
11611161

0 commit comments

Comments
 (0)