Add API to process each result when returning it

This commit is contained in:
Kovid Goyal 2022-08-08 07:38:14 +05:30
parent a398c0ece2
commit 9ae2074669
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 10 additions and 4 deletions

View File

@ -987,9 +987,10 @@ class DB:
return self.fts.dirty_book(book_id, *fmts)
def fts_search(self,
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text,
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text, process_each_result
):
yield from self.fts.search(fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text,)
yield from self.fts.search(
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids, return_text, process_each_result)
def shutdown_fts(self):
if self.fts_enabled:

View File

@ -633,6 +633,7 @@ class Cache:
restrict_to_book_ids=None,
return_text=True,
result_type=tuple,
process_each_result=None,
):
return result_type(self.backend.fts_search(
fts_engine_query,
@ -642,6 +643,7 @@ class Cache:
snippet_size=snippet_size,
return_text=return_text,
restrict_to_book_ids=restrict_to_book_ids,
process_each_result=process_each_result,
))
# }}}

View File

@ -150,7 +150,7 @@ class FTS:
def search(self,
fts_engine_query, use_stemming, highlight_start, highlight_end, snippet_size, restrict_to_book_ids,
return_text=True,
return_text=True, process_each_result=None
):
if restrict_to_book_ids is not None and not restrict_to_book_ids:
return
@ -184,12 +184,15 @@ class FTS:
query += f'; DROP TABLE temp.{temp_table_name}'
try:
for record in conn.execute(query, tuple(data)):
ret = yield {
result = {
'id': record[0],
'book_id': record[1],
'format': record[2],
'text': record[3] if return_text else '',
}
if process_each_result is not None:
result = process_each_result(result)
ret = yield result
if ret is True:
break
except apsw.SQLError as e: