/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
1
# Copyright (C) 2008 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for maps built on a CHK versionedfiles facility."""
18
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
19
from itertools import izip
20
3735.16.3 by John Arbash Meinel
Add functions for _search_key_16 and _search_key_255 and some basic tests for them.
21
from bzrlib import (
22
    chk_map,
23
    osutils,
24
    tests,
25
    )
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
26
from bzrlib.chk_map import (
27
    CHKMap,
28
    InternalNode,
29
    LeafNode,
30
    )
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
31
from bzrlib.tests import TestCaseWithTransport
32
33
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
34
class TestCaseWithStore(TestCaseWithTransport):
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
35
36
    def get_chk_bytes(self):
3735.17.6 by John Arbash Meinel
Continue tracking down all the references to development3/4
37
        # The eassiest way to get a CHK store is a development5 repository and
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
38
        # then work with the chk_bytes attribute directly.
3735.17.6 by John Arbash Meinel
Continue tracking down all the references to development3/4
39
        repo = self.make_repository(".", format="development5")
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
40
        repo.lock_write()
41
        self.addCleanup(repo.unlock)
42
        repo.start_write_group()
43
        self.addCleanup(repo.abort_write_group)
44
        return repo.chk_bytes
45
3735.2.43 by Robert Collins
Teach CHKMap how to iter items in 2-tuple keyspaces.
46
    def _get_map(self, a_dict, maximum_size=0, chk_bytes=None, key_width=1):
3735.2.30 by Robert Collins
Start iter_changes between CHKMap instances.
47
        if chk_bytes is None:
48
            chk_bytes = self.get_chk_bytes()
3735.2.43 by Robert Collins
Teach CHKMap how to iter items in 2-tuple keyspaces.
49
        root_key = CHKMap.from_dict(chk_bytes, a_dict,
50
            maximum_size=maximum_size, key_width=key_width)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
51
        chkmap = CHKMap(chk_bytes, root_key)
52
        return chkmap
53
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
54
    def read_bytes(self, chk_bytes, key):
55
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
56
        record = stream.next()
57
        if record.storage_kind == 'absent':
58
            self.fail('Store does not contain the key %s' % (key,))
59
        return record.get_bytes_as("fulltext")
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
60
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
61
    def to_dict(self, node, *args):
62
        return dict(node.iteritems(*args))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
63
64
65
class TestMap(TestCaseWithStore):
66
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
67
    def assertHasABMap(self, chk_bytes):
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
68
        ab_leaf_bytes = 'chkleaf:\n0\n1\n1\na\n\x001\nb\n'
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
69
        ab_sha1 = osutils.sha_string(ab_leaf_bytes)
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
70
        self.assertEqual('90986195696b177c8895d48fdb4b7f2366f798a0', ab_sha1)
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
71
        root_key = ('sha1:' + ab_sha1,)
72
        self.assertEqual(ab_leaf_bytes, self.read_bytes(chk_bytes, root_key))
3735.2.24 by Robert Collins
test_chk_map tests all passing.
73
        return root_key
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
74
75
    def assertHasEmptyMap(self, chk_bytes):
3735.15.14 by John Arbash Meinel
Change the apply_ab_empty and apply_empty_ab tests to make them slightly more sha1 independent.
76
        empty_leaf_bytes = 'chkleaf:\n0\n1\n0\n\n'
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
77
        empty_sha1 = osutils.sha_string(empty_leaf_bytes)
3735.15.14 by John Arbash Meinel
Change the apply_ab_empty and apply_empty_ab tests to make them slightly more sha1 independent.
78
        self.assertEqual('8571e09bf1bcc5b9621ce31b3d4c93d6e9a1ed26', empty_sha1)
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
79
        root_key = ('sha1:' + empty_sha1,)
80
        self.assertEqual(empty_leaf_bytes, self.read_bytes(chk_bytes, root_key))
3735.2.24 by Robert Collins
test_chk_map tests all passing.
81
        return root_key
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
82
3735.11.2 by John Arbash Meinel
Add some CHKMap test helpers to make sure that maps end up in 'canonical' form.
83
    def assertMapLayoutEqual(self, map_one, map_two):
84
        """Assert that the internal structure is identical between the maps."""
85
        map_one._ensure_root()
86
        node_one_stack = [map_one._root_node]
87
        map_two._ensure_root()
88
        node_two_stack = [map_two._root_node]
89
        while node_one_stack:
90
            node_one = node_one_stack.pop()
91
            node_two = node_two_stack.pop()
92
            if node_one.__class__ != node_two.__class__:
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
93
                self.assertEqualDiff(map_one._dump_tree(include_keys=True),
94
                                     map_two._dump_tree(include_keys=True))
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
95
            self.assertEqual(node_one._search_prefix,
96
                             node_two._search_prefix)
3735.11.2 by John Arbash Meinel
Add some CHKMap test helpers to make sure that maps end up in 'canonical' form.
97
            if isinstance(node_one, InternalNode):
98
                # Internal nodes must have identical references
99
                self.assertEqual(sorted(node_one._items.keys()),
100
                                 sorted(node_two._items.keys()))
101
                node_one_stack.extend(node_one._iter_nodes(map_one._store))
102
                node_two_stack.extend(node_two._iter_nodes(map_two._store))
103
            else:
104
                # Leaf nodes must have identical contents
105
                self.assertEqual(node_one._items, node_two._items)
106
107
    def assertCanonicalForm(self, chkmap):
108
        """Assert that the chkmap is in 'canonical' form.
109
110
        We do this by adding all of the key value pairs from scratch, both in
111
        forward order and reverse order, and assert that the final tree layout
112
        is identical.
113
        """
114
        items = list(chkmap.iteritems())
115
        map_forward = chk_map.CHKMap(None, None)
116
        map_forward._root_node.set_maximum_size(chkmap._root_node.maximum_size)
117
        for key, value in items:
118
            map_forward.map(key, value)
119
        self.assertMapLayoutEqual(map_forward, chkmap)
120
        map_reverse = chk_map.CHKMap(None, None)
121
        map_reverse._root_node.set_maximum_size(chkmap._root_node.maximum_size)
122
        for key, value in reversed(items):
123
            map_reverse.map(key, value)
124
        self.assertMapLayoutEqual(map_reverse, chkmap)
125
126
    def test_assert_map_layout_equal(self):
127
        store = self.get_chk_bytes()
128
        map_one = CHKMap(store, None)
129
        map_one._root_node.set_maximum_size(20)
130
        map_two = CHKMap(store, None)
131
        map_two._root_node.set_maximum_size(20)
132
        self.assertMapLayoutEqual(map_one, map_two)
133
        map_one.map('aaa', 'value')
134
        self.assertRaises(AssertionError,
135
            self.assertMapLayoutEqual, map_one, map_two)
136
        map_two.map('aaa', 'value')
137
        self.assertMapLayoutEqual(map_one, map_two)
138
        # Split the tree, so we ensure that internal nodes and leaf nodes are
139
        # properly checked
140
        map_one.map('aab', 'value')
141
        self.assertIsInstance(map_one._root_node, InternalNode)
142
        self.assertRaises(AssertionError,
143
            self.assertMapLayoutEqual, map_one, map_two)
144
        map_two.map('aab', 'value')
145
        self.assertMapLayoutEqual(map_one, map_two)
146
        map_one.map('aac', 'value')
147
        self.assertRaises(AssertionError,
148
            self.assertMapLayoutEqual, map_one, map_two)
149
        self.assertCanonicalForm(map_one)
150
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
151
    def test_from_dict_empty(self):
152
        chk_bytes = self.get_chk_bytes()
153
        root_key = CHKMap.from_dict(chk_bytes, {})
3735.2.24 by Robert Collins
test_chk_map tests all passing.
154
        # Check the data was saved and inserted correctly.
155
        expected_root_key = self.assertHasEmptyMap(chk_bytes)
156
        self.assertEqual(expected_root_key, root_key)
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
157
158
    def test_from_dict_ab(self):
159
        chk_bytes = self.get_chk_bytes()
3735.15.24 by John Arbash Meinel
We were incorrectly using '' when we wanted to use None as the common prefix.
160
        root_key = CHKMap.from_dict(chk_bytes, {"a": "b"})
3735.2.24 by Robert Collins
test_chk_map tests all passing.
161
        # Check the data was saved and inserted correctly.
162
        expected_root_key = self.assertHasABMap(chk_bytes)
163
        self.assertEqual(expected_root_key, root_key)
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
164
165
    def test_apply_empty_ab(self):
166
        # applying a delta (None, "a", "b") to an empty chkmap generates the
167
        # same map as from_dict_ab.
168
        chk_bytes = self.get_chk_bytes()
169
        root_key = CHKMap.from_dict(chk_bytes, {})
170
        chkmap = CHKMap(chk_bytes, root_key)
171
        new_root = chkmap.apply_delta([(None, "a", "b")])
3735.2.24 by Robert Collins
test_chk_map tests all passing.
172
        # Check the data was saved and inserted correctly.
173
        expected_root_key = self.assertHasABMap(chk_bytes)
174
        self.assertEqual(expected_root_key, new_root)
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
175
        # The update should have left us with an in memory root node, with an
176
        # updated key.
177
        self.assertEqual(new_root, chkmap._root_node._key)
178
179
    def test_apply_ab_empty(self):
3735.2.30 by Robert Collins
Start iter_changes between CHKMap instances.
180
        # applying a delta ("a", None, None) to a map with 'a' in it generates
181
        # an empty map.
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
182
        chk_bytes = self.get_chk_bytes()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
183
        root_key = CHKMap.from_dict(chk_bytes, {("a",):"b"})
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
184
        chkmap = CHKMap(chk_bytes, root_key)
3735.2.24 by Robert Collins
test_chk_map tests all passing.
185
        new_root = chkmap.apply_delta([(("a",), None, None)])
186
        # Check the data was saved and inserted correctly.
187
        expected_root_key = self.assertHasEmptyMap(chk_bytes)
188
        self.assertEqual(expected_root_key, new_root)
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
189
        # The update should have left us with an in memory root node, with an
190
        # updated key.
191
        self.assertEqual(new_root, chkmap._root_node._key)
192
3735.9.3 by John Arbash Meinel
Add a direct test case that shows how the map() function is not
193
    def test_apply_delta_is_deterministic(self):
194
        chk_bytes = self.get_chk_bytes()
195
        chkmap1 = CHKMap(chk_bytes, None)
196
        chkmap1._root_node.set_maximum_size(10)
197
        chkmap1.apply_delta([(None, ('aaa',), 'common'),
198
                             (None, ('bba',), 'target2'),
199
                             (None, ('bbb',), 'common')])
200
        root_key1 = chkmap1._save()
3735.11.2 by John Arbash Meinel
Add some CHKMap test helpers to make sure that maps end up in 'canonical' form.
201
        self.assertCanonicalForm(chkmap1)
202
3735.9.3 by John Arbash Meinel
Add a direct test case that shows how the map() function is not
203
        chkmap2 = CHKMap(chk_bytes, None)
204
        chkmap2._root_node.set_maximum_size(10)
205
        chkmap2.apply_delta([(None, ('bbb',), 'common'),
206
                             (None, ('bba',), 'target2'),
207
                             (None, ('aaa',), 'common')])
208
        root_key2 = chkmap2._save()
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
209
        self.assertEqualDiff(chkmap1._dump_tree(include_keys=True),
210
                             chkmap2._dump_tree(include_keys=True))
3735.9.3 by John Arbash Meinel
Add a direct test case that shows how the map() function is not
211
        self.assertEqual(root_key1, root_key2)
3735.11.2 by John Arbash Meinel
Add some CHKMap test helpers to make sure that maps end up in 'canonical' form.
212
        self.assertCanonicalForm(chkmap2)
3735.9.3 by John Arbash Meinel
Add a direct test case that shows how the map() function is not
213
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
214
    def test_stable_splitting(self):
215
        store = self.get_chk_bytes()
216
        chkmap = CHKMap(store, None)
