@@ -1673,36 +1673,39 @@ async def multiScanByPrefBack(self, pref, multilen, byts, db=None):
16731673 size = preflen + len (byts )
16741674 maxv = int .from_bytes (b'\xff ' * multilen , 'big' )
16751675
1676+ intpref = int .from_bytes (pref , 'big' )
1677+ maxpref = int .from_bytes (b'\xff ' * len (pref ), 'big' )
1678+
1679+ intbyts = int .from_bytes (byts , 'big' )
1680+ maxbyts = int .from_bytes (b'\xff ' * len (byts ), 'big' )
1681+
16761682 async def scangenr (pval ):
16771683 with ScanBack (self , db ) as scan :
16781684 skey = pval .to_bytes (multilen , 'big' )
16791685
16801686 while True :
1681- intoff = int .from_bytes (byts , "big" )
1682- intoff += 1
1683- try :
1684- nextbyts = intoff .to_bytes (len (byts ), "big" )
1687+ if (intbyts + 1 ) <= maxbyts :
1688+ nextbyts = (intbyts + 1 ).to_bytes (len (byts ), "big" )
16851689 nextpref = pref + skey + nextbyts
1686- if not scan .set_range (nextpref ):
1687- return
1688-
1689- if scan .atitem [0 ] == nextpref :
1690- if not scan .next_key ():
1691- return
1692-
1693- except OverflowError :
1694- if (pval := int .from_bytes (skey , 'big' ) + 1 ) > maxv :
1695- if not scan .first ():
1696- return
16971690
1691+ elif (pval := int .from_bytes (skey , 'big' ) + 1 ) <= maxv :
1692+ # Prefix can't be incremented, try to increment multi key instead
16981693 skey = pval .to_bytes (multilen , 'big' )
16991694 nextpref = pref + skey
1700- if not scan .set_range (nextpref ) and not scan .first ():
1701- return
17021695
1703- if scan .atitem [0 ] == nextpref :
1704- if not scan .next_key ():
1705- return
1696+ elif (intpref + 1 ) <= maxpref :
1697+ # Multi key can't be incremented, try to increment prefix
1698+ nextpref = (intpref + 1 ).to_bytes (len (pref ), "big" )
1699+
1700+ else :
1701+ # No room to increment any keys, just go to the last item in the db
1702+ nextpref = None
1703+
1704+ if nextpref is not None and scan .set_range (nextpref ):
1705+ if scan .atitem [0 ] == nextpref and not scan .next_key ():
1706+ return
1707+ elif not scan .first ():
1708+ return
17061709
17071710 fkey = scan .atitem [0 ]
17081711 if not fkey .startswith (pref ):
0 commit comments