~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_btree_index.py

  • Committer: John Arbash Meinel
  • Date: 2009-10-20 19:46:46 UTC
  • mfrom: (4759 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4771.
  • Revision ID: john@arbash-meinel.com-20091020194646-wnqpd15qs19y28z7
Merge bzr.dev 4759, bringing in static_tuple and streaming improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib import (
24
24
    btree_index,
25
25
    errors,
 
26
    fifo_cache,
 
27
    lru_cache,
26
28
    osutils,
27
29
    tests,
28
30
    )
122
124
 
123
125
class TestBTreeBuilder(BTreeTestCase):
124
126
 
 
127
    def test_clear_cache(self):
 
128
        builder = btree_index.BTreeBuilder(reference_lists=0, key_elements=1)
 
129
        # This is a no-op, but we need the api to be consistent with other
 
130
        # BTreeGraphIndex apis.
 
131
        builder.clear_cache()
 
132
 
125
133
    def test_empty_1_0(self):
126
134
        builder = btree_index.BTreeBuilder(key_elements=1, reference_lists=0)
127
135
        # NamedTemporaryFile dies on builder.finish().read(). weird.
637
645
        size = trans.put_file('index', stream)
638
646
        return btree_index.BTreeGraphIndex(trans, 'index', size)
639
647
 
 
648
    def test_clear_cache(self):
 
649
        nodes = self.make_nodes(160, 2, 2)
 
650
        index = self.make_index(ref_lists=2, key_elements=2, nodes=nodes)
 
651
        self.assertEqual(1, len(list(index.iter_entries([nodes[30][0]]))))
 
652
        self.assertEqual([1, 4], index._row_lengths)
 
653
        self.assertIsNot(None, index._root_node)
 
654
        internal_node_pre_clear = index._internal_node_cache.keys()
 
655
        self.assertTrue(len(index._leaf_node_cache) > 0)
 
656
        index.clear_cache()
 
657
        # We don't touch _root_node or _internal_node_cache, both should be
 
658
        # small, and can save a round trip or two
 
659
        self.assertIsNot(None, index._root_node)
 
660
        # NOTE: We don't want to affect the _internal_node_cache, as we expect
 
661
        #       it will be small, and if we ever do touch this index again, it
 
662
        #       will save round-trips.  This assertion isn't very strong,
 
663
        #       becuase without a 3-level index, we don't have any internal
 
664
        #       nodes cached.
 
665
        self.assertEqual(internal_node_pre_clear,
 
666
                         index._internal_node_cache.keys())
 
667
        self.assertEqual(0, len(index._leaf_node_cache))
 
668
 
640
669
    def test_trivial_constructor(self):
641
670
        transport = get_transport('trace+' + self.get_url(''))
642
671
        index = btree_index.BTreeGraphIndex(transport, 'index', None)
1115
1144
        self.assertEqual({}, parent_map)
1116
1145
        self.assertEqual(set([('one',), ('two',)]), missing_keys)
1117
1146
 
 
1147
    def test_supports_unlimited_cache(self):
 
1148
        builder = btree_index.BTreeBuilder(reference_lists=0, key_elements=1)
 
1149
        # We need enough nodes to cause a page split (so we have both an
 
1150
        # internal node and a couple leaf nodes. 500 seems to be enough.)
 
1151
        nodes = self.make_nodes(500, 1, 0)
 
1152
        for node in nodes:
 
1153
            builder.add_node(*node)
 
1154
        stream = builder.finish()
 
1155
        trans = get_transport(self.get_url())
 
1156
        size = trans.put_file('index', stream)
 
1157
        index = btree_index.BTreeGraphIndex(trans, 'index', size)
 
1158
        self.assertEqual(500, index.key_count())
 
1159
        # We have an internal node
 
1160
        self.assertEqual(2, len(index._row_lengths))
 
1161
        # We have at least 2 leaf nodes
 
1162
        self.assertTrue(index._row_lengths[-1] >= 2)
 
1163
        self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
 
1164
        self.assertEqual(btree_index._NODE_CACHE_SIZE,
 
1165
                         index._leaf_node_cache._max_cache)
 
1166
        self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
 
1167
        self.assertEqual(100, index._internal_node_cache._max_cache)
 
1168
        # No change if unlimited_cache=False is passed
 
1169
        index = btree_index.BTreeGraphIndex(trans, 'index', size,
 
1170
                                            unlimited_cache=False)
 
1171
        self.assertIsInstance(index._leaf_node_cache, lru_cache.LRUCache)
 
1172
        self.assertEqual(btree_index._NODE_CACHE_SIZE,
 
1173
                         index._leaf_node_cache._max_cache)
 
1174
        self.assertIsInstance(index._internal_node_cache, fifo_cache.FIFOCache)
 
1175
        self.assertEqual(100, index._internal_node_cache._max_cache)
 
1176
        index = btree_index.BTreeGraphIndex(trans, 'index', size,
 
1177
                                            unlimited_cache=True)
 
1178
        self.assertIsInstance(index._leaf_node_cache, dict)
 
1179
        self.assertIs(type(index._internal_node_cache), dict)
 
1180
        # Exercise the lookup code
 
1181
        entries = set(index.iter_entries([n[0] for n in nodes]))
 
1182
        self.assertEqual(500, len(entries))
 
1183
 
1118
1184
 
1119
1185
class TestBTreeNodes(BTreeTestCase):
1120
1186