def test_delete(self): """Test that items are deleted, including their index references. """ db = IndexedDatabase(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)
def test_delete(self): """Test that items are deleted, including their index references. """ db = IndexedDatabase( 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)
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 = IndexedDatabase(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()))
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 = IndexedDatabase(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: 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)
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 = IndexedDatabase( 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, "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') self.assertEqual(2, curs.value()[0]) self.assertEqual('2', curs.key()) iterator = curs.iter() self.assertEqual(2, next(iterator)[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 = IndexedDatabase( 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()))
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 = IndexedDatabase( 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: 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)
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 = IndexedDatabase(os.path.join(self._temp_dir, 'test_db'), _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())
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 = IndexedDatabase( os.path.join(self._temp_dir, 'test_db'), _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())
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 = IndexedDatabase(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_rev()) self.assertEqual([(1, "foo", "bar"), (3, "bob", "Bob's data"), (2, "alice", "Alice's data")], ordered_values)
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 = IndexedDatabase( 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, "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])
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 = IndexedDatabase( 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_rev()) self.assertEqual( [(1, "foo", "bar"), (3, "bob", "Bob's data"), (2, "alice", "Alice's data")], ordered_values)
def start_bot_api(host, port, connection, timeout, registry, connects=None, client_max_size=None): """Builds the web app, adds route handlers, and finally starts the app. """ #tele_db = LMDBNoLockDatabase(TELE_DB_FILENAME, 'c') tele_db = IndexedDatabase(TELE_DB_FILENAME, serialize_data, deserialize_data, indexes={ 'name': lambda dict: [dict['name'].encode()], 'type': lambda dict: [dict['type'].encode()] }, flag='c', _size=DEFAULT_DB_SIZE, dupsort=True) if "ROOT" in tele_db: LOGGER.info('TAKE ROOT FROM DB=%s', tele_db["ROOT"]) #for key in tele_db.keys(): LOGGER.info('KEYS=%s', list(tele_db.keys())) with tele_db.cursor() as curs: for val in curs.iter(): LOGGER.info('values=%s', val) with tele_db.cursor(index='name') as curs: #values = list(curs.iter()) for val in curs.iter(): LOGGER.info('Name values=%s', val) #LOGGER.info('ordered_values=%s',values) else: tele_db.put("ROOT", {'val': 1, 'name': 'sticker', 'type': 'user'}) loop = asyncio.get_event_loop() connection.open() bot = BgxTeleBot(loop, connection, tele_db, TOKEN, connects=connects) #Tbot(loop, connection,TOKEN) # add handler for intention bot.add_intent_handler('smalltalk.greetings.hello', bot.intent_hello) bot.add_intent_handler('smalltalk.greetings.bye', bot.intent_bye) bot.add_intent_handler('smalltalk.agent.can_you_help', bot.intent_help) bot.add_intent_handler('smalltalk.dialog.hold_on', bot.intent_hold_on) bot.add_intent_handler('smalltalk.user.needs_advice', bot.intent_needs_advice) #bot.add_intent_handler('smalltalk.agent.get_wallet',bot.intent_get_wallet) bot.add_intent_handler('smalltalk.agent.check_wallet', bot.intent_check_wallet) bot.add_intent_handler('smalltalk.agent.check_wallet_history', bot.intent_check_wallet_history) bot.add_intent_handler('smalltalk.agent.create_wallet', bot.intent_create_wallet) bot.add_intent_handler('smalltalk.agent.trans_token', bot.intent_trans_token) bot.add_intent_handler('smalltalk.agent.inc_wallet', bot.intent_inc_wallet) bot.add_intent_handler('smalltalk.agent.dec_wallet', bot.intent_dec_wallet) bot.add_intent_handler('smalltalk.agent.buy_stuff', bot.intent_buy_stuff) bot.add_intent_handler('smalltalk.agent.sell_stuff', bot.intent_sell_stuff) # make stuff bot.add_intent_handler('smalltalk.agent.create_stuff', bot.intent_create_stuff) bot.add_intent_handler('smalltalk.agent.update_stuff', bot.intent_update_stuff) bot.add_intent_handler('smalltalk.agent.show_stuff', bot.intent_show_stuff) bot.add_intent_handler("smalltalk.agent.show_stuff_history", bot.intent_show_stuff_history) bot.add_intent_handler("smalltalk.agent.show_stuff_list", bot.intent_show_stuff_list) # bot.add_intent_handler("smalltalk.agent.show_gateway", bot.intent_show_gateway) bot.add_intent_handler("smalltalk.agent.show_gateway_list", bot.intent_show_gateway_list) bot.add_intent_handler("smalltalk.agent.set_gateway", bot.intent_set_gateway) bot.add_intent_handler("smalltalk.agent.peers_down", bot.intent_peers_down) bot.add_intent_handler("smalltalk.agent.peers_up", bot.intent_peers_up) bot.add_intent_handler("smalltalk.agent.peers_control_list", bot.intent_peers_control_list) bot.add_intent_handler("smalltalk.agent.peer_info", bot.intent_peer_info) bot.add_intent_handler("smalltalk.agent.pause", bot.intent_pause) bot.add_intent_handler("smalltalk.agent.unpause", bot.intent_unpause) bot.add_intent_handler('smalltalk.agent.chat_admins', bot.intent_chat_admins) bot.add_intent_handler('smalltalk.agent.get_users', bot.intent_get_users) LOGGER.info('start_bot_api for=%s', TOKEN) bot.start() """
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 = IndexedDatabase(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, "alice", "Alice's data")) db.put('2', (2, "bob", "Bob's data")) db.put('3', (3, 'charlie', "Charlie's data")) print("keys={}".format(db.keys())) 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) """ 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(iter)) # 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())) print("key '{}' {}".format(curs.key(),curs.value())) riter = curs.iter_rev() print("key1 '{}'".format(curs.key()),'value',curs.value()) last = next(riter) print("key2 '{}'={},{}".format(curs.key(),last,next(riter))) last1 = next(riter) print("key3 '{}'={}".format(curs.key(),last1)) curs.iter_rev() print("key4 '{}'={}".format(curs.key(),list(curs.iter_rev()))) print("key5 '{}'".format(curs.key())) key = curs.key() print("key6 '{}'={}".format(key,list(curs.iter_rev()))) # Read forward again iterator = curs.iter() print('key 1',curs.key(),type(curs.key())) #print('VALUE',curs.value()) forward = next(iterator) print('key 2',curs.key()) 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])