Commits

Thomas Waldmann  committed 082581e

indexing: move creation of the whoosh storage object to get_storage method

this way, we can more easily support other whoosh storage backends, currently
only FileStorage is supported.

added some notes about backend-specific methods of the indexing middleware.

removed some exception handler that might be too specific for everything else
than the FileStorage backend.

  • Participants
  • Parent commits d56cd19

Comments (0)

Files changed (1)

File MoinMoin/storage/middleware/indexing.py

 from whoosh.qparser import WordNode
 from whoosh.query import Every, Term
 from whoosh.sorting import FieldFacet
-from whoosh.filedb.filestore import FileStorage
 
 from MoinMoin import log
 logging = log.getLogger(__name__)
         # or latest revs index):
         self.common_fields = set(latest_revs_fields.keys()) & set(all_revs_fields.keys())
 
+    def get_storage(self, tmp=False, create=False):
+        """
+        Get the whoosh storage (whoosh supports different kinds of storage,
+        e.g. to filesystem or to GAE).
+        Currently we only support the FileStorage.
+        """
+        from whoosh.filedb.filestore import FileStorage
+        index_dir = self.index_dir_tmp if tmp else self.index_dir
+        if create:
+            try:
+                os.mkdir(index_dir)
+            except:
+                # ignore exception, we'll get another exception below
+                # in case there are problems with the index_dir
+                pass
+        storage = FileStorage(index_dir)
+        return storage
+
     def open(self):
         """
         Open all indexes.
         """
-        index_dir = self.index_dir
-        try:
-            storage = FileStorage(index_dir)
-            for name in INDEXES:
-                self.ix[name] = storage.open_index(name)
-        except (IOError, OSError, EmptyIndexError) as err:
-            logging.error(u"{0!s} [while trying to open index '{1}' in '{2}']".format(err, name, index_dir))
-            raise
+        storage = self.get_storage()
+        for name in INDEXES:
+            self.ix[name] = storage.open_index(name)
 
     def close(self):
         """
         """
         Create all indexes (empty).
         """
-        index_dir = self.index_dir_tmp if tmp else self.index_dir
-        try:
-            os.mkdir(index_dir)
-        except:
-            # ignore exception, we'll get another exception below
-            # in case there are problems with the index_dir
-            pass
-        try:
-            storage = FileStorage(index_dir)
-            for name in INDEXES:
-                storage.create_index(self.schemas[name], indexname=name)
-        except (IOError, OSError) as err:
-            logging.error(u"{0!s} [while trying to create index '{1}' in '{2}']".format(err, name, index_dir))
-            raise
+        storage = self.get_storage(tmp, create=True)
+        for name in INDEXES:
+            storage.create_index(self.schemas[name], indexname=name)
 
     def destroy(self, tmp=False):
         """
         Destroy all indexes.
         """
+        # XXX this is whoosh backend specific and currently only works for FileStorage.
         index_dir = self.index_dir_tmp if tmp else self.index_dir
         if os.path.exists(index_dir):
             shutil.rmtree(index_dir)
         """
         Move freshly built indexes from index_dir_tmp to index_dir.
         """
+        # XXX this is whoosh backend specific and currently only works for FileStorage.
         self.destroy()
         os.rename(self.index_dir_tmp, self.index_dir)
 
               create, rebuild wiki1, rebuild wiki2, ...
               create (tmp), rebuild wiki1, rebuild wiki2, ..., move
         """
-        index_dir = self.index_dir_tmp if tmp else self.index_dir
-        storage = FileStorage(index_dir)
+        storage = self.get_storage(tmp)
         index = storage.open_index(ALL_REVS)
         try:
             # build an index of all we have (so we know what we have)
 
         :returns: index changed (bool)
         """
-        index_dir = self.index_dir_tmp if tmp else self.index_dir
-        storage = FileStorage(index_dir)
+        storage = self.get_storage(tmp)
         index_all = storage.open_index(ALL_REVS)
         try:
             # NOTE: self.backend iterator gives (mountpoint, revid) tuples, which is NOT
         """
         Optimize whoosh index.
         """
-        index_dir = self.index_dir_tmp if tmp else self.index_dir
-        storage = FileStorage(index_dir)
+        storage = self.get_storage(tmp)
         for name in INDEXES:
             ix = storage.open_index(name)
             try:
         """
         Yield key/value tuple lists for all documents in the indexes, fields sorted.
         """
-        index_dir = self.index_dir_tmp if tmp else self.index_dir
-        storage = FileStorage(index_dir)
+        storage = self.get_storage(tmp)
         ix = storage.open_index(idx_name)
         try:
             with ix.searcher() as searcher: