commit ca82ea247f857ccc9b60c8729b9a1ee67b7c4378
parent 384c79bed0cead2792767fb4ff2348017d84156f
Author: lash <dev@holbrook.no>
Date: Thu, 28 Apr 2022 06:45:59 +0000
Filter list persistencE
Diffstat:
7 files changed, 80 insertions(+), 8 deletions(-)
diff --git a/chainsyncer/driver/base.py b/chainsyncer/driver/base.py
@@ -141,3 +141,12 @@ class SyncDriver:
def get(self, conn):
raise NotImplementedError()
+
+
+ def save_filter_list(self):
+ raise NotImplementedError()
+
+
+ def load_filter_list(self):
+ raise NotImplementedError()
+
diff --git a/chainsyncer/runnable/lock.py b/chainsyncer/runnable/lock.py
@@ -58,19 +58,12 @@ def main():
state_dir = config.get('_STATE_DIR')
factory = syncer_store_class(state_dir)
- store = SyncStore(state_dir, no_session=True)
- #base_state = PersistedState(factory.add, 0, check_alias=False)
- #state = FilterState(base_state, scan=True)
+ store = SyncStore(state_dir)
store.setup_filter_state(factory=factory)
store.connect()
store.filter_state.scan()
locked_state = store.filter_state.list(store.filter_state.from_name('RESET'))
print(locked_state)
-# if locked_state == None:
-# sys.stderr.write('state in {} backend "{}" is not locked\n'.format(state_dir, config.get('SYNCER_BACKEND')))
-# sys.exit(1)
-
-
if __name__ == '__main__':
diff --git a/chainsyncer/store/base.py b/chainsyncer/store/base.py
@@ -196,6 +196,8 @@ class SyncStore:
if self.started:
return
+ self.save_filter_list()
+
self.load(target)
if self.first:
diff --git a/chainsyncer/store/fs.py b/chainsyncer/store/fs.py
@@ -74,3 +74,25 @@ class SyncFsStore(SyncStore):
f.write(str(v))
f.close()
self.target = v
+
+
+ def load_filter_list(self):
+ fltr = []
+ fp = os.path.join(self.session_path, 'filter_list')
+ f = open(fp, 'r')
+ while True:
+ v = f.readline()
+ if len(v) == 0:
+ break
+ v = v.rstrip()
+ fltr.append(v)
+ f.close()
+ return fltr
+
+
+ def save_filter_list(self):
+ fp = os.path.join(self.session_path, 'filter_list')
+ f = open(fp, 'w')
+ for fltr in self.filters:
+ f.write(fltr.common_name() + '\n')
+ f.close()
diff --git a/chainsyncer/store/mem.py b/chainsyncer/store/mem.py
@@ -35,3 +35,11 @@ class SyncMemStore(SyncStore):
if item != None:
super(SyncMemStore, self).stop(item)
logg.info('I am an in-memory only state store. I am shutting down now, so all state will now be discarded.')
+
+
+ def save_filter_list(self):
+ pass
+
+
+ def load_filter_list(self):
+ return []
diff --git a/chainsyncer/store/rocksdb.py b/chainsyncer/store/rocksdb.py
@@ -64,3 +64,17 @@ class SyncRocksDbStore(SyncStore):
if item != None:
super(SyncRocksDbStore, self).stop(item)
self.factory.close()
+
+
+ def save_filter_list(self):
+ fltr = []
+ for v in self.filters:
+ fltr.append(v.common_name())
+ self.target_db.put('filter_list', ','.join(fltr))
+
+
+ def load_filter_list(self):
+ v = self.target_db.get('filter_list')
+ v = v.decode('utf-8')
+ return v.split(',')
+
diff --git a/chainsyncer/unittest/store.py b/chainsyncer/unittest/store.py
@@ -50,6 +50,7 @@ class TestStoreBase(unittest.TestCase):
"default",
"store_start",
"store_resume",
+ "filter_list",
"sync_process_nofilter",
"sync_process_onefilter",
"sync_process_outoforder",
@@ -275,3 +276,26 @@ class TestStoreBase(unittest.TestCase):
o.next(advance_block=True)
with self.assertRaises(SyncDone):
o.next(advance_block=True)
+
+
+ def t_filter_list(self):
+ bogus_item = MockItem(0, 0, 0, 0)
+ store = self.store_factory()
+
+ if store.session_path == None:
+ return
+
+ fltr_one = MockFilter('foo_bar')
+ store.register(fltr_one)
+
+ fltr_two = MockFilter('bar_baz')
+ store.register(fltr_two)
+
+ store.start()
+ store.stop(bogus_item)
+
+ store = self.store_factory()
+ r = store.load_filter_list()
+
+ self.assertEqual(r[0], 'foo_bar')
+ self.assertEqual(r[1], 'bar_baz')