forked from LBRYCommunity/lbry-sdk
batch save supports
This commit is contained in:
parent
ebc9ed01c5
commit
76dea9e89b
3 changed files with 17 additions and 17 deletions
|
@ -2522,13 +2522,13 @@ class Daemon(metaclass=JSONRPCServerType):
|
||||||
if not preview:
|
if not preview:
|
||||||
await tx.sign([account])
|
await tx.sign([account])
|
||||||
await account.ledger.broadcast(tx)
|
await account.ledger.broadcast(tx)
|
||||||
await self.storage.save_supports(claim_id, [{
|
await self.storage.save_supports({claim_id: [{
|
||||||
'txid': tx.id,
|
'txid': tx.id,
|
||||||
'nout': tx.position,
|
'nout': tx.position,
|
||||||
'address': claim_address,
|
'address': claim_address,
|
||||||
'claim_id': claim_id,
|
'claim_id': claim_id,
|
||||||
'amount': dewies_to_lbc(amount)
|
'amount': dewies_to_lbc(amount)
|
||||||
}])
|
}]})
|
||||||
await self.analytics_manager.send_claim_action('new_support')
|
await self.analytics_manager.send_claim_action('new_support')
|
||||||
else:
|
else:
|
||||||
await account.ledger.release_tx(tx)
|
await account.ledger.release_tx(tx)
|
||||||
|
|
|
@ -539,10 +539,12 @@ class SQLiteStorage(SQLiteMixin):
|
||||||
|
|
||||||
# # # # # # # # # support functions # # # # # # # # #
|
# # # # # # # # # support functions # # # # # # # # #
|
||||||
|
|
||||||
def save_supports(self, claim_id, supports):
|
def save_supports(self, claim_id_to_supports: dict):
|
||||||
# TODO: add 'address' to support items returned for a claim from lbrycrdd and lbryum-server
|
# TODO: add 'address' to support items returned for a claim from lbrycrdd and lbryum-server
|
||||||
def _save_support(transaction):
|
def _save_support(transaction):
|
||||||
transaction.execute("delete from support where claim_id=?", (claim_id,))
|
bind = "({})".format(','.join(['?'] * len(claim_id_to_supports)))
|
||||||
|
transaction.execute(f"delete from support where claim_id in {bind}", list(claim_id_to_supports.keys()))
|
||||||
|
for claim_id, supports in claim_id_to_supports.items():
|
||||||
for support in supports:
|
for support in supports:
|
||||||
transaction.execute(
|
transaction.execute(
|
||||||
"insert into support values (?, ?, ?, ?)",
|
"insert into support values (?, ?, ?, ?)",
|
||||||
|
@ -576,7 +578,7 @@ class SQLiteStorage(SQLiteMixin):
|
||||||
# # # # # # # # # claim functions # # # # # # # # #
|
# # # # # # # # # claim functions # # # # # # # # #
|
||||||
|
|
||||||
async def save_claims(self, claim_infos):
|
async def save_claims(self, claim_infos):
|
||||||
support_callbacks = []
|
claim_id_to_supports = {}
|
||||||
update_file_callbacks = []
|
update_file_callbacks = []
|
||||||
|
|
||||||
def _save_claims(transaction):
|
def _save_claims(transaction):
|
||||||
|
@ -602,7 +604,7 @@ class SQLiteStorage(SQLiteMixin):
|
||||||
# if this response doesn't have support info don't overwrite the existing
|
# if this response doesn't have support info don't overwrite the existing
|
||||||
# support info
|
# support info
|
||||||
if 'supports' in claim_info:
|
if 'supports' in claim_info:
|
||||||
support_callbacks.append((claim_id, claim_info['supports']))
|
claim_id_to_supports[claim_id] = claim_info['supports']
|
||||||
if not source_hash:
|
if not source_hash:
|
||||||
continue
|
continue
|
||||||
stream_hash = transaction.execute(
|
stream_hash = transaction.execute(
|
||||||
|
@ -632,10 +634,8 @@ class SQLiteStorage(SQLiteMixin):
|
||||||
await self.db.run(_save_claims)
|
await self.db.run(_save_claims)
|
||||||
if update_file_callbacks:
|
if update_file_callbacks:
|
||||||
await asyncio.wait(update_file_callbacks)
|
await asyncio.wait(update_file_callbacks)
|
||||||
if support_callbacks:
|
if claim_id_to_supports:
|
||||||
await asyncio.wait([
|
await self.save_supports(claim_id_to_supports)
|
||||||
self.save_supports(*args) for args in support_callbacks
|
|
||||||
])
|
|
||||||
|
|
||||||
def save_claims_for_resolve(self, claim_infos):
|
def save_claims_for_resolve(self, claim_infos):
|
||||||
to_save = []
|
to_save = []
|
||||||
|
|
|
@ -130,7 +130,7 @@ class TestSQLiteStorage(StorageTest):
|
||||||
} for i in range(20)]
|
} for i in range(20)]
|
||||||
expected_supports = {}
|
expected_supports = {}
|
||||||
for idx, claim_id in enumerate(claim_ids):
|
for idx, claim_id in enumerate(claim_ids):
|
||||||
await self.storage.save_supports(claim_id, random_supports[idx*2:idx*2+2])
|
await self.storage.save_supports({claim_id: random_supports[idx*2:idx*2+2]})
|
||||||
for random_support in random_supports[idx*2:idx*2+2]:
|
for random_support in random_supports[idx*2:idx*2+2]:
|
||||||
random_support['claim_id'] = claim_id
|
random_support['claim_id'] = claim_id
|
||||||
expected_supports.setdefault(claim_id, []).append(random_support)
|
expected_supports.setdefault(claim_id, []).append(random_support)
|
||||||
|
|
Loading…
Add table
Reference in a new issue