Skip to content

Commit

Permalink
Take advantage of LMDB append optimization (#1912)
Browse files Browse the repository at this point in the history
* Take advantage of LMDB append optimization

* Assert return values of put/putmulti

* Remove an inappropriate use of append
  • Loading branch information
jnwatson authored Oct 22, 2020
1 parent b2423a4 commit df35952
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 4 additions & 3 deletions synapse/lib/lmdbslab.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ async def puts(self, name, items, reqid=None):

for item in items:

self.slab.put(abrv + s_common.int64en(offs), s_msgpack.en(item), db=self.qdata)
putv = self.slab.put(abrv + s_common.int64en(offs), s_msgpack.en(item), db=self.qdata)
assert putv, 'Put failed'

self.sizes.inc(name, 1)
offs = self.offsets.inc(name, 1)
Expand Down Expand Up @@ -1421,9 +1422,9 @@ def pop(self, lkey, db=None):
def delete(self, lkey, val=None, db=None):
return self._xact_action(self.delete, lmdb.Transaction.delete, lkey, val, db=db)

def put(self, lkey, lval, dupdata=False, overwrite=True, db=None):
def put(self, lkey, lval, dupdata=False, overwrite=True, append=False, db=None):
return self._xact_action(self.put, lmdb.Transaction.put, lkey, lval, dupdata=dupdata, overwrite=overwrite,
db=db)
append=append, db=db)

def replace(self, lkey, lval, db=None):
'''
Expand Down
7 changes: 5 additions & 2 deletions synapse/lib/slabseqn.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def add(self, item, indx=None):
return indx

indx = self.indx
self.slab.put(s_common.int64en(indx), s_msgpack.en(item), db=self.db)
retn = self.slab.put(s_common.int64en(indx), s_msgpack.en(item), append=True, db=self.db)
assert retn, "Not adding the largest index"

self.indx += 1

Expand Down Expand Up @@ -89,9 +90,11 @@ def save(self, items):

rows.append((lkey, byts))

self.slab.putmulti(rows, append=True, db=self.db)
retn = self.slab.putmulti(rows, append=True, db=self.db)
took = s_common.now() - tick

assert retn, "Not adding the largest indices"

origindx = self.indx
self.indx = indx

Expand Down
5 changes: 4 additions & 1 deletion synapse/tests/test_lib_lmdbslab.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ async def test_lmdbslab_base(self):

slab.put(b'\x00\x01', b'hehe', db=baz)
slab.put(b'\xff', b'haha', db=baz)
slab.put(b'\xff\xff', b'hoho', db=baz)

slab.put(b'\xff\xff', b'hoho', append=True, db=baz) # Should succeed
slab.put(b'\xaa\xff', b'hoho', append=True, db=baz) # Should fail (not the last key)

self.true(slab.dirty)

Expand All @@ -151,6 +153,7 @@ async def test_lmdbslab_base(self):
self.false(slab.prefexists(b'\x02', db=baz))
self.true(slab.prefexists(b'\xff\xff', db=baz))
self.false(slab.prefexists(b'\xff\xff', db=foo))
self.false(slab.prefexists(b'\xaa\xff', db=baz))

self.true(slab.rangeexists(b'\x00', b'\x01', db=baz))
self.true(slab.rangeexists(b'\x00\x00', b'\x00\x04', db=baz))
Expand Down

0 comments on commit df35952

Please sign in to comment.