mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
refactor
This commit is contained in:
parent
ca94574150
commit
f3cc167edb
@ -1,18 +1,24 @@
|
||||
"""Query implementation for MongoDB"""
|
||||
|
||||
from time import time
|
||||
|
||||
from pymongo import ReturnDocument
|
||||
|
||||
from bigchaindb.backend import query
|
||||
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
|
||||
|
||||
|
||||
@query.write_transaction.register(MongoDBConnection)
|
||||
register_query = module_dispatch_registrar(backend.query)
|
||||
|
||||
|
||||
@register_query(MongoDBConnection)
|
||||
def write_transaction(conn, signed_transaction):
|
||||
return conn.db['backlog'].insert_one(signed_transaction)
|
||||
|
||||
|
||||
@query.update_transaction.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def update_transaction(conn, transaction_id, doc):
|
||||
return conn.db['backlog']\
|
||||
.find_one_and_update({'id': transaction_id},
|
||||
@ -20,44 +26,44 @@ def update_transaction(conn, transaction_id, doc):
|
||||
return_document=ReturnDocument.AFTER)
|
||||
|
||||
|
||||
@query.delete_transaction.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def delete_transaction(conn, *transaction_id):
|
||||
return conn.db['backlog'].delete_many({'id': {'$in': transaction_id}})
|
||||
|
||||
|
||||
@query.get_stale_transactions.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_stale_transactions(conn, reassign_delay):
|
||||
return conn.db['backlog']\
|
||||
.find({'assignment_timestamp': {'$lt': time() - reassign_delay}})
|
||||
|
||||
|
||||
@query.get_transaction_from_block.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_transaction_from_block(conn, block_id, tx_id):
|
||||
# this is definitely wrong, but it's something like this
|
||||
return conn.db['bigchain'].find_one({'id': block_id,
|
||||
'block.transactions.id': tx_id})
|
||||
|
||||
|
||||
@query.get_transaction_from_backlog.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_transaction_from_backlog(conn, transaction_id):
|
||||
return conn.db['backlog'].find_one({'id': transaction_id})
|
||||
|
||||
|
||||
@query.get_blocks_status_from_transaction.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_blocks_status_from_transaction(conn, transaction_id):
|
||||
return conn.db['bigchain']\
|
||||
.find({'block.transactions.id': transaction_id},
|
||||
projection=['id', 'block.voters'])
|
||||
|
||||
|
||||
@query.get_txids_by_asset_id.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_txids_by_asset_id(conn, asset_id):
|
||||
return conn.db['bigchain']\
|
||||
.find({'block.transactions.asset.id': asset_id},
|
||||
projection=['id'])
|
||||
|
||||
|
||||
@query.get_asset_by_id.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_asset_by_id(conn, asset_id):
|
||||
return conn.db['bigchain']\
|
||||
.find_one({'block.transactions.asset.id': asset_id,
|
||||
@ -65,7 +71,7 @@ def get_asset_by_id(conn, asset_id):
|
||||
projection=['block.transactions.asset'])
|
||||
|
||||
|
||||
@query.get_spent.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_spent(conn, transaction_id, condition_id):
|
||||
return conn.db['bigchain']\
|
||||
.find_one({'block.transactions.fulfillments.input.txid':
|
||||
@ -74,64 +80,64 @@ def get_spent(conn, transaction_id, condition_id):
|
||||
condition_id})
|
||||
|
||||
|
||||
@query.get_owned_ids.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_owned_ids(conn, owner):
|
||||
return conn.db['bigchain']\
|
||||
.find({'block.transactions.transaction.conditions.owners_after':
|
||||
owner})
|
||||
|
||||
|
||||
@query.get_votes_by_block_id.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_votes_by_block_id(conn, block_id):
|
||||
return conn.db['votes']\
|
||||
.find({'vote.voting_for_block': block_id})
|
||||
|
||||
|
||||
@query.get_votes_by_block_id_and_voter.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_votes_block_id_and_voter(conn, block_id, node_pubkey):
|
||||
return conn.db['votes']\
|
||||
.find({'vote.voting_for_block': block_id,
|
||||
'node_pubkey': node_pubkey})
|
||||
|
||||
|
||||
@query.write_block.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def write_block(conn, block):
|
||||
return conn.db['bigchain'].insert_one(block.to_dict())
|
||||
|
||||
|
||||
@query.get_block.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_block(conn, block_id):
|
||||
return conn.db['bigchain'].find_one({'id': block_id})
|
||||
|
||||
|
||||
@query.has_transaction.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def has_transaction(conn, transaction_id):
|
||||
return bool(conn.db['bigchain']
|
||||
.find_one({'block.transactions.id': transaction_id}))
|
||||
|
||||
|
||||
@query.count_blocks.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def count_blocks(conn):
|
||||
return conn.db['bigchain'].count()
|
||||
|
||||
|
||||
@query.count_backlog(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def count_backlog(conn):
|
||||
return conn.db['backlog'].count()
|
||||
|
||||
|
||||
@query.write_vote.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def write_vote(conn, vote):
|
||||
return conn.db['votes'].insert_one(vote)
|
||||
|
||||
|
||||
@query.get_genesis_block.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_genesis_block(conn):
|
||||
return conn.db['bigchain'].find_one({'block.transactions.0.operation' ==
|
||||
'GENESIS'})
|
||||
|
||||
|
||||
@query.get_last_voted_block.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_last_voted_block(conn, node_pubkey):
|
||||
last_voted = conn.db['votes']\
|
||||
.find({'node_pubkey': node_pubkey},
|
||||
@ -158,6 +164,6 @@ def get_last_voted_block(conn, node_pubkey):
|
||||
return get_block(conn, last_block_id)
|
||||
|
||||
|
||||
@query.get_unvoted_blocks.register(MongoDBConnection)
|
||||
@register_query(MongoDBConnection)
|
||||
def get_unvoted_blocks(conn, node_pubkey):
|
||||
pass
|
||||
|
Loading…
x
Reference in New Issue
Block a user