Add caching for extracting text to search

This commit is contained in:
Kovid Goyal 2021-05-17 09:36:57 +05:30
parent 9fd48d9911
commit a79c17d555
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 27 additions and 4 deletions

View File

@ -360,7 +360,7 @@ class DB:
mt = fdata.mimetype or 'application/octet-stream' mt = fdata.mimetype or 'application/octet-stream'
if fdata.encoded: if fdata.encoded:
result = Blob([base64decode(result)], {'type':mt}) result = Blob([base64decode(result)], {'type':mt})
proceed({'ok': True, 'result': result, 'name': name, 'mt': mt}) proceed({'ok': True, 'result': req.result, 'name': name, 'mt': mt})
req.onerror = def(event): req.onerror = def(event):
details = get_error_details(event) details = get_error_details(event)
proceed({'ok': False, 'name': name, 'details': details}) proceed({'ok': False, 'name': name, 'details': details})

View File

@ -53,14 +53,18 @@ class SearchOverlay:
self._worker = start_worker('read_book.search') self._worker = start_worker('read_book.search')
return self._worker return self._worker
def queue_search(self, book, current_name): def queue_search(self, query, book, current_name):
spine = book.manifest.spine spine = book.manifest.spine
self.request_counter += 1 self.request_counter += 1
self.worker.postMessage({ self.worker.postMessage({
'type': 'search', 'book_hash': book.book_hash, 'spine': spine, 'current_name': current_name, 'type': 'search', 'book_hash': book.book_hash, 'spine': spine, 'current_name': current_name,
'id': self.request_counter, 'stored_files': book.stored_files 'id': self.request_counter, 'stored_files': book.stored_files, 'query': query
}) })
def clear_caches(self):
if self._worker:
self.worker.postMessage({'type': 'clear_caches'})
def onkeydown(self, event): def onkeydown(self, event):
if event.key is 'Escape' or event.key is 'Esc': if event.key is 'Escape' or event.key is 'Esc':
self.hide() self.hide()

View File

@ -3,6 +3,7 @@
from __python__ import bound_methods, hash_literals from __python__ import bound_methods, hash_literals
from read_book.db import DB from read_book.db import DB
from read_book.resources import text_from_serialized_html
GET_SPINE_FAILED = 1 GET_SPINE_FAILED = 1
CONNECT_FAILED = 2 CONNECT_FAILED = 2
@ -19,11 +20,15 @@ class Worker:
self.searching = False self.searching = False
self.current_query = None self.current_query = None
self.current_query_id = None self.current_query_id = None
self.text_cache = {}
@property @property
def initialize_error_msg(self): def initialize_error_msg(self):
return self.db?.initialize_error_msg return self.db?.initialize_error_msg
def clear_caches(self):
self.text_cache = {}
wc = Worker() wc = Worker()
@ -33,11 +38,19 @@ def send_search_complete():
wc.current_query = wc.current_query_id = None wc.current_query = wc.current_query_id = None
def search_in_text_of(name):
pass
def queue_next_spine_item(spine_idx): def queue_next_spine_item(spine_idx):
name = wc.current_query.spine[spine_idx] name = wc.current_query.spine[spine_idx]
if not name: if not name:
send_search_complete() send_search_complete()
return return
if wc.text_cache[name]:
search_in_text_of(name)
setTimeout(queue_next_spine_item.bind(None, spine_idx + 1), 0)
return
query = wc.current_query query = wc.current_query
wc.db.get_book_file(query.book_hash, query.stored_files, name, got_spine_item.bind(None, query.id, spine_idx)) wc.db.get_book_file(query.book_hash, query.stored_files, name, got_spine_item.bind(None, query.id, spine_idx))
@ -46,7 +59,10 @@ def got_spine_item(query_id, spine_idx, result):
if query_id is not wc.current_query_id: if query_id is not wc.current_query_id:
return return
if result.ok: if result.ok:
queue_next_spine_item(spine_idx + 1) name = wc.current_query.spine[spine_idx]
wc.text_cache[name] = text_from_serialized_html(result.result)
search_in_text_of(name)
setTimeout(queue_next_spine_item.bind(None, spine_idx + 1), 0)
else: else:
if result.details is 'ENOENT': if result.details is 'ENOENT':
queue_next_spine_item(spine_idx + 1) queue_next_spine_item(spine_idx + 1)
@ -97,6 +113,8 @@ def worker_main():
perform_search(e.data) perform_search(e.data)
else: else:
wc.pending_search = e.data wc.pending_search = e.data
elif e.data.type is 'clear_caches':
wc.clear_caches()
self.onerror = def(e): self.onerror = def(e):
send_error(UNHANDLED_ERROR, f'{e.line}:{e.message}') send_error(UNHANDLED_ERROR, f'{e.line}:{e.message}')

View File

@ -932,6 +932,7 @@ class View:
self.loaded_resources = {} self.loaded_resources = {}
self.content_popup_overlay.loaded_resources = {} self.content_popup_overlay.loaded_resources = {}
self.timers.start_book(book) self.timers.start_book(book)
self.search_overlay.clear_caches()
unkey = username_key(get_interface_data().username) unkey = username_key(get_interface_data().username)
self.book = current_book.book = book self.book = current_book.book = book
hl = None hl = None