improve LogSearchIndex error handling, add explicit close() method, and modify get_index to not keep the index open persistently
--- a/log_search.py Sat Feb 14 19:23:25 2009 +0200
+++ b/log_search.py Sat Feb 14 20:09:08 2009 +0200
@@ -16,6 +16,18 @@
pass
+class SearchIndexError (LogSearchError) :
+ """
+ Error manipulating the index
+ """
+
+ def __init__ (self, msg, db) :
+ """
+ Build the error from the given message + HyperEstraier.Database
+ """
+
+ super(SearchIndexError, self).__init__("%s: %s" % (msg, db.err_msg(db.error())))
+
class NoResultsFound (LogSearchError) :
"""
No results found
@@ -98,7 +110,15 @@
# open
if not self.db.open(path, flags) :
- raise Exception("Index open failed: %s, mode=%s, flags=%#06x: %s" % (path, mode, flags, self.db.err_msg(self.db.error())))
+ raise SearchIndexError("Index open failed: %s, mode=%s, flags=%#06x" % (path, mode, flags), self.db)
+
+ def close (self) :
+ """
+ Explicitly close the index, this is done automatically on del
+ """
+
+ if not self.db.close() :
+ raise SearchIndexError("Index close failed", self.db)
def insert (self, channel, lines) :
"""
@@ -119,8 +139,6 @@
# return
return count
-
-
def insert_line (self, channel, line) :
"""
Adds a single LogLine for the given LogChannel to the index
@@ -183,7 +201,7 @@
# put, "clean up dispensable regions of the overwritten document"
if not self.db.put_doc(doc, hype.Database.PDCLEAN) :
- raise Exeception("Index put_doc failed")
+ raise SearchIndexError("put_doc", self.db)
def search_cond (self, cond) :
"""
@@ -336,19 +354,13 @@
skip = skip
)
-# global read-only index
-_index = None
-
def get_index () :
"""
Returns the default read-only index, suitable for searching
"""
- global _index
-
- # open?
- if not _index :
- _index = LogSearchIndex(config.LOG_CHANNELS, config.SEARCH_INDEX_PATH, 'r')
+ # XXX: no caching, just open it every time
+ _index = LogSearchIndex(config.LOG_CHANNELS, config.SEARCH_INDEX_PATH, 'r')
# return
return _index