Skip to content

Commit

Permalink
Merge pull request #8 from asedeno/segment-reader-cached-searcher
Browse files Browse the repository at this point in the history
[RFC] Give SegmentWriter a cachable Searcher
  • Loading branch information
ZeroCool940711 authored Jan 3, 2024
2 parents 362ea02 + 5870705 commit 6411df2
Showing 1 changed file with 19 additions and 0 deletions.
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

0 comments on commit 6411df2

Please sign in to comment.