API for extra files cache: resolve conflicts with edbf95a plus a few changes after more testing

This commit is contained in:
Charles Haley 2023-04-23 11:06:41 +01:00
parent 5468a465ee
commit 310ccfd832
2 changed files with 18 additions and 15 deletions

View File

@ -69,7 +69,7 @@ class MetadataBackup(Thread):
return return
traceback.print_exc() traceback.print_exc()
self.db.check_save_extra_files_cache_needed() self.db.save_extra_files_cache()
try: try:
book_id = self.db.get_a_dirtied_book() book_id = self.db.get_a_dirtied_book()

View File

@ -294,7 +294,7 @@ class Cache:
self.extra_files_cache_dirty = True self.extra_files_cache_dirty = True
@write_api @write_api
def save_extra_files_cache_if_needed(self): def save_extra_files_cache(self):
if self.extra_files_cache_dirty: if self.extra_files_cache_dirty:
self.backend.prefs.set('extra_files_cache', self.extra_files_cache) self.backend.prefs.set('extra_files_cache', self.extra_files_cache)
self.extra_files_cache_dirty = False self.extra_files_cache_dirty = False
@ -2695,7 +2695,7 @@ class Cache:
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
# do this last in case a plugin changes the extra files # do this last in case a plugin changes the extra files
self.check_save_extra_files_cache_needed() self.save_extra_files_cache()
self._shutdown_fts(stage=2) self._shutdown_fts(stage=2)
with self.write_lock: with self.write_lock:
self.backend.close() self.backend.close()
@ -3121,26 +3121,29 @@ class Cache:
@write_api @write_api
def list_extra_files(self, book_id): def list_extra_files(self, book_id):
''' '''
For book_id, returns the dict { Returns information for files in the book's data directory.
'relpath': file's relative path from the book's 'data' directory,
'file_path': full path to the file, :param book_id: the database book id for the book
'mtime': the file's modification time as a floating point number,
'fsize': the file's size in bytes :return: {rel_path: {'file_path', mtime, fsize} ... }
} where:
rel_path is the relative path to the file from the data/ directory
file_path is the full path to the file
mtime is the file's modification time. The epoch is OS dependent
fsize is the file's size in bytes
''' '''
ans = self.get_extra_files_from_cache(book_id) ans = self.get_extra_files_from_cache(book_id)
if not ans: if not ans:
print('not cached', book_id)
path = self._field_for('path', book_id) path = self._field_for('path', book_id)
if path: if path:
book_path = (path + '/data').replace('/', os.sep) book_path = (path + '/data').replace('/', os.sep)
for (relpath, file_path, mtime, fsize) in self.backend.iter_extra_files( for (relpath, file_path, mtime, fsize) in self.backend.iter_extra_files(
book_id, book_path, None, yield_paths=True): book_id, book_path, None, yield_paths=True):
ans = dict(zip(('relpath', 'file_path', 'mtime', 'fsize'), ans[file_path] = dict(zip(('relpath', 'mtime', 'fsize'),
(relpath, file_path, mtime, fsize))) (relpath, mtime, fsize)))
self.add_to_extra_files_cache(book_id, ans) if ans:
else: self.add_to_extra_files_cache(book_id, ans)
print('cached', book_id)
return ans return ans
@read_api @read_api