Esempio n. 1
0
def run_test_2(kvdb: hse.Kvdb, kvs: hse.Kvs):
    t1 = kvdb.transaction()
    t1.begin()
    kvs.prefix_delete(b"def", txn=t1)  # LC
    kvdb.sync()
    kvs.prefix_delete(b"def", txn=t1)  # LC
    kvdb.sync()

    with kvdb.transaction() as t:
        kvs.put(b"def01", b"val1", txn=t)  # C0
        kvs.put(b"def02", b"val1", txn=t)  # C0
        kvs.put(b"def03", b"val1", txn=t)  # C0
        kvs.put(b"def04", b"val1", txn=t)  # C0
    t1.commit()  # ptomb has higher seqno

    with kvs.cursor(filt=b"def") as c:
        c.read()
        assert c.eof is True

        ##c.update()
        c.read()
        assert c.eof is True

        c.seek(b"def03")
        c.read()
        assert c.eof is True

        c.seek(b"def02")
        ##c.update()
        c.read()
        assert c.eof is True
Esempio n. 2
0
def create_pfx_data(kvdb: hse.Kvdb, kvs: hse.Kvs, pfx: int):
    pfx_bytes = pfx.to_bytes(4, byteorder="big")

    with kvdb.transaction() as txn:
        for sfx in range(NUM_KEYS):
            key = pfx_bytes + sfx.to_bytes(1, byteorder="big")
            kvs.put(key, key, txn=txn)
Esempio n. 3
0
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 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) == None

            # Probe
            cnt, *kv = kvs.prefix_probe(b"ab0", txn=t)
            assert cnt == hse.KvsPfxProbeCnt.ONE
            assert kv == [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()
            assert c.read() == (b"ab01", b"val-cn")
            ##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() == (None, None) and c.eof == True
Esempio n. 4
0
def long_put_abort(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.put(b"ab01", b"val", txn=t1)

    t2 = kvdb.transaction()
    t2.begin()

    t1.commit()

    t3 = kvdb.transaction()
    t3.begin()

    kvs.prefix_delete(b"ab", txn=t3)
    t3.abort()

    try:
        kvs.put(b"ab01", b"val2", txn=t2)
        assert False
    except hse.HseException as e:
        assert e.returncode == errno.ECANCELED
    t2.abort()

    with kvdb.transaction() as t:
        kvs.put(b"ab01", b"new", txn=t)
    assert kvs.get(b"ab01")[0] == b"new"
Esempio n. 5
0
def ptombs_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.prefix_delete(b"ab", 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:
        if cursor_sync:
            kvdb.sync()

        # Point Get
        assert kvs.get(b"ab01")[0] == None
        assert kvs.get(b"ab02")[0] == b"val-c0"

        # Probe
        cnt, k, _, v, _ = kvs.prefix_probe(b"ab0")
        assert cnt == hse.KvsPfxProbeCnt.ONE
        assert (k, v) == (b"ab02", b"val-c0")

        # Read all keys
        assert c.read() == (b"ab02", b"val-c0")
        assert c.read() == (None, None) and c.eof == True

        # Seek, read
        c.seek(b"ab01")
        assert c.read() == (b"ab02", b"val-c0")
        c.seek(b"ab02")
        assert c.read() == (b"ab02", b"val-c0")

        # Read, update, read
        c.seek(b"ab01")
        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, read
        c.seek(b"ab01")
        c.update_view()
        assert c.read() == (b"ab02", b"val-c0")
        c.seek(b"ab02")
        c.update_view()
        assert c.read() == (b"ab02", b"val-c0")
Esempio n. 6
0
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
Esempio n. 7
0
def put_before_pdel(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.put(b"ab01", b"val", txn=t1)

    t2 = kvdb.transaction()
    t2.begin()
    try:
        kvs.prefix_delete(b"ab", txn=t2)
        assert False
    except hse.HseException as e:
        assert e.returncode == errno.ECANCELED

    t1.commit()
    t2.commit()
    assert kvs.get(b"ab01")[0] == b"val"
Esempio n. 8
0
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", flags=hse.CursorFlag.REVERSE) as c:
        if cursor_sync:
            kvdb.sync()

        # 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"ab02")
        assert c.read() == (b"ab01", b"val-cn")
        c.seek(b"ab01")
        assert c.read() == (b"ab01", b"val-cn")
        c.seek(b"ab00")
        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"ab00")
        c.update_view()
        assert c.read() == (None, None) and c.eof == True
Esempio n. 9
0
def run_test_1(kvdb: hse.Kvdb, kvs: hse.Kvs):
    with kvdb.transaction() as t:
        kvs.put(b"abc01", b"val0", txn=t)  # LC
        kvs.put(b"abc02", b"val0", txn=t)  # LC
        kvs.prefix_delete(b"abc", txn=t)  # LC
        kvdb.sync()
        kvs.put(b"abc01", b"val1", txn=t)  # C0
        kvs.put(b"abc02", b"val1", txn=t)  # C0
        kvs.put(b"abc03", b"val1", txn=t)  # C0
        kvs.put(b"abc04", b"val1", txn=t)  # C0

        with kvs.cursor(filt=b"abc", txn=t) as c:
            kv = c.read()
            assert kv == (b"abc01", b"val1")
            kv = c.read()
            assert kv == (b"abc02", b"val1")

            ##c.update()
            kv = c.read()
            assert kv == (b"abc03", b"val1")

            c.seek(b"abc02")
            ##c.update()
            kv = c.read()
            assert kv == (b"abc02", b"val1")
Esempio n. 10
0
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")
Esempio n. 11
0
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
Esempio n. 12
0
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
Esempio n. 13
0
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)

    # Cursor
    with kvs.cursor(filt=b"ab", flags=hse.CursorFlag.REVERSE) as c:
        # Read all keys
        assert c.read() == (b"ab03", b"val-c0")
        assert c.read() == (b"ab02", b"val-lc")
        assert c.read() == (b"ab01", b"val-cn")
        assert c.read() == (None, None) and c.eof == True

        # Seek, read
        c.seek(b"ab03")
        assert c.read() == (b"ab03", b"val-c0")
        c.seek(b"ab02")
        assert c.read() == (b"ab02", b"val-lc")
        c.seek(b"ab01")
        assert c.read() == (b"ab01", b"val-cn")

        # Read, update, read
        c.seek(b"ab03")
        assert c.read() == (b"ab03", b"val-c0")
        c.update_view()
        assert c.read() == (b"ab02", b"val-lc")
        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"ab03")
        c.update_view()
        assert c.read() == (b"ab03", b"val-c0")
        c.seek(b"ab02")
        c.update_view()
        assert c.read() == (b"ab02", b"val-lc")
        c.seek(b"ab01")
        c.update_view()
        assert c.read() == (b"ab01", b"val-cn")
Esempio n. 14
0
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
Esempio n. 15
0
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
Esempio n. 16
0
def add_keys(kvs: hse.Kvs, pfx: str, start: int, end: int):
    for k_id in range(start, end):
        key = f"{pfx}-{k_id:0>10}"
        kvs.put(key.encode(), b"val")
Esempio n. 17
0
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)

    with kvdb.transaction() as t:
        # Cursor
        with kvs.cursor(filt=b"ab", txn=t) 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", txn=t) == b"val-lc"
            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 == 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()
            assert c.read() == (b"ab01", b"val-lc")
            ##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-lc")
            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
Esempio n. 18
0
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