MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / zzlInsert

Function zzlInsert

src/t_zset.cpp:1059–1090  ·  view source on GitHub ↗

Insert (element,score) pair in ziplist. This function assumes the element is * not yet present in the list. */

Source from the content-addressed store, hash-verified

1057/* Insert (element,score) pair in ziplist. This function assumes the element is
1058 * not yet present in the list. */
1059unsigned char *zzlInsert(unsigned char *zl, sds ele, double score) {
1060 unsigned char *eptr = ziplistIndex(zl,0), *sptr;
1061 double s;
1062
1063 while (eptr != NULL) {
1064 sptr = ziplistNext(zl,eptr);
1065 serverAssert(sptr != NULL);
1066 s = zzlGetScore(sptr);
1067
1068 if (s > score) {
1069 /* First element with score larger than score for element to be
1070 * inserted. This means we should take its spot in the list to
1071 * maintain ordering. */
1072 zl = zzlInsertAt(zl,eptr,ele,score);
1073 break;
1074 } else if (s == score) {
1075 /* Ensure lexicographical ordering for elements. */
1076 if (zzlCompareElements(eptr,(unsigned char*)ele,sdslen(ele)) > 0) {
1077 zl = zzlInsertAt(zl,eptr,ele,score);
1078 break;
1079 }
1080 }
1081
1082 /* Move to next element. */
1083 eptr = ziplistNext(zl,sptr);
1084 }
1085
1086 /* Push on tail of list when it was not yet inserted. */
1087 if (eptr == NULL)
1088 zl = zzlInsertAt(zl,NULL,ele,score);
1089 return zl;
1090}
1091
1092unsigned char *zzlDeleteRangeByScore(unsigned char *zl, zrangespec *range, unsigned long *deleted) {
1093 unsigned char *eptr, *sptr;

Callers 1

zsetAddFunction · 0.85

Calls 6

ziplistIndexFunction · 0.85
ziplistNextFunction · 0.85
zzlGetScoreFunction · 0.85
zzlInsertAtFunction · 0.85
zzlCompareElementsFunction · 0.85
sdslenFunction · 0.85

Tested by

no test coverage detected