From b91181e6409dc4d86e1e07ea81577cf61bca0227 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Wed, 30 May 2018 04:26:00 -0300 Subject: [PATCH] refactor: simplify add_completed_blob + changelog --- CHANGELOG.md | 1 + lbrynet/database/storage.py | 20 ++++--------------- .../tests/unit/database/test_SQLiteStorage.py | 3 +-- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 469a76f95..d417dfee7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ at anytime. * ### Changed + * refactor `add_completed_blobs` on storage.py, simplifying into less queries * check headers file integrity on startup, removing/truncating the file to force re-download when necessary * support partial headers file download from S3 * changed txrequests for treq diff --git a/lbrynet/database/storage.py b/lbrynet/database/storage.py index aae8f180a..8789f43df 100644 --- a/lbrynet/database/storage.py +++ b/lbrynet/database/storage.py @@ -208,27 +208,15 @@ class SQLiteStorage(object): # # # # # # # # # blob functions # # # # # # # # # - @defer.inlineCallbacks - def add_completed_blob(self, blob_hash, length, next_announce_time, should_announce): + def add_completed_blob(self, blob_hash, length, next_announce_time, should_announce, status="finished"): log.debug("Adding a completed blob. blob_hash=%s, length=%i", blob_hash, length) - yield self.add_known_blob(blob_hash, length) - yield self.set_blob_status(blob_hash, "finished") - yield self.set_should_announce(blob_hash, next_announce_time, should_announce) - yield self.db.runOperation( - "update blob set blob_length=? where blob_hash=?", (length, blob_hash) - ) + values = (blob_hash, length, next_announce_time or 0, int(bool(should_announce)), status, 0, 0) + return self.db.runOperation("insert or replace into blob values (?, ?, ?, ?, ?, ?, ?)", values) def set_should_announce(self, blob_hash, next_announce_time, should_announce): - next_announce_time = next_announce_time or 0 - should_announce = 1 if should_announce else 0 return self.db.runOperation( "update blob set next_announce_time=?, should_announce=? where blob_hash=?", - (next_announce_time, should_announce, blob_hash) - ) - - def set_blob_status(self, blob_hash, status): - return self.db.runOperation( - "update blob set status=? where blob_hash=?", (status, blob_hash) + (next_announce_time or 0, int(bool(should_announce)), blob_hash) ) def get_blob_status(self, blob_hash): diff --git a/lbrynet/tests/unit/database/test_SQLiteStorage.py b/lbrynet/tests/unit/database/test_SQLiteStorage.py index 5df80ee2e..3829b25ed 100644 --- a/lbrynet/tests/unit/database/test_SQLiteStorage.py +++ b/lbrynet/tests/unit/database/test_SQLiteStorage.py @@ -98,8 +98,7 @@ class StorageTest(unittest.TestCase): @defer.inlineCallbacks def store_fake_blob(self, blob_hash, blob_length=100, next_announce=0, should_announce=0): yield self.storage.add_completed_blob(blob_hash, blob_length, next_announce, - should_announce) - yield self.storage.set_blob_status(blob_hash, "finished") + should_announce, "finished") @defer.inlineCallbacks def store_fake_stream_blob(self, stream_hash, blob_hash, blob_num, length=100, iv="DEADBEEF"):