Skip to content

Commit 001e388

Browse files
committed
multiScanByPrefBack cleanup
1 parent b5a3fdb commit 001e388

4 files changed

Lines changed: 32 additions & 38 deletions

File tree

synapse/datamodel.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,6 @@ def __init__(self, core=None):
669669
'computed': True,
670670
'doc': 'The form of node which is referenced.'}),
671671

672-
('ndef', ('ndef', {}), {
673-
'computed': True,
674-
'doc': 'The (form, valu) of the node which is referenced.'}),
675-
676672
('value', ('data', {}), {
677673
'computed': True,
678674
'doc': 'The primary property value of the node which is referenced.'}),

synapse/lib/lmdbslab.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

synapse/lib/types.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def postTypeInit(self):
580580
if (typeopts := self.opts.get('typeopts')) is None:
581581
typeopts = {}
582582

583-
# TODO can we polyprop with multiple type+typeopts defs????
583+
# TODO allow polyprop with multiple type+typeopts defs?
584584
if not typeopts:
585585
if typename in self.modl.ifaces or ((forminfo := self.modl.forminfos.get(typename)) is not None and not forminfo.get('runt')):
586586
typename = (typename,)
@@ -707,17 +707,6 @@ async def _normPyTuple(self, valu, view=None, newinfos=None):
707707
'virts': {vkey: dict(vval) for vkey, vval in virts.items()}
708708
}
709709

710-
# if virts:
711-
# realvirts = collections.defaultdict(list)
712-
# for norm in norms:
713-
# if (virt := virts.get(norm)) is not None:
714-
# for vkey, vval in virt.items():
715-
# realvirts[vkey].append(vval)
716-
#
717-
# norminfo['virts'] = dict(realvirts)
718-
# else:
719-
# norminfo['virts'] = {}
720-
721710
return tuple(norms), norminfo
722711

723712
def repr(self, valu):
@@ -2357,7 +2346,6 @@ async def getStorCmprs(self, cmpr, valu, virts=None):
23572346
return (('ndef=', valu.valu, s_layer.STOR_TYPE_POLY),)
23582347
valu = valu.valu[1]
23592348

2360-
# TODO better runtnode handling?
23612349
elif isinstance(valu, s_node.RuntNode):
23622350
if self.formfilter(valu.form):
23632351
return (('=', valu.ndef[1], valu.form.type.stortype | s_layer.STOR_FLAG_POLY),)
@@ -2370,7 +2358,6 @@ async def getStorCmprs(self, cmpr, valu, virts=None):
23702358

23712359
for ntyp in self.modl.getTypeSet(forms=self.forms, interfaces=self.ifaces):
23722360
try:
2373-
# TODO: better way to keep these ordered?
23742361
for ncmpr in await ntyp.getStorCmprs(cmpr, valu, virts=virts):
23752362
cmprs[ncmpr] = True
23762363
isvalid = True

synapse/tests/test_lib_lmdbslab.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ async def test_lmdbslab_multiscan(self):
122122
multilen = 8
123123

124124
self.eq((), await s_t_utils.alist(slab.multiScanByPref(pref, multilen, b'newp', db=testdb)))
125+
self.eq((), await s_t_utils.alist(slab.multiScanByPrefBack(pref, multilen, b'\xff', db=testdb)))
126+
self.eq((), await s_t_utils.alist(slab.multiScanByPrefBack(b'\xff', multilen, b'\xff', db=testdb)))
125127

126128
await slab.put(pref + s_common.int64en(0) + b'abar', b'haha', db=testdb)
127129
await slab.put(pref + s_common.int64en(0) + b'afoo', b'haha', db=testdb)
@@ -147,6 +149,12 @@ async def test_lmdbslab_multiscan(self):
147149

148150
self.eq(exp[::-1], await s_t_utils.alist(slab.multiScanByPrefBack(pref, multilen, b'a', db=testdb)))
149151

152+
self.eq((), await s_t_utils.alist(slab.multiScanByPrefBack(pref, multilen, b'0', db=testdb)))
153+
self.eq((), await s_t_utils.alist(slab.multiScanByPrefBack(pref, multilen, b'abaq', db=testdb)))
154+
self.eq((), await s_t_utils.alist(slab.multiScanByPrefBack(pref, multilen, b'\xff', db=testdb)))
155+
self.eq((), await s_t_utils.alist(slab.multiScanByPrefBack(pref, multilen, b'\xfe', db=testdb)))
156+
self.eq((), await s_t_utils.alist(slab.multiScanByPrefBack(pref, multilen, b'\xfe', db=testdb)))
157+
150158
exp = (
151159
(pref + s_common.int64en(0) + b'abar', b'haha'),
152160
(pref + s_common.int64en(2) + b'afaz', b'haha'),

0 commit comments

Comments
 (0)