changeset 765:6c07d901c6b4

Fixing removing indexes from test dir, some changes in WhooshIndex API Fixes #34.
author Michael Mayorov <marchael@kb.csu.ru>
date Mon, 22 Aug 2011 02:43:28 +0000
parents 3a042c4b99ee
children 788eb43bf3b7
files MoinMoin/search/indexing.py MoinMoin/storage/_tests/test_indexing.py MoinMoin/storage/backends/indexing.py
diffstat 3 files changed, 26 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/indexing.py	Sun Aug 21 23:58:28 2011 +0000
+++ b/MoinMoin/search/indexing.py	Mon Aug 22 02:43:28 2011 +0000
@@ -52,10 +52,11 @@
                 'all_revisions_index': 'all_revisions_schema',
                }
 
-    def __init__(self, index_dir=None, cfg=None):
+    def __init__(self, index_dir=None, cfg=None, force_create=False):
         """
         Create and open indexes in index_dir
 
+        :param force_create: Create empty index in index_dir even if index exists
         :param index_dir: Directory where whoosh indexes will be created, default None
         :param cfg: Application config (app.cfg), default None
         """
@@ -101,19 +102,26 @@
             self.all_revisions_schema.add(glob, field_type, glob=True)
 
         for index_name, index_schema in self._indexes.items():
-            self.open_index(index_name, index_schema, create=True, index_dir=self._index_dir)
+            self.open_index(index_name, index_schema, create=True, force_create=force_create,
+                            index_dir=self._index_dir
+                           )
 
-    def open_index(self, indexname, schema, create=False, index_dir=None):
+    def open_index(self, indexname, schema, create=False, force_create=False, index_dir=None):
         """
         Open index <indexname> in <index_dir>. if opening fails and <create>
         is True, try creating the index and retry opening it afterwards.
         return index object.
 
-        :param index_dir: Directory where whoosh indexes will be created
+
         :param indexname: Name of created index
         :param schema: which schema applies
+        :param create: create index if index doesn't exist
+        :param force_create: force create new empty index in index_dir
+        :param index_dir: Directory where whoosh indexes will be created
         """
         index_dir = index_dir or self._cfg.index_dir
+        if force_create:
+            self.create_index(index_dir, indexname, schema)
         try:
             index = open_dir(index_dir, indexname=indexname)
             setattr(self, indexname, index)
@@ -148,3 +156,9 @@
         except (IOError, OSError) as err:
             logging.error(u"%s [while trying to create index '%s' in '%s']" % (str(err), indexname, index_dir))
 
+    def remove_index(self):
+        """
+        Create empty index in index_dir and removing old
+        """
+        for index_name, index_schema in self._indexes.items():
+            self.create_index(indexname=index_name, schema=index_schema, index_dir=self._index_dir)
--- a/MoinMoin/storage/_tests/test_indexing.py	Sun Aug 21 23:58:28 2011 +0000
+++ b/MoinMoin/storage/_tests/test_indexing.py	Mon Aug 22 02:43:28 2011 +0000
@@ -5,7 +5,7 @@
     MoinMoin - Test - indexing
 """
 
-import py, shutil
+import py
 
 from MoinMoin._tests import update_item, nuke_item
 from MoinMoin._tests.wikiconfig import Config
@@ -48,13 +48,12 @@
 
     def setup_method(self, method):
         self.wikiconfig = Config()
-        shutil.rmtree(self.wikiconfig.index_dir)
-        self.item_index = ItemIndex(self.wikiconfig)
+        self.item_index = ItemIndex(self.wikiconfig, force_create=True)
         self.all_revs_ix = self.item_index.index_object.all_revisions_index
         self.latest_revs_ix = self.item_index.index_object.latest_revisions_index
 
     def teardown_method(self, method):
-         shutil.rmtree(self.wikiconfig.index_dir)
+         self.item_index.remove_index()
 
     def test_create_item(self):
         """ Try to search for non-existent revision, add it to backend and then search again """
--- a/MoinMoin/storage/backends/indexing.py	Sun Aug 21 23:58:28 2011 +0000
+++ b/MoinMoin/storage/backends/indexing.py	Mon Aug 22 02:43:28 2011 +0000
@@ -201,14 +201,17 @@
     """
     Index for Items/Revisions
     """
-    def __init__(self, cfg):
+    def __init__(self, cfg, force_create=False):
         self.wikiname = cfg.interwikiname
-        self.index_object = WhooshIndex(cfg=cfg)
+        self.index_object = WhooshIndex(force_create=force_create, cfg=cfg)
 
     def close(self):
         self.index_object.all_revisions_index.close()
         self.index_object.latest_revisions_index.close()
 
+    def remove_index(self):
+        self.index_object.remove_index()
+
     def update_item(self, metas):
         """
         update item (not revision!) metadata