123
125
class TestBTreeBuilder(BTreeTestCase):
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()
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)
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)
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
665
self.assertEqual(internal_node_pre_clear,
666
index._internal_node_cache.keys())
667
self.assertEqual(0, len(index._leaf_node_cache))
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)
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)
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))
1119
1185
class TestBTreeNodes(BTreeTestCase):