@@ -1501,6 +1501,48 @@ int raxIteratorPrevStep(raxIterator *it, int noup) {
15011501 }
15021502}
15031503
1504+
1505+ int raxIteratorUpStep (raxIterator * it ) {
1506+ if (it -> flags & RAX_ITER_EOF ) {
1507+ return 1 ;
1508+ } else if (it -> flags & RAX_ITER_JUST_SEEKED ) {
1509+ it -> flags &= ~RAX_ITER_JUST_SEEKED ;
1510+ return 1 ;
1511+ }
1512+
1513+ /* Save key len, stack items and the node where we are currently
1514+ * so that on iterator EOF we can restore the current key and state. */
1515+ size_t orig_key_len = it -> key_len ;
1516+ size_t orig_stack_items = it -> stack .items ;
1517+ raxNode * orig_node = it -> node ;
1518+
1519+ while (1 ) {
1520+ /* Already on head? Can't go up, iteration finished. */
1521+ if (it -> node == it -> rt -> head ) {
1522+ it -> flags |= RAX_ITER_EOF ;
1523+ it -> stack .items = orig_stack_items ;
1524+ it -> key_len = orig_key_len ;
1525+ it -> node = orig_node ;
1526+ return 1 ;
1527+ }
1528+
1529+ it -> node = raxStackPop (& it -> stack );
1530+
1531+ /* Adjust the current key to represent the node we are
1532+ * at. */
1533+ int todel = it -> node -> iscompr ? it -> node -> size : 1 ;
1534+ raxIteratorDelChars (it ,todel );
1535+
1536+ /* Return the key: this could be the key we found scanning a new
1537+ * subtree, or if we did not find a new subtree to explore here,
1538+ * before giving up with this node, check if it's a key itself. */
1539+ if (it -> node -> iskey ) {
1540+ it -> data = raxGetData (it -> node );
1541+ return 1 ;
1542+ }
1543+ }
1544+ }
1545+
15041546/* Seek an iterator at the specified element.
15051547 * Return 0 if the seek failed for syntax error or out of memory. Otherwise
15061548 * 1 is returned. When 0 is returned for out of memory, errno is set to
@@ -1718,6 +1760,18 @@ int raxPrev(raxIterator *it) {
17181760 return 1 ;
17191761}
17201762
1763+ int raxUp (raxIterator * it ) {
1764+ if (!raxIteratorUpStep (it )) {
1765+ errno = ENOMEM ;
1766+ return 0 ;
1767+ }
1768+ if (it -> flags & RAX_ITER_EOF ) {
1769+ errno = 0 ;
1770+ return 0 ;
1771+ }
1772+ return 1 ;
1773+ }
1774+
17211775/* Compare the key currently pointed by the iterator to the specified
17221776 * key according to the specified operator. Returns 1 if the comparison is
17231777 * true, otherwise 0 is returned. */
0 commit comments