MCPcopy Create free account
hub / github.com/F-Stack/f-stack / zdiffAlgorithm1

Function zdiffAlgorithm1

app/redis-6.2.6/src/t_zset.c:2394–2446  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2392}
2393
2394static void zdiffAlgorithm1(zsetopsrc *src, long setnum, zset *dstzset, size_t *maxelelen, size_t *totelelen) {
2395 /* DIFF Algorithm 1:
2396 *
2397 * We perform the diff by iterating all the elements of the first set,
2398 * and only adding it to the target set if the element does not exist
2399 * into all the other sets.
2400 *
2401 * This way we perform at max N*M operations, where N is the size of
2402 * the first set, and M the number of sets.
2403 *
2404 * There is also a O(K*log(K)) cost for adding the resulting elements
2405 * to the target set, where K is the final size of the target set.
2406 *
2407 * The final complexity of this algorithm is O(N*M + K*log(K)). */
2408 int j;
2409 zsetopval zval;
2410 zskiplistNode *znode;
2411 sds tmp;
2412
2413 /* With algorithm 1 it is better to order the sets to subtract
2414 * by decreasing size, so that we are more likely to find
2415 * duplicated elements ASAP. */
2416 qsort(src+1,setnum-1,sizeof(zsetopsrc),zuiCompareByRevCardinality);
2417
2418 memset(&zval, 0, sizeof(zval));
2419 zuiInitIterator(&src[0]);
2420 while (zuiNext(&src[0],&zval)) {
2421 double value;
2422 int exists = 0;
2423
2424 for (j = 1; j < setnum; j++) {
2425 /* It is not safe to access the zset we are
2426 * iterating, so explicitly check for equal object.
2427 * This check isn't really needed anymore since we already
2428 * check for a duplicate set in the zsetChooseDiffAlgorithm
2429 * function, but we're leaving it for future-proofing. */
2430 if (src[j].subject == src[0].subject ||
2431 zuiFind(&src[j],&zval,&value)) {
2432 exists = 1;
2433 break;
2434 }
2435 }
2436
2437 if (!exists) {
2438 tmp = zuiNewSdsFromValue(&zval);
2439 znode = zslInsert(dstzset->zsl,zval.score,tmp);
2440 dictAdd(dstzset->dict,tmp,&znode->score);
2441 if (sdslen(tmp) > *maxelelen) *maxelelen = sdslen(tmp);
2442 (*totelelen) += sdslen(tmp);
2443 }
2444 }
2445 zuiClearIterator(&src[0]);
2446}
2447
2448
2449static void zdiffAlgorithm2(zsetopsrc *src, long setnum, zset *dstzset, size_t *maxelelen, size_t *totelelen) {

Callers 1

zdiffFunction · 0.85

Calls 10

qsortFunction · 0.85
memsetFunction · 0.85
zuiInitIteratorFunction · 0.85
zuiNextFunction · 0.85
zuiFindFunction · 0.85
zuiNewSdsFromValueFunction · 0.85
zslInsertFunction · 0.85
sdslenFunction · 0.85
zuiClearIteratorFunction · 0.85
dictAddFunction · 0.70

Tested by

no test coverage detected