Rev 5081: Expose an api for getting a specifically named index out again. in http://bzr.arbash-meinel.com/branches/bzr/lp/2.2.0b2-contained-pack
John Arbash Meinel
john at arbash-meinel.com
Thu Mar 4 23:04:47 GMT 2010
At http://bzr.arbash-meinel.com/branches/bzr/lp/2.2.0b2-contained-pack
------------------------------------------------------------
revno: 5081
revision-id: john at arbash-meinel.com-20100304230418-xdt364o2rr5qitra
parent: john at arbash-meinel.com-20100304225223-hkgegogaj2vusvol
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.2.0b2-contained-pack
timestamp: Thu 2010-03-04 17:04:18 -0600
message:
Expose an api for getting a specifically named index out again.
-------------- next part --------------
=== modified file 'bzrlib/sack.py'
--- a/bzrlib/sack.py 2010-03-04 22:52:23 +0000
+++ b/bzrlib/sack.py 2010-03-04 23:04:18 +0000
@@ -129,11 +129,22 @@
# This is a bootstrap method, if all you have is the sack file, read
# its tail and figure out where the internal indices are.
file_st = transport.stat(filename)
- _,tail = transport.readv(filename, [(file_st.st_size-12, 12)]).next()
+ _, tail = transport.readv(filename, [(file_st.st_size-12, 12)]).next()
ti = TrailingIndex.parse_tail_bytes(tail)
ti._read_named_sections(transport, filename, file_st.st_size)
return ti
+ def get_named_index(self, name, index_class, **kwargs):
+ """Create an Index using the content at the named location.
+
+ :param name: The name of the given section
+ :param index_class: BTreeGraphIndex or GraphIndex, etc
+ :param **kwargs: Any other named arguments will be passed to the index
+ constructor
+ """
+ start, end = self._section_file_map[name]
+ return index_class(self._file_view, name, size=(end-start), **kwargs)
+
class Sack(object):
"""A self-contained pack file.
=== modified file 'bzrlib/tests/test_sack.py'
--- a/bzrlib/tests/test_sack.py 2010-03-04 22:43:22 +0000
+++ b/bzrlib/tests/test_sack.py 2010-03-04 23:04:18 +0000
@@ -34,10 +34,7 @@
pass
-def assert_btree_matches(test, a_dict, bytes):
- t = memory.MemoryTransport('')
- t.put_bytes('index', bytes)
- index = btree_index.BTreeGraphIndex(t, 'index', len(bytes))
+def assert_index_content(test, a_dict, index):
as_dict = {}
for info in index.iter_all_entries():
key = info[1]
@@ -46,6 +43,13 @@
test.assertEqual(a_dict, as_dict)
+def assert_btree_matches(test, a_dict, bytes):
+ t = memory.MemoryTransport('')
+ t.put_bytes('index', bytes)
+ index = btree_index.BTreeGraphIndex(t, 'index', len(bytes))
+ assert_index_content(test, a_dict, index)
+
+
class TestTrailingIndexBuilder(tests.TestCase):
def assertAsBytes(self, index_content, ti):
@@ -108,3 +112,24 @@
self.assertEqual({'root-index': (516, 500+len(content)-12),
'texts': (150, 500),
}, ti._section_file_map)
+
+ def test_get_named_index(self):
+ index_builder = btree_index.BTreeBuilder(0, 1)
+ index_builder.add_node(('key1',), 'value1')
+ index_builder.add_node(('key2',), 'value2')
+ text_idx_content = index_builder.finish().read()
+ trail_start = len(text_idx_content)
+ trailing_builder = sack.TrailingIndexBuilder(
+ start_offset=trail_start)
+ trailing_builder.add_index_info('texts', 0, trail_start)
+ content = text_idx_content + trailing_builder.finish()
+ t = memory.MemoryTransport('')
+ t.put_bytes('test.sack', content)
+ ti = sack.TrailingIndex.from_transport(t, 'test.sack')
+ self.assertEqual({'root-index': (trail_start+16, len(content)-12),
+ 'texts': (0, trail_start),
+ }, ti._section_file_map)
+ text_index = ti.get_named_index('texts', btree_index.BTreeGraphIndex)
+ assert_index_content(self, {('key1',): ('value1',),
+ ('key2',): ('value2',),
+ }, text_index)
More information about the bazaar-commits
mailing list