Rev 11: Finish hooking up the UI and search layers. Searching now works. in http://people.ubuntu.com/~robertc/baz2.0/plugins/search/trunk

Robert Collins robertc at robertcollins.net
Mon Jun 9 00:42:18 BST 2008


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

------------------------------------------------------------
revno: 11
revision-id: robertc at robertcollins.net-20080608234217-e3v0a9e8kak9jas7
parent: robertc at robertcollins.net-20080608221948-66c28r366alynula
committer: Robert Collins <robertc at robertcollins.net>
branch nick: trunk
timestamp: Mon 2008-06-09 09:42:17 +1000
message:
  Finish hooking up the UI and search layers. Searching now works.
modified:
  commands.py                    commands.py-20080608052041-z5bahsl8kwl0uf4x-5
  index.py                       index.py-20080608055509-hnimeek7q8tctkqf-2
  tests/test_blackbox.py         test_blackbox.py-20080608052041-z5bahsl8kwl0uf4x-9
  tests/test_index.py            test_index.py-20080608055509-hnimeek7q8tctkqf-4
=== modified file 'commands.py'
--- a/commands.py	2008-06-08 22:19:48 +0000
+++ b/commands.py	2008-06-08 23:42:17 +0000
@@ -52,7 +52,9 @@
         trans = get_transport('.')
         index = _mod_index.open_index_url(trans.base)
         # XXX: Have a query translator etc.
+        seen_count = 0
         for result in index.search(query_list):
-            pass
-        else:
+            self.outf.write(result.document_name() + '\n')
+            seen_count += 1
+        if seen_count == 0:
             raise errors.NoMatch(query_list)

=== modified file 'index.py'
--- a/index.py	2008-06-08 22:19:48 +0000
+++ b/index.py	2008-06-08 23:42:17 +0000
@@ -262,7 +262,20 @@
         :return: An iterator of SearchResults for documents indexed by all
             terms in the termlist.
         """
-        return []
+        # Slow but gets the UI up
+        terms = dict(self.all_terms())
+        candidate_documents = None
+        for term in termlist:
+            if term not in terms:
+                return
+            if candidate_documents is None:
+                candidate_documents = set(terms[term])
+            else:
+                candidate_documents.intersection_update(set(terms[term]))
+        for document in candidate_documents:
+            if document[0] == 'r':
+                # revision
+                yield RevisionHit(document[2:3])
 
     def _terms_for_revs(self, repository, revision_ids):
         """Generate the posting list for the revision texts of revision_ids.
@@ -271,6 +284,7 @@
         :return: An iterable of (term, posting_list) for the revision texts
             (not the inventories or user texts) of revision_ids.
         """
+        self._refresh_indices()
         terms = {}
         for revision in repository.get_revisions(revision_ids):
             # its a revision, second component is ignored, third is id.
@@ -290,3 +304,19 @@
                 posting_list = terms.setdefault(term,set())
                 posting_list.add(document_key)
         return terms.iteritems()
+
+
+class RevisionHit(object):
+    """A match found during a search in a revision object."""
+
+    def __init__(self, revision_key):
+        """Create a RevisionHit.
+
+        :param revision_key: The revision_key that was hit.
+        """
+        self.revision_key = revision_key
+
+    def document_name(self):
+        """The name of the document found, for human consumption."""
+        # Perhaps need to utf_decode this?
+        return "Revision id '%s'." % self.revision_key[0]

=== modified file 'tests/test_blackbox.py'
--- a/tests/test_blackbox.py	2008-06-08 22:19:48 +0000
+++ b/tests/test_blackbox.py	2008-06-08 23:42:17 +0000
@@ -17,7 +17,11 @@
 
 """Tests for the commands supplied by search."""
 
-from bzrlib.plugins.search.index import init_index, open_index_url
+from bzrlib.plugins.search.index import (
+    index_url,
+    init_index,
+    open_index_url,
+    )
 from bzrlib.tests import TestCaseWithTransport
 
 
@@ -34,6 +38,16 @@
         init_index(branch)
         self.run_bzr_error(['No matches'], ['search', 'robert'])
 
+    def test_simple_hits(self):
+        tree = self.make_branch_and_tree('.')
+        init_index(tree.branch)
+        rev_id1 = tree.commit('first post')
+        index_url(self.get_url('.'))
+        index = open_index_url(self.get_url('.'))
+        out, err = self.run_bzr(['search', 'post'])
+        self.assertEqual('', err)
+        self.assertEqual("Revision id '%s'.\n" % rev_id1, out)
+
 
 class TestIndex(TestCaseWithTransport):
 

=== modified file 'tests/test_index.py'
--- a/tests/test_index.py	2008-06-08 22:19:48 +0000
+++ b/tests/test_index.py	2008-06-08 23:42:17 +0000
@@ -115,4 +115,26 @@
     def test_search_no_hits(self):
         tree = self.make_branch_and_tree('')
         rev_index = index.init_index(tree.branch)
+        # No exception because its a generator (and thus not guaranteed to run
+        # to completion).
         self.assertEqual([], list(rev_index.search(['missing_term'])))
+
+    def test_search_trivial(self):
+        tree = self.make_branch_and_tree('tree')
+        rev_index = index.init_index(tree.branch)
+        # The double-space is a cheap smoke test for the tokeniser.
+        revid = tree.commit('first  post')
+        rev_index.index_revisions(tree.branch, [revid])
+        results = list(rev_index.search(['post']))
+        self.assertEqual(1, len(results))
+        self.assertIsInstance(results[0], index.RevisionHit)
+        self.assertEqual((revid,), results[0].revision_key)
+
+
+class TestResults(TestCaseWithTransport):
+
+    def test_RevisionHit(self):
+        result = index.RevisionHit(('foo',))
+        self.assertEqualDiff(u"Revision id 'foo'.",
+            result.document_name())
+        self.assertEqual(('foo',), result.revision_key)




More information about the bazaar-commits mailing list