Example #1
0
def test_skiplist_real_scenario_with_compact():
    """almost all things are same as `test_skiplist_real_scenario` except randomly compaction"""
    pool_folder, conf_path, block_file = _get_common_file_paths()
    _clean_up()

    comparision_dict = {}
    index = SkipListIndex(
        MemoryManager(pool_folder=pool_folder,
                      conf_path=conf_path,
                      block_file=block_file))
    for _ in range(20000):
        ind = random.randint(1, 10)
        # set: 3, get: 3, remove: 3, clear: 1
        if ind <= 3:
            op = "set"
        elif ind <= 6:
            op = "get"
        elif ind <= 9:
            op = "remove"
        else:
            op = "clear"
        # dispatch operations
        if op == "set":
            key, value = random.randint(1, 50), random.randint(1, 1000)
            comparision_dict[key] = value
            index.set(key, value)
        elif op == "get":
            key = random.randint(1, 50)
            assert comparision_dict.get(key, -1) == index.get(key, -1)
        elif op == "remove":
            key = random.randint(1, 50)
            if key in comparision_dict:
                value1 = True
                comparision_dict.pop(key)
            else:
                value1 = False
            value2 = index.remove(key)
            assert value1 == value2
        elif op == "clear":
            comparision_dict.clear()
            index.clear()
        # do compaction randomly
        if random.random() < 0.2:
            index.compact()
        # compare key-value pairs
        assert sorted(list(index.key_value_pairs())) == sorted([
            (key, value) for key, value in comparision_dict.items()
        ])
        # check skiplist lists
        cnt = sum(map(lambda head: not head.right, index._heads))
        # it could have at most one empty list, and the only scenario is empty SkipList
        assert cnt <= 1
        if cnt == 1:
            assert len(index._heads) == 1
    index._memory_manager.close()

    _clean_up()
Example #2
0
def test_skiplist_clear():
    pool_folder, conf_path, block_file = _get_common_file_paths()
    _clean_up()

    index = SkipListIndex(
        MemoryManager(pool_folder=pool_folder,
                      conf_path=conf_path,
                      block_file=block_file))
    for i in range(100):
        index.set(random.randint(1, 100), random.randint(1, 1000))
    index.clear()

    assert index.keys() == []
    assert (len(index._heads) == 1 and not index._heads[0].right
            and not index._heads[0].down)

    _clean_up()
Example #3
0
def test_skiplist_compact():
    pool_folder, conf_path, block_file = _get_common_file_paths()
    _clean_up()

    index = SkipListIndex(
        MemoryManager(pool_folder=pool_folder,
                      conf_path=conf_path,
                      block_file=block_file))
    index.set(1, 10)
    index.set(100, 20)
    index.clear()
    index.compact()

    # all blocks are compacted
    for block in index._blocks:
        assert block.used_memory == 0 and block.free_memory == block.block_size

    index.set(1, 10)
    index.set(2, 100)
    free_memory_list_origin = [block.free_memory for block in index._blocks]

    index.remove(1)
    index.compact()
    free_memory_list_now = [block.free_memory for block in index._blocks]

    assert index.get(1, -1)
    assert index.get(2, 100)
    assert index.keys() == [2]

    assert len(free_memory_list_origin) == len(free_memory_list_now)

    cnt = 0
    for origin, now in zip(free_memory_list_origin, free_memory_list_now):
        assert origin <= now
        if origin < now:
            cnt += 1
    assert cnt > 0

    _clean_up()