Exemple #1
0
    def test_seek(self):
        """Given a database with three items and a cursor on the primary keys,
        test that the cursor can be properly position to an arbitrary element
        in the natural order of the keys
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)
        print('TEST_SEEK ...')
        db.put('1', (1, "alice", "Alice's data"))
        db.put('2', (2, "bob", "Bob's data"))
        db.put('3', (3, 'charlie', "Charlie's data"))

        with db.cursor() as curs:
            curs.seek('2')
            print('TEST_SEEK', curs.key())
            self.assertEqual(2, curs.value()[0])

            self.assertEqual('2', curs.key())

            iterator = curs.iter()
            print('TEST_SEEK 1', curs.key())
            self.assertEqual(2, next(iterator)[0])
Exemple #2
0
    def test_indexing(self):
        """Test basic indexing around name
        """
        db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'),
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        self.assertEqual((1, "foo", "bar"), db.get('1'))

        self.assertEqual((2, "alice", "Alice's data"),
                         db.get('alice', index='name'))
Exemple #3
0
    def test_index_empty_db(self):
        """Given an empty database, show that the cursor will return a value of
        None for the first position.
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        with db.cursor(index='name') as curs:
            curs.first()
            self.assertIsNone(curs.value())

        with db.cursor() as curs:
            curs.first()
            self.assertIsNone(curs.value())
Exemple #4
0
    def test_count(self):
        """Test that a database with three records, plus an index will
        return the correct count of primary key/values, using `len`.
        """
        db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'),
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        self.assertEqual(3, len(db))

        self.assertEqual(3, db.count())
        self.assertEqual(3, db.count(index="name"))
Exemple #5
0
    def test_hex_ordered_indexing(self):
        """Test that an index that uses hex-encoded keys will properly order
        the keys (i.e. the natural order of the keys is in numerical order).
        """
        def to_hex(num):
            return "{0:#0{1}x}".format(num, 18)

        def age_index_key_fn(tup):
            return [to_hex(tup[0]).encode()]

        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'age': age_index_key_fn},
                            flag='c',
                            _size=1024**2)
        print("TEST_HEX_ORDERED_INDEXING ....")
        entry_count = 100
        for i in range(1, entry_count):
            db.put(str(entry_count - i), (i, "foo" + str(i), "data"))
        print("TEST_HEX_ORDERED_INDEXING",
              [to_hex(i) for i in range(1, entry_count)])
        print("TEST_HEX_ORDERED_INDEXING LIST", list(db.keys(index='age')))
        self.assertEqual([to_hex(i) for i in range(1, entry_count)],
                         list(db.keys(index='age')))
        print("TEST_HEX_ORDERED_INDEXING DONE")
Exemple #6
0
    def test_count(self):
        """Test that a database with three records, plus an index will
        return the correct count of primary key/values, using `len`.
        """
        #logging.basicConfig()
        #log = logging.getLogger("LOG")
        #log.info('OrientDatabaseTest test_count...')
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        self.assertEqual(3, len(db))

        self.assertEqual(3, db.count())
        self.assertEqual(3, db.count(index="name"))
Exemple #7
0
    def test_hex_ordered_indexing(self):
        """Test that an index that uses hex-encoded keys will properly order
        the keys (i.e. the natural order of the keys is in numerical order).
        """
        def to_hex(num):
            return "{0:#0{1}x}".format(num, 18)

        def age_index_key_fn(tup):
            return [to_hex(tup[0]).encode()]

        db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'),
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'age': age_index_key_fn},
                            flag='c',
                            _size=1024**2)

        entry_count = 100
        for i in range(1, entry_count):
            db.put(str(entry_count - i), (i, "foo" + str(i), "data"))

        self.assertEqual([to_hex(i) for i in range(1, entry_count)],
                         list(db.keys(index='age')))
