fix reindexing for #10

pull/19/head
deadc0de6 4 years ago
parent 5d0114d0e9
commit aec8950380

@ -67,17 +67,24 @@ class Noder:
Logger.err('No node at path \"{}\"'.format(path)) Logger.err('No node at path \"{}\"'.format(path))
return None return None
def get_node_if_changed(self, top, path): def get_node_if_changed(self, top, path, treepath):
'''return the node (if any) and if it has changed''' '''
treepath = path.lstrip(os.sep) return the node (if any) and if it has changed
@top: top node (storage)
@path: abs path to file
@treepath: rel path from indexed directory
'''
treepath = treepath.lstrip(os.sep)
node = self.get_node(top, treepath, quiet=True) node = self.get_node(top, treepath, quiet=True)
# node does not exist # node does not exist
if not node: if not node:
self._debug('node does not exist')
return None, True return None, True
# force re-indexing if no maccess # force re-indexing if no maccess
maccess = os.path.getmtime(path) maccess = os.path.getmtime(path)
if not self._has_attr(node, 'maccess') or \ if not self._has_attr(node, 'maccess') or \
not node.maccess: not node.maccess:
self._debug('changed as no maccess found')
return node, True return node, True
# maccess changed # maccess changed
old_maccess = node.maccess old_maccess = node.maccess
@ -477,4 +484,4 @@ class Noder:
def _debug(self, string): def _debug(self, string):
if not self.verbose: if not self.verbose:
return return
Logger.info('getting node size recursively') Logger.info(string)

@ -55,19 +55,25 @@ class Walker:
def reindex(self, path, parent, top): def reindex(self, path, parent, top):
'''reindex a directory and store in tree''' '''reindex a directory and store in tree'''
cnt = self._reindex(path, parent, top, '') cnt = self._reindex(path, parent, top)
cnt += self.noder.clean_not_flagged(parent) cnt += self.noder.clean_not_flagged(parent)
return cnt return cnt
def _reindex(self, path, parent, top, storagepath): def _reindex(self, path, parent, top, storagepath=''):
'''reindex a directory and store in tree''' '''
reindex a directory and store in tree
@path: directory path to re-index
@top: top node (storage)
@storagepath: rel path relative to indexed directory
'''
self._debug('reindexing starting at {}'.format(path)) self._debug('reindexing starting at {}'.format(path))
cnt = 0 cnt = 0
for (root, dirs, files) in os.walk(path): for (root, dirs, files) in os.walk(path):
for f in files: for f in files:
self._debug('found file {} under {}'.format(f, path)) self._debug('found file \"{}\" under {}'.format(f, path))
sub = os.path.join(root, f) sub = os.path.join(root, f)
reindex, n = self._need_reindex(parent, sub) treepath = os.path.join(storagepath, f)
reindex, n = self._need_reindex(parent, sub, treepath)
if not reindex: if not reindex:
self._debug('\tignore file {}'.format(sub)) self._debug('\tignore file {}'.format(sub))
self.noder.flag(n) self.noder.flag(n)
@ -79,10 +85,11 @@ class Walker:
self.noder.flag(n) self.noder.flag(n)
cnt += 1 cnt += 1
for d in dirs: for d in dirs:
self._debug('found dir {} under {}'.format(d, path)) self._debug('found dir \"{}\" under {}'.format(d, path))
base = os.path.basename(d) base = os.path.basename(d)
sub = os.path.join(root, d) sub = os.path.join(root, d)
reindex, dummy = self._need_reindex(parent, sub) treepath = os.path.join(storagepath, d)
reindex, dummy = self._need_reindex(parent, sub, treepath)
if reindex: if reindex:
self._debug('\tre-index directory {}'.format(sub)) self._debug('\tre-index directory {}'.format(sub))
dummy = self.noder.dir_node(base, sub, parent, storagepath) dummy = self.noder.dir_node(base, sub, parent, storagepath)
@ -98,22 +105,27 @@ class Walker:
self._log(None) self._log(None)
return cnt return cnt
def _need_reindex(self, top, path): def _need_reindex(self, top, path, treepath):
'''test if node needs re-indexing''' '''
cnode, changed = self.noder.get_node_if_changed(top, path) test if node needs re-indexing
@top: top node (storage)
@path: abs path to file
@treepath: rel path from indexed directory
'''
cnode, changed = self.noder.get_node_if_changed(top, path, treepath)
if not cnode: if not cnode:
self._debug('\tdoes not exist') self._debug('{} does not exist'.format(path))
return True, cnode return True, cnode
if cnode and not changed: if cnode and not changed:
# ignore this node # ignore this node
self._debug('\thas not changed') self._debug('{} has not changed'.format(path))
return False, cnode return False, cnode
if cnode and changed: if cnode and changed:
# remove this node and re-add # remove this node and re-add
self._debug('\thas changed') self._debug('{} has changed'.format(path))
self._debug('\tremoving node {}'.format(cnode)) self._debug('removing node {} for {}'.format(cnode, path))
cnode.parent = None cnode.parent = None
self._debug('\tis to be re-indexed') self._debug('{} is to be re-indexed'.format(path))
return True, cnode return True, cnode
def _debug(self, string): def _debug(self, string):

Loading…
Cancel
Save