# HG changeset patch # User Paul Boddie # Date 1253389375 -7200 # Node ID eff15120152bff94b1d13c03d7a55124d0753206 # Parent 495d396e86c55a0a33ae9735a18b7d017c85e448 Added support for updating empty indexes. Added a "clean" option to the test program which just deletes the test results. diff -r 495d396e86c5 -r eff15120152b iixr/index.py --- a/iixr/index.py Sat Sep 19 21:36:32 2009 +0200 +++ b/iixr/index.py Sat Sep 19 21:42:55 2009 +0200 @@ -209,12 +209,14 @@ 'doc_interval' and 'flush_interval'. """ + self._ensure_directory() + self.writer = IndexWriter(self.pathname, interval, doc_interval, flush_interval) + return self.writer + + def _ensure_directory(self): if not exists(self.pathname): mkdir(self.pathname) - self.writer = IndexWriter(self.pathname, interval, doc_interval, flush_interval) - return self.writer - def get_reader(self, partition=0): "Return a reader for the index." @@ -341,6 +343,8 @@ "Copy the content of the 'other_indexes' into this index and merge." + self._ensure_directory() + for i, index in enumerate(other_indexes): for partition in index.get_term_partitions(): copy_term_files(index.pathname, partition, self.pathname, "-added-%d" % i) diff -r 495d396e86c5 -r eff15120152b test.py --- a/test.py Sat Sep 19 21:36:32 2009 +0200 +++ b/test.py Sat Sep 19 21:42:55 2009 +0200 @@ -5,23 +5,27 @@ from iixr.terms import * from iixr.positions import * from iixr.index import * -import os +import os, sys # Remove old test files. -for filename in ("test", "testF", "testFI", "testI", "testP"): +for filename in ("test", "testF", "testFI", "testI", "testP", "testPI"): try: os.remove(filename) except OSError: pass try: - for filename in os.listdir("test_index"): - os.remove(os.path.join("test_index", filename)) - os.rmdir("test_index") + for dirname in ("test_index", "test_index2", "test_index3"): + for filename in os.listdir(dirname): + os.remove(os.path.join(dirname, filename)) + os.rmdir(dirname) except OSError: pass +if "clean" in sys.argv: + sys.exit(0) + # Test basic data types. numbers = [12345678, 0, 1, 127, 128, 255, 256] @@ -439,7 +443,6 @@ # Test index updates. index = Index("test_index") - index2 = Index("test_index2") wi = index2.get_writer(3, 2, 6) for docnum, text in docs: @@ -461,7 +464,10 @@ # Add the extra documents to the expected result. - for docnum, positions in doc_positions[:]: + orig_doc_positions = doc_positions + doc_positions = doc_positions[:] + + for docnum, positions in orig_doc_positions: doc_positions.append((docnum + 100, positions)) frequency *= 2 @@ -471,4 +477,19 @@ print frequency == fr, frequency, fr index2.close() +# (Test update of an empty index.) + +index = Index("test_index") +index3 = Index("test_index3") +index3.update([index]) +index.close() + +rd = index3.get_reader() +for term, frequency, doc_positions in doc_tests: + dp = list(rd.find_positions(term)) + print doc_positions == dp, doc_positions, dp + fr = rd.get_frequency(term) + print frequency == fr, frequency, fr +index3.close() + # vim: tabstop=4 expandtab shiftwidth=4