mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 22:45:44 +00:00
Merge pull request #8 from LaurentDeMontBlanc/planetmint-tarantool
Planetmint tarantool
This commit is contained in:
commit
a1b21aae63
@ -10,6 +10,7 @@ from planetmint.config import Config
|
||||
from planetmint.transactions.common.exceptions import ConfigurationError
|
||||
from planetmint.utils import Lazy
|
||||
from planetmint.backend.connection import Connection
|
||||
from planetmint.utils import Lazy
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -17,13 +18,12 @@ logger = logging.getLogger(__name__)
|
||||
class TarantoolDBConnection(Connection):
|
||||
def __init__(self, host: str = "localhost", port: int = 3303, user: str = None, password: str = None, **kwargs):
|
||||
try:
|
||||
super().__init__(**kwargs)
|
||||
self.host = host
|
||||
self.port = port
|
||||
# TODO add user support later on
|
||||
print(f"host : {host}")
|
||||
print(f"port : {port}")
|
||||
# TODO : raise configuraiton error if the connection cannot be established
|
||||
self.db_connect = tarantool.connect(host=self.host, port=self.port)
|
||||
self.init_path = Config().get()["database"]["init_config"]["absolute_path"]
|
||||
self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"]
|
||||
self.SPACE_NAMES = ["abci_chains", "assets", "blocks", "blocks_tx",
|
||||
@ -36,27 +36,27 @@ class TarantoolDBConnection(Connection):
|
||||
logger.info('Exception in _connect(): {}')
|
||||
raise ConfigurationError
|
||||
|
||||
def query(self):
|
||||
return Lazy()
|
||||
|
||||
def _file_content_to_bytes(self, path):
|
||||
with open(path, "r") as f:
|
||||
execute = f.readlines()
|
||||
f.close()
|
||||
return "".join(execute).encode()
|
||||
|
||||
def query(self):
|
||||
return Lazy()
|
||||
|
||||
def _reconnect(self):
|
||||
self.db_connect = tarantool.connect(host=self.host, port=self.port)
|
||||
def _connect(self):
|
||||
return tarantool.connect(host=self.host, port=self.port)
|
||||
|
||||
def get_space(self, space_name: str):
|
||||
return self.db_connect.space(space_name)
|
||||
return self.conn.space(space_name)
|
||||
|
||||
def space(self, space_name: str):
|
||||
return self.query().space(space_name)
|
||||
|
||||
def run(self, query, only_data=True):
|
||||
try:
|
||||
return query.run(self.db_connect).data if only_data else query.run(self.db_connect)
|
||||
return query.run(self.conn).data if only_data else query.run(self.conn)
|
||||
except tarantool.error.SchemaError:
|
||||
return None
|
||||
except tarantool.error.OperationalError as op_error:
|
||||
@ -65,17 +65,15 @@ class TarantoolDBConnection(Connection):
|
||||
raise net_error
|
||||
|
||||
def get_connection(self):
|
||||
return self.db_connect
|
||||
return self.conn
|
||||
|
||||
def drop_database(self):
|
||||
db_config = Config().get()["database"]
|
||||
cmd_resp = self.run_command(command=self.drop_path, config=db_config)
|
||||
self._reconnect()
|
||||
|
||||
def init_database(self):
|
||||
db_config = Config().get()["database"]
|
||||
cmd_resp = self.run_command(command=self.init_path, config=db_config)
|
||||
self._reconnect()
|
||||
|
||||
def run_command(self, command: str, config: dict):
|
||||
from subprocess import run
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
# Code is Apache-2.0 and docs are CC-BY-4.0
|
||||
|
||||
"""Query implementation for Tarantool"""
|
||||
|
||||
from secrets import token_hex
|
||||
from operator import itemgetter
|
||||
|
||||
@ -18,7 +17,7 @@ from json import dumps, loads
|
||||
|
||||
register_query = module_dispatch_registrar(query)
|
||||
|
||||
|
||||
@register_query(TarantoolDBConnection)
|
||||
def _group_transaction_by_ids(connection, txids: list):
|
||||
txspace = connection.get_space("transactions")
|
||||
inxspace = connection.get_space("inputs")
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
import warnings
|
||||
import logging
|
||||
|
||||
import tarantool
|
||||
from planetmint.backend.utils import module_dispatch_registrar
|
||||
from planetmint import backend
|
||||
from planetmint.backend.tarantool.connection import TarantoolDBConnection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
register_schema = module_dispatch_registrar(backend.schema)
|
||||
|
||||
SPACE_NAMES = ("abci_chains", "assets", "blocks", "blocks_tx",
|
||||
@ -162,14 +163,15 @@ def drop_database(connection, not_used=None):
|
||||
|
||||
|
||||
@register_schema(TarantoolDBConnection)
|
||||
def create_database(connection, not_used=None):
|
||||
def create_database(connection, dbname):
|
||||
'''
|
||||
|
||||
For tarantool implementation, this function runs
|
||||
create_tables, to initiate spaces, schema and indexes.
|
||||
|
||||
'''
|
||||
create_tables(None, None)
|
||||
logger.info('Create database `%s`.', dbname)
|
||||
create_tables(connection, dbname)
|
||||
|
||||
|
||||
def run_command_with_output(command):
|
||||
|
||||
@ -24,7 +24,7 @@ import aiohttp
|
||||
|
||||
from uuid import uuid4
|
||||
from concurrent.futures import CancelledError
|
||||
from planetmint import config
|
||||
from planetmint.config import Config
|
||||
from planetmint.web.websocket_dispatcher import Dispatcher
|
||||
|
||||
|
||||
@ -146,6 +146,6 @@ def start(sync_event_source, loop=None):
|
||||
|
||||
app = init_app(tx_source, blk_source, loop=loop)
|
||||
aiohttp.web.run_app(app,
|
||||
host=config['wsserver']['host'],
|
||||
port=config['wsserver']['port'],
|
||||
host=Config().get()['wsserver']['host'],
|
||||
port=Config().get()['wsserver']['port'],
|
||||
loop=loop)
|
||||
|
||||
3
setup.py
3
setup.py
@ -95,8 +95,7 @@ install_requires = [
|
||||
'setproctitle==1.2.2',
|
||||
'werkzeug==2.0.3',
|
||||
'nest-asyncio==1.5.5',
|
||||
'protobuf==3.19.0'
|
||||
|
||||
'protobuf==3.20'
|
||||
]
|
||||
|
||||
if sys.version_info < (3, 9):
|
||||
|
||||
@ -131,7 +131,7 @@ def _setup_database(_configure_planetmint): # TODO Here is located setup databa
|
||||
conn = connect()
|
||||
|
||||
_drop_db(conn, dbname)
|
||||
schema.init_database(conn)
|
||||
schema.init_database(conn, dbname)
|
||||
print('Finishing init database')
|
||||
|
||||
yield
|
||||
@ -145,7 +145,8 @@ def _setup_database(_configure_planetmint): # TODO Here is located setup databa
|
||||
|
||||
@pytest.fixture
|
||||
def _bdb(_setup_database, _configure_planetmint):
|
||||
from planetmint.backend.connection import connect
|
||||
print(f"BDB CALL")
|
||||
from planetmint.backend import connect
|
||||
from planetmint.transactions.common.memoize import to_dict, from_dict
|
||||
from planetmint.models import Transaction
|
||||
from .utils import flush_db
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user