Filter list persistencE

This commit is contained in:
lash 2022-04-28 06:45:59 +00:00
parent 384c79bed0
commit ca82ea247f
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
7 changed files with 80 additions and 8 deletions

View File

@ -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()

View File

@ -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__':

View File

@ -196,6 +196,8 @@ class SyncStore:
if self.started:
return
self.save_filter_list()
self.load(target)
if self.first:

View File

@ -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()

View File

@ -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 []

View File

@ -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(',')

View File

@ -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')