mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
add some mongodb setup
This commit is contained in:
parent
dc5fd211c3
commit
aa08227740
0
bigchaindb/backend/mongodb/__init__.py
Normal file
0
bigchaindb/backend/mongodb/__init__.py
Normal file
46
bigchaindb/backend/mongodb/connection.py
Normal file
46
bigchaindb/backend/mongodb/connection.py
Normal file
@ -0,0 +1,46 @@
|
||||
import time
|
||||
import logging
|
||||
|
||||
from pymongo import MongoClient
|
||||
from pymongo.errors import ConnectionError
|
||||
|
||||
import bigchaindb
|
||||
from bigchaindb.backend.connection import Connection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class MongoDBConnection(Connection):
|
||||
|
||||
def __init__(self, host=None, port=None, db=None, max_tries=3):
|
||||
"""Create a new Connection instance.
|
||||
|
||||
Args:
|
||||
host (str, optional): the host to connect to.
|
||||
port (int, optional): the port to connect to.
|
||||
db (str, optional): the database to use.
|
||||
max_tries (int, optional): how many tries before giving up.
|
||||
"""
|
||||
|
||||
self.host = host or bigchaindb.config['database']['host']
|
||||
self.port = port or bigchaindb.config['database']['port']
|
||||
self.db = db or bigchaindb.config['database']['name']
|
||||
self.max_tries = max_tries
|
||||
self.conn = None
|
||||
|
||||
@property
|
||||
def db(self):
|
||||
if self.conn is None:
|
||||
self._connect()
|
||||
|
||||
else:
|
||||
return self.conn[self.db]
|
||||
|
||||
def _connect(self):
|
||||
for i in range(self.max_tries):
|
||||
try:
|
||||
self.conn = MongoClient(self.host, self.port)
|
||||
except ConnectionError as exc:
|
||||
if i + 1 == self.max_tries:
|
||||
raise
|
||||
else:
|
||||
time.sleep(2**i)
|
42
bigchaindb/backend/mongodb/query.py
Normal file
42
bigchaindb/backend/mongodb/query.py
Normal file
@ -0,0 +1,42 @@
|
||||
from bigchaindb.backend import query
|
||||
from bigchaindb.backend.mongodb.connection import MongoDBConnection
|
||||
|
||||
|
||||
@query.write_transaction.register(MongoDBConnection)
|
||||
def write_transaction(conn, signed_transaction):
|
||||
return conn.db['backlog'].insert_one(signed_transaction)
|
||||
|
||||
|
||||
@query.write_vote.register(MongoDBConnection)
|
||||
def write_vote(conn, vote):
|
||||
return conn.db['votes'].insert_one(vote)
|
||||
|
||||
|
||||
@query.write_block.register(MongoDBConnection)
|
||||
def write_block(conn, block):
|
||||
return conn.db['bigchain'].insert_one(block.to_dict())
|
||||
|
||||
|
||||
@query.genesis_block_exists.register(MongoDBConnection)
|
||||
def genesis_block_exists(conn):
|
||||
return conn.db['bigchain'].count() > 0
|
||||
|
||||
|
||||
@query.search_block_election_on_index.register(MongoDBConnection)
|
||||
def search_block_election_on_index(conn, value, index):
|
||||
return conn.db['bigchain']\
|
||||
.find({index: value}, projection={'votes', 'id', 'block.voters'})
|
||||
|
||||
|
||||
@query.get_votes_on_block.register(MongoDBConnection)
|
||||
def get_votes_on_block(conn, block_id):
|
||||
return conn.db['votes'].find({'vote.voting_for_block': block_id})
|
||||
|
||||
|
||||
@query.transaction_exists.register(MongoDBConnection)
|
||||
def transaction_exists(conn, transaction_id):
|
||||
if conn.db['bigchain']\
|
||||
.find_one({'block.transactions.id': transaction_id}):
|
||||
return True
|
||||
else:
|
||||
return False
|
Loading…
x
Reference in New Issue
Block a user