217
        # Should fit 2 keys per LeafNode
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
218
        chkmap._root_node.set_maximum_size(35)
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
219
        chkmap.map(('aaa',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
220
        self.assertEqualDiff("'' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
221
                             "      ('aaa',) 'v'\n",
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
222
                             chkmap._dump_tree())
223
        chkmap.map(('aab',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
224
        self.assertEqualDiff("'' LeafNode\n"
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
225
                             "      ('aaa',) 'v'\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
226
                             "      ('aab',) 'v'\n",
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
227
                             chkmap._dump_tree())
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
228
        self.assertCanonicalForm(chkmap)
229
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
230
        # Creates a new internal node, and splits the others into leaves
231
        chkmap.map(('aac',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
232
        self.assertEqualDiff("'' InternalNode\n"
233
                             "  'aaa' LeafNode\n"
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
234
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
235
                             "  'aab' LeafNode\n"
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
236
                             "      ('aab',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
237
                             "  'aac' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
238
                             "      ('aac',) 'v'\n",
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
239
                             chkmap._dump_tree())
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
240
        self.assertCanonicalForm(chkmap)
241
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
242
        # Splits again, because it can't fit in the current structure
243
        chkmap.map(('bbb',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
244
        self.assertEqualDiff("'' InternalNode\n"
245
                             "  'a' InternalNode\n"
246
                             "    'aaa' LeafNode\n"
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
247
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
248
                             "    'aab' LeafNode\n"
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
249
                             "      ('aab',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
250
                             "    'aac' LeafNode\n"
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
251
                             "      ('aac',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
252
                             "  'b' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
253
                             "      ('bbb',) 'v'\n",
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
254
                             chkmap._dump_tree())
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
255
        self.assertCanonicalForm(chkmap)
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
256
3735.11.6 by John Arbash Meinel
It seems that map()'s split code fails when the new key is simply longer.
257
    def test_map_splits_with_longer_key(self):
258
        store = self.get_chk_bytes()
259
        chkmap = CHKMap(store, None)
260
        # Should fit 1 key per LeafNode
261
        chkmap._root_node.set_maximum_size(10)
262
        chkmap.map(('aaa',), 'v')
263
        chkmap.map(('aaaa',), 'v')
264
        self.assertCanonicalForm(chkmap)
265
        self.assertIsInstance(chkmap._root_node, InternalNode)
266
3735.2.72 by John Arbash Meinel
Change deserialise to properly handle when there is a '\r' in the key.
267
    def test_with_linefeed_in_key(self):
268
        store = self.get_chk_bytes()
269
        chkmap = CHKMap(store, None)
270
        # Should fit 1 key per LeafNode
271
        chkmap._root_node.set_maximum_size(10)
272
        chkmap.map(('a\ra',), 'val1')
273
        chkmap.map(('a\rb',), 'val2')
274
        chkmap.map(('ac',), 'val3')
275
        self.assertCanonicalForm(chkmap)
276
        self.assertEqualDiff("'' InternalNode\n"
277
                             "  'a\\r' InternalNode\n"
278
                             "    'a\\ra' LeafNode\n"
279
                             "      ('a\\ra',) 'val1'\n"
280
                             "    'a\\rb' LeafNode\n"
281
                             "      ('a\\rb',) 'val2'\n"
282
                             "  'ac' LeafNode\n"
283
                             "      ('ac',) 'val3'\n",
284
                             chkmap._dump_tree())
285
        # We should also successfully serialise and deserialise these items
286
        root_key = chkmap._save()
287
        chkmap = CHKMap(store, root_key)
288
        self.assertEqualDiff("'' InternalNode\n"
289
                             "  'a\\r' InternalNode\n"
290
                             "    'a\\ra' LeafNode\n"
291
                             "      ('a\\ra',) 'val1'\n"
292
                             "    'a\\rb' LeafNode\n"
293
                             "      ('a\\rb',) 'val2'\n"
294
                             "  'ac' LeafNode\n"
295
                             "      ('ac',) 'val3'\n",
296
                             chkmap._dump_tree())
297
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
298
    def test_deep_splitting(self):
299
        store = self.get_chk_bytes()
300
        chkmap = CHKMap(store, None)
301
        # Should fit 2 keys per LeafNode
302
        chkmap._root_node.set_maximum_size(40)
303
        chkmap.map(('aaaaaaaa',), 'v')
304
        chkmap.map(('aaaaabaa',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
305
        self.assertEqualDiff("'' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
306
                             "      ('aaaaaaaa',) 'v'\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
307
                             "      ('aaaaabaa',) 'v'\n",
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
308
                             chkmap._dump_tree())
309
        chkmap.map(('aaabaaaa',), 'v')
310
        chkmap.map(('aaababaa',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
311
        self.assertEqualDiff("'' InternalNode\n"
312
                             "  'aaaa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
313
                             "      ('aaaaaaaa',) 'v'\n"
314
                             "      ('aaaaabaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
315
                             "  'aaab' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
316
                             "      ('aaabaaaa',) 'v'\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
317
                             "      ('aaababaa',) 'v'\n",
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
318
                             chkmap._dump_tree())
319
        chkmap.map(('aaabacaa',), 'v')
320
        chkmap.map(('aaabadaa',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
321
        self.assertEqualDiff("'' InternalNode\n"
322
                             "  'aaaa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
323
                             "      ('aaaaaaaa',) 'v'\n"
324
                             "      ('aaaaabaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
325
                             "  'aaab' InternalNode\n"
326
                             "    'aaabaa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
327
                             "      ('aaabaaaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
328
                             "    'aaabab' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
329
                             "      ('aaababaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
330
                             "    'aaabac' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
331
                             "      ('aaabacaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
332
                             "    'aaabad' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
333
                             "      ('aaabadaa',) 'v'\n",
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
334
                             chkmap._dump_tree())
3735.15.25 by John Arbash Meinel
Change the deep_split code to use a bigger value so we still get a split.
335
        chkmap.map(('aaababba',), 'val')
336
        chkmap.map(('aaababca',), 'val')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
337
        self.assertEqualDiff("'' InternalNode\n"
338
                             "  'aaaa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
339
                             "      ('aaaaaaaa',) 'v'\n"
340
                             "      ('aaaaabaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
341
                             "  'aaab' InternalNode\n"
342
                             "    'aaabaa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
343
                             "      ('aaabaaaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
344
                             "    'aaabab' InternalNode\n"
345
                             "      'aaababa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
346
                             "      ('aaababaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
347
                             "      'aaababb' LeafNode\n"
3735.15.25 by John Arbash Meinel
Change the deep_split code to use a bigger value so we still get a split.
348
                             "      ('aaababba',) 'val'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
349
                             "      'aaababc' LeafNode\n"
3735.15.25 by John Arbash Meinel
Change the deep_split code to use a bigger value so we still get a split.
350
                             "      ('aaababca',) 'val'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
351
                             "    'aaabac' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
352
                             "      ('aaabacaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
353
                             "    'aaabad' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
354
                             "      ('aaabadaa',) 'v'\n",
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
355
                             chkmap._dump_tree())
356
        # Now we add a node that should fit around an existing InternalNode,
357
        # but has a slightly different key prefix, which causes a new
358
        # InternalNode split
359
        chkmap.map(('aaabDaaa',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
360
        self.assertEqualDiff("'' InternalNode\n"
361
                             "  'aaaa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
362
                             "      ('aaaaaaaa',) 'v'\n"
363
                             "      ('aaaaabaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
364
                             "  'aaab' InternalNode\n"
365
                             "    'aaabD' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
366
                             "      ('aaabDaaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
367
                             "    'aaaba' InternalNode\n"
368
                             "      'aaabaa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
369
                             "      ('aaabaaaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
370
                             "      'aaabab' InternalNode\n"
371
                             "        'aaababa' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
372
                             "      ('aaababaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
373
                             "        'aaababb' LeafNode\n"
3735.15.25 by John Arbash Meinel
Change the deep_split code to use a bigger value so we still get a split.
374
                             "      ('aaababba',) 'val'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
375
                             "        'aaababc' LeafNode\n"
3735.15.25 by John Arbash Meinel
Change the deep_split code to use a bigger value so we still get a split.
376
                             "      ('aaababca',) 'val'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
377
                             "      'aaabac' LeafNode\n"
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
378
                             "      ('aaabacaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
379
                             "      'aaabad' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
380
                             "      ('aaabadaa',) 'v'\n",
3735.9.11 by John Arbash Meinel
Handle when an InternalNode decides it needs to split.
381
                             chkmap._dump_tree())
382
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
383
    def test_map_collapses_if_size_changes(self):
384
        store = self.get_chk_bytes()
385
        chkmap = CHKMap(store, None)
386
        # Should fit 2 keys per LeafNode
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
387
        chkmap._root_node.set_maximum_size(35)
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
388
        chkmap.map(('aaa',), 'v')
389
        chkmap.map(('aab',), 'very long value that splits')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
390
        self.assertEqualDiff("'' InternalNode\n"
391
                             "  'aaa' LeafNode\n"
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
392
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
393
                             "  'aab' LeafNode\n"
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
394
                             "      ('aab',) 'very long value that splits'\n",
395
                             chkmap._dump_tree())
396
        self.assertCanonicalForm(chkmap)
397
        # Now changing the value to something small should cause a rebuild
398
        chkmap.map(('aab',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
399
        self.assertEqualDiff("'' LeafNode\n"
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
400
                             "      ('aaa',) 'v'\n"
401
                             "      ('aab',) 'v'\n",
402
                             chkmap._dump_tree())
403
        self.assertCanonicalForm(chkmap)
404
405
    def test_map_double_deep_collapses(self):
406
        store = self.get_chk_bytes()
407
        chkmap = CHKMap(store, None)
408
        # Should fit 3 small keys per LeafNode
409
        chkmap._root_node.set_maximum_size(40)
410
        chkmap.map(('aaa',), 'v')
411
        chkmap.map(('aab',), 'very long value that splits')
412
        chkmap.map(('abc',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
413
        self.assertEqualDiff("'' InternalNode\n"
414
                             "  'aa' InternalNode\n"
415
                             "    'aaa' LeafNode\n"
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
416
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
417
                             "    'aab' LeafNode\n"
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
418
                             "      ('aab',) 'very long value that splits'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
419
                             "  'ab' LeafNode\n"
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
420
                             "      ('abc',) 'v'\n",
421
                             chkmap._dump_tree())
422
        chkmap.map(('aab',), 'v')
423
        self.assertCanonicalForm(chkmap)
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
424
        self.assertEqualDiff("'' LeafNode\n"
3735.11.11 by John Arbash Meinel
Add logic to map() so that it can also collapse when necessary.
425
                             "      ('aaa',) 'v'\n"
426
                             "      ('aab',) 'v'\n"
427
                             "      ('abc',) 'v'\n",
428
                             chkmap._dump_tree())
429
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
430
    def test_stable_unmap(self):
431
        store = self.get_chk_bytes()
432
        chkmap = CHKMap(store, None)
433
        # Should fit 2 keys per LeafNode
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
434
        chkmap._root_node.set_maximum_size(35)
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
435
        chkmap.map(('aaa',), 'v')
436
        chkmap.map(('aab',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
437
        self.assertEqualDiff("'' LeafNode\n"
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
438
                             "      ('aaa',) 'v'\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
439
                             "      ('aab',) 'v'\n",
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
440
                             chkmap._dump_tree())
441
        # Creates a new internal node, and splits the others into leaves
442
        chkmap.map(('aac',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
443
        self.assertEqualDiff("'' InternalNode\n"
444
                             "  'aaa' LeafNode\n"
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
445
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
446
                             "  'aab' LeafNode\n"
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
447
                             "      ('aab',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
448
                             "  'aac' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
449
                             "      ('aac',) 'v'\n",
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
450
                             chkmap._dump_tree())
451
        self.assertCanonicalForm(chkmap)
452
        # Now lets unmap one of the keys, and assert that we collapse the
453
        # structures.
454
        chkmap.unmap(('aac',))
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
455
        self.assertEqualDiff("'' LeafNode\n"
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
456
                             "      ('aaa',) 'v'\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
457
                             "      ('aab',) 'v'\n",
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
458
                             chkmap._dump_tree())
459
        self.assertCanonicalForm(chkmap)
460
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
461
    def test_unmap_double_deep(self):
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
462
        store = self.get_chk_bytes()
463
        chkmap = CHKMap(store, None)
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
464
        # Should fit 3 keys per LeafNode
465
        chkmap._root_node.set_maximum_size(40)
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
466
        chkmap.map(('aaa',), 'v')
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
467
        chkmap.map(('aaab',), 'v')
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
468
        chkmap.map(('aab',), 'very long value')
469
        chkmap.map(('abc',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
470
        self.assertEqualDiff("'' InternalNode\n"
471
                             "  'aa' InternalNode\n"
472
                             "    'aaa' LeafNode\n"
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
473
                             "      ('aaa',) 'v'\n"
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
474
                             "      ('aaab',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
475
                             "    'aab' LeafNode\n"
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
476
                             "      ('aab',) 'very long value'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
477
                             "  'ab' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
478
                             "      ('abc',) 'v'\n",
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
479
                             chkmap._dump_tree())
480
        # Removing the 'aab' key should cause everything to collapse back to a
481
        # single node
482
        chkmap.unmap(('aab',))
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
483
        self.assertEqualDiff("'' LeafNode\n"
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
484
                             "      ('aaa',) 'v'\n"
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
485
                             "      ('aaab',) 'v'\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
486
                             "      ('abc',) 'v'\n",
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
487
                             chkmap._dump_tree())
488
489
    def test_unmap_double_deep_non_empty_leaf(self):
490
        store = self.get_chk_bytes()
491
        chkmap = CHKMap(store, None)
492
        # Should fit 3 keys per LeafNode
493
        chkmap._root_node.set_maximum_size(40)
494
        chkmap.map(('aaa',), 'v')
495
        chkmap.map(('aab',), 'long value')
496
        chkmap.map(('aabb',), 'v')
497
        chkmap.map(('abc',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
498
        self.assertEqualDiff("'' InternalNode\n"
499
                             "  'aa' InternalNode\n"
500
                             "    'aaa' LeafNode\n"
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
501
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
502
                             "    'aab' LeafNode\n"
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
503
                             "      ('aab',) 'long value'\n"
504
                             "      ('aabb',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
505
                             "  'ab' LeafNode\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
506
                             "      ('abc',) 'v'\n",
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
507
                             chkmap._dump_tree())
508
        # Removing the 'aab' key should cause everything to collapse back to a
509
        # single node
510
        chkmap.unmap(('aab',))
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
511
        self.assertEqualDiff("'' LeafNode\n"
3735.11.5 by John Arbash Meinel
Ensure that unmap() work even when the LeafNode isn't empty.
512
                             "      ('aaa',) 'v'\n"
513
                             "      ('aabb',) 'v'\n"
3735.11.9 by John Arbash Meinel
Switch _dump_tree to returning trailing '\n' for nicer results
514
                             "      ('abc',) 'v'\n",
3735.11.4 by John Arbash Meinel
Add a test that unmap() properly chains back up multiple levels.
515
                             chkmap._dump_tree())
516
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
517
    def test_unmap_with_known_internal_node_doesnt_page(self):
518
        store = self.get_chk_bytes()
519
        chkmap = CHKMap(store, None)
520
        # Should fit 3 keys per LeafNode
521
        chkmap._root_node.set_maximum_size(30)
522
        chkmap.map(('aaa',), 'v')
523
        chkmap.map(('aab',), 'v')
524
        chkmap.map(('aac',), 'v')
525
        chkmap.map(('abc',), 'v')
526
        chkmap.map(('acd',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
527
        self.assertEqualDiff("'' InternalNode\n"
528
                             "  'aa' InternalNode\n"
529
                             "    'aaa' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
530
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
531
                             "    'aab' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
532
                             "      ('aab',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
533
                             "    'aac' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
534
                             "      ('aac',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
535
                             "  'ab' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
536
                             "      ('abc',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
537
                             "  'ac' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
538
                             "      ('acd',) 'v'\n",
539
                             chkmap._dump_tree())
540
        # Save everything to the map, and start over
541
        chkmap = CHKMap(store, chkmap._save())
542
        # Mapping an 'aa' key loads the internal node, but should not map the
543
        # 'ab' and 'ac' nodes
544
        chkmap.map(('aad',), 'v')
545
        self.assertIsInstance(chkmap._root_node._items['aa'], InternalNode)
546
        self.assertIsInstance(chkmap._root_node._items['ab'], tuple)
547
        self.assertIsInstance(chkmap._root_node._items['ac'], tuple)
548
        # Unmapping 'acd' can notice that 'aa' is an InternalNode and not have
549
        # to map in 'ab'
550
        chkmap.unmap(('acd',))
551
        self.assertIsInstance(chkmap._root_node._items['aa'], InternalNode)
552
        self.assertIsInstance(chkmap._root_node._items['ab'], tuple)
553
554
    def test_unmap_without_fitting_doesnt_page_in(self):
555
        store = self.get_chk_bytes()
556
        chkmap = CHKMap(store, None)
557
        # Should fit 2 keys per LeafNode
558
        chkmap._root_node.set_maximum_size(20)
559
        chkmap.map(('aaa',), 'v')
560
        chkmap.map(('aab',), 'v')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
561
        self.assertEqualDiff("'' InternalNode\n"
562
                             "  'aaa' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
563
                             "      ('aaa',) 'v'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
564
                             "  'aab' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
565
                             "      ('aab',) 'v'\n",
566
                             chkmap._dump_tree())
567
        # Save everything to the map, and start over
568
        chkmap = CHKMap(store, chkmap._save())
569
        chkmap.map(('aac',), 'v')
570
        chkmap.map(('aad',), 'v')
571
        chkmap.map(('aae',), 'v')
572
        chkmap.map(('aaf',), 'v')
573
        # At this point, the previous nodes should not be paged in, but the
574
        # newly added nodes would be
575
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
576
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
577
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
578
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
579
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
580
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
581
        # Now unmapping one of the new nodes will use only the already-paged-in
582
        # nodes to determine that we don't need to do more.
583
        chkmap.unmap(('aaf',))
584
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
585
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
586
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
587
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
588
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
589
590
    def test_unmap_pages_in_if_necessary(self):
591
        store = self.get_chk_bytes()
592
        chkmap = CHKMap(store, None)
593
        # Should fit 2 keys per LeafNode
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
594
        chkmap._root_node.set_maximum_size(30)
595
        chkmap.map(('aaa',), 'val')
596
        chkmap.map(('aab',), 'val')
597
        chkmap.map(('aac',), 'val')
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
598
        self.assertEqualDiff("'' InternalNode\n"
599
                             "  'aaa' LeafNode\n"
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
600
                             "      ('aaa',) 'val'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
601
                             "  'aab' LeafNode\n"
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
602
                             "      ('aab',) 'val'\n"
3735.15.17 by John Arbash Meinel
Update a bunch of tests that were using None rather than not displaying keys.
603
                             "  'aac' LeafNode\n"
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
604
                             "      ('aac',) 'val'\n",
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
605
                             chkmap._dump_tree())
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
606
        root_key = chkmap._save()
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
607
        # Save everything to the map, and start over
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
608
        chkmap = CHKMap(store, root_key)
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
609
        chkmap.map(('aad',), 'v')
610
        # At this point, the previous nodes should not be paged in, but the
611
        # newly added node would be
612
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
613
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
614
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
615
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
616
        # Unmapping the new node will check the existing nodes to see if they
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
617
        # would fit.
618
        # Clear the page cache so we ensure we have to read all the children
619
        chk_map._page_cache.clear()
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
620
        chkmap.unmap(('aad',))
621
        self.assertIsInstance(chkmap._root_node._items['aaa'], LeafNode)
622
        self.assertIsInstance(chkmap._root_node._items['aab'], LeafNode)
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
623
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
624
3735.15.27 by John Arbash Meinel
Split out the unmap_pages_in tests and add one that shows if we
625
    def test_unmap_pages_in_from_page_cache(self):
626
        store = self.get_chk_bytes()
627
        chkmap = CHKMap(store, None)
628
        # Should fit 2 keys per LeafNode
629
        chkmap._root_node.set_maximum_size(30)
630
        chkmap.map(('aaa',), 'val')
631
        chkmap.map(('aab',), 'val')
632
        chkmap.map(('aac',), 'val')
633
        root_key = chkmap._save()
634
        # Save everything to the map, and start over
635
        chkmap = CHKMap(store, root_key)
636
        chkmap.map(('aad',), 'val')
637
        self.assertEqualDiff("'' InternalNode\n"
638
                             "  'aaa' LeafNode\n"
639
                             "      ('aaa',) 'val'\n"
640
                             "  'aab' LeafNode\n"
641
                             "      ('aab',) 'val'\n"
642
                             "  'aac' LeafNode\n"
643
                             "      ('aac',) 'val'\n"
644
                             "  'aad' LeafNode\n"
645
                             "      ('aad',) 'val'\n",
646
                             chkmap._dump_tree())
647
        # Save everything to the map, start over after _dump_tree
648
        chkmap = CHKMap(store, root_key)
649
        chkmap.map(('aad',), 'v')
650
        # At this point, the previous nodes should not be paged in, but the
651
        # newly added node would be
652
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
653
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
654
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
655
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
656
        # Now clear the page cache, and only include 2 of the children in the
657
        # cache
658
        aab_key = chkmap._root_node._items['aab']
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
659
        aab_bytes = chk_map._page_cache[aab_key]
3735.15.27 by John Arbash Meinel
Split out the unmap_pages_in tests and add one that shows if we
660
        aac_key = chkmap._root_node._items['aac']
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
661
        aac_bytes = chk_map._page_cache[aac_key]
662
        chk_map._page_cache.clear()
663
        chk_map._page_cache[aab_key] = aab_bytes
664
        chk_map._page_cache[aac_key] = aac_bytes
3735.15.27 by John Arbash Meinel
Split out the unmap_pages_in tests and add one that shows if we
665
666
        # Unmapping the new node will check the nodes from the page cache
667
        # first, and not have to read in 'aaa'
3735.15.26 by John Arbash Meinel
Update the unmap_pages_in to also handle specific page-cache edge cases.
668
        chkmap.unmap(('aad',))
669
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
670
        self.assertIsInstance(chkmap._root_node._items['aab'], LeafNode)
671
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
672
3735.15.27 by John Arbash Meinel
Split out the unmap_pages_in tests and add one that shows if we
673
    def test_unmap_uses_existing_items(self):
674
        store = self.get_chk_bytes()
675
        chkmap = CHKMap(store, None)
676
        # Should fit 2 keys per LeafNode
677
        chkmap._root_node.set_maximum_size(30)
678
        chkmap.map(('aaa',), 'val')
679
        chkmap.map(('aab',), 'val')
680
        chkmap.map(('aac',), 'val')
681
        root_key = chkmap._save()
682
        # Save everything to the map, and start over
683
        chkmap = CHKMap(store, root_key)
684
        chkmap.map(('aad',), 'val')
685
        chkmap.map(('aae',), 'val')
686
        chkmap.map(('aaf',), 'val')
687
        # At this point, the previous nodes should not be paged in, but the
688
        # newly added node would be
689
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
690
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
691
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
692
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
693
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
694
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
695
696
        # Unmapping a new node will see the other nodes that are already in
697
        # memory, and not need to page in anything else
698
        chkmap.unmap(('aad',))
699
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
700
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
701
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
702
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
703
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
704
3735.2.30 by Robert Collins
Start iter_changes between CHKMap instances.
705
    def test_iter_changes_empty_ab(self):
706
        # Asking for changes between an empty dict to a dict with keys returns
707
        # all the keys.
3735.2.31 by Robert Collins
CHKMap.iter_changes
708
        basis = self._get_map({}, maximum_size=10)
3735.2.30 by Robert Collins
Start iter_changes between CHKMap instances.
709
        target = self._get_map(
710
            {('a',): 'content here', ('b',): 'more content'},
3735.2.31 by Robert Collins
CHKMap.iter_changes
711
            chk_bytes=basis._store, maximum_size=10)
3735.2.30 by Robert Collins
Start iter_changes between CHKMap instances.
712
        self.assertEqual([(('a',), None, 'content here'),
3735.2.31 by Robert Collins
CHKMap.iter_changes
713
            (('b',), None, 'more content')],
714
            sorted(list(target.iter_changes(basis))))
715
716
    def test_iter_changes_ab_empty(self):
717
        # Asking for changes between a dict with keys to an empty dict returns
718
        # all the keys.
719
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
720
            maximum_size=10)
721
        target = self._get_map({}, chk_bytes=basis._store, maximum_size=10)
722
        self.assertEqual([(('a',), 'content here', None),
723
            (('b',), 'more content', None)],
724
            sorted(list(target.iter_changes(basis))))
725
726
    def test_iter_changes_empty_empty_is_empty(self):
727
        basis = self._get_map({}, maximum_size=10)
728
        target = self._get_map({}, chk_bytes=basis._store, maximum_size=10)
729
        self.assertEqual([], sorted(list(target.iter_changes(basis))))
730
731
    def test_iter_changes_ab_ab_is_empty(self):
732
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
733
            maximum_size=10)
734
        target = self._get_map(
735
            {('a',): 'content here', ('b',): 'more content'},
736
            chk_bytes=basis._store, maximum_size=10)
737
        self.assertEqual([], sorted(list(target.iter_changes(basis))))
738
739
    def test_iter_changes_ab_ab_nodes_not_loaded(self):
740
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
741
            maximum_size=10)
742
        target = self._get_map(
743
            {('a',): 'content here', ('b',): 'more content'},
744
            chk_bytes=basis._store, maximum_size=10)
745
        list(target.iter_changes(basis))
746
        self.assertIsInstance(target._root_node, tuple)
747
        self.assertIsInstance(basis._root_node, tuple)
748
749
    def test_iter_changes_ab_ab_changed_values_shown(self):
750
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
751
            maximum_size=10)
752
        target = self._get_map(
753
            {('a',): 'content here', ('b',): 'different content'},
754
            chk_bytes=basis._store, maximum_size=10)
755
        result = sorted(list(target.iter_changes(basis)))
756
        self.assertEqual([(('b',), 'more content', 'different content')],
757
            result)
758
759
    def test_iter_changes_mixed_node_length(self):
760
        # When one side has different node lengths than the other, common
761
        # but different keys still need to be show, and new-and-old included
762
        # appropriately.
763
        # aaa - common unaltered
764
        # aab - common altered
765
        # b - basis only
766
        # at - target only
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
767
        # we expect:
3735.2.31 by Robert Collins
CHKMap.iter_changes
768
        # aaa to be not loaded (later test)
769
        # aab, b, at to be returned.
770
        # basis splits at byte 0,1,2, aaa is commonb is basis only
771
        basis_dict = {('aaa',): 'foo bar',
772
            ('aab',): 'common altered a', ('b',): 'foo bar b'}
773
        # target splits at byte 1,2, at is target only
774
        target_dict = {('aaa',): 'foo bar',
775
            ('aab',): 'common altered b', ('at',): 'foo bar t'}
776
        changes = [
777
            (('aab',), 'common altered a', 'common altered b'),
778
            (('at',), None, 'foo bar t'),
779
            (('b',), 'foo bar b', None),
780
            ]
781
        basis = self._get_map(basis_dict, maximum_size=10)
782
        target = self._get_map(target_dict, maximum_size=10,
783
            chk_bytes=basis._store)
784
        self.assertEqual(changes, sorted(list(target.iter_changes(basis))))
785
786
    def test_iter_changes_common_pages_not_loaded(self):
787
        # aaa - common unaltered
788
        # aab - common altered
789
        # b - basis only
790
        # at - target only
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
791
        # we expect:
3735.2.31 by Robert Collins
CHKMap.iter_changes
792
        # aaa to be not loaded
793
        # aaa not to be in result.
794
        basis_dict = {('aaa',): 'foo bar',
795
            ('aab',): 'common altered a', ('b',): 'foo bar b'}
796
        # target splits at byte 1, at is target only
797
        target_dict = {('aaa',): 'foo bar',
798
            ('aab',): 'common altered b', ('at',): 'foo bar t'}
799
        basis = self._get_map(basis_dict, maximum_size=10)
800
        target = self._get_map(target_dict, maximum_size=10,
801
            chk_bytes=basis._store)
3735.2.32 by Robert Collins
Activate test for common node skipping. - 50 times performance improvement.
802
        basis_get = basis._store.get_record_stream
803
        def get_record_stream(keys, order, fulltext):
804
            if ('sha1:1adf7c0d1b9140ab5f33bb64c6275fa78b1580b7',) in keys:
805
                self.fail("'aaa' pointer was followed %r" % keys)
806
            return basis_get(keys, order, fulltext)
807
        basis._store.get_record_stream = get_record_stream
3735.2.31 by Robert Collins
CHKMap.iter_changes
808
        result = sorted(list(target.iter_changes(basis)))
809
        for change in result:
810
            if change[0] == ('aaa',):
811
                self.fail("Found unexpected change: %s" % change)
812
813
    def test_iter_changes_unchanged_keys_in_multi_key_leafs_ignored(self):
814
        # Within a leaf there are no hash's to exclude keys, make sure multi
815
        # value leaf nodes are handled well.
816
        basis_dict = {('aaa',): 'foo bar',
817
            ('aab',): 'common altered a', ('b',): 'foo bar b'}
818
        target_dict = {('aaa',): 'foo bar',
819
            ('aab',): 'common altered b', ('at',): 'foo bar t'}
820
        changes = [
821
            (('aab',), 'common altered a', 'common altered b'),
822
            (('at',), None, 'foo bar t'),
823
            (('b',), 'foo bar b', None),
824
            ]
825
        basis = self._get_map(basis_dict)
826
        target = self._get_map(target_dict, chk_bytes=basis._store)
827
        self.assertEqual(changes, sorted(list(target.iter_changes(basis))))
3735.2.30 by Robert Collins
Start iter_changes between CHKMap instances.
828
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
829
    def test_iteritems_empty(self):
830
        chk_bytes = self.get_chk_bytes()
831
        root_key = CHKMap.from_dict(chk_bytes, {})
832
        chkmap = CHKMap(chk_bytes, root_key)
833
        self.assertEqual([], list(chkmap.iteritems()))
834
835
    def test_iteritems_two_items(self):
836
        chk_bytes = self.get_chk_bytes()
837
        root_key = CHKMap.from_dict(chk_bytes,
838
            {"a":"content here", "b":"more content"})
839
        chkmap = CHKMap(chk_bytes, root_key)
3735.2.24 by Robert Collins
test_chk_map tests all passing.
840
        self.assertEqual([(("a",), "content here"), (("b",), "more content")],
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
841
            sorted(list(chkmap.iteritems())))
842
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
843
    def test_iteritems_selected_one_of_two_items(self):
3735.2.24 by Robert Collins
test_chk_map tests all passing.
844
        chkmap = self._get_map( {("a",):"content here", ("b",):"more content"})
845
        self.assertEqual({("a",): "content here"},
846
            self.to_dict(chkmap, [("a",)]))
3735.2.9 by Robert Collins
Get a working chk_map using inventory implementation bootstrapped.
847
3735.2.43 by Robert Collins
Teach CHKMap how to iter items in 2-tuple keyspaces.
848
    def test_iteritems_keys_prefixed_by_2_width_nodes(self):
849
        chkmap = self._get_map(
850
            {("a","a"):"content here", ("a", "b",):"more content",
851
             ("b", ""): 'boring content'},
852
            maximum_size=10, key_width=2)
853
        self.assertEqual(
854
            {("a", "a"): "content here", ("a", "b"): 'more content'},
855
            self.to_dict(chkmap, [("a",)]))
856
857
    def test_iteritems_keys_prefixed_by_2_width_one_leaf(self):
858
        chkmap = self._get_map(
859
            {("a","a"):"content here", ("a", "b",):"more content",
860
             ("b", ""): 'boring content'}, key_width=2)
861
        self.assertEqual(
862
            {("a", "a"): "content here", ("a", "b"): 'more content'},
863
            self.to_dict(chkmap, [("a",)]))
864
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
865
    def test___len__empty(self):
866
        chkmap = self._get_map({})
867
        self.assertEqual(0, len(chkmap))
868
869
    def test___len__2(self):
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
870
        chkmap = self._get_map({("foo",):"bar", ("gam",):"quux"})
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
871
        self.assertEqual(2, len(chkmap))
872
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
873
    def test_max_size_100_bytes_new(self):
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
874
        # When there is a 100 byte upper node limit, a tree is formed.
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
875
        chkmap = self._get_map({("k1"*50,):"v1", ("k2"*50,):"v2"}, maximum_size=100)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
876
        # We expect three nodes:
877
        # A root, with two children, and with two key prefixes - k1 to one, and
878
        # k2 to the other as our node splitting is only just being developed.
879
        # The maximum size should be embedded
880
        chkmap._ensure_root()
881
        self.assertEqual(100, chkmap._root_node.maximum_size)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
882
        self.assertEqual(1, chkmap._root_node._key_width)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
883
        # There should be two child nodes, and prefix of 2(bytes):
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
884
        self.assertEqual(2, len(chkmap._root_node._items))
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
885
        self.assertEqual("k", chkmap._root_node._compute_search_prefix())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
886
        # The actual nodes pointed at will change as serialisers change; so
887
        # here we test that the key prefix is correct; then load the nodes and
888
        # check they have the right pointed at key; whether they have the
889
        # pointed at value inline or not is also unrelated to this test so we
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
890
        # don't check that in detail - rather we just check the aggregate
891
        # value.
892
        nodes = sorted(chkmap._root_node._items.items())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
893
        ptr1 = nodes[0]
894
        ptr2 = nodes[1]
895
        self.assertEqual('k1', ptr1[0])
896
        self.assertEqual('k2', ptr2[0])
3735.16.2 by John Arbash Meinel
Start passing around the search_key_func in more places.
897
        node1 = chk_map._deserialise(chkmap._read_bytes(ptr1[1]), ptr1[1], None)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
898
        self.assertIsInstance(node1, LeafNode)
899
        self.assertEqual(1, len(node1))
900
        self.assertEqual({('k1'*50,): 'v1'}, self.to_dict(node1, chkmap._store))
3735.16.2 by John Arbash Meinel
Start passing around the search_key_func in more places.
901
        node2 = chk_map._deserialise(chkmap._read_bytes(ptr2[1]), ptr2[1], None)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
902
        self.assertIsInstance(node2, LeafNode)
903
        self.assertEqual(1, len(node2))
904
        self.assertEqual({('k2'*50,): 'v2'}, self.to_dict(node2, chkmap._store))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
905
        # Having checked we have a good structure, check that the content is
906
        # still accessible.
907
        self.assertEqual(2, len(chkmap))
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
908
        self.assertEqual({("k1"*50,): "v1", ("k2"*50,): "v2"},
909
            self.to_dict(chkmap))
910
911
    def test_init_root_is_LeafNode_new(self):
912
        chk_bytes = self.get_chk_bytes()
913
        chkmap = CHKMap(chk_bytes, None)
914
        self.assertIsInstance(chkmap._root_node, LeafNode)
915
        self.assertEqual({}, self.to_dict(chkmap))
916
        self.assertEqual(0, len(chkmap))
917
918
    def test_init_and_save_new(self):
919
        chk_bytes = self.get_chk_bytes()
920
        chkmap = CHKMap(chk_bytes, None)
921
        key = chkmap._save()
922
        leaf_node = LeafNode()
923
        self.assertEqual([key], leaf_node.serialise(chk_bytes))
924
925
    def test_map_first_item_new(self):
926
        chk_bytes = self.get_chk_bytes()
927
        chkmap = CHKMap(chk_bytes, None)
928
        chkmap.map(("foo,",), "bar")
929
        self.assertEqual({('foo,',): 'bar'}, self.to_dict(chkmap))
930
        self.assertEqual(1, len(chkmap))
931
        key = chkmap._save()
932
        leaf_node = LeafNode()
933
        leaf_node.map(chk_bytes, ("foo,",), "bar")
934
        self.assertEqual([key], leaf_node.serialise(chk_bytes))
935
936
    def test_unmap_last_item_root_is_leaf_new(self):
937
        chkmap = self._get_map({("k1"*50,): "v1", ("k2"*50,): "v2"})
938
        chkmap.unmap(("k1"*50,))
939
        chkmap.unmap(("k2"*50,))
940
        self.assertEqual(0, len(chkmap))
941
        self.assertEqual({}, self.to_dict(chkmap))
942
        key = chkmap._save()
943
        leaf_node = LeafNode()
944
        self.assertEqual([key], leaf_node.serialise(chkmap._store))
3735.2.17 by Robert Collins
Cache node length to avoid full iteration on __len__ calls.
945
3735.9.2 by John Arbash Meinel
Add a _dump_tree helper that assists in debugging what is going on.
946
    def test__dump_tree(self):
947
        chkmap = self._get_map({("aaa",): "value1", ("aab",): "value2",
948
                                ("bbb",): "value3",},
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
949
                               maximum_size=15)
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
950
        self.assertEqualDiff("'' InternalNode\n"
951
                             "  'a' InternalNode\n"
952
                             "    'aaa' LeafNode\n"
953
                             "      ('aaa',) 'value1'\n"
954
                             "    'aab' LeafNode\n"
955
                             "      ('aab',) 'value2'\n"
956
                             "  'b' LeafNode\n"
957
                             "      ('bbb',) 'value3'\n",
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
958
                             chkmap._dump_tree())
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
959
        self.assertEqualDiff("'' InternalNode\n"
960
                             "  'a' InternalNode\n"
961
                             "    'aaa' LeafNode\n"
962
                             "      ('aaa',) 'value1'\n"
963
                             "    'aab' LeafNode\n"
964
                             "      ('aab',) 'value2'\n"
965
                             "  'b' LeafNode\n"
966
                             "      ('bbb',) 'value3'\n",
967
                             chkmap._dump_tree())
968
        self.assertEqualDiff(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
969
            "'' InternalNode sha1:0690d471eb0a624f359797d0ee4672bd68f4e236\n"
970
            "  'a' InternalNode sha1:1514c35503da9418d8fd90c1bed553077cb53673\n"
971
            "    'aaa' LeafNode sha1:4cc5970454d40b4ce297a7f13ddb76f63b88fefb\n"
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
972
            "      ('aaa',) 'value1'\n"
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
973
            "    'aab' LeafNode sha1:1d68bc90914ef8a3edbcc8bb28b00cb4fea4b5e2\n"
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
974
            "      ('aab',) 'value2'\n"
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
975
            "  'b' LeafNode sha1:3686831435b5596515353364eab0399dc45d49e7\n"
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
976
            "      ('bbb',) 'value3'\n",
977
            chkmap._dump_tree(include_keys=True))
3735.9.4 by John Arbash Meinel
Some small cleanups, and fix _dump_tree to handle in-progress nodes.
978
979
    def test__dump_tree_in_progress(self):
980
        chkmap = self._get_map({("aaa",): "value1", ("aab",): "value2"},
981
                               maximum_size=10)
982
        chkmap.map(('bbb',), 'value3')
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
983
        self.assertEqualDiff("'' InternalNode\n"
984
                             "  'a' InternalNode\n"
985
                             "    'aaa' LeafNode\n"
986
                             "      ('aaa',) 'value1'\n"
987
                             "    'aab' LeafNode\n"
988
                             "      ('aab',) 'value2'\n"
989
                             "  'b' LeafNode\n"
990
                             "      ('bbb',) 'value3'\n",
991
                             chkmap._dump_tree())
992
        # For things that are updated by adding 'bbb', we don't have a sha key
993
        # for them yet, so they are listed as None
994
        self.assertEqualDiff(
995
            "'' InternalNode None\n"
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
996
            "  'a' InternalNode sha1:6b0d881dd739a66f733c178b24da64395edfaafd\n"
997
            "    'aaa' LeafNode sha1:40b39a08d895babce17b20ae5f62d187eaa4f63a\n"
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
998
            "      ('aaa',) 'value1'\n"
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
999
            "    'aab' LeafNode sha1:ad1dc7c4e801302c95bf1ba7b20bc45e548cd51a\n"
3735.15.16 by John Arbash Meinel
Properly fix up the dump_tree tests, we now suppress the keys by default.
1000
            "      ('aab',) 'value2'\n"
1001
            "  'b' LeafNode None\n"
1002
            "      ('bbb',) 'value3'\n",
1003
            chkmap._dump_tree(include_keys=True))
3735.9.2 by John Arbash Meinel
Add a _dump_tree helper that assists in debugging what is going on.
1004
3735.2.8 by Robert Collins
New chk_map module for use in tree based inventory storage.
1005
3735.16.9 by John Arbash Meinel
Handle collisions.
1006
def _search_key_single(key):
1007
    """A search key function that maps all nodes to the same value"""
1008
    return 'value'
1009
3735.16.1 by John Arbash Meinel
(broken) Start tracking down more code that needs to pass around the 'search_key_func'
1010
def _test_search_key(key):
1011
    return 'test:' + '\x00'.join(key)
1012
1013
1014
class TestMapSearchKeys(TestCaseWithStore):
1015
1016
    def test_default_chk_map_uses_flat_search_key(self):
1017
        chkmap = chk_map.CHKMap(self.get_chk_bytes(), None)
1018
        self.assertEqual('1',
1019
                         chkmap._search_key_func(('1',)))
1020
        self.assertEqual('1\x002',
1021
                         chkmap._search_key_func(('1', '2')))
1022
        self.assertEqual('1\x002\x003',
1023
                         chkmap._search_key_func(('1', '2', '3')))
1024
1025
    def test_search_key_is_passed_to_root_node(self):
1026
        chkmap = chk_map.CHKMap(self.get_chk_bytes(), None,
1027
                                search_key_func=_test_search_key)
1028
        self.assertIs(_test_search_key, chkmap._search_key_func)
1029
        self.assertEqual('test:1\x002\x003',
1030
                         chkmap._search_key_func(('1', '2', '3')))
1031
        self.assertEqual('test:1\x002\x003',
1032
                         chkmap._root_node._search_key(('1', '2', '3')))
1033
1034
    def test_search_key_passed_via__ensure_root(self):
1035
        chk_bytes = self.get_chk_bytes()
1036
        chkmap = chk_map.CHKMap(chk_bytes, None,
1037
                                search_key_func=_test_search_key)
1038
        root_key = chkmap._save()
1039
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1040
                                search_key_func=_test_search_key)
1041
        chkmap._ensure_root()
1042
        self.assertEqual('test:1\x002\x003',
1043
                         chkmap._root_node._search_key(('1', '2', '3')))
1044
1045
    def test_search_key_with_internal_node(self):
1046
        chk_bytes = self.get_chk_bytes()
1047
        chkmap = chk_map.CHKMap(chk_bytes, None,
1048
                                search_key_func=_test_search_key)
1049
        chkmap._root_node.set_maximum_size(10)
1050
        chkmap.map(('1',), 'foo')
1051
        chkmap.map(('2',), 'bar')
1052
        chkmap.map(('3',), 'baz')
1053
        self.assertEqualDiff("'' InternalNode\n"
1054
                             "  'test:1' LeafNode\n"
1055
                             "      ('1',) 'foo'\n"
1056
                             "  'test:2' LeafNode\n"
1057
                             "      ('2',) 'bar'\n"
1058
                             "  'test:3' LeafNode\n"
1059
                             "      ('3',) 'baz'\n"
1060
                             , chkmap._dump_tree())
1061
        root_key = chkmap._save()
1062
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1063
                                search_key_func=_test_search_key)
1064
        self.assertEqualDiff("'' InternalNode\n"
1065
                             "  'test:1' LeafNode\n"
1066
                             "      ('1',) 'foo'\n"
1067
                             "  'test:2' LeafNode\n"
1068
                             "      ('2',) 'bar'\n"
1069
                             "  'test:3' LeafNode\n"
1070
                             "      ('3',) 'baz'\n"
1071
                             , chkmap._dump_tree())
1072
3735.16.4 by John Arbash Meinel
Add some tests that we can use the search keys as proper mappings.
1073
    def test_search_key_16(self):
1074
        chk_bytes = self.get_chk_bytes()
1075
        chkmap = chk_map.CHKMap(chk_bytes, None,
1076
                                search_key_func=chk_map._search_key_16)
1077
        chkmap._root_node.set_maximum_size(10)
1078
        chkmap.map(('1',), 'foo')
1079
        chkmap.map(('2',), 'bar')
1080
        chkmap.map(('3',), 'baz')
1081
        self.assertEqualDiff("'' InternalNode\n"
1082
                             "  '1' LeafNode\n"
1083
                             "      ('2',) 'bar'\n"
1084
                             "  '6' LeafNode\n"
1085
                             "      ('3',) 'baz'\n"
1086
                             "  '7' LeafNode\n"
1087
                             "      ('1',) 'foo'\n"
1088
                             , chkmap._dump_tree())
3735.16.5 by John Arbash Meinel
Add tests that we can lookup things after being serialized.
1089
        root_key = chkmap._save()
1090
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1091
                                search_key_func=chk_map._search_key_16)
1092
        # We can get the values back correctly
1093
        self.assertEqual([(('1',), 'foo')],
1094
                         list(chkmap.iteritems([('1',)])))
1095
        self.assertEqualDiff("'' InternalNode\n"
1096
                             "  '1' LeafNode\n"
1097
                             "      ('2',) 'bar'\n"
1098
                             "  '6' LeafNode\n"
1099
                             "      ('3',) 'baz'\n"
1100
                             "  '7' LeafNode\n"
1101
                             "      ('1',) 'foo'\n"
1102
                             , chkmap._dump_tree())
3735.16.4 by John Arbash Meinel
Add some tests that we can use the search keys as proper mappings.
1103
1104
    def test_search_key_255(self):
1105
        chk_bytes = self.get_chk_bytes()
1106
        chkmap = chk_map.CHKMap(chk_bytes, None,
1107
                                search_key_func=chk_map._search_key_255)
1108
        chkmap._root_node.set_maximum_size(10)
1109
        chkmap.map(('1',), 'foo')
1110
        chkmap.map(('2',), 'bar')
1111
        chkmap.map(('3',), 'baz')
1112
        self.assertEqualDiff("'' InternalNode\n"
1113
                             "  '\\x1a' LeafNode\n"
1114
                             "      ('2',) 'bar'\n"
1115
                             "  'm' LeafNode\n"
1116
                             "      ('3',) 'baz'\n"
1117
                             "  '\\x83' LeafNode\n"
1118
                             "      ('1',) 'foo'\n"
1119
                             , chkmap._dump_tree())
3735.16.5 by John Arbash Meinel
Add tests that we can lookup things after being serialized.
1120
        root_key = chkmap._save()
1121
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1122
                                search_key_func=chk_map._search_key_255)
1123
        # We can get the values back correctly
1124
        self.assertEqual([(('1',), 'foo')],
1125
                         list(chkmap.iteritems([('1',)])))
1126
        self.assertEqualDiff("'' InternalNode\n"
1127
                             "  '\\x1a' LeafNode\n"
1128
                             "      ('2',) 'bar'\n"
1129
                             "  'm' LeafNode\n"
1130
                             "      ('3',) 'baz'\n"
1131
                             "  '\\x83' LeafNode\n"
1132
                             "      ('1',) 'foo'\n"
1133
                             , chkmap._dump_tree())
3735.16.4 by John Arbash Meinel
Add some tests that we can use the search keys as proper mappings.
1134
3735.16.9 by John Arbash Meinel
Handle collisions.
1135
    def test_search_key_collisions(self):
1136
        chkmap = chk_map.CHKMap(self.get_chk_bytes(), None,
1137
                                search_key_func=_search_key_single)
1138
        # The node will want to expand, but it cannot, because it knows that
1139
        # all the keys must map to this node
1140
        chkmap._root_node.set_maximum_size(20)
1141
        chkmap.map(('1',), 'foo')
1142
        chkmap.map(('2',), 'bar')
1143
        chkmap.map(('3',), 'baz')
1144
        self.assertEqualDiff("'' LeafNode\n"
1145
                             "      ('1',) 'foo'\n"
1146
                             "      ('2',) 'bar'\n"
1147
                             "      ('3',) 'baz'\n"
1148
                             , chkmap._dump_tree())
1149
3735.16.1 by John Arbash Meinel
(broken) Start tracking down more code that needs to pass around the 'search_key_func'
1150
3735.16.3 by John Arbash Meinel
Add functions for _search_key_16 and _search_key_255 and some basic tests for them.
1151
class TestSearchKeyFuncs(tests.TestCase):
1152
1153
    def assertSearchKey16(self, expected, key):
1154
        self.assertEqual(expected, chk_map._search_key_16(key))
1155
1156
    def assertSearchKey255(self, expected, key):
1157
        actual = chk_map._search_key_255(key)
1158
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
1159
1160
    def test_simple_16(self):
1161
        self.assertSearchKey16('738C9ADF', ('foo',))
1162
        self.assertSearchKey16('738C9ADF\x00738C9ADF', ('foo', 'foo'))
1163
        self.assertSearchKey16('738C9ADF\x0076FF8CAA', ('foo', 'bar'))
1164
        self.assertSearchKey16('127D32EF', ('abcd',))
1165
1166
    def test_simple_255(self):
1167
        self.assertSearchKey255('\x8cse!', ('foo',))
1168
        self.assertSearchKey255('\x8cse!\x00\x8cse!', ('foo', 'foo'))
1169
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', ('foo', 'bar'))
1170
        # The standard mapping for these would include '\n', so it should be
1171
        # mapped to '_'
1172
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', ('<', 'V'))
1173
1174
    def test_255_does_not_include_newline(self):
1175
        # When mapping via _search_key_255, we should never have the '\n'
1176
        # character, but all other 255 values should be present
1177
        chars_used = set()
1178
        for char_in in range(256):
1179
            search_key = chk_map._search_key_255((chr(char_in),))
1180
            chars_used.update(search_key)
1181
        all_chars = set([chr(x) for x in range(256)])
1182
        unused_chars = all_chars.symmetric_difference(chars_used)
1183
        self.assertEqual(set('\n'), unused_chars)
1184
1185
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1186
class TestLeafNode(TestCaseWithStore):
1187
1188
    def test_current_size_empty(self):
1189
        node = LeafNode()
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1190
        self.assertEqual(16, node._current_size())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1191
1192
    def test_current_size_size_changed(self):
1193
        node = LeafNode()
1194
        node.set_maximum_size(10)
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1195
        self.assertEqual(17, node._current_size())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1196
1197
    def test_current_size_width_changed(self):
1198
        node = LeafNode()
1199
        node._key_width = 10
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1200
        self.assertEqual(17, node._current_size())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1201
1202
    def test_current_size_items(self):
1203
        node = LeafNode()
1204
        base_size = node._current_size()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1205
        node.map(None, ("foo bar",), "baz")
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1206
        self.assertEqual(base_size + 14, node._current_size())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1207
1208
    def test_deserialise_empty(self):
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1209
        node = LeafNode.deserialise("chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1210
        self.assertEqual(0, len(node))
1211
        self.assertEqual(10, node.maximum_size)
1212
        self.assertEqual(("sha1:1234",), node.key())
3735.15.24 by John Arbash Meinel
We were incorrectly using '' when we wanted to use None as the common prefix.
1213
        self.assertIs(None, node._search_prefix)
1214
        self.assertIs(None, node._common_serialised_prefix)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1215
1216
    def test_deserialise_items(self):
1217
        node = LeafNode.deserialise(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1218
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
1219
            ("sha1:1234",))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1220
        self.assertEqual(2, len(node))
1221
        self.assertEqual([(("foo bar",), "baz"), (("quux",), "blarh")],
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1222
            sorted(node.iteritems(None)))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1223
3735.2.25 by Robert Collins
CHKInventory core tests passing.
1224
    def test_deserialise_item_with_null_width_1(self):
1225
        node = LeafNode.deserialise(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1226
            "chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
3735.2.25 by Robert Collins
CHKInventory core tests passing.
1227
            ("sha1:1234",))
1228
        self.assertEqual(2, len(node))
1229
        self.assertEqual([(("foo",), "bar\x00baz"), (("quux",), "blarh")],
1230
            sorted(node.iteritems(None)))
1231
1232
    def test_deserialise_item_with_null_width_2(self):
1233
        node = LeafNode.deserialise(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1234
            "chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
1235
            "quux\x00\x001\nblarh\n",
3735.2.25 by Robert Collins
CHKInventory core tests passing.
1236
            ("sha1:1234",))
1237
        self.assertEqual(2, len(node))
1238
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("quux", ""), "blarh")],
1239
            sorted(node.iteritems(None)))
1240
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1241
    def test_iteritems_selected_one_of_two_items(self):
1242
        node = LeafNode.deserialise(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1243
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
1244
            ("sha1:1234",))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1245
        self.assertEqual(2, len(node))
1246
        self.assertEqual([(("quux",), "blarh")],
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1247
            sorted(node.iteritems(None, [("quux",), ("qaz",)])))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1248
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1249
    def test_deserialise_item_with_common_prefix(self):
1250
        node = LeafNode.deserialise(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1251
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1252
            ("sha1:1234",))
1253
        self.assertEqual(2, len(node))
1254
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("foo", "2"), "blarh")],
1255
            sorted(node.iteritems(None)))
3735.15.24 by John Arbash Meinel
We were incorrectly using '' when we wanted to use None as the common prefix.
1256
        self.assertEqual('foo\x00', node._search_prefix)
1257
        self.assertEqual('foo\x00', node._common_serialised_prefix)
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1258
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1259
    def test_deserialise_multi_line(self):
1260
        node = LeafNode.deserialise(
1261
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
1262
            ("sha1:1234",))
1263
        self.assertEqual(2, len(node))
1264
        self.assertEqual([(("foo", "1"), "bar\nbaz"),
1265
                          (("foo", "2"), "blarh\n"),
1266
                         ], sorted(node.iteritems(None)))
1267
        self.assertEqual('foo\x00', node._search_prefix)
1268
        self.assertEqual('foo\x00', node._common_serialised_prefix)
1269
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1270
    def test_key_new(self):
1271
        node = LeafNode()
1272
        self.assertEqual(None, node.key())
1273
1274
    def test_key_after_map(self):
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1275
        node = LeafNode.deserialise("chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1276
        node.map(None, ("foo bar",), "baz quux")
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1277
        self.assertEqual(None, node.key())
1278
1279
    def test_key_after_unmap(self):
1280
        node = LeafNode.deserialise(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1281
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
1282
            ("sha1:1234",))
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1283
        node.unmap(None, ("foo bar",))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1284
        self.assertEqual(None, node.key())
1285
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1286
    def test_map_exceeding_max_size_only_entry_new(self):
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1287
        node = LeafNode()
1288
        node.set_maximum_size(10)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1289
        result = node.map(None, ("foo bar",), "baz quux")
1290
        self.assertEqual(("foo bar", [("", node)]), result)
1291
        self.assertTrue(10 < node._current_size())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1292
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1293
    def test_map_exceeding_max_size_second_entry_early_difference_new(self):
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1294
        node = LeafNode()
1295
        node.set_maximum_size(10)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1296
        node.map(None, ("foo bar",), "baz quux")
1297
        prefix, result = list(node.map(None, ("blue",), "red"))
1298
        self.assertEqual("", prefix)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1299
        self.assertEqual(2, len(result))
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1300
        split_chars = set([result[0][0], result[1][0]])
1301
        self.assertEqual(set(["f", "b"]), split_chars)
1302
        nodes = dict(result)
1303
        node = nodes["f"]
1304
        self.assertEqual({("foo bar",): "baz quux"}, self.to_dict(node, None))
1305
        self.assertEqual(10, node.maximum_size)
1306
        self.assertEqual(1, node._key_width)
1307
        node = nodes["b"]
1308
        self.assertEqual({("blue",): "red"}, self.to_dict(node, None))
1309
        self.assertEqual(10, node.maximum_size)
1310
        self.assertEqual(1, node._key_width)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1311
1312
    def test_map_first(self):
1313
        node = LeafNode()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1314
        result = node.map(None, ("foo bar",), "baz quux")
1315
        self.assertEqual(("foo bar", [("", node)]), result)
1316
        self.assertEqual({("foo bar",):"baz quux"}, self.to_dict(node, None))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1317
        self.assertEqual(1, len(node))
1318
1319
    def test_map_second(self):
1320
        node = LeafNode()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1321
        node.map(None, ("foo bar",), "baz quux")
1322
        result = node.map(None, ("bingo",), "bango")
1323
        self.assertEqual(("", [("", node)]), result)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1324
        self.assertEqual({("foo bar",):"baz quux", ("bingo",):"bango"},
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1325
            self.to_dict(node, None))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1326
        self.assertEqual(2, len(node))
1327
1328
    def test_map_replacement(self):
1329
        node = LeafNode()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1330
        node.map(None, ("foo bar",), "baz quux")
1331
        result = node.map(None, ("foo bar",), "bango")
1332
        self.assertEqual(("foo bar", [("", node)]), result)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1333
        self.assertEqual({("foo bar",): "bango"},
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1334
            self.to_dict(node, None))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1335
        self.assertEqual(1, len(node))
1336
1337
    def test_serialise_empty(self):
1338
        store = self.get_chk_bytes()
1339
        node = LeafNode()
1340
        node.set_maximum_size(10)
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1341
        expected_key = ("sha1:f34c3f0634ea3f85953dffa887620c0a5b1f4a51",)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1342
        self.assertEqual([expected_key],
1343
            list(node.serialise(store)))
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1344
        self.assertEqual("chkleaf:\n10\n1\n0\n\n", self.read_bytes(store, expected_key))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1345
        self.assertEqual(expected_key, node.key())
1346
1347
    def test_serialise_items(self):
1348
        store = self.get_chk_bytes()
1349
        node = LeafNode()
1350
        node.set_maximum_size(10)
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1351
        node.map(None, ("foo bar",), "baz quux")
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1352
        expected_key = ("sha1:f89fac7edfc6bdb1b1b54a556012ff0c646ef5e0",)
3735.15.23 by John Arbash Meinel
Fix up the LeafNode direct tests, and add some tests with serialized
1353
        self.assertEqual('foo bar', node._common_serialised_prefix)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1354
        self.assertEqual([expected_key],
1355
            list(node.serialise(store)))
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1356
        self.assertEqual("chkleaf:\n10\n1\n1\nfoo bar\n\x001\nbaz quux\n",
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1357
            self.read_bytes(store, expected_key))
1358
        self.assertEqual(expected_key, node.key())
1359
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1360
    def test_unique_serialised_prefix_empty_new(self):
1361
        node = LeafNode()
3735.15.24 by John Arbash Meinel
We were incorrectly using '' when we wanted to use None as the common prefix.
1362
        self.assertIs(None, node._compute_search_prefix())
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1363
1364
    def test_unique_serialised_prefix_one_item_new(self):
1365
        node = LeafNode()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1366
        node.map(None, ("foo bar", "baz"), "baz quux")
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1367
        self.assertEqual("foo bar\x00baz", node._compute_search_prefix())
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1368
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1369
    def test_unmap_missing(self):
1370
        node = LeafNode()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1371
        self.assertRaises(KeyError, node.unmap, None, ("foo bar",))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1372
1373
    def test_unmap_present(self):
1374
        node = LeafNode()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1375
        node.map(None, ("foo bar",), "baz quux")
1376
        result = node.unmap(None, ("foo bar",))
1377
        self.assertEqual(node, result)
1378
        self.assertEqual({}, self.to_dict(node, None))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1379
        self.assertEqual(0, len(node))
1380
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1381
    def test_map_maintains_common_prefixes(self):
1382
        node = LeafNode()
1383
        node._key_width = 2
1384
        node.map(None, ("foo bar", "baz"), "baz quux")
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1385
        self.assertEqual('foo bar\x00baz', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1386
        self.assertEqual('foo bar\x00baz', node._common_serialised_prefix)
1387
        node.map(None, ("foo bar", "bing"), "baz quux")
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1388
        self.assertEqual('foo bar\x00b', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1389
        self.assertEqual('foo bar\x00b', node._common_serialised_prefix)
1390
        node.map(None, ("fool", "baby"), "baz quux")
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1391
        self.assertEqual('foo', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1392
        self.assertEqual('foo', node._common_serialised_prefix)
1393
        node.map(None, ("foo bar", "baz"), "replaced")
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1394
        self.assertEqual('foo', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1395
        self.assertEqual('foo', node._common_serialised_prefix)
1396
        node.map(None, ("very", "different"), "value")
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1397
        self.assertEqual('', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1398
        self.assertEqual('', node._common_serialised_prefix)
1399
1400
    def test_unmap_maintains_common_prefixes(self):
1401
        node = LeafNode()
1402
        node._key_width = 2
1403
        node.map(None, ("foo bar", "baz"), "baz quux")
1404
        node.map(None, ("foo bar", "bing"), "baz quux")
1405
        node.map(None, ("fool", "baby"), "baz quux")
1406
        node.map(None, ("very", "different"), "value")
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1407
        self.assertEqual('', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1408
        self.assertEqual('', node._common_serialised_prefix)
1409
        node.unmap(None, ("very", "different"))
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1410
        self.assertEqual("foo", node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1411
        self.assertEqual("foo", node._common_serialised_prefix)
1412
        node.unmap(None, ("fool", "baby"))
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1413
        self.assertEqual('foo bar\x00b', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1414
        self.assertEqual('foo bar\x00b', node._common_serialised_prefix)
1415
        node.unmap(None, ("foo bar", "baz"))
3735.15.13 by John Arbash Meinel
Change the term 'lookup' to the term 'search', as it is closer to what Robert envisioned.
1416
        self.assertEqual('foo bar\x00bing', node._search_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1417
        self.assertEqual('foo bar\x00bing', node._common_serialised_prefix)
1418
        node.unmap(None, ("foo bar", "bing"))
3735.15.24 by John Arbash Meinel
We were incorrectly using '' when we wanted to use None as the common prefix.
1419
        self.assertEqual(None, node._search_prefix)
1420
        self.assertEqual(None, node._common_serialised_prefix)
3735.15.6 by John Arbash Meinel
Add tests that LeafNodes track the common prefix for both their lookup keys
1421
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1422
1423
class TestInternalNode(TestCaseWithStore):
1424
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1425
    def test_add_node_empty_new(self):
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
1426
        node = InternalNode('fo')
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1427
        child = LeafNode()
1428
        child.set_maximum_size(100)
1429
        child.map(None, ("foo",), "bar")
1430
        node.add_node("foo", child)
1431
        # Note that node isn't strictly valid now as a tree (only one child),
1432
        # but thats ok for this test.
1433
        # The first child defines the node's width:
1434
        self.assertEqual(3, node._node_width)
1435
        # We should be able to iterate over the contents without doing IO.
1436
        self.assertEqual({('foo',): 'bar'}, self.to_dict(node, None))
1437
        # The length should be known:
1438
        self.assertEqual(1, len(node))
1439
        # serialising the node should serialise the child and the node.
1440
        chk_bytes = self.get_chk_bytes()
1441
        keys = list(node.serialise(chk_bytes))
1442
        child_key = child.serialise(chk_bytes)[0]
1443
        self.assertEqual(
3735.17.7 by John Arbash Meinel
Get all of test_chk_map passing.
1444
            [child_key, ('sha1:cf67e9997d8228a907c1f5bfb25a8bd9cd916fac',)],
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1445
            keys)
1446
        # We should be able to access deserialised content.
1447
        bytes = self.read_bytes(chk_bytes, keys[1])
3735.16.2 by John Arbash Meinel
Start passing around the search_key_func in more places.
1448
        node = chk_map._deserialise(bytes, keys[1], None)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1449
        self.assertEqual(1, len(node))
1450
        self.assertEqual({('foo',): 'bar'}, self.to_dict(node, chk_bytes))
1451
        self.assertEqual(3, node._node_width)
1452
1453
    def test_add_node_resets_key_new(self):
3735.9.5 by John Arbash Meinel
Don't allow an InternalNode to add a key that doesn't fit.
1454
        node = InternalNode('fo')
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1455
        child = LeafNode()
1456
        child.set_maximum_size(100)
1457
        child.map(None, ("foo",), "bar")
1458
        node.add_node("foo", child)
1459
        chk_bytes = self.get_chk_bytes()
1460
        keys = list(node.serialise(chk_bytes))
1461
        self.assertEqual(keys[1], node._key)
1462
        node.add_node("fos", child)
1463
        self.assertEqual(None, node._key)
1464
1465
#    def test_add_node_empty_oversized_one_ok_new(self):
1466
#    def test_add_node_one_oversized_second_kept_minimum_fan(self):
1467
#    def test_add_node_two_oversized_third_kept_minimum_fan(self):
1468
#    def test_add_node_one_oversized_second_splits_errors(self):
1469
1470
    def test_iteritems_empty_new(self):
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1471
        node = InternalNode()
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1472
        self.assertEqual([], sorted(node.iteritems(None)))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1473
1474
    def test_iteritems_two_children(self):
1475
        node = InternalNode()
1476
        leaf1 = LeafNode()
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1477
        leaf1.map(None, ('foo bar',), 'quux')
1478
        leaf2 = LeafNode()
1479
        leaf2.map(None, ('strange',), 'beast')
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1480
        node.add_node("f", leaf1)
1481
        node.add_node("s", leaf2)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1482
        self.assertEqual([(('foo bar',), 'quux'), (('strange',), 'beast')],
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1483
            sorted(node.iteritems(None)))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1484
1485
    def test_iteritems_two_children_partial(self):
1486
        node = InternalNode()
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1487
        leaf1 = LeafNode()
1488
        leaf1.map(None, ('foo bar',), 'quux')
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1489
        leaf2 = LeafNode()
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1490
        leaf2.map(None, ('strange',), 'beast')
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1491
        node.add_node("f", leaf1)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1492
        # This sets up a path that should not be followed - it will error if
1493
        # the code tries to.
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1494
        node._items['f'] = None
1495
        node.add_node("s", leaf2)
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1496
        self.assertEqual([(('strange',), 'beast')],
3735.2.24 by Robert Collins
test_chk_map tests all passing.
1497
            sorted(node.iteritems(None, [('strange',), ('weird',)])))
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1498
1499
    def test_iteritems_partial_empty(self):
1500
        node = InternalNode()
1501
        self.assertEqual([], sorted(node.iteritems([('missing',)])))
1502
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1503
    def test_map_to_new_child_new(self):
1504
        chkmap = self._get_map({('k1',):'foo', ('k2',):'bar'}, maximum_size=10)
1505
        chkmap._ensure_root()
1506
        node = chkmap._root_node
1507
        # Ensure test validity: nothing paged in below the root.
1508
        self.assertEqual(2,
1509
            len([value for value in node._items.values()
1510
                if type(value) == tuple]))
1511
        # now, mapping to k3 should add a k3 leaf
1512
        prefix, nodes = node.map(None, ('k3',), 'quux')
1513
        self.assertEqual("k", prefix)
1514
        self.assertEqual([("", node)], nodes)
1515
        # check new child details
1516
        child = node._items['k3']
1517
        self.assertIsInstance(child, LeafNode)
1518
        self.assertEqual(1, len(child))
1519
        self.assertEqual({('k3',): 'quux'}, self.to_dict(child, None))
1520
        self.assertEqual(None, child._key)
1521
        self.assertEqual(10, child.maximum_size)
1522
        self.assertEqual(1, child._key_width)
1523
        # Check overall structure:
1524
        self.assertEqual(3, len(chkmap))
1525
        self.assertEqual({('k1',): 'foo', ('k2',): 'bar', ('k3',): 'quux'},
1526
            self.to_dict(chkmap))
1527
        # serialising should only serialise the new data - k3 and the internal
1528
        # node.
1529
        keys = list(node.serialise(chkmap._store))
1530
        child_key = child.serialise(chkmap._store)[0]
1531
        self.assertEqual([child_key, keys[1]], keys)
1532
1533
    def test_map_to_child_child_splits_new(self):
1534
        chkmap = self._get_map({('k1',):'foo', ('k22',):'bar'}, maximum_size=10)
1535
        # Check for the canonical root value for this tree:
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
1536
        self.assertEqualDiff("'' InternalNode\n"
1537
                             "  'k1' LeafNode\n"
1538
                             "      ('k1',) 'foo'\n"
1539
                             "  'k2' LeafNode\n"
1540
                             "      ('k22',) 'bar'\n"
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
1541
                             , chkmap._dump_tree())
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
1542
        # _dump_tree pages everything in, so reload using just the root
1543
        chkmap = CHKMap(chkmap._store, chkmap._root_node)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1544
        chkmap._ensure_root()
1545
        node = chkmap._root_node
1546
        # Ensure test validity: nothing paged in below the root.
1547
        self.assertEqual(2,
1548
            len([value for value in node._items.values()
1549
                if type(value) == tuple]))
1550
        # now, mapping to k23 causes k22 ('k2' in node) to split into k22 and
1551
        # k23, which for simplicity in the current implementation generates
1552
        # a new internal node between node, and k22/k23.
1553
        prefix, nodes = node.map(chkmap._store, ('k23',), 'quux')
1554
        self.assertEqual("k", prefix)
1555
        self.assertEqual([("", node)], nodes)
1556
        # check new child details
1557
        child = node._items['k2']
1558
        self.assertIsInstance(child, InternalNode)
1559
        self.assertEqual(2, len(child))
1560
        self.assertEqual({('k22',): 'bar', ('k23',): 'quux'},
1561
            self.to_dict(child, None))
1562
        self.assertEqual(None, child._key)
1563
        self.assertEqual(10, child.maximum_size)
1564
        self.assertEqual(1, child._key_width)
1565
        self.assertEqual(3, child._node_width)
1566
        # Check overall structure:
1567
        self.assertEqual(3, len(chkmap))
1568
        self.assertEqual({('k1',): 'foo', ('k22',): 'bar', ('k23',): 'quux'},
1569
            self.to_dict(chkmap))
1570
        # serialising should only serialise the new data - although k22 hasn't
1571
        # changed because its a special corner case (splitting on with only one
1572
        # key leaves one node unaltered), in general k22 is serialised, so we
1573
        # expect k22, k23, the new internal node, and node, to be serialised.
1574
        keys = list(node.serialise(chkmap._store))
1575
        child_key = child._key
1576
        k22_key = child._items['k22']._key
1577
        k23_key = child._items['k23']._key
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
1578
        self.assertEqual([k22_key, k23_key, child_key, node.key()], keys)
1579
        self.assertEqualDiff("'' InternalNode\n"
1580
                             "  'k1' LeafNode\n"
1581
                             "      ('k1',) 'foo'\n"
1582
                             "  'k2' InternalNode\n"
1583
                             "    'k22' LeafNode\n"
1584
                             "      ('k22',) 'bar'\n"
1585
                             "    'k23' LeafNode\n"
1586
                             "      ('k23',) 'quux'\n"
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
1587
                             , chkmap._dump_tree())
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1588
1589
    def test_unmap_k23_from_k1_k22_k23_gives_k1_k22_tree_new(self):
1590
        chkmap = self._get_map(
1591
            {('k1',):'foo', ('k22',):'bar', ('k23',): 'quux'}, maximum_size=10)
1592
        # Check we have the expected tree.
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
1593
        self.assertEqualDiff("'' InternalNode\n"
1594
                             "  'k1' LeafNode\n"
1595
                             "      ('k1',) 'foo'\n"
3735.15.19 by John Arbash Meinel
Fix the other k1_k22_k23 test
1596
                             "  'k2' InternalNode\n"
1597
                             "    'k22' LeafNode\n"
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
1598
                             "      ('k22',) 'bar'\n"
3735.15.19 by John Arbash Meinel
Fix the other k1_k22_k23 test
1599
                             "    'k23' LeafNode\n"
1600
                             "      ('k23',) 'quux'\n"
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
1601
                             , chkmap._dump_tree())
1602
        chkmap = CHKMap(chkmap._store, chkmap._root_node)
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1603
        chkmap._ensure_root()
1604
        node = chkmap._root_node
1605
        # unmapping k23 should give us a root, with k1 and k22 as direct
1606
        # children.
1607
        result = node.unmap(chkmap._store, ('k23',))
1608
        # check the pointed-at object within node - k2 should now point at the
3735.11.3 by John Arbash Meinel
At the end of unmap() see if children can be packed into a single Leaf.
1609
        # k22 leaf (which has been paged in to see if we can collapse the tree)
1610
        child = node._items['k2']
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1611
        self.assertIsInstance(child, LeafNode)
1612
        self.assertEqual(1, len(child))
1613
        self.assertEqual({('k22',): 'bar'},
1614
            self.to_dict(child, None))
1615
        # Check overall structure is instact:
1616
        self.assertEqual(2, len(chkmap))
1617
        self.assertEqual({('k1',): 'foo', ('k22',): 'bar'},
1618
            self.to_dict(chkmap))
1619
        # serialising should only serialise the new data - the root node.
1620
        keys = list(node.serialise(chkmap._store))
1621
        self.assertEqual([keys[-1]], keys)
3735.15.19 by John Arbash Meinel
Fix the other k1_k22_k23 test
1622
        chkmap = CHKMap(chkmap._store, keys[-1])
1623
        self.assertEqualDiff("'' InternalNode\n"
1624
                             "  'k1' LeafNode\n"
1625
                             "      ('k1',) 'foo'\n"
1626
                             "  'k2' LeafNode\n"
1627
                             "      ('k22',) 'bar'\n"
1628
                             , chkmap._dump_tree())
3735.2.18 by Robert Collins
Partial multi-layer chk dictionary trees.
1629
3735.2.23 by Robert Collins
Test unmapping with one child left but multiple keys.
1630
    def test_unmap_k1_from_k1_k22_k23_gives_k22_k23_tree_new(self):
1631
        chkmap = self._get_map(
1632
            {('k1',):'foo', ('k22',):'bar', ('k23',): 'quux'}, maximum_size=10)
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
1633
        self.assertEqualDiff("'' InternalNode\n"
1634
                             "  'k1' LeafNode\n"
1635
                             "      ('k1',) 'foo'\n"
1636
                             "  'k2' InternalNode\n"
1637
                             "    'k22' LeafNode\n"
1638
                             "      ('k22',) 'bar'\n"
1639
                             "    'k23' LeafNode\n"
1640
                             "      ('k23',) 'quux'\n"
1641
                             , chkmap._dump_tree())
1642
        orig_root = chkmap._root_node
1643
        chkmap = CHKMap(chkmap._store, orig_root)
3735.2.23 by Robert Collins
Test unmapping with one child left but multiple keys.
1644
        chkmap._ensure_root()
1645
        node = chkmap._root_node
1646
        k2_ptr = node._items['k2']
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
1647
        # unmapping k1 should give us a root, with k22 and k23 as direct
3735.2.23 by Robert Collins
Test unmapping with one child left but multiple keys.
1648
        # children, and should not have needed to page in the subtree.
1649
        result = node.unmap(chkmap._store, ('k1',))
1650
        self.assertEqual(k2_ptr, result)
3735.15.18 by John Arbash Meinel
Fix up the k1_from_k1 test.
1651
        chkmap = CHKMap(chkmap._store, orig_root)
1652
        # Unmapping at the CHKMap level should switch to the new root
1653
        chkmap.unmap(('k1',))
1654
        self.assertEqual(k2_ptr, chkmap._root_node)
1655
        self.assertEqualDiff("'' InternalNode\n"
1656
                             "  'k22' LeafNode\n"
1657
                             "      ('k22',) 'bar'\n"
1658
                             "  'k23' LeafNode\n"
1659
                             "      ('k23',) 'quux'\n"
1660
                             , chkmap._dump_tree())
3735.2.23 by Robert Collins
Test unmapping with one child left but multiple keys.
1661
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1662
1663
# leaf:
1664
# map -> fits - done
1665
# map -> doesn't fit - shrink from left till fits
1666
#        key data to return: the common prefix, new nodes.
1667
1668
# unmap -> how to tell if siblings can be combined.
1669
#          combing leaf nodes means expanding the prefix to the left; so gather the size of
1670
#          all the leaf nodes addressed by expanding the prefix by 1; if any adjacent node
1671
#          is an internal node, we know that that is a dense subtree - can't combine.
1672
#          otherwise as soon as the sum of serialised values exceeds the split threshold
1673
#          we know we can't combine - stop.
1674
# unmap -> key return data - space in node, common prefix length? and key count
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
1675
# internal:
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1676
# variable length prefixes? -> later start with fixed width to get something going
1677
# map -> fits - update pointer to leaf
1678
#        return [prefix and node] - seems sound.
1679
# map -> doesn't fit - find unique prefix and shift right
1680
#        create internal nodes for all the partitions, return list of unique
1681
#        prefixes and nodes.
1682
# map -> new prefix - create a leaf
1683
# unmap -> if child key count 0, remove
1684
# unmap -> return space in node, common prefix length? (why?), key count
1685
# map:
1686
# map, if 1 node returned, use it, otherwise make an internal and populate.
1687
# map - unmap - if empty, use empty leafnode (avoids special cases in driver
1688
# code)
1689
# map inits as empty leafnode.
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
1690
# tools:
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1691
# visualiser
1692
1693
1694
# how to handle:
1695
# AA, AB, AC, AD, BA
1696
# packed internal node - ideal:
1697
# AA, AB, AC, AD, BA
1698
# single byte fanout - A,B,   AA,AB,AC,AD,     BA
1699
# build order's:
1700
# BA
3735.15.15 by John Arbash Meinel
Change child_child to use _dump_tree,
1701
# AB - split, but we want to end up with AB, BA, in one node, with
3735.2.21 by Robert Collins
BROKEN: multi level CHKMap tries, unfinished, subsystem in flux.
1702
# 1-4K get0
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1703
1704
1705
class TestIterInterestingNodes(TestCaseWithStore):
1706
1707
    def get_chk_bytes(self):
1708
        if getattr(self, '_chk_bytes', None) is None:
1709
            self._chk_bytes = super(TestIterInterestingNodes,
1710
                                    self).get_chk_bytes()
1711
        return self._chk_bytes
1712
1713
    def get_map_key(self, a_dict):
1714
        c_map = self._get_map(a_dict, maximum_size=10,
1715
                              chk_bytes=self.get_chk_bytes())
1716
        return c_map.key()
1717
1718
    def assertIterInteresting(self, expected, interesting_keys,
1719
                              uninteresting_keys):
1720
        """Check the result of iter_interesting_nodes.
1721
1722
        :param expected: A list of (record_keys, interesting_chk_pages,
1723
                                    interesting key value pairs)
1724
        """
1725
        store = self.get_chk_bytes()
1726
        iter_nodes = chk_map.iter_interesting_nodes(store, interesting_keys,
1727
                                                    uninteresting_keys)
1728
        for count, (exp, act) in enumerate(izip(expected, iter_nodes)):
3735.9.8 by John Arbash Meinel
Simplify the interface.
1729
            exp_record_keys, exp_items = exp
1730
            records, items = act
3735.9.15 by John Arbash Meinel
Found a bug in iter_interesting_nodes and its test suite.
1731
            exp_tuple = (sorted(exp_record_keys), sorted(exp_items))
1732
            act_tuple = (sorted(records.keys()), sorted(items))
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1733
            self.assertEqual(exp_tuple, act_tuple)
1734
        self.assertEqual(len(expected), count + 1)
1735
3735.9.19 by John Arbash Meinel
Refactor iter_interesting a little bit.
1736
    def test_empty_to_one_keys(self):
1737
        target = self.get_map_key({('a',): 'content'})
1738
        self.assertIterInteresting(
1739
            [([target], [(('a',), 'content')])],
1740
            [target], [])
1741
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1742
    def test_none_to_one_key(self):
1743
        basis = self.get_map_key({})
1744
        target = self.get_map_key({('a',): 'content'})
1745
        self.assertIterInteresting(
3735.9.8 by John Arbash Meinel
Simplify the interface.
1746
            [([target], [(('a',), 'content')])],
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1747
            [target], [basis])
1748
1749
    def test_one_to_none_key(self):
1750
        basis = self.get_map_key({('a',): 'content'})
1751
        target = self.get_map_key({})
1752
        self.assertIterInteresting(
3735.9.8 by John Arbash Meinel
Simplify the interface.
1753
            [([target], [])],
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1754
            [target], [basis])
1755
1756
    def test_common_pages(self):
1757
        basis = self.get_map_key({('a',): 'content',
1758
                                  ('b',): 'content',
1759
                                  ('c',): 'content',
1760
                                 })
1761
        target = self.get_map_key({('a',): 'content',
1762
                                   ('b',): 'other content',
1763
                                   ('c',): 'content',
1764
                                  })
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1765
        target_map = CHKMap(self.get_chk_bytes(), target)
3735.15.20 by John Arbash Meinel
Fix the IterInteresting tests.
1766
        self.assertEqualDiff(
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1767
            "'' InternalNode\n"
1768
            "  'a' LeafNode\n"
3735.15.20 by John Arbash Meinel
Fix the IterInteresting tests.
1769
            "      ('a',) 'content'\n"
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1770
            "  'b' LeafNode\n"
3735.15.20 by John Arbash Meinel
Fix the IterInteresting tests.
1771
            "      ('b',) 'other content'\n"
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1772
            "  'c' LeafNode\n"
3735.15.20 by John Arbash Meinel
Fix the IterInteresting tests.
1773
            "      ('c',) 'content'\n",
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1774
            target_map._dump_tree())
1775
        b_key = target_map._root_node._items['b'].key()
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1776
        # This should return the root node, and the node for the 'b' key
1777
        self.assertIterInteresting(
3735.9.8 by John Arbash Meinel
Simplify the interface.
1778
            [([target], []),
1779
             ([b_key], [(('b',), 'other content')])],
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1780
            [target], [basis])
1781
1782
    def test_common_sub_page(self):
1783
        basis = self.get_map_key({('aaa',): 'common',
1784
                                  ('c',): 'common',
1785
                                 })
1786
        target = self.get_map_key({('aaa',): 'common',
1787
                                   ('aab',): 'new',
1788
                                   ('c',): 'common',
1789
                                  })
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1790
        target_map = CHKMap(self.get_chk_bytes(), target)
3735.9.6 by John Arbash Meinel
Add a first pass to the interesting search.
1791
        self.assertEqualDiff(
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1792
            "'' InternalNode\n"
1793
            "  'a' InternalNode\n"
1794
            "    'aaa' LeafNode\n"
3735.9.6 by John Arbash Meinel
Add a first pass to the interesting search.
1795
            "      ('aaa',) 'common'\n"
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1796
            "    'aab' LeafNode\n"
3735.9.6 by John Arbash Meinel
Add a first pass to the interesting search.
1797
            "      ('aab',) 'new'\n"
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1798
            "  'c' LeafNode\n"
3735.11.10 by John Arbash Meinel
Change how _check_remap works so it doesn't have to load all keys.
1799
            "      ('c',) 'common'\n",
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1800
            target_map._dump_tree())
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1801
        # The key for the internal aa node
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1802
        a_key = target_map._root_node._items['a'].key()
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1803
        # The key for the leaf aab node
3735.15.21 by John Arbash Meinel
Change the IterInteresting to avoid sha hashes and be serialization agnostic.
1804
        aab_key = target_map._root_node._items['a']._items['aab'].key()
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1805
        self.assertIterInteresting(
3735.9.8 by John Arbash Meinel
Simplify the interface.
1806
            [([target], []),
3735.15.20 by John Arbash Meinel
Fix the IterInteresting tests.
1807
             ([a_key], []),
3735.9.8 by John Arbash Meinel
Simplify the interface.
1808
             ([aab_key], [(('aab',), 'new')])],
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1809
            [target], [basis])
1810
1811
    def test_multiple_maps(self):
1812
        basis1 = self.get_map_key({('aaa',): 'common',
1813
                                   ('aab',): 'basis1',
1814
                                  })
1815
        basis2 = self.get_map_key({('bbb',): 'common',
1816
                                   ('bbc',): 'basis2',
1817
                                  })
1818
        target1 = self.get_map_key({('aaa',): 'common',
1819
                                    ('aac',): 'target1',
1820
                                    ('bbb',): 'common',
1821
                                   })
1822
        target2 = self.get_map_key({('aaa',): 'common',
1823
                                    ('bba',): 'target2',
1824
                                    ('bbb',): 'common',
1825
                                   })
3735.15.22 by John Arbash Meinel
Change the multiple_maps test to also be independent of sha hash.
1826
        target1_map = CHKMap(self.get_chk_bytes(), target1)
1827
        self.assertEqualDiff(
1828
            "'' InternalNode\n"
1829
            "  'a' InternalNode\n"
1830
            "    'aaa' LeafNode\n"
1831
            "      ('aaa',) 'common'\n"
1832
            "    'aac' LeafNode\n"
1833
            "      ('aac',) 'target1'\n"
1834
            "  'b' LeafNode\n"
1835
            "      ('bbb',) 'common'\n",
1836
            target1_map._dump_tree())
1837
        # The key for the target1 internal a node
1838
        a_key = target1_map._root_node._items['a'].key()
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1839
        # The key for the leaf aac node
3735.15.22 by John Arbash Meinel
Change the multiple_maps test to also be independent of sha hash.
1840
        aac_key = target1_map._root_node._items['a']._items['aac'].key()
1841
1842
        target2_map = CHKMap(self.get_chk_bytes(), target2)
1843
        self.assertEqualDiff(
1844
            "'' InternalNode\n"
1845
            "  'a' LeafNode\n"
1846
            "      ('aaa',) 'common'\n"
1847
            "  'b' InternalNode\n"
1848
            "    'bba' LeafNode\n"
1849
            "      ('bba',) 'target2'\n"
1850
            "    'bbb' LeafNode\n"
1851
            "      ('bbb',) 'common'\n",
1852
            target2_map._dump_tree())
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1853
        # The key for the target2 internal bb node
3735.15.22 by John Arbash Meinel
Change the multiple_maps test to also be independent of sha hash.
1854
        b_key = target2_map._root_node._items['b'].key()
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1855
        # The key for the leaf bba node
3735.15.22 by John Arbash Meinel
Change the multiple_maps test to also be independent of sha hash.
1856
        bba_key = target2_map._root_node._items['b']._items['bba'].key()
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1857
        self.assertIterInteresting(
3735.9.8 by John Arbash Meinel
Simplify the interface.
1858
            [([target1, target2], []),
3735.15.22 by John Arbash Meinel
Change the multiple_maps test to also be independent of sha hash.
1859
             ([a_key], []),
1860
             ([b_key], []),
3735.9.19 by John Arbash Meinel
Refactor iter_interesting a little bit.
1861
             ([aac_key], [(('aac',), 'target1')]),
1862
             ([bba_key], [(('bba',), 'target2')]),
3735.9.1 by John Arbash Meinel
Start working on an iter_interesting_nodes, which can find nodes to transmit
1863
            ], [target1, target2], [basis1, basis2])