Esempio n. 1
0
def test_follow_RedBlackTree():
    db = connect('test.dbdb')
    storage = db._storage
    rbtree = RedBlackTree(storage)
    rbtree.set('1', '2')
    rbtree.set('2', '3')
    root = rbtree._follow(rbtree._tree_ref)
    assert root.key == '1'
    os.remove('test.dbdb')
Esempio n. 2
0
def test_insert_RedBlackTree():
    db = connect('test.dbdb')
    storage = db._storage
    rbtree = RedBlackTree(storage)
    root = rbtree._follow(rbtree._tree_ref)
    value_ref = ValueRef('2')
    rbtree._tree_ref = rbtree._insert(root, '1', value_ref)
    assert rbtree._tree_ref._referent.key == '1'
    os.remove('test.dbdb')
Esempio n. 3
0
    def __init__(self, f):
        """
        Initialize DBDB with a file, and create the tree and its associated storage

        Parameters:
        f: File

        Returns:
        None
        """
        self._storage = Storage(f)
        self._tree = RedBlackTree(self._storage)
Esempio n. 4
0
def test__find_max_RedBlackTree():
    db = connect('test.dbdb')
    storage = db._storage
    rbtree = RedBlackTree(storage)
    rbtree.set('1', 2)
    rbtree.set('2', 3)
    rbtree.set('3', 4)
    root = rbtree._follow(rbtree._tree_ref)
    rbtree._find_max(root).key == '3'
    os.remove('test.dbdb')
Esempio n. 5
0
def test_get_set_RedBlackTree():
    db = connect('test.dbdb')
    storage = db._storage
    rbtree = RedBlackTree(storage)
    rbtree.set('1', 'aaa')
    rbtree.set('2', 'bbb')
    rbtree.set('3', 'ccc')
    assert rbtree.get('3') == 'ccc'
    os.remove('test.dbdb')
Esempio n. 6
0
def test_init_storage():
    dbname = "test.dbdb"
    try:
        f = open(dbname, 'r+b')
    except IOError:
        fd = os.open(dbname, os.O_RDWR | os.O_CREAT)
        f = os.fdopen(fd, 'r+b')
    storage = Storage(f)
    assert type(storage) == Storage
    redBlackTree = RedBlackTree(storage)
    db = DBDB(f)
    db.close()
    os.remove('test.dbdb')
Esempio n. 7
0
class DBDB(object):
    """
    DBDB class contains both the tree and its associated storage (disk)
    Functions are wrappers around tree and storage.
    """

    def __init__(self, f):
        """
        Initialize DBDB with a file, and create the tree and its associated storage

        Parameters:
        f: File

        Returns:
        None
        """
        self._storage = Storage(f)
        self._tree = RedBlackTree(self._storage)

    def _assert_not_closed(self):
        """
        Raises an error when associated storage is closed
        """
        if self._storage.closed:
            raise ValueError('Database closed.')

    def close(self):
        """
        Closes associated storage
        """
        self._storage.close()

    def commit(self):
        """
        Commits changes to the tree
        """
        self._assert_not_closed()
        self._tree.commit()

    def get(self, key):
        """
        Get the value associated with the key from the tree
        """
        self._assert_not_closed()
        return self._tree.get(key)

    def get_smaller_nodes(self, key):
        self._assert_not_closed()
        return self._tree.get_smaller_nodes(key)

    def set(self, key, value):
        """
        Set the value at the associated key in the tree
        """
        self._assert_not_closed()
        return self._tree.set(key, value)

    def delete(self, key):
        """
        delete the key
        """
        self._assert_not_closed()
        return self._tree.delete(key)
Esempio n. 8
0
def test_red_black_tree():
    db = connect('test.dbdb')
    storage = db._storage
    rbtree = RedBlackTree(storage)
    rbtree.set(13, 'aaa')
    rbtree.set(8, 'bbb')
    rbtree.set(17, 'ccc')
    rbtree.set(1, 'ddd')
    rbtree.set(11, 'eee')
    rbtree.set(6, 'fff')
    rbtree.set(15, 'ggg')
    rbtree.set(25, 'hhh')
    rbtree.set(22, 'iii')
    rbtree.set(27, 'jjj')
    root = rbtree._follow(rbtree._tree_ref)
    leftNode = root._follow(root.left_ref)
    rightNode = root._follow(root.right_ref)
    leftleftNode = leftNode._follow(leftNode.left_ref)
    leftrightNode = leftNode._follow(leftNode.right_ref)
    rightleftNode = rightNode._follow(rightNode.left_ref)
    rightrightNode = rightNode._follow(rightNode.right_ref)
    leftleftrightNode = leftleftNode._follow(leftleftNode.right_ref)
    rightrightleftNode = rightrightNode._follow(rightrightNode.left_ref)
    rightrightrightNode = rightrightNode._follow(rightrightNode.right_ref)

    q = Queue()
    q.put(root)
    q.put(leftNode)
    q.put(rightNode)
    q.put(leftleftNode)
    q.put(leftrightNode)
    q.put(rightleftNode)
    q.put(rightrightNode)
    q.put(leftleftrightNode)
    q.put(rightrightleftNode)
    q.put(rightrightrightNode)

    #level = 1
    assert q.empty() == False
    node1 = q.get()
    assert node1.key == 13
    assert node1._follow(node1.color_ref) == "black"
    # level = 2
    node2 = q.get()
    assert node2.key == 8
    assert node2._follow(node2.color_ref) == "red"
    node3 = q.get()
    assert node3.key == 17
    assert node3._follow(node3.color_ref) == "red"
    # level = 3
    node4 = q.get()
    assert node4.key == 1
    assert node4._follow(node4.color_ref) == "black"
    node5 = q.get()
    assert node5.key == 11
    assert node5._follow(node5.color_ref) == "black"
    node6 = q.get()
    assert node6.key == 15
    assert node6._follow(node6.color_ref) == "black"
    node7 = q.get()
    assert node7.key == 25
    assert node7._follow(node7.color_ref) == "black"
    # level = 4
    node8 = q.get()
    assert node8.key == 6
    assert node8._follow(node8.color_ref) == "red"
    node9 = q.get()
    assert node9.key == 22
    assert node9._follow(node9.color_ref) == "red"
    node10 = q.get()
    assert node10.key == 27
    assert node10._follow(node10.color_ref) == "red"

    os.remove('test.dbdb')
Esempio n. 9
0
def test_init_RedBlackTree():
    db = connect('test.dbdb')
    storage = db._storage
    tree = RedBlackTree(storage)
    assert type(tree) == RedBlackTree
    os.remove('test.dbdb')