mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Add caching for extracting text to search
This commit is contained in:
parent
9fd48d9911
commit
a79c17d555
@ -360,7 +360,7 @@ class DB:
|
||||
mt = fdata.mimetype or 'application/octet-stream'
|
||||
if fdata.encoded:
|
||||
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):
|
||||
details = get_error_details(event)
|
||||
proceed({'ok': False, 'name': name, 'details': details})
|
||||
|
@ -53,14 +53,18 @@ class SearchOverlay:
|
||||
self._worker = start_worker('read_book.search')
|
||||
return self._worker
|
||||
|
||||
def queue_search(self, book, current_name):
|
||||
def queue_search(self, query, book, current_name):
|
||||
spine = book.manifest.spine
|
||||
self.request_counter += 1
|
||||
self.worker.postMessage({
|
||||
'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):
|
||||
if event.key is 'Escape' or event.key is 'Esc':
|
||||
self.hide()
|
||||
|
@ -3,6 +3,7 @@
|
||||
from __python__ import bound_methods, hash_literals
|
||||
|
||||
from read_book.db import DB
|
||||
from read_book.resources import text_from_serialized_html
|
||||
|
||||
GET_SPINE_FAILED = 1
|
||||
CONNECT_FAILED = 2
|
||||
@ -19,11 +20,15 @@ class Worker:
|
||||
self.searching = False
|
||||
self.current_query = None
|
||||
self.current_query_id = None
|
||||
self.text_cache = {}
|
||||
|
||||
@property
|
||||
def initialize_error_msg(self):
|
||||
return self.db?.initialize_error_msg
|
||||
|
||||
def clear_caches(self):
|
||||
self.text_cache = {}
|
||||
|
||||
|
||||
wc = Worker()
|
||||
|
||||
@ -33,11 +38,19 @@ def send_search_complete():
|
||||
wc.current_query = wc.current_query_id = None
|
||||
|
||||
|
||||
def search_in_text_of(name):
|
||||
pass
|
||||
|
||||
|
||||
def queue_next_spine_item(spine_idx):
|
||||
name = wc.current_query.spine[spine_idx]
|
||||
if not name:
|
||||
send_search_complete()
|
||||
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
|
||||
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:
|
||||
return
|
||||
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:
|
||||
if result.details is 'ENOENT':
|
||||
queue_next_spine_item(spine_idx + 1)
|
||||
@ -97,6 +113,8 @@ def worker_main():
|
||||
perform_search(e.data)
|
||||
else:
|
||||
wc.pending_search = e.data
|
||||
elif e.data.type is 'clear_caches':
|
||||
wc.clear_caches()
|
||||
|
||||
self.onerror = def(e):
|
||||
send_error(UNHANDLED_ERROR, f'{e.line}:{e.message}')
|
||||
|
@ -932,6 +932,7 @@ class View:
|
||||
self.loaded_resources = {}
|
||||
self.content_popup_overlay.loaded_resources = {}
|
||||
self.timers.start_book(book)
|
||||
self.search_overlay.clear_caches()
|
||||
unkey = username_key(get_interface_data().username)
|
||||
self.book = current_book.book = book
|
||||
hl = None
|
||||
|
Loading…
x
Reference in New Issue
Block a user