Skip to content

Commit d0bf28c

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

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
@@ -1227,50 +1227,50 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
12271227
* used into CASE 4 is highly inefficient. */
12281228
if (count * HRANDFIELD_SUB_STRATEGY_MUL > size) {
12291229
/* Hashtable encoding (generic implementation) */
1230-
dict *d = dictCreate(&sdsReplyDictType);
1231-
dictExpand(d, size);
1230+
hashtable *ht = hashtableCreate(&hashHashtableType);
1231+
hashtableExpand(ht, size);
12321232
hashTypeIterator hi;
12331233
hashTypeInitIterator(hash, &hi);
12341234

1235-
/* Add all the elements into the temporary dictionary. */
1236-
while ((hashTypeNext(&hi)) != C_ERR) {
1237-
int ret = DICT_ERR;
1235+
/* Add all the elements into the temporary hashtable. */
1236+
while (hashTypeNext(&hi) != C_ERR) {
1237+
int ret = 0;
12381238
sds field, value = NULL;
12391239

12401240
field = hashTypeCurrentObjectNewSds(&hi, OBJ_HASH_FIELD);
12411241
if (withvalues) value = hashTypeCurrentObjectNewSds(&hi, OBJ_HASH_VALUE);
1242-
ret = dictAdd(d, field, value);
1243-
1244-
serverAssert(ret == DICT_OK);
1242+
hashTypeEntry *entry = hashTypeCreateEntry(field, value);
1243+
sdsfree(field);
1244+
ret = hashtableAdd(ht, entry);
1245+
serverAssert(ret);
12451246
}
1246-
serverAssert(dictSize(d) == size);
1247+
serverAssert(hashtableSize(ht) == size);
12471248
hashTypeResetIterator(&hi);
12481249

12491250
/* Remove random elements to reach the right count. */
12501251
while (size > count) {
1251-
dictEntry *de;
1252-
de = dictGetFairRandomKey(d);
1253-
dictUnlink(d, dictGetKey(de));
1254-
sdsfree(dictGetKey(de));
1255-
sdsfree(dictGetVal(de));
1256-
dictFreeUnlinkedEntry(d, de);
1252+
void *element;
1253+
hashtableFairRandomEntry(ht, &element);
1254+
sds field = hashTypeEntryGetField((hashTypeEntry*)element);
1255+
hashtableDelete(ht, field);
12571256
size--;
12581257
}
12591258

1260-
/* Reply with what's in the dict and release memory */
1261-
dictIterator *di;
1262-
dictEntry *de;
1263-
di = dictGetIterator(d);
1264-
while ((de = dictNext(di)) != NULL) {
1265-
sds field = dictGetKey(de);
1266-
sds value = dictGetVal(de);
1259+
/* Reply with what's in the temporary hashtable and release memory */
1260+
hashtableIterator iter;
1261+
hashtableInitIterator(&iter, ht, 0);
1262+
void *next;
1263+
while (hashtableNext(&iter, &next)) {
1264+
hashTypeEntry* entry = (hashTypeEntry*)next;
1265+
sds field = hashTypeEntryGetField(entry);
1266+
sds value = hashTypeEntryGetValue(entry);
12671267
if (withvalues && c->resp > 2) addWritePreparedReplyArrayLen(wpc, 2);
1268-
addWritePreparedReplyBulkSds(wpc, field);
1269-
if (withvalues) addWritePreparedReplyBulkSds(wpc, value);
1268+
addWritePreparedReplyBulkSds(wpc, sdsdup(field));
1269+
if (withvalues) addWritePreparedReplyBulkSds(wpc, sdsdup(value));
12701270
}
1271-
1272-
dictReleaseIterator(di);
1273-
dictRelease(d);
1271+
1272+
hashtableResetIterator(&iter);
1273+
hashtableRelease(ht);
12741274
}
12751275

12761276
/* CASE 4: We have a big hash compared to the requested number of elements.
@@ -1281,16 +1281,16 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
12811281
/* Hashtable encoding (generic implementation) */
12821282
unsigned long added = 0;
12831283
listpackEntry field, value;
1284-
dict *d = dictCreate(&hashDictType);
1285-
dictExpand(d, count);
1284+
hashtable *ht = hashtableCreate(&setHashtableType);
1285+
hashtableExpand(ht, count);
12861286
while (added < count) {
12871287
hashTypeRandomElement(hash, size, &field, withvalues ? &value : NULL);
12881288

1289-
/* Try to add the object to the dictionary. If it already exists
1289+
/* Try to add the object to the hashtable. If it already exists
12901290
* free it, otherwise increment the number of objects we have
1291-
* in the result dictionary. */
1291+
* in the result hashtable. */
12921292
sds sfield = hashSdsFromListpackEntry(&field);
1293-
if (dictAdd(d, sfield, NULL) != DICT_OK) {
1293+
if (!hashtableAdd(ht, sfield)) {
12941294
sdsfree(sfield);
12951295
continue;
12961296
}
@@ -1303,7 +1303,7 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
13031303
}
13041304

13051305
/* Release memory */
1306-
dictRelease(d);
1306+
hashtableRelease(ht);
13071307
}
13081308
}
13091309

0 commit comments

Comments
 (0)