def pdel_before_put(kvdb: hse.Kvdb, kvs: hse.Kvs): with kvdb.transaction() as t: kvs.put(b"ab01", b"old", txn=t) t1 = kvdb.transaction() t1.begin() kvs.prefix_delete(b"ab", txn=t1) t2 = kvdb.transaction() t2.begin() try: kvs.put(b"ab01", b"val", txn=t2) assert False except hse.HseException as e: assert e.returncode == errno.ECANCELED t2.commit() assert kvs.get(b"ab01")[0] == b"old" t1.commit() assert kvs.get(b"ab01")[0] == None with kvdb.transaction() as t: kvs.put(b"ab01", b"new", txn=t) assert kvs.get(b"ab01")[0] == b"new"
def separate_keys(kvdb: hse.Kvdb, kvs: hse.Kvs): with kvdb.transaction() as t: kvs.put(b"ab01", b"val-cn", txn=t) with kvdb.transaction() as t: kvs.put(b"ab02", b"val-lc", txn=t) kvdb.sync() with kvdb.transaction() as t: kvs.put(b"ab03", b"val-c0", txn=t) with kvdb.transaction() as t: # Point Get assert kvs.get(b"ab01", txn=t) == b"val-cn" assert kvs.get(b"ab02", txn=t) == b"val-lc" assert kvs.get(b"ab03", txn=t) == b"val-c0" # Probe cnt, *kv = kvs.prefix_probe(b"ab0", txn=t) assert cnt == hse.KvsPfxProbeCnt.MUL assert kv == [b"ab03", b"val-c0"] # Cursor with kvs.cursor(filt=b"ab", txn=t) as c: # Read all keys assert c.read() == (b"ab01", b"val-cn") assert c.read() == (b"ab02", b"val-lc") assert c.read() == (b"ab03", b"val-c0") assert c.read() == (None, None) and c.eof == True # Seek, read c.seek(b"ab01") assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab02") assert c.read() == (b"ab02", b"val-lc") c.seek(b"ab03") assert c.read() == (b"ab03", b"val-c0") # Read, update, read c.seek(b"ab01") ##c.update() assert c.read() == (b"ab01", b"val-cn") ##c.update() assert c.read() == (b"ab02", b"val-lc") ##c.update() assert c.read() == (b"ab03", b"val-c0") ##c.update() assert c.read() == (None, None) and c.eof == True # Seek, update, read c.seek(b"ab01") ##c.update() assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab02") ##c.update() assert c.read() == (b"ab02", b"val-lc") c.seek(b"ab03") ##c.update() assert c.read() == (b"ab03", b"val-c0")
def verify(kvs: hse.Kvs, pfx: str, cnt: int): # [HSE_REVISIT] Getting all keys is too slow/freezes get_cnt = 100 if cnt < get_cnt: get_cnt = cnt for i in range(get_cnt): k = "{}-{:028}".format(pfx, i) val = kvs.get(k.encode()) assert val is not None assert val.decode() == k with kvs.cursor(pfx.encode()) as c: assert sum(1 for _ in c.items()) == cnt with kvs.cursor(pfx.encode(), flags=hse.CursorFlag.REVERSE) as rc: assert sum(1 for _ in rc.items()) == cnt # create, seek, reads with kvs.cursor(pfx.encode()) as c: c.seek(pfx.encode()) assert sum(1 for _ in c.items()) == cnt with kvs.cursor(pfx.encode(), flags=hse.CursorFlag.REVERSE) as rc: # Bump up the last character so prefix is larger than all keys ch = pfx[-1] i = ord(ch[0]) i += 1 seek_key = pfx[:-1] + chr(i) rc.seek(seek_key.encode()) assert sum(1 for _ in rc.items()) == cnt
def duplicate_c0_lc(kvdb: hse.Kvdb, kvs: hse.Kvs, cursor_sync: bool = False): with kvdb.transaction() as t: kvs.put(b"ab01", b"val-cn", txn=t) with kvdb.transaction() as t: kvs.put(b"ab02", b"val-lc", txn=t) kvdb.sync() with kvdb.transaction() as t: kvs.put(b"ab02", b"val-c0", txn=t) with kvdb.transaction() as t: with kvs.cursor(filt=b"ab", txn=t) as c: if cursor_sync: kvdb.sync() # Point Get assert kvs.get(b"ab01", txn=t) == b"val-cn" assert kvs.get(b"ab02", txn=t) == b"val-c0" # Probe cnt, *kv = kvs.prefix_probe(b"ab0", txn=t) assert cnt == hse.KvsPfxProbeCnt.MUL assert kv == [b"ab02", b"val-c0"] # Read all keys assert c.read() == (b"ab01", b"val-cn") assert c.read() == (b"ab02", b"val-c0") assert c.read() == (None, None) and c.eof == True # Seek, read c.seek(b"ab00") assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab01") assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab02") assert c.read() == (b"ab02", b"val-c0") c.seek(b"ab03") assert c.read() == (None, None) and c.eof == True # Read, update, read c.seek(b"ab01") ##c.update() assert c.read() == (b"ab01", b"val-cn") ##c.update() assert c.read() == (b"ab02", b"val-c0") ##c.update() assert c.read() == (None, None) and c.eof == True # Seek, update, read c.seek(b"ab01") ##c.update() assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab02") ##c.update() assert c.read() == (b"ab02", b"val-c0") c.seek(b"ab03") ##c.update() assert c.read() == (None, None) and c.eof == True
def tombs_c0_lc(kvdb: hse.Kvdb, kvs: hse.Kvs, cursor_sync: bool = False): with kvdb.transaction() as t: kvs.put(b"ab01", b"val-cn", txn=t) with kvdb.transaction() as t: kvs.put(b"ab02", b"val-lc", txn=t) kvdb.sync() with kvdb.transaction() as t: kvs.delete(b"ab02", txn=t) # Cursor with kvs.cursor(filt=b"ab") as c: if cursor_sync: kvdb.sync() # Point Get assert kvs.get(b"ab01")[0] == b"val-cn" assert kvs.get(b"ab02")[0] == None # Probe cnt, k, _, v, _ = kvs.prefix_probe(b"ab0") assert cnt == hse.KvsPfxProbeCnt.ONE assert (k, v) == (b"ab01", b"val-cn") # Read all keys assert c.read() == (b"ab01", b"val-cn") assert c.read() == (None, None) and c.eof == True # Seek, read c.seek(b"ab00") assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab01") assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab02") assert c.read() == (None, None) and c.eof == True # Read, update, read c.seek(b"ab01") c.update_view() assert c.read() == (b"ab01", b"val-cn") c.update_view() assert c.read() == (None, None) and c.eof == True # Seek, update, read c.seek(b"ab01") c.update_view() assert c.read() == (b"ab01", b"val-cn") c.seek(b"ab02") c.update_view() assert c.read() == (None, None) and c.eof == True
def ptombs_c0_lc(kvdb: hse.Kvdb, kvs: hse.Kvs, cursor_sync: bool = False): with kvdb.transaction() as t: kvs.put(b"ab01", b"val-cn", txn=t) with kvdb.transaction() as t: kvs.put(b"ab02", b"val-lc", txn=t) kvdb.sync() with kvdb.transaction() as t: kvs.prefix_delete(b"ab", txn=t) # Cursor with kvdb.transaction() as t: with kvs.cursor(filt=b"ab", txn=t) as c: if cursor_sync: kvdb.sync() # Point Get assert kvs.get(b"ab01", txn=t) == None assert kvs.get(b"ab02", txn=t) == None # Probe cnt, *_ = kvs.prefix_probe(b"ab0", txn=t) assert cnt == hse.KvsPfxProbeCnt.ZERO # Read all keys assert c.read() == (None, None) and c.eof == True # Seek, read c.seek(b"ab00") assert c.read() == (None, None) and c.eof == True c.seek(b"ab01") assert c.read() == (None, None) and c.eof == True c.seek(b"ab02") assert c.read() == (None, None) and c.eof == True c.seek(b"ab03") assert c.read() == (None, None) and c.eof == True # Read, update, read c.seek(b"ab01") ##c.update() assert c.read() == (None, None) and c.eof == True # Seek, update, read c.seek(b"ab01") ##c.update() assert c.read() == (None, None) and c.eof == True c.seek(b"ab02") ##c.update() assert c.read() == (None, None) and c.eof == True
def run_test(kvdb: hse.Kvdb, kvs: hse.Kvs): t0 = kvdb.transaction() t0.begin() t1 = kvdb.transaction() t1.begin() kvs.put(b"aa1", b"uncommitted-aa1", txn=t0) # commit kvs.put(b"aa2", b"uncommitted-aa2", txn=t0) # commit kvs.put(b"aa3", b"uncommitted-aa3", txn=t1) # abort val = kvs.get(b"aa1", txn=t0) assert val == b"uncommitted-aa1" with kvdb.transaction() as t5: kvs.put(b"ab1", b"val1", txn=t5) kvs.put(b"ab2", b"val2", txn=t5) kvs.put(b"ab3", b"val3", txn=t5) kvdb.sync() # Get from C0 with kvdb.transaction() as t5: val = kvs.get(b"ab3", txn=t5) assert val == b"val3" # Get from LC val = kvs.get(b"aa1", txn=t0) assert val == b"uncommitted-aa1" # uncommitted data from current txn val = kvs.get(b"aa3", txn=t0) assert val == None # uncommitted data from some other txn val = kvs.get(b"aa3", txn=t1) assert val == b"uncommitted-aa3" # uncommitted data from current txn t0.commit() t1.abort() kvdb.sync() # Get from CN. Keys were previously in LC. with kvdb.transaction() as t5: # Committed. Should be visible val = kvs.get(b"aa1", txn=t5) assert val == b"uncommitted-aa1" # Aborted. Should not see this key. val = kvs.get(b"aa3", txn=t5) assert val == None pass
def run_test_3(kvdb: hse.Kvdb, kvs: hse.Kvs): with kvdb.transaction() as t: kvs.put(b"ghi01", b"val1", txn=t) # LC kvdb.sync() with kvdb.transaction() as t: kvs.put(b"ghi01", b"val2", txn=t) # C0 with kvs.cursor(filt=b"ghi") as c: kvdb.sync( ) # Moves val2 to cn while val1 stays in LC until it's garbage collected for (k, v) in c.items(): getval = kvs.get(k) assert v assert v.decode() == "val2" assert v == getval pass
def run_test_4(kvdb: hse.Kvdb, kvs: hse.Kvs): with kvdb.transaction() as t: kvs.put(b"jkl01", b"val1", txn=t) kvs.put(b"jkl02", b"val1", txn=t) kvs.put(b"jkl03", b"val1", txn=t) with kvdb.transaction() as t: kvs.prefix_delete(b"jkl", txn=t) kvs.put(b"jkl03", b"val2", txn=t) kvdb.sync() assert kvs.get(b"jkl01", txn=t) is None assert kvs.get(b"jkl03", txn=t) == b"val2" cnt, *kv = kvs.prefix_probe(b"jkl", txn=t) assert cnt == hse.KvsPfxProbeCnt.ONE assert kv == [b"jkl03", b"val2"] pass
def run_test_3(kvdb: hse.Kvdb, kvs: hse.Kvs): with kvdb.transaction() as t: kvs.put(b"jkl01", b"val1", txn=t) kvs.put(b"jkl02", b"val1", txn=t) kvs.put(b"jkl03", b"val1", txn=t) with kvdb.transaction() as t: kvs.prefix_delete(b"jkl", txn=t) kvs.put(b"jkl03", b"val2", txn=t) kvdb.sync() assert kvs.get(b"jkl01", txn=t)[0] is None assert kvs.get(b"jkl03", txn=t)[0] == b"val2" cnt, k, _, v, _ = kvs.prefix_probe(b"jkl", txn=t) assert cnt == hse.KvsPfxProbeCnt.ONE assert (k, v) == (b"jkl03", b"val2") pass
def pdel_commits(kvdb: hse.Kvdb, kvs: hse.Kvs): with kvdb.transaction() as t: kvs.put(b"ab01", b"old", txn=t) t1 = kvdb.transaction() t1.begin() kvs.prefix_delete(b"ab", txn=t1) t2 = kvdb.transaction() t2.begin() # Commit pdel. t2 began before this point, so it should not allow puts. t1.commit() try: kvs.put(b"ab01", b"val", txn=t2) assert False except hse.HseException as e: assert e.returncode == errno.ECANCELED t2.commit() assert kvs.get(b"ab01")[0] == None
def run_test(kvdb: hse.Kvdb, kvs: hse.Kvs): with kvdb.transaction() as t5: kvs.put(b"ab1", b"val1", txn=t5) kvs.put(b"ab2", b"val2", txn=t5) kvs.put(b"ab3", b"val3", txn=t5) t0 = kvdb.transaction() t0.begin() t1 = kvdb.transaction() t1.begin() kvs.put(b"aa1", b"uncommitted-aa1", txn=t0) # commit kvs.put(b"aa2", b"uncommitted-aa2", txn=t0) # commit kvs.put(b"aa3", b"uncommitted-aa3", txn=t1) # abort val = kvs.get(b"aa1", txn=t0)[0] assert val == b"uncommitted-aa1" with kvs.cursor(txn=t0) as c: assert sum(1 for _ in c.items()) == 5 c.seek(b"aa2") kv = c.read() assert kv == (b"aa2", b"uncommitted-aa2") kv = c.read() assert kv == (b"ab1", b"val1") kv = c.read() assert kv == (b"ab2", b"val2") kv = c.read() assert kv == (b"ab3", b"val3") c.read() assert c.eof c.seek(b"ab2") kv = c.read() assert kv == (b"ab2", b"val2") kv = c.read() assert kv == (b"ab3", b"val3") c.read() assert c.eof with kvs.cursor( txn=t0, flags=hse.CursorCreateFlag.REV ) as c: assert sum(1 for _ in c.items()) == 5 c.seek(b"aa2") kv = c.read() assert kv == (b"aa2", b"uncommitted-aa2") kv = c.read() assert kv == (b"aa1", b"uncommitted-aa1") c.read() assert c.eof c.seek(b"ab2") kv = c.read() assert kv == (b"ab2", b"val2") kv = c.read() assert kv == (b"ab1", b"val1") kv = c.read() assert kv == (b"aa2", b"uncommitted-aa2") kv = c.read() assert kv == (b"aa1", b"uncommitted-aa1") c.read() assert c.eof kvdb.sync() # Get from C0 with kvdb.transaction() as t5: val = kvs.get(b"ab3", txn=t5)[0] assert val == b"val3" # Get from LC val = kvs.get(b"aa1", txn=t0)[0] assert val == b"uncommitted-aa1" # uncommitted data from current txn val = kvs.get(b"aa3", txn=t0)[0] assert val == None # uncommitted data from some other txn val = kvs.get(b"aa3", txn=t1)[0] assert val == b"uncommitted-aa3" # uncommitted data from current txn t0.commit() t1.abort() kvdb.sync() # Get from CN. Keys were previously in LC. with kvdb.transaction() as t5: # Committed. Should be visible val = kvs.get(b"aa1", txn=t5)[0] assert val == b"uncommitted-aa1" # Aborted. Should not see this key. val = kvs.get(b"aa3", txn=t5)[0] assert val == None pass
def duplicate_lc_cn(kvdb: hse.Kvdb, kvs: hse.Kvs, cursor_sync: bool = False): with kvdb.transaction() as t: kvs.put(b"ab01", b"val-cn", txn=t) with kvdb.transaction() as t: kvs.put(b"ab01", b"val-lc", txn=t) kvdb.sync() with kvdb.transaction() as t: kvs.put(b"ab02", b"val-c0", txn=t) # Cursor with kvs.cursor(filt=b"ab") as c: probe_kv = (b"ab02", b"val-c0") if cursor_sync: probe_kv = (b"ab01", b"val-lc") kvdb.sync() # Point Get assert kvs.get(b"ab01")[0] == b"val-lc" assert kvs.get(b"ab02")[0] == b"val-c0" # Probe cnt, k, _, v, _ = kvs.prefix_probe(b"ab0") assert cnt == hse.KvsPfxProbeCnt.MUL assert (k, v) == probe_kv # Read all keys assert c.read() == (b"ab01", b"val-lc") assert c.read() == (b"ab02", b"val-c0") assert c.read() == (None, None) and c.eof == True # Seek, read c.seek(b"ab00") assert c.read() == (b"ab01", b"val-lc") c.seek(b"ab01") assert c.read() == (b"ab01", b"val-lc") c.seek(b"ab02") assert c.read() == (b"ab02", b"val-c0") c.seek(b"ab03") assert c.read() == (None, None) and c.eof == True # Read, update, read c.seek(b"ab01") c.update_view() assert c.read() == (b"ab01", b"val-lc") c.update_view() assert c.read() == (b"ab02", b"val-c0") c.update_view() assert c.read() == (None, None) and c.eof == True # Seek, update_view, read c.seek(b"ab01") c.update_view() assert c.read() == (b"ab01", b"val-lc") c.seek(b"ab02") c.update_view() assert c.read() == (b"ab02", b"val-c0") c.seek(b"ab03") c.update_view() assert c.read() == (None, None) and c.eof == True