mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 06:55:45 +00:00
Fixed 1/3 from test_queries.py
This commit is contained in:
parent
f0cc7e1ebc
commit
7df1c2a072
@ -10,16 +10,17 @@ from planetmint.config import Config
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TarantoolDB:
|
class TarantoolDB:
|
||||||
def __init__(self, host: str = None, port: int = None, user: str = None, password: str = None,
|
def __init__(self, host: str = None, port: int = None, user: str = None, password: str = None,
|
||||||
reset_database: bool = False):
|
reset_database: bool = False):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
# TODO add user support later on
|
# TODO add user support later on
|
||||||
print( f"host : {host}")
|
print(f"host : {host}")
|
||||||
print( f"port : {port}")
|
print(f"port : {port}")
|
||||||
#self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password)
|
# self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password)
|
||||||
self.db_connect = tarantool.connect(host=self.host , port=self.port)
|
self.db_connect = tarantool.connect(host=self.host, port=self.port)
|
||||||
self.init_path = Config().get()["database"]["init_config"]["absolute_path"]
|
self.init_path = Config().get()["database"]["init_config"]["absolute_path"]
|
||||||
self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"]
|
self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"]
|
||||||
if reset_database:
|
if reset_database:
|
||||||
@ -43,7 +44,7 @@ class TarantoolDB:
|
|||||||
|
|
||||||
def run_command(self, command: str, config: dict):
|
def run_command(self, command: str, config: dict):
|
||||||
import subprocess
|
import subprocess
|
||||||
print( f" commands: {command}")
|
print(f" commands: {command}")
|
||||||
ret = subprocess.Popen(
|
ret = subprocess.Popen(
|
||||||
['%s %s:%s < %s' % ("tarantoolctl connect", "localhost", "3303", command)],
|
['%s %s:%s < %s' % ("tarantoolctl connect", "localhost", "3303", command)],
|
||||||
stdin=subprocess.PIPE,
|
stdin=subprocess.PIPE,
|
||||||
@ -52,4 +53,4 @@ class TarantoolDB:
|
|||||||
bufsize=0,
|
bufsize=0,
|
||||||
shell=True)
|
shell=True)
|
||||||
# TODO verify if subprocess creation worked properly
|
# TODO verify if subprocess creation worked properly
|
||||||
return True #if ret > 0 else False
|
return True # if ret > 0 else False
|
||||||
|
|||||||
@ -7,6 +7,7 @@ assets = box.schema.space.create('assets' , {engine='memtx' , is_sync=false})
|
|||||||
assets:format({{name='data' , type='any'}, {name='tx_id', type='string'}, {name='asset_id', type='string'}})
|
assets:format({{name='data' , type='any'}, {name='tx_id', type='string'}, {name='asset_id', type='string'}})
|
||||||
assets:create_index('txid_search', {type='hash', parts={'tx_id'}})
|
assets:create_index('txid_search', {type='hash', parts={'tx_id'}})
|
||||||
assets:create_index('assetid_search', {type='tree',unique=false, parts={'asset_id', 'tx_id'}})
|
assets:create_index('assetid_search', {type='tree',unique=false, parts={'asset_id', 'tx_id'}})
|
||||||
|
assets:create_index('only_asset_search', {type='tree', unique=false, parts={'asset_id'}})
|
||||||
|
|
||||||
blocks = box.schema.space.create('blocks' , {engine='memtx' , is_sync=false})
|
blocks = box.schema.space.create('blocks' , {engine='memtx' , is_sync=false})
|
||||||
blocks:format{{name='app_hash',type='string'},{name='height' , type='integer'},{name='block_id' , type='string'}}
|
blocks:format{{name='app_hash',type='string'},{name='height' , type='integer'},{name='block_id' , type='string'}}
|
||||||
|
|||||||
@ -115,13 +115,13 @@ def get_metadata(connection, transaction_ids: list):
|
|||||||
@register_query(TarantoolDB)
|
@register_query(TarantoolDB)
|
||||||
# asset: {"id": "asset_id"}
|
# asset: {"id": "asset_id"}
|
||||||
# asset: {"data": any} -> insert (tx_id, asset["data"]).
|
# asset: {"data": any} -> insert (tx_id, asset["data"]).
|
||||||
def store_asset(connection, asset: dict, tx_id=None): # TODO convert to str all asset["id"]
|
def store_asset(connection, asset: dict, tx_id=None):
|
||||||
space = connection.space("assets")
|
space = connection.space("assets")
|
||||||
try:
|
try:
|
||||||
if tx_id is not None:
|
if tx_id is not None:
|
||||||
space.insert((asset, tx_id))
|
space.insert((asset, tx_id, tx_id))
|
||||||
else:
|
else:
|
||||||
space.insert((str(asset["id"]), asset))
|
space.insert((asset, str(asset["id"]), str(asset["id"]))) # TODO Review this function
|
||||||
except: # TODO Add Raise For Duplicate
|
except: # TODO Add Raise For Duplicate
|
||||||
print("DUPLICATE ERROR")
|
print("DUPLICATE ERROR")
|
||||||
|
|
||||||
@ -150,10 +150,12 @@ def get_assets(connection, assets_ids: list) -> list:
|
|||||||
space = connection.space("assets")
|
space = connection.space("assets")
|
||||||
for _id in list(set(assets_ids)):
|
for _id in list(set(assets_ids)):
|
||||||
asset = space.select(str(_id), index="txid_search")
|
asset = space.select(str(_id), index="txid_search")
|
||||||
|
if len(asset) == 0:
|
||||||
|
continue
|
||||||
asset = asset.data[0]
|
asset = asset.data[0]
|
||||||
_returned_data.append(asset[0])
|
_returned_data.append(asset[0])
|
||||||
# return sorted(_returned_data, key=lambda k: k["id"], reverse=False)
|
|
||||||
return _returned_data
|
return sorted(_returned_data, key=lambda k: k["id"], reverse=False)
|
||||||
|
|
||||||
|
|
||||||
@register_query(TarantoolDB)
|
@register_query(TarantoolDB)
|
||||||
@ -195,21 +197,28 @@ def get_txids_filtered(connection, asset_id: str, operation: str = None,
|
|||||||
actions = {
|
actions = {
|
||||||
"CREATE": {"sets": ["CREATE", asset_id], "index": "transaction_search"},
|
"CREATE": {"sets": ["CREATE", asset_id], "index": "transaction_search"},
|
||||||
# 1 - operation, 2 - id (only in transactions) +
|
# 1 - operation, 2 - id (only in transactions) +
|
||||||
"TRANSFER": {"sets": ["TRANSFER", asset_id], "index": "asset_search"},
|
"TRANSFER": {"sets": ["TRANSFER", asset_id], "index": "transaction_search"},
|
||||||
# 1 - operation, 2 - asset.id (linked mode) + OPERATOR OR
|
# 1 - operation, 2 - asset.id (linked mode) + OPERATOR OR
|
||||||
None: {"sets": [asset_id, asset_id]}
|
None: {"sets": [asset_id, asset_id]}
|
||||||
}[operation]
|
}[operation]
|
||||||
space = connection.space("transactions")
|
tx_space = connection.space("transactions")
|
||||||
if actions["sets"][0] == "CREATE":
|
assets_space = connection.space("assets")
|
||||||
_transactions = space.select([operation, asset_id], index=actions["index"])
|
_transactions = []
|
||||||
_transactions = _transactions.data
|
if actions["sets"][0] == "CREATE": # +
|
||||||
elif actions["sets"][0] == "TRANSFER":
|
_transactions = tx_space.select([operation, asset_id], index=actions["index"])
|
||||||
_transactions = space.select([operation, asset_id], index=actions["index"])
|
|
||||||
_transactions = _transactions.data
|
_transactions = _transactions.data
|
||||||
|
elif actions["sets"][0] == "TRANSFER": # +
|
||||||
|
_assets = assets_space.select([asset_id], index="only_asset_search").data
|
||||||
|
for asset in _assets:
|
||||||
|
_txid = asset[1]
|
||||||
|
_transactions = tx_space.select([operation, _txid], index=actions["index"]).data
|
||||||
|
if len(_transactions) != 0:
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
_tx_ids = space.select([asset_id], index="id_search")
|
_tx_ids = tx_space.select([asset_id], index="id_search")
|
||||||
_assets_ids = space.select([asset_id], index="only_asset_search")
|
# _assets_ids = tx_space.select([asset_id], index="only_asset_search")
|
||||||
return tuple(set([sublist[0] for sublist in _assets_ids.data] + [sublist[0] for sublist in _tx_ids.data]))
|
_assets_ids = assets_space.select([asset_id], index="only_asset_search")
|
||||||
|
return tuple(set([sublist[1] for sublist in _assets_ids.data] + [sublist[0] for sublist in _tx_ids.data]))
|
||||||
|
|
||||||
if last_tx:
|
if last_tx:
|
||||||
return tuple(next(iter(_transactions)))
|
return tuple(next(iter(_transactions)))
|
||||||
|
|||||||
@ -127,7 +127,7 @@ class TransactionCompose:
|
|||||||
|
|
||||||
def __init__(self, db_results):
|
def __init__(self, db_results):
|
||||||
self.db_results = db_results
|
self.db_results = db_results
|
||||||
self._map = self.db_results["transaction"][4]
|
self._map = self.db_results["transaction"][3]
|
||||||
|
|
||||||
def _get_transaction_operation(self):
|
def _get_transaction_operation(self):
|
||||||
return self.db_results["transaction"][1]
|
return self.db_results["transaction"][1]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user