From aeb2bba61bb8a5e02f1a69002fd53c098d3654f0 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Wed, 7 Dec 2022 15:45:11 +0100 Subject: [PATCH] adjusted get_txids_filtered for new indexes Signed-off-by: Lorenz Herzberger --- planetmint/backend/models/asset.py | 4 +-- planetmint/backend/tarantool/init.lua | 8 +++++ planetmint/backend/tarantool/query.py | 48 ++++++++++----------------- planetmint/lib.py | 2 +- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/planetmint/backend/models/asset.py b/planetmint/backend/models/asset.py index 9a1a8f6..e4ab20f 100644 --- a/planetmint/backend/models/asset.py +++ b/planetmint/backend/models/asset.py @@ -13,8 +13,8 @@ class Asset: data: str = "" @staticmethod - def from_dict(asset_tuple: dict) -> Asset: - return Asset(asset_tuple["data"]) + def from_dict(asset_dict: dict) -> Asset: + return Asset(asset_dict["data"]) if "data" in asset_dict.keys() else Asset(asset_dict["id"]) def to_dict(self) -> dict: return { diff --git a/planetmint/backend/tarantool/init.lua b/planetmint/backend/tarantool/init.lua index b1cd75c..3a46ddb 100644 --- a/planetmint/backend/tarantool/init.lua +++ b/planetmint/backend/tarantool/init.lua @@ -46,6 +46,14 @@ function init() { field = 'inputs[*].fulfills["transaction_id"]', type = 'string', is_nullable = true }, { field = 'inputs[*].fulfills["output_index"]', type = 'unsigned', is_nullable = true } }}) + transactions:create_index('transactions_by_id_and_operation', { + if_not_exists = true, + parts = { + { field = 'id', type = 'string' }, + { field = 'operation', type = 'string' }, + { field = 'assets[*].id', type = 'string', is_nullable = true } + } + }) -- Outputs diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index 850076c..2344cd2 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -160,42 +160,30 @@ def store_block(connection, block: dict): @register_query(TarantoolDBConnection) -def get_txids_filtered( - connection, asset_ids: list[str], operation: str = None, last_tx: any = None -): # TODO here is used 'OR' operator - actions = { - "CREATE": {"sets": ["CREATE", asset_ids], "index": "transaction_search"}, - # 1 - operation, 2 - id (only in transactions) + - "TRANSFER": {"sets": ["TRANSFER", asset_ids], "index": "transaction_search"}, - # 1 - operation, 2 - asset.id (linked mode) + OPERATOR OR - None: {"sets": [asset_ids, asset_ids]}, - }[operation] - _transactions = [] - if actions["sets"][0] == "CREATE": # + - _transactions = connection.run( - connection.space(TARANT_TABLE_TRANSACTION).select([operation, asset_ids[0]], index=actions["index"]) +def get_txids_filtered(connection, asset_ids: list[str], operation: str = "", last_tx: bool = False): + transactions = [] + if operation == "CREATE": + transactions = connection.run( + connection.space(TARANT_TABLE_TRANSACTION).select([asset_ids[0], operation], index="transactions_by_id_and_operation") + ) + elif operation == "TRANSFER": + transactions = connection.run( + connection.space(TARANT_TABLE_TRANSACTION).select(["", operation, asset_ids], index="transactions_by_id_and_operation") ) - elif actions["sets"][0] == "TRANSFER": # + - _assets = connection.run(connection.space(TARANT_TABLE_ASSETS).select(asset_ids, index="only_asset_search")) - - for asset in _assets: - _txid = asset[1] - _tmp_transactions = connection.run( - connection.space(TARANT_TABLE_TRANSACTION).select([operation, _txid], index=actions["index"]) - ) - if len(_tmp_transactions) != 0: - _transactions.extend(_tmp_transactions) else: - _tx_ids = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(asset_ids, index=TARANT_ID_SEARCH)) - _assets_ids = connection.run( - connection.space(TARANT_TABLE_ASSETS).select(asset_ids, index="only_asset_search") + txs = connection.run(connection.space(TARANT_TABLE_TRANSACTION).select(asset_ids, index=TARANT_ID_SEARCH)) + asset_txs = connection.run( + connection.space(TARANT_TABLE_TRANSACTION).select(asset_ids, index="transactions_by_asset") ) - return tuple(set([sublist[1] for sublist in _assets_ids] + [sublist[0] for sublist in _tx_ids])) + transactions = txs + asset_txs + ids = tuple([tx[0] for tx in transactions]) + + # NOTE: check when and where this is used and remove if not if last_tx: - return tuple(next(iter(_transactions))) + return ids[0] - return tuple([elem[0] for elem in _transactions]) + return ids @register_query(TarantoolDBConnection) diff --git a/planetmint/lib.py b/planetmint/lib.py index 7626583..ed029bf 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -233,7 +233,7 @@ class Planetmint(object): def get_transactions(self, txn_ids): return backend.query.get_transactions(self.connection, txn_ids) - def get_transactions_filtered(self, asset_ids, operation=None, last_tx=None): + def get_transactions_filtered(self, asset_ids, operation=None, last_tx=False): """Get a list of transactions filtered on some criteria""" txids = backend.query.get_txids_filtered(self.connection, asset_ids, operation, last_tx) for txid in txids: