def test_copy_compact(self): _, env = testlib.temp_env() txn = env.begin(write=True) txn.put(B('a'), B('b')) txn.commit() dest_dir = testlib.temp_dir() env.copy(dest_dir, compact=True) assert os.path.exists(dest_dir + '/data.mdb') cenv = lmdb.open(dest_dir) ctxn = cenv.begin() assert ctxn.get(B('a')) == B('b') env.close() self.assertRaises(Exception, lambda: env.copy(testlib.temp_dir()))
def test_dupsort_putmulti_append(self): _, env = testlib.temp_env() db1 = env.open_db(B('db1'), dupsort=True) txn = env.begin(write=True, db=db1) with txn.cursor() as c: tups = [BT('a', 'value1'), BT('b', 'value1'), BT('b', 'value2')] assert (3, 3) == c.putmulti(tups, append=True)
def test_integerdup(self): _, env = testlib.temp_env() db1 = env.open_db(B('db1'), dupsort=True, integerdup=True) txn = env.begin(write=True, db=db1) assert txn.put(UINT_0001, UINT_0002) assert txn.put(UINT_0001, UINT_0001) assert txn.get(UINT_0001) == UINT_0001
def test_subdir_false_junk(self): path = testlib.temp_file() fp = open(path, 'wb') fp.write(B('A' * 8192)) fp.close() self.assertRaises(lmdb.InvalidError, lambda: lmdb.open(path, subdir=False))
def test_ok(self): path, env = testlib.temp_env() with env as env_: assert env_ is env with env.begin() as txn: txn.get(B('foo')) self.assertRaises(Exception, lambda: env.begin())
def test_stat(self): _, env = testlib.temp_env() stat = env.stat() for k in 'psize', 'depth', 'branch_pages', 'overflow_pages',\ 'entries': assert isinstance(stat[k], INT_TYPES), k assert stat[k] >= 0 assert stat['entries'] == 0 txn = env.begin(write=True) txn.put(B('a'), B('b')) txn.commit() stat = env.stat() assert stat['entries'] == 1 env.close() self.assertRaises(Exception, lambda: env.stat())
def test_copyfd_compact(self): path, env = testlib.temp_env() txn = env.begin(write=True) txn.put(B('a'), B('b')) txn.commit() dst_path = testlib.temp_file(create=False) fp = open(dst_path, 'wb') env.copyfd(fp.fileno(), compact=True) dstenv = lmdb.open(dst_path, subdir=False) dtxn = dstenv.begin() assert dtxn.get(B('a')) == B('b') env.close() self.assertRaises(Exception, lambda: env.copyfd(fp.fileno())) fp.close()
def test_info(self): _, env = testlib.temp_env() info = env.info() for k in 'map_addr', 'map_size', 'last_pgno', 'last_txnid', \ 'max_readers', 'num_readers': assert isinstance(info[k], INT_TYPES), k assert info[k] >= 0 assert info['last_txnid'] == 0 txn = env.begin(write=True) txn.put(B('a'), B('')) txn.commit() info = env.info() assert info['last_txnid'] == 1 env.close() self.assertRaises(Exception, lambda: env.info())
def setUp(self): self.path, self.env = testlib.temp_env() db = self.env.open_db(B('db1'), dupsort=True) self.txn = self.env.begin(db, write=True) for _ in range(2): putData(self.txn) self.c = self.txn.cursor() self.empty_entry = ('', '')
def test_big_reverse(self): path, env = testlib.temp_env() txn = env.begin(write=True) keys = [B('%05d' % i) for i in range(0xffff)] for k in keys: txn.put(k, k, append=True) assert list(txn.cursor().iterprev(values=False)) == list( reversed(keys))
def testSetRange(self): self.assertEqual(False, self.c.set_range(B('x'))) testlib.putData(self.txn) self.assertEqual(False, self.c.set_range(B('x'))) self.assertEqual(True, self.c.set_range(B('a'))) self.assertEqual(B('a'), self.c.key()) self.assertEqual(True, self.c.set_range(B('ba'))) self.assertEqual(B('baa'), self.c.key()) self.c.set_range(B('')) self.assertEqual(B('a'), self.c.key())
def test_dupsort(self): _, env = testlib.temp_env() db1 = env.open_db(B('db1'), dupsort=True) txn = env.begin(write=True, db=db1) assert txn.put(B('a'), B('a')) assert txn.put(B('a'), B('b')) assert txn.get(B('a')) == B('a')
def test_17bf75b12eb94d9903cd62329048b146d5313bad(self): """ me_txn0 previously cached MDB_TXN_ERROR permanently. Fixed by 17bf75b12eb94d9903cd62329048b146d5313bad. """ path, env = testlib.temp_env(map_size=4096 * 9, sync=False, max_spare_txns=0) for i in itertools.count(): try: with env.begin(write=True) as txn: txn.put(B(str(i)), B(str(i))) except lmdb.MapFullError: break # Should not crash with MDB_BAD_TXN: with env.begin(write=True) as txn: txn.delete(B('1'))
def test_readonly_env_sub_eperm(self): # https://github.com/dw/py-lmdb/issues/109 path, env = testlib.temp_env() env.close() env = lmdb.open(path, max_dbs=10, readonly=True) self.assertRaises( lmdb.ReadonlyError, lambda: env.open_db(B('node_schedules'), create=True))
def test_dupsort_put_append(self): _, env = testlib.temp_env() db1 = env.open_db(B('db1'), dupsort=True) txn = env.begin(write=True, db=db1) with txn.cursor() as c: assert c.put(B('a'), B('value1'), append=True) assert c.put(B('b'), B('value1'), append=True) assert c.put(B('b'), B('value2'), append=True)
def test_flags(self): path, env = testlib.temp_env() txn = env.begin(write=True) for flag, val in self.FLAG_SETS: key = B('%s-%s' % (flag, val)) db = env.open_db(key, txn=txn, **{flag: val}) assert db.flags(txn)[flag] == val txn.commit() # Test flag persistence. env.close() env = lmdb.open(path, max_dbs=10) txn = env.begin(write=True) for flag, val in self.FLAG_SETS: key = B('%s-%s' % (flag, val)) db = env.open_db(key, txn=txn) assert db.flags(txn)[flag] == val
def test_crash(self): path, env = testlib.temp_env() txn = env.begin(write=True) try: with txn.cursor() as curs: curs.put(123, 123) except: pass self.assertRaises(Exception, lambda: curs.get(B('foo')))
def test1(self): """Ensure MDB_NEXT is ignored on `c1' when it was previously positioned on the key that `c2' just deleted.""" txn = self.env.begin(write=True) cur = txn.cursor() while cur.first(): cur.delete() for i in range(1, 10): cur.put(O(ord('a') + i) * i, B('')) c1 = txn.cursor() c1f = c1.iternext(values=False) while next(c1f) != B('ddd'): pass c2 = txn.cursor() assert c2.set_key(B('ddd')) c2.delete() assert next(c1f) == B('eeee')
def test_copyfd_compact(self): path, env = testlib.temp_env() txn = env.begin(write=True) txn.put(B('a'), B('b')) txn.commit() dst_path = testlib.temp_file(create=False) fp = open(dst_path, 'wb') env.copyfd(fp.fileno()) dstenv = lmdb.open(dst_path, subdir=False) dtxn = dstenv.begin() assert dtxn.get(B('a')) == B('b') fp.close() # Test copy with transaction provided dst_path = testlib.temp_file(create=False) fp = open(dst_path, 'wb') with env.begin(write=True) as txn: copy_txn = env.begin() txn.put(B('b'), B('b')) assert dtxn.get(B('b')) is None if have_txn_patch: env.copyfd(fp.fileno(), compact=True, txn=copy_txn) dstenv = lmdb.open(dst_path, subdir=False) dtxn = dstenv.begin() assert dtxn.get(B('a')) == B('b') # Verify that the write that occurred outside the txn isn't seen in the # copy assert dtxn.get(B('b')) is None dstenv.close() else: self.assertRaises( Exception, lambda: env.copyfd(fp.fileno(), compact=True, txn=copy_txn)) env.close() self.assertRaises(Exception, lambda: env.copyfd(fp.fileno())) fp.close()
def test_monster(self): # Generate predictable sequence of sizes. rand = random.Random() rand.seed(0) txn = self.env.begin(write=True) keys = [] for i in range(20000): key = B('%06x' % i) val = B('x' * rand.randint(76, 350)) assert txn.put(key, val) keys.append(key) deleted = 0 for key in txn.cursor().iternext(values=False): assert txn.delete(key), key deleted += 1 assert deleted == len(keys), deleted
def test_empty(self): _, env = testlib.temp_env() db1 = env.open_db(B('db1')) txn = env.begin(write=True) txn.put(B('a'), B('a'), db=db1) assert txn.get(B('a'), db=db1) == B('a') txn.drop(db1, False) assert txn.get(B('a')) is None txn.drop(db1, False) # should succeed. assert txn.get(B('a')) is None
def test_copy_compact(self): _, env = testlib.temp_env() txn = env.begin(write=True) txn.put(B('a'), B('b')) txn.commit() dest_dir = testlib.temp_dir() env.copy(dest_dir, compact=True) assert os.path.exists(dest_dir + '/data.mdb') cenv = lmdb.open(dest_dir) ctxn = cenv.begin() assert ctxn.get(B('a')) == B('b') # Test copy with transaction provided dest_dir = testlib.temp_dir() with env.begin(write=True) as txn: copy_txn = env.begin() txn.put(B('b'), B('b')) assert ctxn.get(B('b')) is None if have_txn_patch: env.copy(dest_dir, compact=True, txn=copy_txn) assert os.path.exists(dest_dir + '/data.mdb') cenv = lmdb.open(dest_dir) ctxn = cenv.begin() assert ctxn.get(B('a')) == B('b') # Verify that the write that occurred outside the txn isn't seen in the # copy assert ctxn.get(B('b')) is None else: self.assertRaises( Exception, lambda: env.copy(dest_dir, compact=True, txn=copy_txn)) env.close() self.assertRaises(Exception, lambda: env.copy(testlib.temp_dir()))
def test_close(self): _, env = testlib.temp_env() # Attempting things should be ok. txn = env.begin(write=True) txn.put(B('a'), B('')) cursor = txn.cursor() list(cursor) cursor.first() it = iter(cursor) env.close() # Repeated calls are ignored: env.close() # Attempting to use invalid objects should crash. self.assertRaises(Exception, lambda: txn.cursor()) self.assertRaises(Exception, lambda: txn.commit()) self.assertRaises(Exception, lambda: cursor.first()) self.assertRaises(Exception, lambda: list(it)) # Abort should be OK though. txn.abort() # Attempting to start new txn should crash. self.assertRaises(Exception, lambda: env.begin())
def test_2list_preserve(self): l = [BT('a', ''), BT('a', '')] consumed, added = self.c.putmulti(l, overwrite=False) assert consumed == 2 assert added == 1 assert self.c.set_key(B('a')) assert self.c.delete() li = iter(l) consumed, added = self.c.putmulti(li, overwrite=False) assert consumed == 2 assert added == 1
def test_db(self): _, env = testlib.temp_env() maindb = env.open_db(None) db1 = env.open_db(B('db1')) txn = env.begin() assert txn.get(B('a'), db=db1) is None txn.abort() txn = env.begin(write=True) txn.put(B('a'), B('a'), db=db1) txn.commit() txn = env.begin() assert txn.get(B('a')) is None txn.abort() txn = env.begin(db=db1) assert txn.get(B('a')) == B('a') assert txn.get(B('a'), db=maindb) is None
def test_double_delete(self): _, env = testlib.temp_env() db1 = env.open_db(B('db1')) txn = env.begin(write=True, db=db1) txn.put(B('a'), B('a'), db=db1) txn.drop(db1) self.assertRaises(lmdb.InvalidParameterError, lambda: txn.get(B('a'), db=db1)) self.assertRaises(lmdb.InvalidParameterError, lambda: txn.drop(db1)) txn.commit() db1 = env.open_db(B('db1')) txn = env.begin(write=True, db=db1) txn.put(B('a'), B('a'), db=db1) txn.drop(db1) self.assertRaises(lmdb.InvalidParameterError, lambda: txn.get(B('a'), db=db1)) self.assertRaises(lmdb.InvalidParameterError, lambda: txn.drop(db1)) txn.commit()
def test_buffers(self): _, env = testlib.temp_env() txn = env.begin(write=True, buffers=True) assert txn.put(B('a'), B('a')) b = txn.get(B('a')) assert b is not None assert len(b) == 1 assert not isinstance(b, type(B(''))) txn.commit() txn = env.begin(buffers=False) b = txn.get(B('a')) assert b is not None assert len(b) == 1 assert isinstance(b, type(B(''))) txn.abort()
def testSetKey(self): self.assertRaises(Exception, (lambda: self.c.set_key(B('')))) self.assertEqual(False, self.c.set_key(B('missing'))) testlib.putData(self.txn) self.assertEqual(True, self.c.set_key(B('b'))) self.assertEqual(False, self.c.set_key(B('ba')))
def testKeyValueItemEmpty(self): self.assertEqual(B(''), self.c.key()) self.assertEqual(B(''), self.c.value()) self.assertEqual(BT('', ''), self.c.item())
def test_ok(self): path, env = testlib.temp_env() txn = env.begin(write=True) with txn.cursor() as curs: curs.put(B('foo'), B('123')) self.assertRaises(Exception, lambda: curs.get(B('foo')))