From 7d840022e6bd987ea1827f91357b096d47933b7d Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Thu, 27 Oct 2011 14:18:37 +0200 Subject: [PATCH] reusable index. first attempt at rich search query --- apps/search/__init__.py | 2 +- apps/search/index.py | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/apps/search/__init__.py b/apps/search/__init__.py index b34facba1..44da555a7 100644 --- a/apps/search/__init__.py +++ b/apps/search/__init__.py @@ -1,3 +1,3 @@ -from index import Index, Search, ReusableIndex import lucene lucene.initVM(lucene.CLASSPATH) +from index import Index, Search, ReusableIndex diff --git a/apps/search/index.py b/apps/search/index.py index 33f2aa1aa..d539257ee 100644 --- a/apps/search/index.py +++ b/apps/search/index.py @@ -14,6 +14,7 @@ import errno from librarian import dcparser from librarian.parser import WLDocument import catalogue.models +from multiprocessing.pool import ThreadPool import atexit @@ -247,24 +248,38 @@ class ReusableIndex(Index): if you cannot rely on atexit, use ReusableIndex.close_reusable() yourself. """ index = None - def open(self, analyzer=None): + pool = None + pool_jobs = None + + def open(self, analyzer=None, threads=4): if ReusableIndex.index is not None: self.index = ReusableIndex.index else: - Index.open(self,analyzer) + ReusableIndex.pool = ThreadPool(threads) + ReusableIndex.pool_jobs = [] + Index.open(self, analyzer) ReusableIndex.index = self.index atexit.register(ReusableIndex.close_reusable) + def index_book(self, *args, **kw): + job = ReusableIndex.pool.apply_async(Index.index_book, args, kw) + ReusableIndex.pool_jobs.append(job) + @staticmethod def close_reusable(): if ReusableIndex.index is not None: + for job in ReusableIndex.pool_jobs: + job.wait() + ReusableIndex.pool.close() + ReusableIndex.index.optimize() ReusableIndex.index.close() ReusableIndex.index = None - + def close(self): pass + class Search(IndexStore): def __init__(self, default_field="content"): IndexStore.__init__(self) -- 2.20.1