Rev 31: Add post-branch-tip change hook to auto-update indices. in http://people.ubuntu.com/~robertc/baz2.0/plugins/search/trunk

Robert Collins robertc at robertcollins.net
Fri Jun 13 07:15:38 BST 2008


At http://people.ubuntu.com/~robertc/baz2.0/plugins/search/trunk

------------------------------------------------------------
revno: 31
revision-id: robertc at robertcollins.net-20080613061537-uz0no2h1jc9lkku3
parent: robertc at robertcollins.net-20080613043718-65uux3tngc7dyvrp
committer: Robert Collins <robertc at robertcollins.net>
branch nick: trunk
timestamp: Fri 2008-06-13 16:15:37 +1000
message:
  Add post-branch-tip change hook to auto-update indices.
modified:
  __init__.py                    __init__.py-20080608052041-z5bahsl8kwl0uf4x-4
  index.py                       index.py-20080608055509-hnimeek7q8tctkqf-2
  tests/test_index.py            test_index.py-20080608055509-hnimeek7q8tctkqf-4
=== modified file '__init__.py'
--- a/__init__.py	2008-06-11 05:16:32 +0000
+++ b/__init__.py	2008-06-13 06:15:37 +0000
@@ -33,7 +33,9 @@
 import bzrlib.commands
 
 # Relative because at __init__ time the module does not exist.
+from bzrlib.branch import Branch
 import commands
+import errors
 
 
 for command in [
@@ -46,6 +48,24 @@
 version_info = (1, 6, 0, 'dev', 0)
 
 
+def auto_index_branch(result):
+    """Handled for the post_change_branch_tip hook to update a search index."""
+    try:
+        search_index = index.open_index_branch(result.branch)
+    except errors.NoSearchIndex:
+        return
+    search_index.index_branch(result.branch, result.new_revid)
+
+
+def _install_hooks():
+    """Install the hooks this plugin uses."""
+    Branch.hooks.install_named_hook('post_change_branch_tip',
+        auto_index_branch, "index")
+
+
+_install_hooks()
+
+
 def test_suite():
     # Thunk across to load_tests for niceness with older bzr versions
     from bzrlib.tests import TestLoader

=== modified file 'index.py'
--- a/index.py	2008-06-13 04:37:18 +0000
+++ b/index.py	2008-06-13 06:15:37 +0000
@@ -75,40 +75,27 @@
     :param url: The url to index.
     :return: The resulting search index.
     """
-    revs_to_index = set()
     branch = _mod_branch.Branch.open(url)
     branch.lock_read()
     try:
         _last_revid = branch.last_revision()
-        graph =  branch.repository.get_graph()
-        searcher = graph._make_breadth_first_searcher([_last_revid])
         try:
             index = open_index_url(url)
-            index._refresh_indices()
-            revision_index = index._revision_index
-            while True:
-                try:
-                    next_revs, ghosts = searcher.next_with_ghosts()
-                except StopIteration:
-                    break
-                else:
-                    rev_keys = [(rev,) for rev in next_revs]
-                    indexed_revs = set([node[1][0] for node in
-                        revision_index.iter_entries(rev_keys)])
-                    unindexed_revs = next_revs - indexed_revs
-                    searcher.stop_searching_any(indexed_revs)
-                revs_to_index.update(unindexed_revs)
+            index.index_branch(branch, _last_revid)
         except errors.NoSearchIndex:
             index = init_index(branch)
+            graph =  branch.repository.get_graph()
+            searcher = graph._make_breadth_first_searcher([_last_revid])
+            revs_to_index = set()
             while True:
                 try:
                     next_revs, ghosts = searcher.next_with_ghosts()
                 except StopIteration:
                     break
                 revs_to_index.update(next_revs)
-        if NULL_REVISION in revs_to_index:
-            revs_to_index.remove(NULL_REVISION)
-        index.index_revisions(branch, revs_to_index)
+            if NULL_REVISION in revs_to_index:
+                revs_to_index.remove(NULL_REVISION)
+            index.index_revisions(branch, revs_to_index)
     finally:
         branch.unlock()
     return index
@@ -125,6 +112,16 @@
         branch = _mod_branch.Branch.open(url)
     except NotBranchError:
         raise errors.NoSearchIndex(url)
+    return open_index_branch(branch)
+
+
+def open_index_branch(branch):
+    """Open a search index at a branch.
+
+    This could do look-aside stuff for svn branches etc in the future.
+    :param branch: The branch to get an index for.
+    :raises: NoSearchIndex if no index can be located.
+    """
     return Index(branch.bzrdir.transport.clone('bzr-search'), branch)
 
 
@@ -203,6 +200,37 @@
             for node in doc_index.iter_entries(doc_ids):
                 yield tuple(node[2].split(' '))
 
+    def index_branch(self, branch, tip_revision):
+        """Index revisions from a branch.
+
+        :param branch: The branch to index.
+        :param tip_revision: The tip of the branch.
+        """
+        branch.lock_read()
+        try:
+            graph =  branch.repository.get_graph()
+            searcher = graph._make_breadth_first_searcher([tip_revision])
+            self._refresh_indices()
+            revision_index = self._revision_index
+            revs_to_index = set()
+            while True:
+                try:
+                    next_revs, ghosts = searcher.next_with_ghosts()
+                except StopIteration:
+                    break
+                else:
+                    rev_keys = [(rev,) for rev in next_revs]
+                    indexed_revs = set([node[1][0] for node in
+                        revision_index.iter_entries(rev_keys)])
+                    unindexed_revs = next_revs - indexed_revs
+                    searcher.stop_searching_any(indexed_revs)
+                revs_to_index.update(unindexed_revs)
+            if NULL_REVISION in revs_to_index:
+                revs_to_index.remove(NULL_REVISION)
+            self.index_revisions(branch, revs_to_index)
+        finally:
+            branch.unlock()
+
     def index_revisions(self, branch, revisions_to_index):
         """Index some revisions from branch.
         

=== modified file 'tests/test_index.py'
--- a/tests/test_index.py	2008-06-13 04:12:35 +0000
+++ b/tests/test_index.py	2008-06-13 06:15:37 +0000
@@ -19,6 +19,7 @@
 
 from bzrlib.errors import NotBranchError, UnknownFormatError
 from bzrlib.index import GraphIndex
+from bzrlib.plugins import search
 from bzrlib.plugins.search import errors, index
 from bzrlib.tests import TestCaseWithTransport
 
@@ -332,3 +333,19 @@
         self.assertEqual(terms, combined.all_terms())
         self.assertEqual(set([('rev1',), ('rev2',), ('rev-common',)]),
             set(combined.indexed_revisions()))
+
+
+class TestAutoIndex(TestCaseWithTransport):
+
+    def test_no_index_no_error(self):
+        tree = self.make_branch_and_tree("foo")
+        search._install_hooks()
+        tree.commit('foo')
+
+    def test_index_is_populated(self):
+        search._install_hooks()
+        tree = self.make_branch_and_tree("foo")
+        search_index = index.init_index(tree.branch)
+        revid1 = tree.commit('foo')
+        self.assertEqual(set([(revid1,)]),
+            set(search_index.indexed_revisions()))




More information about the bazaar-commits mailing list