Exemple #8
0
    def test_index_reverse_iteration(self):
        """Test reverse iteration over the items in a database, using the
        reverse natural order of the index keys.
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        with db.cursor(index='name') as curs:
            ordered_values = list(curs.iter_rev())

        self.assertEqual([(1, "foo", "bar"), (3, "bob", "Bob's data"),
                          (2, "alice", "Alice's data")], ordered_values)
Exemple #9
0
    def test_iteration(self):
        """Test iteration on over the items in a database, using the natural
        order of the primary keys.
        """
        db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'),
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        with db.cursor() as curs:
            ordered_values = list(curs.iter())

        self.assertEqual([(1, "foo", "bar"), (2, "alice", "Alice's data"),
                          (3, "bob", "Bob's data")], ordered_values)
Exemple #10
0
    def test_last(self):
        """Given a database with three items and a cursor on the primary keys,
        test that the cursor can be properly position to the last element in
        the natural order of the keys
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "alice", "Alice's data"))
        db.put('2', (2, "bob", "Bob's data"))
        db.put('3', (3, 'charlie', "Charlie's data"))

        with db.cursor() as curs:
            # Start from the beginning
            first = next(curs.iter())
            # Read backward again
            iterator = curs.iter_rev()
            backward = next(iterator)

            self.assertEqual(first, backward)

            # Check the iterator is exhausted from there
            with self.assertRaises(StopIteration):
                next(iterator)

            # reset to first element
            curs.last()
            new_iter = curs.iter_rev()

            # verify that we'll see the first element again
            last = next(new_iter)
            self.assertEqual(3, last[0])
Exemple #11
0
    def test_update_replace_index(self):
        """Test that update will properly update insert records that have
        the same index value of a deleted record.
        - insert items should be added
        - inserted items index should be correct
        - deleted items should be removed
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)
        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        db.update([('4', (4, 'foo', "foo's data"))], ['1'])

        self.assertEqual(['2', '3', '4'], db.keys())
        self.assertEqual((4, 'foo', "foo's data"), db.get('foo', index='name'))
Exemple #12
0
    def test_update(self):
        """Test that a database will commit both inserts and deletes using the
        update method.
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        db.update([('4', (4, 'charlie', "Charlie's data"))], ['1'])

        self.assertEqual(['2', '3', '4'], db.keys())
Exemple #13
0
    def test_delete(self):
        """Test that items are deleted, including their index references.
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        print('TEST_DELETE ....')
        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        with db.cursor(index='name') as curs:
            ordered_values = list(curs.iter())
        print('test_delete.ORDERED_VALUES', ordered_values)
        self.assertEqual([(2, "alice", "Alice's data"),
                          (3, "bob", "Bob's data"), (1, "foo", "bar")],
                         ordered_values)

        db.delete('3')

        with db.cursor(index='name') as curs:
            ordered_values = list(curs.iter())
        print('test_delete.ORDERED_VALUES 1', ordered_values)
        self.assertEqual([(2, "alice", "Alice's data"), (1, "foo", "bar")],
                         ordered_values)
Exemple #14
0
    def test_integer_indexing(self):
        """Test that a database can be indexed using integer keys.
        """
        int_index_config = {
            'key_fn': lambda tup: [struct.pack('I', tup[0])],
            'integerkey': True
        }
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'age': int_index_config},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (30, "alice", "Alice's data"))
        db.put('3', (20, "bob", "Bob's data"))
        db.put('4', (12, "charlie", "Charlie's data"))

        print('TEST_INTEGER_INDEXING', db.keys(index='age'))
        """
        self.assertEqual(
            [1, 12, 20, 30],
            [struct.unpack('I', k.encode())[0] for k in db.keys(index='age')])
        print('TEST_INTEGER_INDEXING 1')
        """
        with db.cursor(index='age') as curs:
            print('TEST_INTEGER_INDEXING LIST', list(curs.iter()))
            """
            self.assertEqual([
                (1, "foo", "bar"),
                (12, "charlie", "Charlie's data"),
                (20, "bob", "Bob's data"),
                (30, "alice", "Alice's data")],
                list(curs.iter()))
            """
        print('TEST_INTEGER_INDEXING 2')
Exemple #15
0
    def test_index_iteration_with_concurrent_mods(self):
        """Given a database with three items, and a cursor on the index keys,
        test that a concurrent update will:
        - not change the results
        - not interrupt the iteration with an error
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        with db.cursor(index='name') as curs:
            iterator = curs.iter()

            self.assertEqual(2, next(iterator)[0])

            db.put('4', (4, 'charlie', "Charlie's data"))

            self.assertEqual(3, next(iterator)[0])
            self.assertEqual(1, next(iterator)[0])

            with self.assertRaises(StopIteration):
                next(iterator)
