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

Function raxRandomWalk

src/rax.c:1735–1773  ·  view source on GitHub ↗

Perform a random walk starting in the current position of the iterator. * Return 0 if the tree is empty or on out of memory. Otherwise 1 is returned * and the iterator is set to the node reached after doing a random walk * of 'steps' steps. If the 'steps' argument is 0, the random walk is performed * using a random number of steps between 1 and two times the logarithm of * the number of eleme

Source from the content-addressed store, hash-verified

1733 * tree certain keys will be reported much more often than others. At least
1734 * this function should be able to explore every possible element eventually. */
1735int raxRandomWalk(raxIterator *it, size_t steps) {
1736 if (it->rt->numele == 0) {
1737 it->flags |= RAX_ITER_EOF;
1738 return 0;
1739 }
1740
1741 if (steps == 0) {
1742 size_t fle = 1+floor(log(it->rt->numele));
1743 fle *= 2;
1744 steps = 1 + rand() % fle;
1745 }
1746
1747 raxNode *n = it->node;
1748 while(steps > 0 || !n->iskey) {
1749 int numchildren = n->iscompr ? 1 : n->size;
1750 int r = rand() % (numchildren+(n != it->rt->head));
1751
1752 if (r == numchildren) {
1753 /* Go up to parent. */
1754 n = raxStackPop(&it->stack);
1755 int todel = n->iscompr ? n->size : 1;
1756 raxIteratorDelChars(it,todel);
1757 } else {
1758 /* Select a random child. */
1759 if (n->iscompr) {
1760 if (!raxIteratorAddChars(it,n->data,n->size)) return 0;
1761 } else {
1762 if (!raxIteratorAddChars(it,n->data+r,1)) return 0;
1763 }
1764 raxNode **cp = raxNodeFirstChildPtr(n)+r;
1765 if (!raxStackPush(&it->stack,n)) return 0;
1766 memcpy(&n,cp,sizeof(n));
1767 }
1768 if (n->iskey) steps--;
1769 }
1770 it->node = n;
1771 it->data = raxGetData(it->node);
1772 return 1;
1773}
1774
1775/* Compare the key currently pointed by the iterator to the specified
1776 * key according to the specified operator. Returns 1 if the comparison is

Callers 1

trackingLimitUsedSlotsFunction · 0.85

Calls 5

raxStackPopFunction · 0.85
raxIteratorDelCharsFunction · 0.85
raxIteratorAddCharsFunction · 0.85
raxStackPushFunction · 0.85
raxGetDataFunction · 0.85

Tested by

no test coverage detected