You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2014/05/21 16:44:58 UTC

[10/20] git commit: [#7257] ticket:562 Fixed docstrings & refactored index tasks

[#7257] ticket:562 Fixed docstrings & refactored index tasks


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/b072ab4b
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/b072ab4b
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/b072ab4b

Branch: refs/heads/master
Commit: b072ab4b58e456ac109e041e61e7a00037beb15e
Parents: 4b6f9cd
Author: Ferens Dmitriy <fe...@gmail.com>
Authored: Mon Apr 7 14:39:34 2014 +0300
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Wed May 21 14:44:20 2014 +0000

----------------------------------------------------------------------
 Allura/allura/lib/search.py        |  4 ++--
 Allura/allura/model/session.py     | 13 +++++++---
 Allura/allura/tasks/index_tasks.py | 42 +++++++++++++--------------------
 3 files changed, 28 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/b072ab4b/Allura/allura/lib/search.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/search.py b/Allura/allura/lib/search.py
index 7751552..2a80113 100644
--- a/Allura/allura/lib/search.py
+++ b/Allura/allura/lib/search.py
@@ -42,7 +42,7 @@ class SearchIndexable(object):
 
     def index_id(self):
         """
-        Should return a globally unique artifact identifier.
+        Should return a globally unique object identifier.
 
         Used for SOLR ID, shortlinks, and possibly elsewhere.
         """
@@ -50,7 +50,7 @@ class SearchIndexable(object):
 
     def index(self):
         """
-        Return a :class:`dict` representation of this Artifact suitable for
+        Return a :class:`dict` representation of this object suitable for
         search indexing.
 
         Subclasses should implement this, providing a dictionary of solr_field => value.

http://git-wip-us.apache.org/repos/asf/allura/blob/b072ab4b/Allura/allura/model/session.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/session.py b/Allura/allura/model/session.py
index 2d60afa..9cec981 100644
--- a/Allura/allura/model/session.py
+++ b/Allura/allura/model/session.py
@@ -52,6 +52,11 @@ class ManagedSessionExtension(SessionExtension):
             elif st.status == st.deleted:
                 self.objects_deleted = [obj]
 
+    def after_flush(self, obj=None):
+        self.objects_added = []
+        self.objects_modified = []
+        self.objects_deleted = []
+
 
 class IndexerSessionExtension(ManagedSessionExtension):
 
@@ -71,12 +76,12 @@ class IndexerSessionExtension(ManagedSessionExtension):
         task = tasks.get(action)
         if task:
             if action == 'add':
-                args = ([o._id for o in obj_list],)
+                arg = [o._id for o in obj_list]
             else:
-                args = ([o.index_id() for o in obj_list],)
+                arg = [o.index_id() for o in obj_list]
 
             try:
-                task.post(*args)
+                task.post(arg)
             except:
                 log.error('Error calling %s', task.__name__)
 
@@ -93,6 +98,8 @@ class IndexerSessionExtension(ManagedSessionExtension):
                     tasks = self.TASKS.get(class_path, {})
                     self._index_action(tasks, obj_list, action)
 
+        super(IndexerSessionExtension, self).after_flush(obj)
+
 
 class ArtifactSessionExtension(ManagedSessionExtension):
 

http://git-wip-us.apache.org/repos/asf/allura/blob/b072ab4b/Allura/allura/tasks/index_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tasks/index_tasks.py b/Allura/allura/tasks/index_tasks.py
index badb59e..3daf4ec 100644
--- a/Allura/allura/tasks/index_tasks.py
+++ b/Allura/allura/tasks/index_tasks.py
@@ -29,38 +29,31 @@ from allura.lib.solr import make_solr_from_config
 log = logging.getLogger(__name__)
 
 
-class GenericIndexHandler(object):
+def __get_solr(solr_hosts=None):
+    return make_solr_from_config(solr_hosts) if solr_hosts else g.solr
 
-    _instance = None
 
-    def __new__(cls):
-        if not cls._instance:
-            cls._instance = super(GenericIndexHandler, cls).__new__(cls)
-        return cls._instance
+def __add_objects(objects, solr_hosts=None):
+    solr_instance = __get_solr(solr_hosts)
+    solr_instance.add([obj.solarize() for obj in objects])
 
-    def get_solr(self, solr_hosts=None):
-        return make_solr_from_config(solr_hosts) if solr_hosts else g.solr
 
-    def add_objects(self, objects, solr_hosts=None):
-        solr_instance = self.get_solr(solr_hosts)
-        solr_instance.add(obj.solarize() for obj in objects)
-
-    def del_objects(self, object_solr_ids):
-        solr_instance = self.get_solr()
-        solr_query = 'id:({0})'.format(' || '.join(object_solr_ids))
-        solr_instance.delete(q=solr_query)
+def __del_objects(object_solr_ids):
+    solr_instance = __get_solr()
+    solr_query = 'id:({0})'.format(' || '.join(object_solr_ids))
+    solr_instance.delete(q=solr_query)
 
 
 @task
 def add_projects(project_ids):
     from allura.model.project import Project
     projects = Project.query.find(dict(_id={'$in': project_ids})).all()
-    GenericIndexHandler().add_objects(projects)
+    __add_objects(projects)
 
 
 @task
 def del_projects(project_solr_ids):
-    GenericIndexHandler().del_objects(project_solr_ids)
+    __del_objects(project_solr_ids)
 
 
 @task
@@ -74,7 +67,6 @@ def add_artifacts(ref_ids, update_solr=True, update_refs=True, solr_hosts=None):
     from allura import model as M
     from allura.lib.search import find_shortlinks
 
-    solr = make_solr_from_config(solr_hosts) if solr_hosts else g.solr
     exceptions = []
     solr_updates = []
     with _indexing_disabled(M.session.artifact_orm_session._get()):
@@ -97,7 +89,7 @@ def add_artifacts(ref_ids, update_solr=True, update_refs=True, solr_hosts=None):
             except Exception:
                 log.error('Error indexing artifact %s', ref._id)
                 exceptions.append(sys.exc_info())
-        solr.add(solr_updates)
+        __get_solr(solr_hosts).add(solr_updates)
 
     if len(exceptions) == 1:
         raise exceptions[0][0], exceptions[0][1], exceptions[0][2]
@@ -108,12 +100,10 @@ def add_artifacts(ref_ids, update_solr=True, update_refs=True, solr_hosts=None):
 @task
 def del_artifacts(ref_ids):
     from allura import model as M
-    if not ref_ids:
-        return
-    solr_query = 'id:({0})'.format(' || '.join(ref_ids))
-    g.solr.delete(q=solr_query)
-    M.ArtifactReference.query.remove(dict(_id={'$in': ref_ids}))
-    M.Shortlink.query.remove(dict(ref_id={'$in': ref_ids}))
+    if ref_ids:
+        __del_objects(ref_ids)
+        M.ArtifactReference.query.remove(dict(_id={'$in': ref_ids}))
+        M.Shortlink.query.remove(dict(ref_id={'$in': ref_ids}))
 
 
 @task