Exemple #16
0
    def test_integer_indexing(self):
        """Test that a database can be indexed using integer keys.
        """
        int_index_config = {
            'key_fn': lambda tup: [struct.pack('I', tup[0])],
            'integerkey': True
        }
        db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'),
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'age': int_index_config},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (30, "alice", "Alice's data"))
        db.put('3', (20, "bob", "Bob's data"))
        db.put('4', (12, "charlie", "Charlie's data"))

        self.assertEqual(
            [1, 12, 20, 30],
            [struct.unpack('I', k.encode())[0] for k in db.keys(index='age')])
        with db.cursor(index='age') as curs:
            self.assertEqual([(1, "foo", "bar"),
                              (12, "charlie", "Charlie's data"),
                              (20, "bob", "Bob's data"),
                              (30, "alice", "Alice's data")],
                             list(curs.iter()))
Exemple #17
0
    def test_first(self):
        """Given a database with three items and a cursor on the primary keys,
        test that the cursor can be properly position to the first element in
        the natural order of the keys
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "alice", "Alice's data"))
        db.put('2', (2, "bob", "Bob's data"))
        db.put('3', (3, 'charlie', "Charlie's data"))
        print('TEST_FIRST ...')
        with db.cursor() as curs:
            # Start from the end
            last = next(curs.iter_rev())
            # Read forward again
            iterator = curs.iter()
            forward = next(iterator)

            self.assertEqual(last, forward)
            """
            print("ITER ....")
            iter =curs.iter()
            k = curs.key()
            v = next(iter)
            k1 = curs.key()
            v1 = next(iter)  
            k2 = curs.key() 
            v2 = next(iter)
            k3 = curs.key()   
            print("ITER:[{}]={},[{}]={},[{}]={}=>{}".format(k,v,k1,v1,k2,v2,k3))
            print("POS={}={}".format(curs.key(),curs.value())) 
            print('LIST', list(curs.iter()))
            print("POS={}".format(curs.key()))
            iter = curs.iter_rev()
            print("POS={}".format(curs.key()))
            print('LIST REV',list(iter))
            print("POS={}".format(curs.key()))
            
            
            last = next(curs.iter_rev())
            # Read forward again
            iterator = curs.iter()
            print('ITERATOR',iterator,'LAST',last)
            forward = next(iterator)
            print('LAST',last,'forward',forward)
            self.assertEqual(last, forward)
            """
            # Check the iterator is exhausted from there
            with self.assertRaises(StopIteration):
                next(iterator)
            print("reset to first element ....")
            # reset to first element
            curs.first()
            new_iter = curs.iter()

            # verify that we'll see the first element again
            first = next(new_iter)
            print('FIRST', first)
            self.assertEqual(1, first[0])
Exemple #18
0
    def test_indexing(self):
        """Test basic indexing around name
        """
        db = OrientDatabase(DB_URI,
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))
        print('TEST_INDEXING', db.get('1'))
        self.assertEqual((1, "foo", "bar"), db.get('1'))

        self.assertEqual((2, "alice", "Alice's data"),
                         db.get('alice', index='name'))
Exemple #19
0
    def test_delete(self):
        """Test that items are deleted, including their index references.
        """
        db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'),
                            _serialize_tuple,
                            _deserialize_tuple,
                            indexes={'name': lambda tup: [tup[1].encode()]},
                            flag='c',
                            _size=1024**2)

        db.put('1', (1, "foo", "bar"))
        db.put('2', (2, "alice", "Alice's data"))
        db.put('3', (3, "bob", "Bob's data"))

        with db.cursor(index='name') as curs:
            ordered_values = list(curs.iter())

        self.assertEqual([(2, "alice", "Alice's data"),
                          (3, "bob", "Bob's data"), (1, "foo", "bar")],
                         ordered_values)

        db.delete('3')

        with db.cursor(index='name') as curs:
            ordered_values = list(curs.iter())

        self.assertEqual([(2, "alice", "Alice's data"), (1, "foo", "bar")],
                         ordered_values)