allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [11/18] git commit: [#7257] ticket:562 Fixed docstrings & refactored index tasks
Date Fri, 09 May 2014 21:42:31 GMT
[#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/af67f559
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/af67f559
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/af67f559

Branch: refs/heads/db/7257
Commit: af67f55932ca515dfd9c88ff64643fa6f59a92b8
Parents: c891b2b
Author: Ferens Dmitriy <ferensdima@gmail.com>
Authored: Mon Apr 7 14:39:34 2014 +0300
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Fri May 9 18:56:47 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/af67f559/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/af67f559/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/af67f559/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


Mime
View raw message