diff --git a/bigchaindb/backend/mongodb/connection.py b/bigchaindb/backend/mongodb/connection.py index eaa9852e..61af4469 100644 --- a/bigchaindb/backend/mongodb/connection.py +++ b/bigchaindb/backend/mongodb/connection.py @@ -64,7 +64,7 @@ class MongoDBConnection(Connection): time.sleep(2**i) -def table(name): +def collection(name): return Lazy()[name] diff --git a/bigchaindb/backend/mongodb/query.py b/bigchaindb/backend/mongodb/query.py index f1acf601..14c1ae21 100644 --- a/bigchaindb/backend/mongodb/query.py +++ b/bigchaindb/backend/mongodb/query.py @@ -9,7 +9,7 @@ from pymongo import errors from bigchaindb import backend from bigchaindb.common.exceptions import CyclicBlockchainError from bigchaindb.backend.utils import module_dispatch_registrar -from bigchaindb.backend.mongodb.connection import MongoDBConnection, table +from bigchaindb.backend.mongodb.connection import MongoDBConnection, collection register_query = module_dispatch_registrar(backend.query) @@ -19,7 +19,7 @@ register_query = module_dispatch_registrar(backend.query) def write_transaction(conn, signed_transaction): try: return conn.run( - table('backlog') + collection('backlog') .insert_one(signed_transaction)) except errors.DuplicateKeyError: return @@ -30,7 +30,7 @@ def update_transaction(conn, transaction_id, doc): # with mongodb we need to add update operators to the doc doc = {'$set': doc} return conn.run( - table('backlog') + collection('backlog') .find_one_and_update( {'id': transaction_id}, doc, @@ -40,14 +40,14 @@ def update_transaction(conn, transaction_id, doc): @register_query(MongoDBConnection) def delete_transaction(conn, *transaction_id): return conn.run( - table('backlog') + collection('backlog') .delete_many({'id': {'$in': transaction_id}})) @register_query(MongoDBConnection) def get_stale_transactions(conn, reassign_delay): return conn.run( - table('backlog') + collection('backlog') .find({'assignment_timestamp': {'$lt': time() - reassign_delay}}, projection={'_id': False})) @@ -56,7 +56,7 @@ def get_stale_transactions(conn, reassign_delay): def get_transaction_from_block(conn, transaction_id, block_id): try: return conn.run( - table('bigchain') + collection('bigchain') .aggregate([ {'$match': {'id': block_id}}, {'$project': { @@ -82,7 +82,7 @@ def get_transaction_from_block(conn, transaction_id, block_id): @register_query(MongoDBConnection) def get_transaction_from_backlog(conn, transaction_id): return conn.run( - table('backlog') + collection('backlog') .find_one({'id': transaction_id}, projection={'_id': False, 'assignee': False, @@ -92,7 +92,7 @@ def get_transaction_from_backlog(conn, transaction_id): @register_query(MongoDBConnection) def get_blocks_status_from_transaction(conn, transaction_id): return conn.run( - table('bigchain') + collection('bigchain') .find({'block.transactions.id': transaction_id}, projection=['id', 'block.voters'])) @@ -100,7 +100,7 @@ def get_blocks_status_from_transaction(conn, transaction_id): @register_query(MongoDBConnection) def get_asset_by_id(conn, asset_id): cursor = conn.run( - table('bigchain') + collection('bigchain') .aggregate([ {'$match': { 'block.transactions.id': asset_id, @@ -121,7 +121,7 @@ def get_asset_by_id(conn, asset_id): @register_query(MongoDBConnection) def get_spent(conn, transaction_id, output): cursor = conn.run( - table('bigchain').aggregate([ + collection('bigchain').aggregate([ {'$unwind': '$block.transactions'}, {'$match': { 'block.transactions.inputs.fulfills.txid': transaction_id, @@ -136,7 +136,7 @@ def get_spent(conn, transaction_id, output): @register_query(MongoDBConnection) def get_owned_ids(conn, owner): cursor = conn.run( - table('bigchain') + collection('bigchain') .aggregate([ {'$unwind': '$block.transactions'}, {'$match': { @@ -153,7 +153,7 @@ def get_owned_ids(conn, owner): @register_query(MongoDBConnection) def get_votes_by_block_id(conn, block_id): return conn.run( - table('votes') + collection('votes') .find({'vote.voting_for_block': block_id}, projection={'_id': False})) @@ -161,7 +161,7 @@ def get_votes_by_block_id(conn, block_id): @register_query(MongoDBConnection) def get_votes_by_block_id_and_voter(conn, block_id, node_pubkey): return conn.run( - table('votes') + collection('votes') .find({'vote.voting_for_block': block_id, 'node_pubkey': node_pubkey}, projection={'_id': False})) @@ -170,14 +170,14 @@ def get_votes_by_block_id_and_voter(conn, block_id, node_pubkey): @register_query(MongoDBConnection) def write_block(conn, block): return conn.run( - table('bigchain') + collection('bigchain') .insert_one(block.to_dict())) @register_query(MongoDBConnection) def get_block(conn, block_id): return conn.run( - table('bigchain') + collection('bigchain') .find_one({'id': block_id}, projection={'_id': False})) @@ -185,27 +185,27 @@ def get_block(conn, block_id): @register_query(MongoDBConnection) def has_transaction(conn, transaction_id): return bool(conn.run( - table('bigchain') + collection('bigchain') .find_one({'block.transactions.id': transaction_id}))) @register_query(MongoDBConnection) def count_blocks(conn): return conn.run( - table('bigchain') + collection('bigchain') .count()) @register_query(MongoDBConnection) def count_backlog(conn): return conn.run( - table('backlog') + collection('backlog') .count()) @register_query(MongoDBConnection) def write_vote(conn, vote): - conn.run(table('votes').insert_one(vote)) + conn.run(collection('votes').insert_one(vote)) vote.pop('_id') return vote @@ -213,7 +213,7 @@ def write_vote(conn, vote): @register_query(MongoDBConnection) def get_genesis_block(conn): return conn.run( - table('bigchain') + collection('bigchain') .find_one( {'block.transactions.0.operation': 'GENESIS'}, {'_id': False} @@ -223,7 +223,7 @@ def get_genesis_block(conn): @register_query(MongoDBConnection) def get_last_voted_block(conn, node_pubkey): last_voted = conn.run( - table('votes') + collection('votes') .find({'node_pubkey': node_pubkey}, sort=[('vote.timestamp', -1)])) @@ -254,7 +254,7 @@ def get_last_voted_block(conn, node_pubkey): @register_query(MongoDBConnection) def get_unvoted_blocks(conn, node_pubkey): return conn.run( - table('bigchain').aggregate([ + collection('bigchain').aggregate([ {'$lookup': { 'from': 'votes', 'localField': 'id', @@ -279,7 +279,7 @@ def get_txids_filtered(conn, asset_id, operation=None): match['block.transactions.operation'] = operation cursor = conn.run( - table('bigchain') + collection('bigchain') .aggregate([ {'$match': match}, {'$unwind': '$block.transactions'},