Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Give SegmentWriter a cachable Searcher #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/whoosh/writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ def __init__(self, ix, poolclass=None, timeout=0.0, delay=0.1, _lk=True,
self.merge = True
self.optimize = False
self.mergetype = None
self._searcher = None

def __repr__(self):
# Author: Ronald Evers
Expand Down Expand Up @@ -809,6 +810,20 @@ def per_document_reader(self):
raise Exception("Per-doc writer is still open")
return self.codec.per_document_reader(self.storage, self.get_segment())

def searcher(self, **kwargs):
# If possible, cache a Searcher that doesn't close until we want it to.
# We have a write lock, nothing is changing. Only cache if kwargs is emtpy
# and the SegmentWriter is still open.
if kwargs or self.is_closed:
return super(SegmentWriter, self).searcher(**kwargs)

if self._searcher is None:
s = super(SegmentWriter, self).searcher()
self._searcher = s
s._orig_close = s.close # called in _finish()
s.close = lambda: None
return self._searcher

# The following methods break out the commit functionality into smaller
# pieces to allow MpWriter to call them individually

Expand Down Expand Up @@ -890,6 +905,10 @@ def _commit_toc(self, segments):
clean_files(self.storage, self.indexname, self.generation, segments)

def _finish(self):
if self._searcher is not None:
# Close the cached Searcher if we have one.
self._searcher._orig_close()
self._searcher = None
self._tempstorage.destroy()
if self.writelock:
self.writelock.release()
Expand Down