Esempio n. 1
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")
Esempio n. 2
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()))
Esempio n. 3
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())
Esempio n. 4
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'))
Esempio n. 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(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')))
Esempio n. 6
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')