Author: andrej
Date: Wed Mar 6 16:10:42 2013
New Revision: 1453398
URL: http://svn.apache.org/r1453398
Log:
adding IEnvironmentSetupParticipant support to bloodhound search - towards #448
(from astaric)
Modified:
incubator/bloodhound/trunk/bloodhound_search/bhsearch/api.py
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/api.py
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py
incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py
Modified: incubator/bloodhound/trunk/bloodhound_search/bhsearch/api.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/trunk/bloodhound_search/bhsearch/api.py?rev=1453398&r1=1453397&r2=1453398&view=diff
==============================================================================
--- incubator/bloodhound/trunk/bloodhound_search/bhsearch/api.py (original)
+++ incubator/bloodhound/trunk/bloodhound_search/bhsearch/api.py Wed Mar 6
16:10:42 2013
@@ -20,7 +20,9 @@
r"""Core Bloodhound Search components."""
from trac.config import ExtensionOption
-from trac.core import Interface, Component, ExtensionPoint, TracError
+from trac.core import (Interface, Component, ExtensionPoint, TracError,
+ implements)
+from trac.env import IEnvironmentSetupParticipant
ASC = "asc"
DESC = "desc"
@@ -103,6 +105,11 @@ class ISearchBackend(Interface):
Optimize index if needed
"""
+ def is_index_outdated():
+ """
+ Check if index is outdated and needs to be recreated.
+ """
+
def recreate_index():
"""
Create a new index, if index exists, it will be deleted
@@ -220,6 +227,8 @@ class BloodhoundSearchApi(Component):
"""Implements core indexing functionality, provides methods for
searching, adding and deleting documents from index.
"""
+ implements(IEnvironmentSetupParticipant)
+
backend = ExtensionOption('bhsearch', 'search_backend',
ISearchBackend, 'WhooshBackend',
'Name of the component implementing Bloodhound Search backend \
@@ -352,5 +361,13 @@ class BloodhoundSearchApi(Component):
"""
self.backend.delete_doc(doc_type, doc_id)
+ # IEnvironmentSetupParticipant methods
+
+ def environment_created(self):
+ self.upgrade_environment(self.env.db_transaction)
+ def environment_needs_upgrade(self, db):
+ return self.backend.is_index_outdated()
+ def upgrade_environment(self, db):
+ self.rebuild_index()
Modified: incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/api.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/api.py?rev=1453398&r1=1453397&r2=1453398&view=diff
==============================================================================
--- incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/api.py
(original)
+++ incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/api.py Wed Mar
6 16:10:42 2013
@@ -119,6 +119,16 @@ class ApiQueryWithWhooshTestCase(BaseBlo
self.assertEqual("t1", docs[0]["summary"])
self.assertEqual("t2", docs[1]["summary"])
+ def test_that_upgrading_environment_adds_documents_to_index(self):
+ self.insert_ticket("t1")
+ self.insert_ticket("t2")
+
+ self.search_api.upgrade_environment(self.env.db_transaction)
+
+ results = self.search_api.query("type:ticket")
+
+ self.assertEqual(2, results.hits)
+
#TODO: check this later
# @unittest.skip("Check with Whoosh community")
Modified:
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py?rev=1453398&r1=1453397&r2=1453398&view=diff
==============================================================================
---
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py
(original)
+++
incubator/bloodhound/trunk/bloodhound_search/bhsearch/tests/whoosh_backend.py
Wed Mar 6 16:10:42 2013
@@ -416,6 +416,20 @@ class WhooshBackendTestCase(BaseBloodhou
def _highlighted(self, term):
return '<em>%s</em>' % term
+ def test_detects_that_index_needs_upgrade(self):
+ index_dir = self.whoosh_backend.index.storage.folder
+ wrong_schema = Schema(content=TEXT())
+ ix = index.create_in(index_dir, schema=wrong_schema)
+
+ self.assertEqual(self.whoosh_backend.is_index_outdated(), False)
+
+ # Inform WhooshBackend about the new index
+ self.whoosh_backend.index = ix
+ self.assertEqual(self.whoosh_backend.is_index_outdated(), True)
+ # Recreate index
+ self.whoosh_backend.recreate_index()
+ self.assertEqual(self.whoosh_backend.is_index_outdated(), False)
+
class WhooshFunctionalityTestCase(unittest.TestCase):
def setUp(self):
Modified:
incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py
URL:
http://svn.apache.org/viewvc/incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py?rev=1453398&r1=1453397&r2=1453398&view=diff
==============================================================================
--- incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py
(original)
+++ incubator/bloodhound/trunk/bloodhound_search/bhsearch/whoosh_backend.py Wed
Mar 6 16:10:42 2013
@@ -151,10 +151,14 @@ class WhooshBackend(Component):
writer = AsyncWriter(self.index)
writer.commit(optimize=True)
+ def is_index_outdated(self):
+ return not self.index.schema == self.SCHEMA
+
def recreate_index(self):
self.log.info('Creating Whoosh index in %s' % self.index_dir)
self._make_dir_if_not_exists()
- return index.create_in(self.index_dir, schema=self.SCHEMA)
+ self.index = index.create_in(self.index_dir, schema=self.SCHEMA)
+ return self.index
def _open_or_create_index_if_missing(self):
if index.exists_in(self.index_dir):