From 76dea9e89b5f1f3d6b0c50c555a1c7251ae7ae72 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Fri, 19 Apr 2019 20:42:35 -0300 Subject: [PATCH] batch save supports --- lbrynet/extras/daemon/Daemon.py | 4 ++-- lbrynet/extras/daemon/storage.py | 28 +++++++++++------------ tests/unit/database/test_SQLiteStorage.py | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lbrynet/extras/daemon/Daemon.py b/lbrynet/extras/daemon/Daemon.py index 04154fb7b..376593857 100644 --- a/lbrynet/extras/daemon/Daemon.py +++ b/lbrynet/extras/daemon/Daemon.py @@ -2522,13 +2522,13 @@ class Daemon(metaclass=JSONRPCServerType): if not preview: await tx.sign([account]) await account.ledger.broadcast(tx) - await self.storage.save_supports(claim_id, [{ + await self.storage.save_supports({claim_id: [{ 'txid': tx.id, 'nout': tx.position, 'address': claim_address, 'claim_id': claim_id, 'amount': dewies_to_lbc(amount) - }]) + }]}) await self.analytics_manager.send_claim_action('new_support') else: await account.ledger.release_tx(tx) diff --git a/lbrynet/extras/daemon/storage.py b/lbrynet/extras/daemon/storage.py index 51b8bc8f8..c325a2c92 100644 --- a/lbrynet/extras/daemon/storage.py +++ b/lbrynet/extras/daemon/storage.py @@ -539,16 +539,18 @@ class SQLiteStorage(SQLiteMixin): # # # # # # # # # 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 def _save_support(transaction): - transaction.execute("delete from support where claim_id=?", (claim_id,)) - for support in supports: - transaction.execute( - "insert into support values (?, ?, ?, ?)", - ("%s:%i" % (support['txid'], support['nout']), claim_id, lbc_to_dewies(support['amount']), - support.get('address', "")) - ) + 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: + transaction.execute( + "insert into support values (?, ?, ?, ?)", + ("%s:%i" % (support['txid'], support['nout']), claim_id, lbc_to_dewies(support['amount']), + support.get('address', "")) + ) return self.db.run(_save_support) def get_supports(self, *claim_ids): @@ -576,7 +578,7 @@ class SQLiteStorage(SQLiteMixin): # # # # # # # # # claim functions # # # # # # # # # async def save_claims(self, claim_infos): - support_callbacks = [] + claim_id_to_supports = {} update_file_callbacks = [] def _save_claims(transaction): @@ -602,7 +604,7 @@ class SQLiteStorage(SQLiteMixin): # if this response doesn't have support info don't overwrite the existing # support 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: continue stream_hash = transaction.execute( @@ -632,10 +634,8 @@ class SQLiteStorage(SQLiteMixin): await self.db.run(_save_claims) if update_file_callbacks: await asyncio.wait(update_file_callbacks) - if support_callbacks: - await asyncio.wait([ - self.save_supports(*args) for args in support_callbacks - ]) + if claim_id_to_supports: + await self.save_supports(claim_id_to_supports) def save_claims_for_resolve(self, claim_infos): to_save = [] diff --git a/tests/unit/database/test_SQLiteStorage.py b/tests/unit/database/test_SQLiteStorage.py index 71cea752c..0f380b17b 100644 --- a/tests/unit/database/test_SQLiteStorage.py +++ b/tests/unit/database/test_SQLiteStorage.py @@ -130,7 +130,7 @@ class TestSQLiteStorage(StorageTest): } for i in range(20)] expected_supports = {} 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]: random_support['claim_id'] = claim_id expected_supports.setdefault(claim_id, []).append(random_support)