Factor out sync state scanner

This commit is contained in:
lash 2022-04-20 12:31:32 +00:00
parent 891f90ae5f
commit 97c2d41df3
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 32 additions and 23 deletions

View File

@ -12,7 +12,7 @@ re_processedname = r'^_?[A-Z,\.]*$'
# TODO: properly clarify interface shared with syncfsstore, move to filter module? # TODO: properly clarify interface shared with syncfsstore, move to filter module?
class SyncState: class SyncState:
def __init__(self, state_store, scan_path=None): def __init__(self, state_store, scan=None):
self.state_store = state_store self.state_store = state_store
self.digest = b'\x00' * 32 self.digest = b'\x00' * 32
self.summed = False self.summed = False
@ -36,7 +36,7 @@ class SyncState:
self.all = self.state_store.all self.all = self.state_store.all
self.started = False self.started = False
self.scan_path = scan_path self.scan = scan
def __verify_sum(self, v): def __verify_sum(self, v):
@ -72,17 +72,17 @@ class SyncState:
k = self.state_store.from_name(v) k = self.state_store.from_name(v)
self.state_store.sync(k) self.state_store.sync(k)
self.__syncs[v] = True self.__syncs[v] = True
if self.scan_path != None: if self.scan != None:
for v in os.listdir(self.scan_path): ks = self.scan()
if re.match(re_processedname, v): for v in ks: #os.listdir(self.scan_path):
k = None k = None
try: try:
k = self.state_store.from_elements(v) k = self.state_store.from_elements(v)
self.state_store.alias(v, k) self.state_store.alias(v, k)
except ValueError: except ValueError:
k = self.state_store.from_name(v) k = self.state_store.from_name(v)
self.state_store.sync(k) self.state_store.sync(k)
self.__syncs[v] = True self.__syncs[v] = True
self.synced = True self.synced = True
self.connected = True self.connected = True

View File

@ -2,7 +2,9 @@
import logging import logging
# local imports # local imports
from shep.persist import PersistedState
from shep.error import StateInvalid from shep.error import StateInvalid
from chainsyncer.state import SyncState
from chainsyncer.error import ( from chainsyncer.error import (
LockError, LockError,
FilterDone, FilterDone,
@ -154,6 +156,18 @@ class SyncStore:
self.started = False self.started = False
def setup_sync_state(self, factory, event_callback):
self.state = PersistedState(factory.add, 2, event_callback=event_callback)
self.state.add('SYNC')
self.state.add('DONE')
def setup_filter_state(self, factory, event_callback):
filter_state_backend = PersistedState(factory.add, 0, check_alias=False, event_callback=event_callback)
self.filter_state = SyncState(filter_state_backend, scan=factory.ls)
self.filters = []
def register(self, fltr): def register(self, fltr):
self.filters.append(fltr) self.filters.append(fltr)
self.filter_state.register(fltr) self.filter_state.register(fltr)

View File

@ -5,10 +5,8 @@ import logging
# external imports # external imports
from shep.store.file import SimpleFileStoreFactory from shep.store.file import SimpleFileStoreFactory
from shep.persist import PersistedState
# local imports # local imports
from chainsyncer.state import SyncState
from chainsyncer.store import ( from chainsyncer.store import (
SyncItem, SyncItem,
SyncStore, SyncStore,
@ -45,16 +43,13 @@ class SyncFsStore(SyncStore):
logg.info('session id {} resolved {} path {}'.format(session_id, self.session_id, self.session_path)) logg.info('session id {} resolved {} path {}'.format(session_id, self.session_id, self.session_path))
factory = SimpleFileStoreFactory(self.session_path, binary=True) base_sync_path = os.path.join(self.session_path, 'sync')
self.state = PersistedState(factory.add, 2, event_callback=state_event_callback) factory = SimpleFileStoreFactory(base_sync_path, binary=True)
self.state.add('SYNC') self.setup_sync_state(factory, state_event_callback)
self.state.add('DONE')
base_filter_path = os.path.join(self.session_path, 'filter') base_filter_path = os.path.join(self.session_path, 'filter')
factory = SimpleFileStoreFactory(base_filter_path, binary=True) factory = SimpleFileStoreFactory(base_filter_path, binary=True)
filter_state_backend = PersistedState(factory.add, 0, check_alias=False, event_callback=filter_state_event_callback) self.setup_filter_state(factory, filter_state_event_callback)
self.filter_state = SyncState(filter_state_backend, scan_path=base_filter_path)
self.filters = [] # used by SyncSession
def __create_path(self, base_path, default_path, session_id=None): def __create_path(self, base_path, default_path, session_id=None):