summaryrefslogtreecommitdiff
path: root/service/pixelated/adapter/search
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2015-01-14 17:01:27 +0100
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-01-14 17:01:27 +0100
commit8fbf5a50feae1281382fac6dca219b491adc8e76 (patch)
treea70a4f5be0a2684ddeb3d9926b0a1a4a0fdc7945 /service/pixelated/adapter/search
parent1aaf30be5f4a0b5db726010e52a7791826b58a48 (diff)
Added a lock to search index_mail.
- gets called from separate threads and causes exceptions otherwise
Diffstat (limited to 'service/pixelated/adapter/search')
-rw-r--r--service/pixelated/adapter/search/__init__.py29
1 files changed, 19 insertions, 10 deletions
diff --git a/service/pixelated/adapter/search/__init__.py b/service/pixelated/adapter/search/__init__.py
index a5eb4ef2..4425c7db 100644
--- a/service/pixelated/adapter/search/__init__.py
+++ b/service/pixelated/adapter/search/__init__.py
@@ -27,6 +27,8 @@ from whoosh.query import Term
from whoosh import sorting
from pixelated.support.functional import unique
from pixelated.support.date import milliseconds
+from threading import Lock
+import traceback
class SearchEngine(object):
@@ -38,6 +40,7 @@ class SearchEngine(object):
if not os.path.exists(self.INDEX_FOLDER):
os.makedirs(self.INDEX_FOLDER)
self._index = self._create_index()
+ self._write_lock = Lock()
def _add_to_tags(self, tags, group, skip_default_tags, count_type, query=None):
query_matcher = re.compile(".*%s.*" % query.lower()) if query else re.compile(".*")
@@ -137,11 +140,16 @@ class SearchEngine(object):
writer.update_document(**index_data)
def index_mails(self, mails, callback=None):
- with self._index.writer() as writer:
- for mail in mails:
- self._index_mail(writer, mail)
- if callback:
- callback()
+ try:
+ with self._write_lock:
+ with self._index.writer() as writer:
+ for mail in mails:
+ self._index_mail(writer, mail)
+ if callback:
+ callback()
+ except Exception, e:
+ traceback.print_exc(e)
+ raise
def _search_with_options(self, options, query):
with self._index.searcher() as searcher:
@@ -179,11 +187,12 @@ class SearchEngine(object):
return MultifieldParser(['raw', 'body'], self._index.schema).parse(query)
def remove_from_index(self, mail_id):
- writer = self._index.writer()
- try:
- writer.delete_by_term('ident', mail_id)
- finally:
- writer.commit()
+ with self._write_lock:
+ writer = self._index.writer()
+ try:
+ writer.delete_by_term('ident', mail_id)
+ finally:
+ writer.commit()
def contacts(self, query):
restrict_q = Term("tag", "drafts") | Term("tag", "trash")