Пример #1
0
def test_insert_split_in_tree(iterator, order, page_size, k_size, v_size,
                              filename, clean_file):
    inserted = set()

    b = BPlusTree(filename=filename,
                  order=order,
                  page_size=page_size,
                  key_size=k_size,
                  value_size=v_size,
                  fsync=Fsync.NEVER)

    for i in iterator:
        b.insert(i, str(i).encode())
        inserted.add(i)

    if filename:
        # Reload tree from file before checking values
        b.close()
        b = BPlusTree(filename=filename,
                      order=order,
                      page_size=page_size,
                      key_size=k_size,
                      value_size=v_size)

    for x in inserted:
        assert b.get(x) == str(x).encode()

    b.close()
Пример #2
0
def test_insert_split_in_tree(iterator, order, page_size, k_size, v_size,
                              serialize_class, cache_size):

    inserted = list()
    for i in iterator:
        v = str(i).encode()
        k = i
        if serialize_class is StrSerializer:
            k = str(i)
        inserted.append((k, v))

    b = BPlusTree(filename, order=order, page_size=page_size,
                  key_size=k_size, value_size=v_size, cache_size=cache_size,
                  serializer=serialize_class())

    if sorted(inserted) == inserted:
        b.batch_insert(inserted)
    else:
        for k, v in inserted:
            b.insert(k, v)

    # Reload tree from file before checking values
    b.close()
    b = BPlusTree(filename, order=order, page_size=page_size,
                  key_size=k_size, value_size=v_size, cache_size=cache_size,
                  serializer=serialize_class())

    for k, v in inserted:
        assert b.get(k) == v

    b.close()
Пример #3
0
def test_length_hint_tree():
    b = BPlusTree(order=100)
    assert b.__length_hint__() == 49
    b.insert(1, b'foo')
    assert b.__length_hint__() == 49
    for i in range(2, 10001):
        b.insert(i, str(i).encode())
    assert b.__length_hint__() == 7242
    b.close()
Пример #4
0
def test_left_record_node_in_tree():
    b = BPlusTree(order=3)
    assert b._left_record_node == b._root_node
    assert isinstance(b._left_record_node, LonelyRootNode)
    b.insert(1, b'1')
    b.insert(2, b'2')
    b.insert(2, b'2')
    assert isinstance(b._left_record_node, LeafNode)
    b.close()
Пример #5
0
def test_create_and_load_file(clean_file):
    b = BPlusTree(filename=filename)
    assert isinstance(b._mem, FileMemory)
    b.insert(5, b'foo')
    b.close()

    b = BPlusTree(filename=filename)
    assert isinstance(b._mem, FileMemory)
    assert b.get(5) == b'foo'
    b.close()
Пример #6
0
def test_iter_slice(b):
    with pytest.raises(ValueError):
        next(b._iter_slice(slice(None, None, -1)))

    with pytest.raises(ValueError):
        next(b._iter_slice(slice(10, 0, None)))

    # Contains from 0 to 9 included
    for i in range(10):
        b.insert(i, str(i).encode())

    iter = b._iter_slice(slice(None, 2))
    assert next(iter).key == 0
    assert next(iter).key == 1
    with pytest.raises(StopIteration):
        next(iter)

    iter = b._iter_slice(slice(5, 7))
    assert next(iter).key == 5
    assert next(iter).key == 6
    with pytest.raises(StopIteration):
        next(iter)

    iter = b._iter_slice(slice(8, 9))
    assert next(iter).key == 8
    with pytest.raises(StopIteration):
        next(iter)

    iter = b._iter_slice(slice(9, 12))
    assert next(iter).key == 9
    with pytest.raises(StopIteration):
        next(iter)

    iter = b._iter_slice(slice(15, 17))
    with pytest.raises(StopIteration):
        next(iter)

    iter = b._iter_slice(slice(-2, 17))
    assert next(iter).key == 0

    b.close()

    # Contains from 10, 20, 30 .. 200
    b = BPlusTree(filename, order=5)
    for i in range(10, 201, 10):
        b.insert(i, str(i).encode())

    iter = b._iter_slice(slice(65, 85))
    assert next(iter).key == 70
    assert next(iter).key == 80
    with pytest.raises(StopIteration):
        next(iter)