/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4431.3.8 by Jonathan Lange
Cherrypick bzr.dev r4477.
1
# Copyright (C) 2008, 2009 Canonical Ltd
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests for maps built on a CHK versionedfiles facility."""
18
19
from itertools import izip
20
21
from bzrlib import (
22
    chk_map,
4526.9.5 by Robert Collins
Require that added ids in inventory deltas be new.
23
    errors,
4476.1.9 by John Arbash Meinel
Switching to make_pack_factory directly.
24
    groupcompress,
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
25
    osutils,
26
    tests,
27
    )
28
from bzrlib.chk_map import (
29
    CHKMap,
30
    InternalNode,
31
    LeafNode,
32
    Node,
33
    )
34
35
36
class TestNode(tests.TestCase):
37
38
    def assertCommonPrefix(self, expected_common, prefix, key):
39
        common = Node.common_prefix(prefix, key)
40
        self.assertTrue(len(common) <= len(prefix))
41
        self.assertTrue(len(common) <= len(key))
42
        self.assertStartsWith(prefix, common)
43
        self.assertStartsWith(key, common)
44
        self.assertEquals(expected_common, common)
45
46
    def test_common_prefix(self):
47
        self.assertCommonPrefix('beg', 'beg', 'begin')
48
49
    def test_no_common_prefix(self):
50
        self.assertCommonPrefix('', 'begin', 'end')
51
52
    def test_equal(self):
53
        self.assertCommonPrefix('begin', 'begin', 'begin')
54
55
    def test_not_a_prefix(self):
56
        self.assertCommonPrefix('b', 'begin', 'b')
57
4358.1.1 by Jelmer Vernooij
Support empty keys when looking for common prefixes in CHKMap.
58
    def test_empty(self):
59
        self.assertCommonPrefix('', '', 'end')
60
        self.assertCommonPrefix('', 'begin', '')
61
        self.assertCommonPrefix('', '', '')
62
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
63
4476.1.8 by John Arbash Meinel
Use TestCaseWithMemoryTransport drops the time to run chk_map tests
64
class TestCaseWithStore(tests.TestCaseWithMemoryTransport):
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
65
66
    def get_chk_bytes(self):
4526.9.11 by Robert Collins
Merge trunk.
67
        # This creates a standalone CHK store.
4476.1.9 by John Arbash Meinel
Switching to make_pack_factory directly.
68
        factory = groupcompress.make_pack_factory(False, False, 1)
69
        self.chk_bytes = factory(self.get_transport())
70
        return self.chk_bytes
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
71
72
    def _get_map(self, a_dict, maximum_size=0, chk_bytes=None, key_width=1,
73
                 search_key_func=None):
74
        if chk_bytes is None:
75
            chk_bytes = self.get_chk_bytes()
76
        root_key = CHKMap.from_dict(chk_bytes, a_dict,
77
            maximum_size=maximum_size, key_width=key_width,
78
            search_key_func=search_key_func)
4413.5.9 by John Arbash Meinel
Some cleanup. Move the check that from_dict works into test_chk_map.
79
        root_key2 = CHKMap._create_via_map(chk_bytes, a_dict,
80
            maximum_size=maximum_size, key_width=key_width,
81
            search_key_func=search_key_func)
82
        self.assertEqual(root_key, root_key2, "CHKMap.from_dict() did not"
83
                         " match CHKMap._create_via_map")
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
84
        chkmap = CHKMap(chk_bytes, root_key, search_key_func=search_key_func)
85
        return chkmap
86
87
    def read_bytes(self, chk_bytes, key):
88
        stream = chk_bytes.get_record_stream([key], 'unordered', True)
89
        record = stream.next()
90
        if record.storage_kind == 'absent':
91
            self.fail('Store does not contain the key %s' % (key,))
92
        return record.get_bytes_as("fulltext")
93
94
    def to_dict(self, node, *args):
95
        return dict(node.iteritems(*args))
96
97
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
98
class TestCaseWithExampleMaps(TestCaseWithStore):
99
100
    def get_chk_bytes(self):
101
        if getattr(self, '_chk_bytes', None) is None:
102
            self._chk_bytes = super(TestCaseWithExampleMaps,
103
                                    self).get_chk_bytes()
104
        return self._chk_bytes
105
106
    def get_map(self, a_dict, maximum_size=100, search_key_func=None):
107
        c_map = self._get_map(a_dict, maximum_size=maximum_size,
108
                              chk_bytes=self.get_chk_bytes(),
109
                              search_key_func=search_key_func)
110
        return c_map
111
112
    def make_root_only_map(self, search_key_func=None):
113
        return self.get_map({
114
            ('aaa',): 'initial aaa content',
115
            ('abb',): 'initial abb content',
116
        }, search_key_func=search_key_func)
117
118
    def make_root_only_aaa_ddd_map(self, search_key_func=None):
119
        return self.get_map({
120
            ('aaa',): 'initial aaa content',
121
            ('ddd',): 'initial ddd content',
122
        }, search_key_func=search_key_func)
123
124
    def make_one_deep_map(self, search_key_func=None):
125
        # Same as root_only_map, except it forces an InternalNode at the root
126
        return self.get_map({
127
            ('aaa',): 'initial aaa content',
128
            ('abb',): 'initial abb content',
129
            ('ccc',): 'initial ccc content',
130
            ('ddd',): 'initial ddd content',
131
        }, search_key_func=search_key_func)
132
133
    def make_two_deep_map(self, search_key_func=None):
134
        # Carefully chosen so that it creates a 2-deep map for both
135
        # _search_key_plain and for _search_key_16
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
136
        # Also so that things line up with make_one_deep_two_prefix_map
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
137
        return self.get_map({
138
            ('aaa',): 'initial aaa content',
139
            ('abb',): 'initial abb content',
140
            ('acc',): 'initial acc content',
141
            ('ace',): 'initial ace content',
142
            ('add',): 'initial add content',
143
            ('adh',): 'initial adh content',
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
144
            ('adl',): 'initial adl content',
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
145
            ('ccc',): 'initial ccc content',
146
            ('ddd',): 'initial ddd content',
147
        }, search_key_func=search_key_func)
148
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
149
    def make_one_deep_two_prefix_map(self, search_key_func=None):
150
        """Create a map with one internal node, but references are extra long.
151
152
        Otherwise has similar content to make_two_deep_map.
153
        """
154
        return self.get_map({
155
            ('aaa',): 'initial aaa content',
156
            ('add',): 'initial add content',
157
            ('adh',): 'initial adh content',
158
            ('adl',): 'initial adl content',
159
        }, search_key_func=search_key_func)
160
4476.1.25 by John Arbash Meinel
A bit more testing.
161
    def make_one_deep_one_prefix_map(self, search_key_func=None):
162
        """Create a map with one internal node, but references are extra long.
163
164
        Similar to make_one_deep_two_prefix_map, except the split is at the
165
        first char, rather than the second.
166
        """
167
        return self.get_map({
168
            ('add',): 'initial add content',
169
            ('adh',): 'initial adh content',
170
            ('adl',): 'initial adl content',
171
            ('bbb',): 'initial bbb content',
172
        }, search_key_func=search_key_func)
173
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
174
175
class TestTestCaseWithExampleMaps(TestCaseWithExampleMaps):
176
    """Actual tests for the provided examples."""
177
178
    def test_root_only_map_plain(self):
179
        c_map = self.make_root_only_map()
180
        self.assertEqualDiff(
181
            "'' LeafNode\n"
182
            "      ('aaa',) 'initial aaa content'\n"
183
            "      ('abb',) 'initial abb content'\n",
184
            c_map._dump_tree())
185
186
    def test_root_only_map_16(self):
187
        c_map = self.make_root_only_map(search_key_func=chk_map._search_key_16)
188
        self.assertEqualDiff(
189
            "'' LeafNode\n"
190
            "      ('aaa',) 'initial aaa content'\n"
191
            "      ('abb',) 'initial abb content'\n",
192
            c_map._dump_tree())
193
194
    def test_one_deep_map_plain(self):
195
        c_map = self.make_one_deep_map()
196
        self.assertEqualDiff(
197
            "'' InternalNode\n"
198
            "  'a' LeafNode\n"
199
            "      ('aaa',) 'initial aaa content'\n"
200
            "      ('abb',) 'initial abb content'\n"
201
            "  'c' LeafNode\n"
202
            "      ('ccc',) 'initial ccc content'\n"
203
            "  'd' LeafNode\n"
204
            "      ('ddd',) 'initial ddd content'\n",
205
            c_map._dump_tree())
206
207
    def test_one_deep_map_16(self):
208
        c_map = self.make_one_deep_map(search_key_func=chk_map._search_key_16)
209
        self.assertEqualDiff(
210
            "'' InternalNode\n"
211
            "  '2' LeafNode\n"
212
            "      ('ccc',) 'initial ccc content'\n"
213
            "  '4' LeafNode\n"
214
            "      ('abb',) 'initial abb content'\n"
215
            "  'F' LeafNode\n"
216
            "      ('aaa',) 'initial aaa content'\n"
217
            "      ('ddd',) 'initial ddd content'\n",
218
            c_map._dump_tree())
219
220
    def test_root_only_aaa_ddd_plain(self):
221
        c_map = self.make_root_only_aaa_ddd_map()
222
        self.assertEqualDiff(
223
            "'' LeafNode\n"
224
            "      ('aaa',) 'initial aaa content'\n"
225
            "      ('ddd',) 'initial ddd content'\n",
226
            c_map._dump_tree())
227
228
    def test_one_deep_map_16(self):
229
        c_map = self.make_root_only_aaa_ddd_map(
230
                search_key_func=chk_map._search_key_16)
231
        # We use 'aaa' and 'ddd' because they happen to map to 'F' when using
232
        # _search_key_16
233
        self.assertEqualDiff(
234
            "'' LeafNode\n"
235
            "      ('aaa',) 'initial aaa content'\n"
236
            "      ('ddd',) 'initial ddd content'\n",
237
            c_map._dump_tree())
238
239
    def test_two_deep_map_plain(self):
240
        c_map = self.make_two_deep_map()
241
        self.assertEqualDiff(
242
            "'' InternalNode\n"
243
            "  'a' InternalNode\n"
244
            "    'aa' LeafNode\n"
245
            "      ('aaa',) 'initial aaa content'\n"
246
            "    'ab' LeafNode\n"
247
            "      ('abb',) 'initial abb content'\n"
248
            "    'ac' LeafNode\n"
249
            "      ('acc',) 'initial acc content'\n"
250
            "      ('ace',) 'initial ace content'\n"
251
            "    'ad' LeafNode\n"
252
            "      ('add',) 'initial add content'\n"
253
            "      ('adh',) 'initial adh content'\n"
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
254
            "      ('adl',) 'initial adl content'\n"
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
255
            "  'c' LeafNode\n"
256
            "      ('ccc',) 'initial ccc content'\n"
257
            "  'd' LeafNode\n"
258
            "      ('ddd',) 'initial ddd content'\n",
259
            c_map._dump_tree())
260
261
    def test_two_deep_map_16(self):
262
        c_map = self.make_two_deep_map(search_key_func=chk_map._search_key_16)
263
        self.assertEqualDiff(
264
            "'' InternalNode\n"
265
            "  '2' LeafNode\n"
266
            "      ('acc',) 'initial acc content'\n"
267
            "      ('ccc',) 'initial ccc content'\n"
268
            "  '4' LeafNode\n"
269
            "      ('abb',) 'initial abb content'\n"
270
            "  'C' LeafNode\n"
271
            "      ('ace',) 'initial ace content'\n"
272
            "  'F' InternalNode\n"
273
            "    'F0' LeafNode\n"
274
            "      ('aaa',) 'initial aaa content'\n"
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
275
            "    'F3' LeafNode\n"
276
            "      ('adl',) 'initial adl content'\n"
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
277
            "    'F4' LeafNode\n"
278
            "      ('adh',) 'initial adh content'\n"
279
            "    'FB' LeafNode\n"
280
            "      ('ddd',) 'initial ddd content'\n"
281
            "    'FD' LeafNode\n"
282
            "      ('add',) 'initial add content'\n",
283
            c_map._dump_tree())
284
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
285
    def test_one_deep_two_prefix_map_plain(self):
286
        c_map = self.make_one_deep_two_prefix_map()
287
        self.assertEqualDiff(
288
            "'' InternalNode\n"
289
            "  'aa' LeafNode\n"
290
            "      ('aaa',) 'initial aaa content'\n"
291
            "  'ad' LeafNode\n"
292
            "      ('add',) 'initial add content'\n"
293
            "      ('adh',) 'initial adh content'\n"
294
            "      ('adl',) 'initial adl content'\n",
295
            c_map._dump_tree())
296
297
    def test_one_deep_two_prefix_map_16(self):
298
        c_map = self.make_one_deep_two_prefix_map(
299
            search_key_func=chk_map._search_key_16)
300
        self.assertEqualDiff(
301
            "'' InternalNode\n"
302
            "  'F0' LeafNode\n"
303
            "      ('aaa',) 'initial aaa content'\n"
304
            "  'F3' LeafNode\n"
305
            "      ('adl',) 'initial adl content'\n"
306
            "  'F4' LeafNode\n"
307
            "      ('adh',) 'initial adh content'\n"
308
            "  'FD' LeafNode\n"
309
            "      ('add',) 'initial add content'\n",
310
            c_map._dump_tree())
311
4476.1.25 by John Arbash Meinel
A bit more testing.
312
    def test_one_deep_one_prefix_map_plain(self):
313
        c_map = self.make_one_deep_one_prefix_map()
314
        self.assertEqualDiff(
315
            "'' InternalNode\n"
316
            "  'a' LeafNode\n"
317
            "      ('add',) 'initial add content'\n"
318
            "      ('adh',) 'initial adh content'\n"
319
            "      ('adl',) 'initial adl content'\n"
320
            "  'b' LeafNode\n"
321
            "      ('bbb',) 'initial bbb content'\n",
322
            c_map._dump_tree())
323
324
    def test_one_deep_one_prefix_map_16(self):
325
        c_map = self.make_one_deep_one_prefix_map(
326
            search_key_func=chk_map._search_key_16)
327
        self.assertEqualDiff(
328
            "'' InternalNode\n"
329
            "  '4' LeafNode\n"
330
            "      ('bbb',) 'initial bbb content'\n"
331
            "  'F' LeafNode\n"
332
            "      ('add',) 'initial add content'\n"
333
            "      ('adh',) 'initial adh content'\n"
334
            "      ('adl',) 'initial adl content'\n",
335
            c_map._dump_tree())
336
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
337
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
338
class TestMap(TestCaseWithStore):
339
340
    def assertHasABMap(self, chk_bytes):
341
        ab_leaf_bytes = 'chkleaf:\n0\n1\n1\na\n\x001\nb\n'
342
        ab_sha1 = osutils.sha_string(ab_leaf_bytes)
343
        self.assertEqual('90986195696b177c8895d48fdb4b7f2366f798a0', ab_sha1)
344
        root_key = ('sha1:' + ab_sha1,)
345
        self.assertEqual(ab_leaf_bytes, self.read_bytes(chk_bytes, root_key))
346
        return root_key
347
348
    def assertHasEmptyMap(self, chk_bytes):
349
        empty_leaf_bytes = 'chkleaf:\n0\n1\n0\n\n'
350
        empty_sha1 = osutils.sha_string(empty_leaf_bytes)
351
        self.assertEqual('8571e09bf1bcc5b9621ce31b3d4c93d6e9a1ed26', empty_sha1)
352
        root_key = ('sha1:' + empty_sha1,)
353
        self.assertEqual(empty_leaf_bytes, self.read_bytes(chk_bytes, root_key))
354
        return root_key
355
356
    def assertMapLayoutEqual(self, map_one, map_two):
357
        """Assert that the internal structure is identical between the maps."""
358
        map_one._ensure_root()
359
        node_one_stack = [map_one._root_node]
360
        map_two._ensure_root()
361
        node_two_stack = [map_two._root_node]
362
        while node_one_stack:
363
            node_one = node_one_stack.pop()
364
            node_two = node_two_stack.pop()
365
            if node_one.__class__ != node_two.__class__:
366
                self.assertEqualDiff(map_one._dump_tree(include_keys=True),
367
                                     map_two._dump_tree(include_keys=True))
368
            self.assertEqual(node_one._search_prefix,
369
                             node_two._search_prefix)
370
            if isinstance(node_one, InternalNode):
371
                # Internal nodes must have identical references
372
                self.assertEqual(sorted(node_one._items.keys()),
373
                                 sorted(node_two._items.keys()))
374
                node_one_stack.extend([n for n, _ in
375
                                       node_one._iter_nodes(map_one._store)])
376
                node_two_stack.extend([n for n, _ in
377
                                       node_two._iter_nodes(map_two._store)])
378
            else:
379
                # Leaf nodes must have identical contents
380
                self.assertEqual(node_one._items, node_two._items)
381
        self.assertEquals([], node_two_stack)
382
383
    def assertCanonicalForm(self, chkmap):
384
        """Assert that the chkmap is in 'canonical' form.
385
386
        We do this by adding all of the key value pairs from scratch, both in
387
        forward order and reverse order, and assert that the final tree layout
388
        is identical.
389
        """
390
        items = list(chkmap.iteritems())
391
        map_forward = chk_map.CHKMap(None, None)
392
        map_forward._root_node.set_maximum_size(chkmap._root_node.maximum_size)
393
        for key, value in items:
394
            map_forward.map(key, value)
395
        self.assertMapLayoutEqual(map_forward, chkmap)
396
        map_reverse = chk_map.CHKMap(None, None)
397
        map_reverse._root_node.set_maximum_size(chkmap._root_node.maximum_size)
398
        for key, value in reversed(items):
399
            map_reverse.map(key, value)
400
        self.assertMapLayoutEqual(map_reverse, chkmap)
401
402
    def test_assert_map_layout_equal(self):
403
        store = self.get_chk_bytes()
404
        map_one = CHKMap(store, None)
405
        map_one._root_node.set_maximum_size(20)
406
        map_two = CHKMap(store, None)
407
        map_two._root_node.set_maximum_size(20)
408
        self.assertMapLayoutEqual(map_one, map_two)
409
        map_one.map('aaa', 'value')
410
        self.assertRaises(AssertionError,
411
            self.assertMapLayoutEqual, map_one, map_two)
412
        map_two.map('aaa', 'value')
413
        self.assertMapLayoutEqual(map_one, map_two)
414
        # Split the tree, so we ensure that internal nodes and leaf nodes are
415
        # properly checked
416
        map_one.map('aab', 'value')
417
        self.assertIsInstance(map_one._root_node, InternalNode)
418
        self.assertRaises(AssertionError,
419
            self.assertMapLayoutEqual, map_one, map_two)
420
        map_two.map('aab', 'value')
421
        self.assertMapLayoutEqual(map_one, map_two)
422
        map_one.map('aac', 'value')
423
        self.assertRaises(AssertionError,
424
            self.assertMapLayoutEqual, map_one, map_two)
425
        self.assertCanonicalForm(map_one)
426
427
    def test_from_dict_empty(self):
428
        chk_bytes = self.get_chk_bytes()
429
        root_key = CHKMap.from_dict(chk_bytes, {})
430
        # Check the data was saved and inserted correctly.
431
        expected_root_key = self.assertHasEmptyMap(chk_bytes)
432
        self.assertEqual(expected_root_key, root_key)
433
434
    def test_from_dict_ab(self):
435
        chk_bytes = self.get_chk_bytes()
436
        root_key = CHKMap.from_dict(chk_bytes, {"a": "b"})
437
        # Check the data was saved and inserted correctly.
438
        expected_root_key = self.assertHasABMap(chk_bytes)
439
        self.assertEqual(expected_root_key, root_key)
440
441
    def test_apply_empty_ab(self):
442
        # applying a delta (None, "a", "b") to an empty chkmap generates the
443
        # same map as from_dict_ab.
444
        chk_bytes = self.get_chk_bytes()
445
        root_key = CHKMap.from_dict(chk_bytes, {})
446
        chkmap = CHKMap(chk_bytes, root_key)
447
        new_root = chkmap.apply_delta([(None, "a", "b")])
448
        # Check the data was saved and inserted correctly.
449
        expected_root_key = self.assertHasABMap(chk_bytes)
450
        self.assertEqual(expected_root_key, new_root)
451
        # The update should have left us with an in memory root node, with an
452
        # updated key.
453
        self.assertEqual(new_root, chkmap._root_node._key)
454
455
    def test_apply_ab_empty(self):
456
        # applying a delta ("a", None, None) to a map with 'a' in it generates
457
        # an empty map.
458
        chk_bytes = self.get_chk_bytes()
459
        root_key = CHKMap.from_dict(chk_bytes, {("a",):"b"})
460
        chkmap = CHKMap(chk_bytes, root_key)
461
        new_root = chkmap.apply_delta([(("a",), None, None)])
462
        # Check the data was saved and inserted correctly.
463
        expected_root_key = self.assertHasEmptyMap(chk_bytes)
464
        self.assertEqual(expected_root_key, new_root)
465
        # The update should have left us with an in memory root node, with an
466
        # updated key.
467
        self.assertEqual(new_root, chkmap._root_node._key)
468
4634.151.8 by Andrew Bennetts
Backport fix from chk-apply-delta-522637-2.1.
469
    def test_apply_delete_to_internal_node(self):
470
        # applying a delta should be convert an internal root node to a leaf
471
        # node if the delta shrinks the map enough.
472
        store = self.get_chk_bytes()
4634.151.9 by Andrew Bennetts
Simplify test.
473
        chkmap = CHKMap(store, None)
474
        # Add three items: 2 small enough to fit in one node, and one huge to
475
        # force multiple nodes.
476
        chkmap._root_node.set_maximum_size(100)
477
        chkmap.map(('small',), 'value')
478
        chkmap.map(('little',), 'value')
479
        chkmap.map(('very-big',), 'x' * 100)
480
        # (Check that we have constructed the scenario we want to test)
481
        self.assertIsInstance(chkmap._root_node, InternalNode)
482
        # Delete the huge item so that the map fits in one node again.
483
        delta = [(('very-big',), None, None)]
4634.151.8 by Andrew Bennetts
Backport fix from chk-apply-delta-522637-2.1.
484
        chkmap.apply_delta(delta)
485
        self.assertCanonicalForm(chkmap)
486
        self.assertIsInstance(chkmap._root_node, LeafNode)
487
4526.9.5 by Robert Collins
Require that added ids in inventory deltas be new.
488
    def test_apply_new_keys_must_be_new(self):
489
        # applying a delta (None, "a", "b") to a map with 'a' in it generates
490
        # an error.
491
        chk_bytes = self.get_chk_bytes()
492
        root_key = CHKMap.from_dict(chk_bytes, {("a",):"b"})
493
        chkmap = CHKMap(chk_bytes, root_key)
494
        self.assertRaises(errors.InconsistentDelta, chkmap.apply_delta,
495
            [(None, ("a",), "b")])
496
        # As an error occured, the update should have left us without changing
497
        # anything (the root should be unchanged).
498
        self.assertEqual(root_key, chkmap._root_node._key)
499
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
500
    def test_apply_delta_is_deterministic(self):
501
        chk_bytes = self.get_chk_bytes()
502
        chkmap1 = CHKMap(chk_bytes, None)
503
        chkmap1._root_node.set_maximum_size(10)
504
        chkmap1.apply_delta([(None, ('aaa',), 'common'),
505
                             (None, ('bba',), 'target2'),
506
                             (None, ('bbb',), 'common')])
507
        root_key1 = chkmap1._save()
508
        self.assertCanonicalForm(chkmap1)
509
510
        chkmap2 = CHKMap(chk_bytes, None)
511
        chkmap2._root_node.set_maximum_size(10)
512
        chkmap2.apply_delta([(None, ('bbb',), 'common'),
513
                             (None, ('bba',), 'target2'),
514
                             (None, ('aaa',), 'common')])
515
        root_key2 = chkmap2._save()
516
        self.assertEqualDiff(chkmap1._dump_tree(include_keys=True),
517
                             chkmap2._dump_tree(include_keys=True))
518
        self.assertEqual(root_key1, root_key2)
519
        self.assertCanonicalForm(chkmap2)
520
521
    def test_stable_splitting(self):
522
        store = self.get_chk_bytes()
523
        chkmap = CHKMap(store, None)
524
        # Should fit 2 keys per LeafNode
525
        chkmap._root_node.set_maximum_size(35)
526
        chkmap.map(('aaa',), 'v')
527
        self.assertEqualDiff("'' LeafNode\n"
528
                             "      ('aaa',) 'v'\n",
529
                             chkmap._dump_tree())
530
        chkmap.map(('aab',), 'v')
531
        self.assertEqualDiff("'' LeafNode\n"
532
                             "      ('aaa',) 'v'\n"
533
                             "      ('aab',) 'v'\n",
534
                             chkmap._dump_tree())
535
        self.assertCanonicalForm(chkmap)
536
537
        # Creates a new internal node, and splits the others into leaves
538
        chkmap.map(('aac',), 'v')
539
        self.assertEqualDiff("'' InternalNode\n"
540
                             "  'aaa' LeafNode\n"
541
                             "      ('aaa',) 'v'\n"
542
                             "  'aab' LeafNode\n"
543
                             "      ('aab',) 'v'\n"
544
                             "  'aac' LeafNode\n"
545
                             "      ('aac',) 'v'\n",
546
                             chkmap._dump_tree())
547
        self.assertCanonicalForm(chkmap)
548
549
        # Splits again, because it can't fit in the current structure
550
        chkmap.map(('bbb',), 'v')
551
        self.assertEqualDiff("'' InternalNode\n"
552
                             "  'a' InternalNode\n"
553
                             "    'aaa' LeafNode\n"
554
                             "      ('aaa',) 'v'\n"
555
                             "    'aab' LeafNode\n"
556
                             "      ('aab',) 'v'\n"
557
                             "    'aac' LeafNode\n"
558
                             "      ('aac',) 'v'\n"
559
                             "  'b' LeafNode\n"
560
                             "      ('bbb',) 'v'\n",
561
                             chkmap._dump_tree())
562
        self.assertCanonicalForm(chkmap)
563
564
    def test_map_splits_with_longer_key(self):
565
        store = self.get_chk_bytes()
566
        chkmap = CHKMap(store, None)
567
        # Should fit 1 key per LeafNode
568
        chkmap._root_node.set_maximum_size(10)
569
        chkmap.map(('aaa',), 'v')
570
        chkmap.map(('aaaa',), 'v')
571
        self.assertCanonicalForm(chkmap)
572
        self.assertIsInstance(chkmap._root_node, InternalNode)
573
574
    def test_with_linefeed_in_key(self):
575
        store = self.get_chk_bytes()
576
        chkmap = CHKMap(store, None)
577
        # Should fit 1 key per LeafNode
578
        chkmap._root_node.set_maximum_size(10)
579
        chkmap.map(('a\ra',), 'val1')
580
        chkmap.map(('a\rb',), 'val2')
581
        chkmap.map(('ac',), 'val3')
582
        self.assertCanonicalForm(chkmap)
583
        self.assertEqualDiff("'' InternalNode\n"
584
                             "  'a\\r' InternalNode\n"
585
                             "    'a\\ra' LeafNode\n"
586
                             "      ('a\\ra',) 'val1'\n"
587
                             "    'a\\rb' LeafNode\n"
588
                             "      ('a\\rb',) 'val2'\n"
589
                             "  'ac' LeafNode\n"
590
                             "      ('ac',) 'val3'\n",
591
                             chkmap._dump_tree())
592
        # We should also successfully serialise and deserialise these items
593
        root_key = chkmap._save()
594
        chkmap = CHKMap(store, root_key)
595
        self.assertEqualDiff("'' InternalNode\n"
596
                             "  'a\\r' InternalNode\n"
597
                             "    'a\\ra' LeafNode\n"
598
                             "      ('a\\ra',) 'val1'\n"
599
                             "    'a\\rb' LeafNode\n"
600
                             "      ('a\\rb',) 'val2'\n"
601
                             "  'ac' LeafNode\n"
602
                             "      ('ac',) 'val3'\n",
603
                             chkmap._dump_tree())
604
605
    def test_deep_splitting(self):
606
        store = self.get_chk_bytes()
607
        chkmap = CHKMap(store, None)
608
        # Should fit 2 keys per LeafNode
609
        chkmap._root_node.set_maximum_size(40)
610
        chkmap.map(('aaaaaaaa',), 'v')
611
        chkmap.map(('aaaaabaa',), 'v')
612
        self.assertEqualDiff("'' LeafNode\n"
613
                             "      ('aaaaaaaa',) 'v'\n"
614
                             "      ('aaaaabaa',) 'v'\n",
615
                             chkmap._dump_tree())
616
        chkmap.map(('aaabaaaa',), 'v')
617
        chkmap.map(('aaababaa',), 'v')
618
        self.assertEqualDiff("'' InternalNode\n"
619
                             "  'aaaa' LeafNode\n"
620
                             "      ('aaaaaaaa',) 'v'\n"
621
                             "      ('aaaaabaa',) 'v'\n"
622
                             "  'aaab' LeafNode\n"
623
                             "      ('aaabaaaa',) 'v'\n"
624
                             "      ('aaababaa',) 'v'\n",
625
                             chkmap._dump_tree())
626
        chkmap.map(('aaabacaa',), 'v')
627
        chkmap.map(('aaabadaa',), 'v')
628
        self.assertEqualDiff("'' InternalNode\n"
629
                             "  'aaaa' LeafNode\n"
630
                             "      ('aaaaaaaa',) 'v'\n"
631
                             "      ('aaaaabaa',) 'v'\n"
632
                             "  'aaab' InternalNode\n"
633
                             "    'aaabaa' LeafNode\n"
634
                             "      ('aaabaaaa',) 'v'\n"
635
                             "    'aaabab' LeafNode\n"
636
                             "      ('aaababaa',) 'v'\n"
637
                             "    'aaabac' LeafNode\n"
638
                             "      ('aaabacaa',) 'v'\n"
639
                             "    'aaabad' LeafNode\n"
640
                             "      ('aaabadaa',) 'v'\n",
641
                             chkmap._dump_tree())
642
        chkmap.map(('aaababba',), 'val')
643
        chkmap.map(('aaababca',), 'val')
644
        self.assertEqualDiff("'' InternalNode\n"
645
                             "  'aaaa' LeafNode\n"
646
                             "      ('aaaaaaaa',) 'v'\n"
647
                             "      ('aaaaabaa',) 'v'\n"
648
                             "  'aaab' InternalNode\n"
649
                             "    'aaabaa' LeafNode\n"
650
                             "      ('aaabaaaa',) 'v'\n"
651
                             "    'aaabab' InternalNode\n"
652
                             "      'aaababa' LeafNode\n"
653
                             "      ('aaababaa',) 'v'\n"
654
                             "      'aaababb' LeafNode\n"
655
                             "      ('aaababba',) 'val'\n"
656
                             "      'aaababc' LeafNode\n"
657
                             "      ('aaababca',) 'val'\n"
658
                             "    'aaabac' LeafNode\n"
659
                             "      ('aaabacaa',) 'v'\n"
660
                             "    'aaabad' LeafNode\n"
661
                             "      ('aaabadaa',) 'v'\n",
662
                             chkmap._dump_tree())
663
        # Now we add a node that should fit around an existing InternalNode,
664
        # but has a slightly different key prefix, which causes a new
665
        # InternalNode split
666
        chkmap.map(('aaabDaaa',), 'v')
667
        self.assertEqualDiff("'' InternalNode\n"
668
                             "  'aaaa' LeafNode\n"
669
                             "      ('aaaaaaaa',) 'v'\n"
670
                             "      ('aaaaabaa',) 'v'\n"
671
                             "  'aaab' InternalNode\n"
672
                             "    'aaabD' LeafNode\n"
673
                             "      ('aaabDaaa',) 'v'\n"
674
                             "    'aaaba' InternalNode\n"
675
                             "      'aaabaa' LeafNode\n"
676
                             "      ('aaabaaaa',) 'v'\n"
677
                             "      'aaabab' InternalNode\n"
678
                             "        'aaababa' LeafNode\n"
679
                             "      ('aaababaa',) 'v'\n"
680
                             "        'aaababb' LeafNode\n"
681
                             "      ('aaababba',) 'val'\n"
682
                             "        'aaababc' LeafNode\n"
683
                             "      ('aaababca',) 'val'\n"
684
                             "      'aaabac' LeafNode\n"
685
                             "      ('aaabacaa',) 'v'\n"
686
                             "      'aaabad' LeafNode\n"
687
                             "      ('aaabadaa',) 'v'\n",
688
                             chkmap._dump_tree())
689
690
    def test_map_collapses_if_size_changes(self):
691
        store = self.get_chk_bytes()
692
        chkmap = CHKMap(store, None)
693
        # Should fit 2 keys per LeafNode
694
        chkmap._root_node.set_maximum_size(35)
695
        chkmap.map(('aaa',), 'v')
696
        chkmap.map(('aab',), 'very long value that splits')
697
        self.assertEqualDiff("'' InternalNode\n"
698
                             "  'aaa' LeafNode\n"
699
                             "      ('aaa',) 'v'\n"
700
                             "  'aab' LeafNode\n"
701
                             "      ('aab',) 'very long value that splits'\n",
702
                             chkmap._dump_tree())
703
        self.assertCanonicalForm(chkmap)
704
        # Now changing the value to something small should cause a rebuild
705
        chkmap.map(('aab',), 'v')
706
        self.assertEqualDiff("'' LeafNode\n"
707
                             "      ('aaa',) 'v'\n"
708
                             "      ('aab',) 'v'\n",
709
                             chkmap._dump_tree())
710
        self.assertCanonicalForm(chkmap)
711
712
    def test_map_double_deep_collapses(self):
713
        store = self.get_chk_bytes()
714
        chkmap = CHKMap(store, None)
715
        # Should fit 3 small keys per LeafNode
716
        chkmap._root_node.set_maximum_size(40)
717
        chkmap.map(('aaa',), 'v')
718
        chkmap.map(('aab',), 'very long value that splits')
719
        chkmap.map(('abc',), 'v')
720
        self.assertEqualDiff("'' InternalNode\n"
721
                             "  'aa' InternalNode\n"
722
                             "    'aaa' LeafNode\n"
723
                             "      ('aaa',) 'v'\n"
724
                             "    'aab' LeafNode\n"
725
                             "      ('aab',) 'very long value that splits'\n"
726
                             "  'ab' LeafNode\n"
727
                             "      ('abc',) 'v'\n",
728
                             chkmap._dump_tree())
729
        chkmap.map(('aab',), 'v')
730
        self.assertCanonicalForm(chkmap)
731
        self.assertEqualDiff("'' LeafNode\n"
732
                             "      ('aaa',) 'v'\n"
733
                             "      ('aab',) 'v'\n"
734
                             "      ('abc',) 'v'\n",
735
                             chkmap._dump_tree())
736
737
    def test_stable_unmap(self):
738
        store = self.get_chk_bytes()
739
        chkmap = CHKMap(store, None)
740
        # Should fit 2 keys per LeafNode
741
        chkmap._root_node.set_maximum_size(35)
742
        chkmap.map(('aaa',), 'v')
743
        chkmap.map(('aab',), 'v')
744
        self.assertEqualDiff("'' LeafNode\n"
745
                             "      ('aaa',) 'v'\n"
746
                             "      ('aab',) 'v'\n",
747
                             chkmap._dump_tree())
748
        # Creates a new internal node, and splits the others into leaves
749
        chkmap.map(('aac',), 'v')
750
        self.assertEqualDiff("'' InternalNode\n"
751
                             "  'aaa' LeafNode\n"
752
                             "      ('aaa',) 'v'\n"
753
                             "  'aab' LeafNode\n"
754
                             "      ('aab',) 'v'\n"
755
                             "  'aac' LeafNode\n"
756
                             "      ('aac',) 'v'\n",
757
                             chkmap._dump_tree())
758
        self.assertCanonicalForm(chkmap)
759
        # Now lets unmap one of the keys, and assert that we collapse the
760
        # structures.
761
        chkmap.unmap(('aac',))
762
        self.assertEqualDiff("'' LeafNode\n"
763
                             "      ('aaa',) 'v'\n"
764
                             "      ('aab',) 'v'\n",
765
                             chkmap._dump_tree())
766
        self.assertCanonicalForm(chkmap)
767
768
    def test_unmap_double_deep(self):
769
        store = self.get_chk_bytes()
770
        chkmap = CHKMap(store, None)
771
        # Should fit 3 keys per LeafNode
772
        chkmap._root_node.set_maximum_size(40)
773
        chkmap.map(('aaa',), 'v')
774
        chkmap.map(('aaab',), 'v')
775
        chkmap.map(('aab',), 'very long value')
776
        chkmap.map(('abc',), 'v')
777
        self.assertEqualDiff("'' InternalNode\n"
778
                             "  'aa' InternalNode\n"
779
                             "    'aaa' LeafNode\n"
780
                             "      ('aaa',) 'v'\n"
781
                             "      ('aaab',) 'v'\n"
782
                             "    'aab' LeafNode\n"
783
                             "      ('aab',) 'very long value'\n"
784
                             "  'ab' LeafNode\n"
785
                             "      ('abc',) 'v'\n",
786
                             chkmap._dump_tree())
787
        # Removing the 'aab' key should cause everything to collapse back to a
788
        # single node
789
        chkmap.unmap(('aab',))
790
        self.assertEqualDiff("'' LeafNode\n"
791
                             "      ('aaa',) 'v'\n"
792
                             "      ('aaab',) 'v'\n"
793
                             "      ('abc',) 'v'\n",
794
                             chkmap._dump_tree())
795
796
    def test_unmap_double_deep_non_empty_leaf(self):
797
        store = self.get_chk_bytes()
798
        chkmap = CHKMap(store, None)
799
        # Should fit 3 keys per LeafNode
800
        chkmap._root_node.set_maximum_size(40)
801
        chkmap.map(('aaa',), 'v')
802
        chkmap.map(('aab',), 'long value')
803
        chkmap.map(('aabb',), 'v')
804
        chkmap.map(('abc',), 'v')
805
        self.assertEqualDiff("'' InternalNode\n"
806
                             "  'aa' InternalNode\n"
807
                             "    'aaa' LeafNode\n"
808
                             "      ('aaa',) 'v'\n"
809
                             "    'aab' LeafNode\n"
810
                             "      ('aab',) 'long value'\n"
811
                             "      ('aabb',) 'v'\n"
812
                             "  'ab' LeafNode\n"
813
                             "      ('abc',) 'v'\n",
814
                             chkmap._dump_tree())
815
        # Removing the 'aab' key should cause everything to collapse back to a
816
        # single node
817
        chkmap.unmap(('aab',))
818
        self.assertEqualDiff("'' LeafNode\n"
819
                             "      ('aaa',) 'v'\n"
820
                             "      ('aabb',) 'v'\n"
821
                             "      ('abc',) 'v'\n",
822
                             chkmap._dump_tree())
823
824
    def test_unmap_with_known_internal_node_doesnt_page(self):
825
        store = self.get_chk_bytes()
826
        chkmap = CHKMap(store, None)
827
        # Should fit 3 keys per LeafNode
828
        chkmap._root_node.set_maximum_size(30)
829
        chkmap.map(('aaa',), 'v')
830
        chkmap.map(('aab',), 'v')
831
        chkmap.map(('aac',), 'v')
832
        chkmap.map(('abc',), 'v')
833
        chkmap.map(('acd',), 'v')
834
        self.assertEqualDiff("'' InternalNode\n"
835
                             "  'aa' InternalNode\n"
836
                             "    'aaa' LeafNode\n"
837
                             "      ('aaa',) 'v'\n"
838
                             "    'aab' LeafNode\n"
839
                             "      ('aab',) 'v'\n"
840
                             "    'aac' LeafNode\n"
841
                             "      ('aac',) 'v'\n"
842
                             "  'ab' LeafNode\n"
843
                             "      ('abc',) 'v'\n"
844
                             "  'ac' LeafNode\n"
845
                             "      ('acd',) 'v'\n",
846
                             chkmap._dump_tree())
847
        # Save everything to the map, and start over
848
        chkmap = CHKMap(store, chkmap._save())
849
        # Mapping an 'aa' key loads the internal node, but should not map the
850
        # 'ab' and 'ac' nodes
851
        chkmap.map(('aad',), 'v')
852
        self.assertIsInstance(chkmap._root_node._items['aa'], InternalNode)
853
        self.assertIsInstance(chkmap._root_node._items['ab'], tuple)
854
        self.assertIsInstance(chkmap._root_node._items['ac'], tuple)
855
        # Unmapping 'acd' can notice that 'aa' is an InternalNode and not have
856
        # to map in 'ab'
857
        chkmap.unmap(('acd',))
858
        self.assertIsInstance(chkmap._root_node._items['aa'], InternalNode)
859
        self.assertIsInstance(chkmap._root_node._items['ab'], tuple)
860
861
    def test_unmap_without_fitting_doesnt_page_in(self):
862
        store = self.get_chk_bytes()
863
        chkmap = CHKMap(store, None)
864
        # Should fit 2 keys per LeafNode
865
        chkmap._root_node.set_maximum_size(20)
866
        chkmap.map(('aaa',), 'v')
867
        chkmap.map(('aab',), 'v')
868
        self.assertEqualDiff("'' InternalNode\n"
869
                             "  'aaa' LeafNode\n"
870
                             "      ('aaa',) 'v'\n"
871
                             "  'aab' LeafNode\n"
872
                             "      ('aab',) 'v'\n",
873
                             chkmap._dump_tree())
874
        # Save everything to the map, and start over
875
        chkmap = CHKMap(store, chkmap._save())
876
        chkmap.map(('aac',), 'v')
877
        chkmap.map(('aad',), 'v')
878
        chkmap.map(('aae',), 'v')
879
        chkmap.map(('aaf',), 'v')
880
        # At this point, the previous nodes should not be paged in, but the
881
        # newly added nodes would be
882
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
883
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
884
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
885
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
886
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
887
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
888
        # Now unmapping one of the new nodes will use only the already-paged-in
889
        # nodes to determine that we don't need to do more.
890
        chkmap.unmap(('aaf',))
891
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
892
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
893
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
894
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
895
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
896
897
    def test_unmap_pages_in_if_necessary(self):
898
        store = self.get_chk_bytes()
899
        chkmap = CHKMap(store, None)
900
        # Should fit 2 keys per LeafNode
901
        chkmap._root_node.set_maximum_size(30)
902
        chkmap.map(('aaa',), 'val')
903
        chkmap.map(('aab',), 'val')
904
        chkmap.map(('aac',), 'val')
905
        self.assertEqualDiff("'' InternalNode\n"
906
                             "  'aaa' LeafNode\n"
907
                             "      ('aaa',) 'val'\n"
908
                             "  'aab' LeafNode\n"
909
                             "      ('aab',) 'val'\n"
910
                             "  'aac' LeafNode\n"
911
                             "      ('aac',) 'val'\n",
912
                             chkmap._dump_tree())
913
        root_key = chkmap._save()
914
        # Save everything to the map, and start over
915
        chkmap = CHKMap(store, root_key)
916
        chkmap.map(('aad',), 'v')
917
        # At this point, the previous nodes should not be paged in, but the
918
        # newly added node would be
919
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
920
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
921
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
922
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
923
        # Unmapping the new node will check the existing nodes to see if they
924
        # would fit.
925
        # Clear the page cache so we ensure we have to read all the children
926
        chk_map._page_cache.clear()
927
        chkmap.unmap(('aad',))
928
        self.assertIsInstance(chkmap._root_node._items['aaa'], LeafNode)
929
        self.assertIsInstance(chkmap._root_node._items['aab'], LeafNode)
930
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
931
932
    def test_unmap_pages_in_from_page_cache(self):
933
        store = self.get_chk_bytes()
934
        chkmap = CHKMap(store, None)
935
        # Should fit 2 keys per LeafNode
936
        chkmap._root_node.set_maximum_size(30)
937
        chkmap.map(('aaa',), 'val')
938
        chkmap.map(('aab',), 'val')
939
        chkmap.map(('aac',), 'val')
940
        root_key = chkmap._save()
941
        # Save everything to the map, and start over
942
        chkmap = CHKMap(store, root_key)
943
        chkmap.map(('aad',), 'val')
944
        self.assertEqualDiff("'' InternalNode\n"
945
                             "  'aaa' LeafNode\n"
946
                             "      ('aaa',) 'val'\n"
947
                             "  'aab' LeafNode\n"
948
                             "      ('aab',) 'val'\n"
949
                             "  'aac' LeafNode\n"
950
                             "      ('aac',) 'val'\n"
951
                             "  'aad' LeafNode\n"
952
                             "      ('aad',) 'val'\n",
953
                             chkmap._dump_tree())
954
        # Save everything to the map, start over after _dump_tree
955
        chkmap = CHKMap(store, root_key)
956
        chkmap.map(('aad',), 'v')
957
        # At this point, the previous nodes should not be paged in, but the
958
        # newly added node would be
959
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
960
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
961
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
962
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
963
        # Now clear the page cache, and only include 2 of the children in the
964
        # cache
965
        aab_key = chkmap._root_node._items['aab']
966
        aab_bytes = chk_map._page_cache[aab_key]
967
        aac_key = chkmap._root_node._items['aac']
968
        aac_bytes = chk_map._page_cache[aac_key]
969
        chk_map._page_cache.clear()
970
        chk_map._page_cache[aab_key] = aab_bytes
971
        chk_map._page_cache[aac_key] = aac_bytes
972
973
        # Unmapping the new node will check the nodes from the page cache
974
        # first, and not have to read in 'aaa'
975
        chkmap.unmap(('aad',))
976
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
977
        self.assertIsInstance(chkmap._root_node._items['aab'], LeafNode)
978
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
979
980
    def test_unmap_uses_existing_items(self):
981
        store = self.get_chk_bytes()
982
        chkmap = CHKMap(store, None)
983
        # Should fit 2 keys per LeafNode
984
        chkmap._root_node.set_maximum_size(30)
985
        chkmap.map(('aaa',), 'val')
986
        chkmap.map(('aab',), 'val')
987
        chkmap.map(('aac',), 'val')
988
        root_key = chkmap._save()
989
        # Save everything to the map, and start over
990
        chkmap = CHKMap(store, root_key)
991
        chkmap.map(('aad',), 'val')
992
        chkmap.map(('aae',), 'val')
993
        chkmap.map(('aaf',), 'val')
994
        # At this point, the previous nodes should not be paged in, but the
995
        # newly added node would be
996
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
997
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
998
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
999
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
1000
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
1001
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
1002
1003
        # Unmapping a new node will see the other nodes that are already in
1004
        # memory, and not need to page in anything else
1005
        chkmap.unmap(('aad',))
1006
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
1007
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
1008
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
1009
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
1010
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
1011
1012
    def test_iter_changes_empty_ab(self):
1013
        # Asking for changes between an empty dict to a dict with keys returns
1014
        # all the keys.
1015
        basis = self._get_map({}, maximum_size=10)
1016
        target = self._get_map(
1017
            {('a',): 'content here', ('b',): 'more content'},
1018
            chk_bytes=basis._store, maximum_size=10)
1019
        self.assertEqual([(('a',), None, 'content here'),
1020
            (('b',), None, 'more content')],
1021
            sorted(list(target.iter_changes(basis))))
1022
1023
    def test_iter_changes_ab_empty(self):
1024
        # Asking for changes between a dict with keys to an empty dict returns
1025
        # all the keys.
1026
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
1027
            maximum_size=10)
1028
        target = self._get_map({}, chk_bytes=basis._store, maximum_size=10)
1029
        self.assertEqual([(('a',), 'content here', None),
1030
            (('b',), 'more content', None)],
1031
            sorted(list(target.iter_changes(basis))))
1032
1033
    def test_iter_changes_empty_empty_is_empty(self):
1034
        basis = self._get_map({}, maximum_size=10)
1035
        target = self._get_map({}, chk_bytes=basis._store, maximum_size=10)
1036
        self.assertEqual([], sorted(list(target.iter_changes(basis))))
1037
1038
    def test_iter_changes_ab_ab_is_empty(self):
1039
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
1040
            maximum_size=10)
1041
        target = self._get_map(
1042
            {('a',): 'content here', ('b',): 'more content'},
1043
            chk_bytes=basis._store, maximum_size=10)
1044
        self.assertEqual([], sorted(list(target.iter_changes(basis))))
1045
1046
    def test_iter_changes_ab_ab_nodes_not_loaded(self):
1047
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
1048
            maximum_size=10)
1049
        target = self._get_map(
1050
            {('a',): 'content here', ('b',): 'more content'},
1051
            chk_bytes=basis._store, maximum_size=10)
1052
        list(target.iter_changes(basis))
1053
        self.assertIsInstance(target._root_node, tuple)
1054
        self.assertIsInstance(basis._root_node, tuple)
1055
1056
    def test_iter_changes_ab_ab_changed_values_shown(self):
1057
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
1058
            maximum_size=10)
1059
        target = self._get_map(
1060
            {('a',): 'content here', ('b',): 'different content'},
1061
            chk_bytes=basis._store, maximum_size=10)
1062
        result = sorted(list(target.iter_changes(basis)))
1063
        self.assertEqual([(('b',), 'more content', 'different content')],
1064
            result)
1065
1066
    def test_iter_changes_mixed_node_length(self):
1067
        # When one side has different node lengths than the other, common
1068
        # but different keys still need to be show, and new-and-old included
1069
        # appropriately.
1070
        # aaa - common unaltered
1071
        # aab - common altered
1072
        # b - basis only
1073
        # at - target only
1074
        # we expect:
1075
        # aaa to be not loaded (later test)
1076
        # aab, b, at to be returned.
1077
        # basis splits at byte 0,1,2, aaa is commonb is basis only
1078
        basis_dict = {('aaa',): 'foo bar',
1079
            ('aab',): 'common altered a', ('b',): 'foo bar b'}
1080
        # target splits at byte 1,2, at is target only
1081
        target_dict = {('aaa',): 'foo bar',
1082
            ('aab',): 'common altered b', ('at',): 'foo bar t'}
1083
        changes = [
1084
            (('aab',), 'common altered a', 'common altered b'),
1085
            (('at',), None, 'foo bar t'),
1086
            (('b',), 'foo bar b', None),
1087
            ]
1088
        basis = self._get_map(basis_dict, maximum_size=10)
1089
        target = self._get_map(target_dict, maximum_size=10,
1090
            chk_bytes=basis._store)
1091
        self.assertEqual(changes, sorted(list(target.iter_changes(basis))))
1092
1093
    def test_iter_changes_common_pages_not_loaded(self):
1094
        # aaa - common unaltered
1095
        # aab - common altered
1096
        # b - basis only
1097
        # at - target only
1098
        # we expect:
1099
        # aaa to be not loaded
1100
        # aaa not to be in result.
1101
        basis_dict = {('aaa',): 'foo bar',
1102
            ('aab',): 'common altered a', ('b',): 'foo bar b'}
1103
        # target splits at byte 1, at is target only
1104
        target_dict = {('aaa',): 'foo bar',
1105
            ('aab',): 'common altered b', ('at',): 'foo bar t'}
1106
        basis = self._get_map(basis_dict, maximum_size=10)
1107
        target = self._get_map(target_dict, maximum_size=10,
1108
            chk_bytes=basis._store)
1109
        basis_get = basis._store.get_record_stream
1110
        def get_record_stream(keys, order, fulltext):
1111
            if ('sha1:1adf7c0d1b9140ab5f33bb64c6275fa78b1580b7',) in keys:
1112
                self.fail("'aaa' pointer was followed %r" % keys)
1113
            return basis_get(keys, order, fulltext)
1114
        basis._store.get_record_stream = get_record_stream
1115
        result = sorted(list(target.iter_changes(basis)))
1116
        for change in result:
1117
            if change[0] == ('aaa',):
1118
                self.fail("Found unexpected change: %s" % change)
1119
1120
    def test_iter_changes_unchanged_keys_in_multi_key_leafs_ignored(self):
1121
        # Within a leaf there are no hash's to exclude keys, make sure multi
1122
        # value leaf nodes are handled well.
1123
        basis_dict = {('aaa',): 'foo bar',
1124
            ('aab',): 'common altered a', ('b',): 'foo bar b'}
1125
        target_dict = {('aaa',): 'foo bar',
1126
            ('aab',): 'common altered b', ('at',): 'foo bar t'}
1127
        changes = [
1128
            (('aab',), 'common altered a', 'common altered b'),
1129
            (('at',), None, 'foo bar t'),
1130
            (('b',), 'foo bar b', None),
1131
            ]
1132
        basis = self._get_map(basis_dict)
1133
        target = self._get_map(target_dict, chk_bytes=basis._store)
1134
        self.assertEqual(changes, sorted(list(target.iter_changes(basis))))
1135
1136
    def test_iteritems_empty(self):
1137
        chk_bytes = self.get_chk_bytes()
1138
        root_key = CHKMap.from_dict(chk_bytes, {})
1139
        chkmap = CHKMap(chk_bytes, root_key)
1140
        self.assertEqual([], list(chkmap.iteritems()))
1141
1142
    def test_iteritems_two_items(self):
1143
        chk_bytes = self.get_chk_bytes()
1144
        root_key = CHKMap.from_dict(chk_bytes,
1145
            {"a":"content here", "b":"more content"})
1146
        chkmap = CHKMap(chk_bytes, root_key)
1147
        self.assertEqual([(("a",), "content here"), (("b",), "more content")],
1148
            sorted(list(chkmap.iteritems())))
1149
1150
    def test_iteritems_selected_one_of_two_items(self):
1151
        chkmap = self._get_map( {("a",):"content here", ("b",):"more content"})
1152
        self.assertEqual({("a",): "content here"},
1153
            self.to_dict(chkmap, [("a",)]))
1154
1155
    def test_iteritems_keys_prefixed_by_2_width_nodes(self):
1156
        chkmap = self._get_map(
1157
            {("a","a"):"content here", ("a", "b",):"more content",
1158
             ("b", ""): 'boring content'},
1159
            maximum_size=10, key_width=2)
1160
        self.assertEqual(
1161
            {("a", "a"): "content here", ("a", "b"): 'more content'},
1162
            self.to_dict(chkmap, [("a",)]))
1163
1164
    def test_iteritems_keys_prefixed_by_2_width_nodes_hashed(self):
1165
        search_key_func = chk_map.search_key_registry.get('hash-16-way')
1166
        self.assertEqual('E8B7BE43\x00E8B7BE43', search_key_func(('a', 'a')))
1167
        self.assertEqual('E8B7BE43\x0071BEEFF9', search_key_func(('a', 'b')))
1168
        self.assertEqual('71BEEFF9\x0000000000', search_key_func(('b', '')))
1169
        chkmap = self._get_map(
1170
            {("a","a"):"content here", ("a", "b",):"more content",
1171
             ("b", ""): 'boring content'},
1172
            maximum_size=10, key_width=2, search_key_func=search_key_func)
1173
        self.assertEqual(
1174
            {("a", "a"): "content here", ("a", "b"): 'more content'},
1175
            self.to_dict(chkmap, [("a",)]))
1176
1177
    def test_iteritems_keys_prefixed_by_2_width_one_leaf(self):
1178
        chkmap = self._get_map(
1179
            {("a","a"):"content here", ("a", "b",):"more content",
1180
             ("b", ""): 'boring content'}, key_width=2)
1181
        self.assertEqual(
1182
            {("a", "a"): "content here", ("a", "b"): 'more content'},
1183
            self.to_dict(chkmap, [("a",)]))
1184
1185
    def test___len__empty(self):
1186
        chkmap = self._get_map({})
1187
        self.assertEqual(0, len(chkmap))
1188
1189
    def test___len__2(self):
1190
        chkmap = self._get_map({("foo",):"bar", ("gam",):"quux"})
1191
        self.assertEqual(2, len(chkmap))
1192
1193
    def test_max_size_100_bytes_new(self):
1194
        # When there is a 100 byte upper node limit, a tree is formed.
1195
        chkmap = self._get_map({("k1"*50,):"v1", ("k2"*50,):"v2"}, maximum_size=100)
1196
        # We expect three nodes:
1197
        # A root, with two children, and with two key prefixes - k1 to one, and
1198
        # k2 to the other as our node splitting is only just being developed.
1199
        # The maximum size should be embedded
1200
        chkmap._ensure_root()
1201
        self.assertEqual(100, chkmap._root_node.maximum_size)
1202
        self.assertEqual(1, chkmap._root_node._key_width)
1203
        # There should be two child nodes, and prefix of 2(bytes):
1204
        self.assertEqual(2, len(chkmap._root_node._items))
1205
        self.assertEqual("k", chkmap._root_node._compute_search_prefix())
1206
        # The actual nodes pointed at will change as serialisers change; so
1207
        # here we test that the key prefix is correct; then load the nodes and
1208
        # check they have the right pointed at key; whether they have the
1209
        # pointed at value inline or not is also unrelated to this test so we
1210
        # don't check that in detail - rather we just check the aggregate
1211
        # value.
1212
        nodes = sorted(chkmap._root_node._items.items())
1213
        ptr1 = nodes[0]
1214
        ptr2 = nodes[1]
1215
        self.assertEqual('k1', ptr1[0])
1216
        self.assertEqual('k2', ptr2[0])
1217
        node1 = chk_map._deserialise(chkmap._read_bytes(ptr1[1]), ptr1[1], None)
1218
        self.assertIsInstance(node1, LeafNode)
1219
        self.assertEqual(1, len(node1))
1220
        self.assertEqual({('k1'*50,): 'v1'}, self.to_dict(node1, chkmap._store))
1221
        node2 = chk_map._deserialise(chkmap._read_bytes(ptr2[1]), ptr2[1], None)
1222
        self.assertIsInstance(node2, LeafNode)
1223
        self.assertEqual(1, len(node2))
1224
        self.assertEqual({('k2'*50,): 'v2'}, self.to_dict(node2, chkmap._store))
1225
        # Having checked we have a good structure, check that the content is
1226
        # still accessible.
1227
        self.assertEqual(2, len(chkmap))
1228
        self.assertEqual({("k1"*50,): "v1", ("k2"*50,): "v2"},
1229
            self.to_dict(chkmap))
1230
1231
    def test_init_root_is_LeafNode_new(self):
1232
        chk_bytes = self.get_chk_bytes()
1233
        chkmap = CHKMap(chk_bytes, None)
1234
        self.assertIsInstance(chkmap._root_node, LeafNode)
1235
        self.assertEqual({}, self.to_dict(chkmap))
1236
        self.assertEqual(0, len(chkmap))
1237
1238
    def test_init_and_save_new(self):
1239
        chk_bytes = self.get_chk_bytes()
1240
        chkmap = CHKMap(chk_bytes, None)
1241
        key = chkmap._save()
1242
        leaf_node = LeafNode()
1243
        self.assertEqual([key], leaf_node.serialise(chk_bytes))
1244
1245
    def test_map_first_item_new(self):
1246
        chk_bytes = self.get_chk_bytes()
1247
        chkmap = CHKMap(chk_bytes, None)
1248
        chkmap.map(("foo,",), "bar")
1249
        self.assertEqual({('foo,',): 'bar'}, self.to_dict(chkmap))
1250
        self.assertEqual(1, len(chkmap))
1251
        key = chkmap._save()
1252
        leaf_node = LeafNode()
1253
        leaf_node.map(chk_bytes, ("foo,",), "bar")
1254
        self.assertEqual([key], leaf_node.serialise(chk_bytes))
1255
1256
    def test_unmap_last_item_root_is_leaf_new(self):
1257
        chkmap = self._get_map({("k1"*50,): "v1", ("k2"*50,): "v2"})
1258
        chkmap.unmap(("k1"*50,))
1259
        chkmap.unmap(("k2"*50,))
1260
        self.assertEqual(0, len(chkmap))
1261
        self.assertEqual({}, self.to_dict(chkmap))
1262
        key = chkmap._save()
1263
        leaf_node = LeafNode()
1264
        self.assertEqual([key], leaf_node.serialise(chkmap._store))
1265
1266
    def test__dump_tree(self):
1267
        chkmap = self._get_map({("aaa",): "value1", ("aab",): "value2",
1268
                                ("bbb",): "value3",},
1269
                               maximum_size=15)
1270
        self.assertEqualDiff("'' InternalNode\n"
1271
                             "  'a' InternalNode\n"
1272
                             "    'aaa' LeafNode\n"
1273
                             "      ('aaa',) 'value1'\n"
1274
                             "    'aab' LeafNode\n"
1275
                             "      ('aab',) 'value2'\n"
1276
                             "  'b' LeafNode\n"
1277
                             "      ('bbb',) 'value3'\n",
1278
                             chkmap._dump_tree())
1279
        self.assertEqualDiff("'' InternalNode\n"
1280
                             "  'a' InternalNode\n"
1281
                             "    'aaa' LeafNode\n"
1282
                             "      ('aaa',) 'value1'\n"
1283
                             "    'aab' LeafNode\n"
1284
                             "      ('aab',) 'value2'\n"
1285
                             "  'b' LeafNode\n"
1286
                             "      ('bbb',) 'value3'\n",
1287
                             chkmap._dump_tree())
1288
        self.assertEqualDiff(
1289
            "'' InternalNode sha1:0690d471eb0a624f359797d0ee4672bd68f4e236\n"
1290
            "  'a' InternalNode sha1:1514c35503da9418d8fd90c1bed553077cb53673\n"
1291
            "    'aaa' LeafNode sha1:4cc5970454d40b4ce297a7f13ddb76f63b88fefb\n"
1292
            "      ('aaa',) 'value1'\n"
1293
            "    'aab' LeafNode sha1:1d68bc90914ef8a3edbcc8bb28b00cb4fea4b5e2\n"
1294
            "      ('aab',) 'value2'\n"
1295
            "  'b' LeafNode sha1:3686831435b5596515353364eab0399dc45d49e7\n"
1296
            "      ('bbb',) 'value3'\n",
1297
            chkmap._dump_tree(include_keys=True))
1298
1299
    def test__dump_tree_in_progress(self):
1300
        chkmap = self._get_map({("aaa",): "value1", ("aab",): "value2"},
1301
                               maximum_size=10)
1302
        chkmap.map(('bbb',), 'value3')
1303
        self.assertEqualDiff("'' InternalNode\n"
1304
                             "  'a' InternalNode\n"
1305
                             "    'aaa' LeafNode\n"
1306
                             "      ('aaa',) 'value1'\n"
1307
                             "    'aab' LeafNode\n"
1308
                             "      ('aab',) 'value2'\n"
1309
                             "  'b' LeafNode\n"
1310
                             "      ('bbb',) 'value3'\n",
1311
                             chkmap._dump_tree())
1312
        # For things that are updated by adding 'bbb', we don't have a sha key
1313
        # for them yet, so they are listed as None
1314
        self.assertEqualDiff(
1315
            "'' InternalNode None\n"
1316
            "  'a' InternalNode sha1:6b0d881dd739a66f733c178b24da64395edfaafd\n"
1317
            "    'aaa' LeafNode sha1:40b39a08d895babce17b20ae5f62d187eaa4f63a\n"
1318
            "      ('aaa',) 'value1'\n"
1319
            "    'aab' LeafNode sha1:ad1dc7c4e801302c95bf1ba7b20bc45e548cd51a\n"
1320
            "      ('aab',) 'value2'\n"
1321
            "  'b' LeafNode None\n"
1322
            "      ('bbb',) 'value3'\n",
1323
            chkmap._dump_tree(include_keys=True))
1324
1325
1326
def _search_key_single(key):
1327
    """A search key function that maps all nodes to the same value"""
1328
    return 'value'
1329
1330
def _test_search_key(key):
1331
    return 'test:' + '\x00'.join(key)
1332
1333
1334
class TestMapSearchKeys(TestCaseWithStore):
1335
1336
    def test_default_chk_map_uses_flat_search_key(self):
1337
        chkmap = chk_map.CHKMap(self.get_chk_bytes(), None)
1338
        self.assertEqual('1',
1339
                         chkmap._search_key_func(('1',)))
1340
        self.assertEqual('1\x002',
1341
                         chkmap._search_key_func(('1', '2')))
1342
        self.assertEqual('1\x002\x003',
1343
                         chkmap._search_key_func(('1', '2', '3')))
1344
1345
    def test_search_key_is_passed_to_root_node(self):
1346
        chkmap = chk_map.CHKMap(self.get_chk_bytes(), None,
1347
                                search_key_func=_test_search_key)
1348
        self.assertIs(_test_search_key, chkmap._search_key_func)
1349
        self.assertEqual('test:1\x002\x003',
1350
                         chkmap._search_key_func(('1', '2', '3')))
1351
        self.assertEqual('test:1\x002\x003',
1352
                         chkmap._root_node._search_key(('1', '2', '3')))
1353
1354
    def test_search_key_passed_via__ensure_root(self):
1355
        chk_bytes = self.get_chk_bytes()
1356
        chkmap = chk_map.CHKMap(chk_bytes, None,
1357
                                search_key_func=_test_search_key)
1358
        root_key = chkmap._save()
1359
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1360
                                search_key_func=_test_search_key)
1361
        chkmap._ensure_root()
1362
        self.assertEqual('test:1\x002\x003',
1363
                         chkmap._root_node._search_key(('1', '2', '3')))
1364
1365
    def test_search_key_with_internal_node(self):
1366
        chk_bytes = self.get_chk_bytes()
1367
        chkmap = chk_map.CHKMap(chk_bytes, None,
1368
                                search_key_func=_test_search_key)
1369
        chkmap._root_node.set_maximum_size(10)
1370
        chkmap.map(('1',), 'foo')
1371
        chkmap.map(('2',), 'bar')
1372
        chkmap.map(('3',), 'baz')
1373
        self.assertEqualDiff("'' InternalNode\n"
1374
                             "  'test:1' LeafNode\n"
1375
                             "      ('1',) 'foo'\n"
1376
                             "  'test:2' LeafNode\n"
1377
                             "      ('2',) 'bar'\n"
1378
                             "  'test:3' LeafNode\n"
1379
                             "      ('3',) 'baz'\n"
1380
                             , chkmap._dump_tree())
1381
        root_key = chkmap._save()
1382
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1383
                                search_key_func=_test_search_key)
1384
        self.assertEqualDiff("'' InternalNode\n"
1385
                             "  'test:1' LeafNode\n"
1386
                             "      ('1',) 'foo'\n"
1387
                             "  'test:2' LeafNode\n"
1388
                             "      ('2',) 'bar'\n"
1389
                             "  'test:3' LeafNode\n"
1390
                             "      ('3',) 'baz'\n"
1391
                             , chkmap._dump_tree())
1392
1393
    def test_search_key_16(self):
1394
        chk_bytes = self.get_chk_bytes()
1395
        chkmap = chk_map.CHKMap(chk_bytes, None,
1396
                                search_key_func=chk_map._search_key_16)
1397
        chkmap._root_node.set_maximum_size(10)
1398
        chkmap.map(('1',), 'foo')
1399
        chkmap.map(('2',), 'bar')
1400
        chkmap.map(('3',), 'baz')
1401
        self.assertEqualDiff("'' InternalNode\n"
1402
                             "  '1' LeafNode\n"
1403
                             "      ('2',) 'bar'\n"
1404
                             "  '6' LeafNode\n"
1405
                             "      ('3',) 'baz'\n"
1406
                             "  '8' LeafNode\n"
1407
                             "      ('1',) 'foo'\n"
1408
                             , chkmap._dump_tree())
1409
        root_key = chkmap._save()
1410
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1411
                                search_key_func=chk_map._search_key_16)
1412
        # We can get the values back correctly
1413
        self.assertEqual([(('1',), 'foo')],
1414
                         list(chkmap.iteritems([('1',)])))
1415
        self.assertEqualDiff("'' InternalNode\n"
1416
                             "  '1' LeafNode\n"
1417
                             "      ('2',) 'bar'\n"
1418
                             "  '6' LeafNode\n"
1419
                             "      ('3',) 'baz'\n"
1420
                             "  '8' LeafNode\n"
1421
                             "      ('1',) 'foo'\n"
1422
                             , chkmap._dump_tree())
1423
1424
    def test_search_key_255(self):
1425
        chk_bytes = self.get_chk_bytes()
1426
        chkmap = chk_map.CHKMap(chk_bytes, None,
1427
                                search_key_func=chk_map._search_key_255)
1428
        chkmap._root_node.set_maximum_size(10)
1429
        chkmap.map(('1',), 'foo')
1430
        chkmap.map(('2',), 'bar')
1431
        chkmap.map(('3',), 'baz')
1432
        self.assertEqualDiff("'' InternalNode\n"
1433
                             "  '\\x1a' LeafNode\n"
1434
                             "      ('2',) 'bar'\n"
1435
                             "  'm' LeafNode\n"
1436
                             "      ('3',) 'baz'\n"
1437
                             "  '\\x83' LeafNode\n"
1438
                             "      ('1',) 'foo'\n"
1439
                             , chkmap._dump_tree())
1440
        root_key = chkmap._save()
1441
        chkmap = chk_map.CHKMap(chk_bytes, root_key,
1442
                                search_key_func=chk_map._search_key_255)
1443
        # We can get the values back correctly
1444
        self.assertEqual([(('1',), 'foo')],
1445
                         list(chkmap.iteritems([('1',)])))
1446
        self.assertEqualDiff("'' InternalNode\n"
1447
                             "  '\\x1a' LeafNode\n"
1448
                             "      ('2',) 'bar'\n"
1449
                             "  'm' LeafNode\n"
1450
                             "      ('3',) 'baz'\n"
1451
                             "  '\\x83' LeafNode\n"
1452
                             "      ('1',) 'foo'\n"
1453
                             , chkmap._dump_tree())
1454
1455
    def test_search_key_collisions(self):
1456
        chkmap = chk_map.CHKMap(self.get_chk_bytes(), None,
1457
                                search_key_func=_search_key_single)
1458
        # The node will want to expand, but it cannot, because it knows that
1459
        # all the keys must map to this node
1460
        chkmap._root_node.set_maximum_size(20)
1461
        chkmap.map(('1',), 'foo')
1462
        chkmap.map(('2',), 'bar')
1463
        chkmap.map(('3',), 'baz')
1464
        self.assertEqualDiff("'' LeafNode\n"
1465
                             "      ('1',) 'foo'\n"
1466
                             "      ('2',) 'bar'\n"
1467
                             "      ('3',) 'baz'\n"
1468
                             , chkmap._dump_tree())
1469
1470
1471
class TestSearchKeyFuncs(tests.TestCase):
1472
1473
    def assertSearchKey16(self, expected, key):
1474
        self.assertEqual(expected, chk_map._search_key_16(key))
1475
1476
    def assertSearchKey255(self, expected, key):
1477
        actual = chk_map._search_key_255(key)
1478
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
1479
1480
    def test_simple_16(self):
1481
        self.assertSearchKey16('8C736521', ('foo',))
1482
        self.assertSearchKey16('8C736521\x008C736521', ('foo', 'foo'))
1483
        self.assertSearchKey16('8C736521\x0076FF8CAA', ('foo', 'bar'))
1484
        self.assertSearchKey16('ED82CD11', ('abcd',))
1485
1486
    def test_simple_255(self):
1487
        self.assertSearchKey255('\x8cse!', ('foo',))
1488
        self.assertSearchKey255('\x8cse!\x00\x8cse!', ('foo', 'foo'))
1489
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', ('foo', 'bar'))
1490
        # The standard mapping for these would include '\n', so it should be
1491
        # mapped to '_'
1492
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', ('<', 'V'))
1493
1494
    def test_255_does_not_include_newline(self):
1495
        # When mapping via _search_key_255, we should never have the '\n'
1496
        # character, but all other 255 values should be present
1497
        chars_used = set()
1498
        for char_in in range(256):
1499
            search_key = chk_map._search_key_255((chr(char_in),))
1500
            chars_used.update(search_key)
1501
        all_chars = set([chr(x) for x in range(256)])
1502
        unused_chars = all_chars.symmetric_difference(chars_used)
1503
        self.assertEqual(set('\n'), unused_chars)
1504
1505
1506
class TestLeafNode(TestCaseWithStore):
1507
1508
    def test_current_size_empty(self):
1509
        node = LeafNode()
1510
        self.assertEqual(16, node._current_size())
1511
1512
    def test_current_size_size_changed(self):
1513
        node = LeafNode()
1514
        node.set_maximum_size(10)
1515
        self.assertEqual(17, node._current_size())
1516
1517
    def test_current_size_width_changed(self):
1518
        node = LeafNode()
1519
        node._key_width = 10
1520
        self.assertEqual(17, node._current_size())
1521
1522
    def test_current_size_items(self):
1523
        node = LeafNode()
1524
        base_size = node._current_size()
1525
        node.map(None, ("foo bar",), "baz")
1526
        self.assertEqual(base_size + 14, node._current_size())
1527
1528
    def test_deserialise_empty(self):
1529
        node = LeafNode.deserialise("chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
1530
        self.assertEqual(0, len(node))
1531
        self.assertEqual(10, node.maximum_size)
1532
        self.assertEqual(("sha1:1234",), node.key())
1533
        self.assertIs(None, node._search_prefix)
1534
        self.assertIs(None, node._common_serialised_prefix)
1535
1536
    def test_deserialise_items(self):
1537
        node = LeafNode.deserialise(
1538
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
1539
            ("sha1:1234",))
1540
        self.assertEqual(2, len(node))
1541
        self.assertEqual([(("foo bar",), "baz"), (("quux",), "blarh")],
1542
            sorted(node.iteritems(None)))
1543
1544
    def test_deserialise_item_with_null_width_1(self):
1545
        node = LeafNode.deserialise(
1546
            "chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
1547
            ("sha1:1234",))
1548
        self.assertEqual(2, len(node))
1549
        self.assertEqual([(("foo",), "bar\x00baz"), (("quux",), "blarh")],
1550
            sorted(node.iteritems(None)))
1551
1552
    def test_deserialise_item_with_null_width_2(self):
1553
        node = LeafNode.deserialise(
1554
            "chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
1555
            "quux\x00\x001\nblarh\n",
1556
            ("sha1:1234",))
1557
        self.assertEqual(2, len(node))
1558
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("quux", ""), "blarh")],
1559
            sorted(node.iteritems(None)))
1560
1561
    def test_iteritems_selected_one_of_two_items(self):
1562
        node = LeafNode.deserialise(
1563
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
1564
            ("sha1:1234",))
1565
        self.assertEqual(2, len(node))
1566
        self.assertEqual([(("quux",), "blarh")],
1567
            sorted(node.iteritems(None, [("quux",), ("qaz",)])))
1568
1569
    def test_deserialise_item_with_common_prefix(self):
1570
        node = LeafNode.deserialise(
1571
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
1572
            ("sha1:1234",))
1573
        self.assertEqual(2, len(node))
1574
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("foo", "2"), "blarh")],
1575
            sorted(node.iteritems(None)))
1576
        self.assertIs(chk_map._unknown, node._search_prefix)
1577
        self.assertEqual('foo\x00', node._common_serialised_prefix)
1578
1579
    def test_deserialise_multi_line(self):
1580
        node = LeafNode.deserialise(
1581
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
1582
            ("sha1:1234",))
1583
        self.assertEqual(2, len(node))
1584
        self.assertEqual([(("foo", "1"), "bar\nbaz"),
1585
                          (("foo", "2"), "blarh\n"),
1586
                         ], sorted(node.iteritems(None)))
1587
        self.assertIs(chk_map._unknown, node._search_prefix)
1588
        self.assertEqual('foo\x00', node._common_serialised_prefix)
1589
1590
    def test_key_new(self):
1591
        node = LeafNode()
1592
        self.assertEqual(None, node.key())
1593
1594
    def test_key_after_map(self):
1595
        node = LeafNode.deserialise("chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
1596
        node.map(None, ("foo bar",), "baz quux")
1597
        self.assertEqual(None, node.key())
1598
1599
    def test_key_after_unmap(self):
1600
        node = LeafNode.deserialise(
1601
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
1602
            ("sha1:1234",))
1603
        node.unmap(None, ("foo bar",))
1604
        self.assertEqual(None, node.key())
1605
1606
    def test_map_exceeding_max_size_only_entry_new(self):
1607
        node = LeafNode()
1608
        node.set_maximum_size(10)
1609
        result = node.map(None, ("foo bar",), "baz quux")
1610
        self.assertEqual(("foo bar", [("", node)]), result)
1611
        self.assertTrue(10 < node._current_size())
1612
1613
    def test_map_exceeding_max_size_second_entry_early_difference_new(self):
1614
        node = LeafNode()
1615
        node.set_maximum_size(10)
1616
        node.map(None, ("foo bar",), "baz quux")
1617
        prefix, result = list(node.map(None, ("blue",), "red"))
1618
        self.assertEqual("", prefix)
1619
        self.assertEqual(2, len(result))
1620
        split_chars = set([result[0][0], result[1][0]])
1621
        self.assertEqual(set(["f", "b"]), split_chars)
1622
        nodes = dict(result)
1623
        node = nodes["f"]
1624
        self.assertEqual({("foo bar",): "baz quux"}, self.to_dict(node, None))
1625
        self.assertEqual(10, node.maximum_size)
1626
        self.assertEqual(1, node._key_width)
1627
        node = nodes["b"]
1628
        self.assertEqual({("blue",): "red"}, self.to_dict(node, None))
1629
        self.assertEqual(10, node.maximum_size)
1630
        self.assertEqual(1, node._key_width)
1631
1632
    def test_map_first(self):
1633
        node = LeafNode()
1634
        result = node.map(None, ("foo bar",), "baz quux")
1635
        self.assertEqual(("foo bar", [("", node)]), result)
1636
        self.assertEqual({("foo bar",):"baz quux"}, self.to_dict(node, None))
1637
        self.assertEqual(1, len(node))
1638
1639
    def test_map_second(self):
1640
        node = LeafNode()
1641
        node.map(None, ("foo bar",), "baz quux")
1642
        result = node.map(None, ("bingo",), "bango")
1643
        self.assertEqual(("", [("", node)]), result)
1644
        self.assertEqual({("foo bar",):"baz quux", ("bingo",):"bango"},
1645
            self.to_dict(node, None))
1646
        self.assertEqual(2, len(node))
1647
1648
    def test_map_replacement(self):
1649
        node = LeafNode()
1650
        node.map(None, ("foo bar",), "baz quux")
1651
        result = node.map(None, ("foo bar",), "bango")
1652
        self.assertEqual(("foo bar", [("", node)]), result)
1653
        self.assertEqual({("foo bar",): "bango"},
1654
            self.to_dict(node, None))
1655
        self.assertEqual(1, len(node))
1656
1657
    def test_serialise_empty(self):
1658
        store = self.get_chk_bytes()
1659
        node = LeafNode()
1660
        node.set_maximum_size(10)
1661
        expected_key = ("sha1:f34c3f0634ea3f85953dffa887620c0a5b1f4a51",)
1662
        self.assertEqual([expected_key],
1663
            list(node.serialise(store)))
1664
        self.assertEqual("chkleaf:\n10\n1\n0\n\n", self.read_bytes(store, expected_key))
1665
        self.assertEqual(expected_key, node.key())
1666
1667
    def test_serialise_items(self):
1668
        store = self.get_chk_bytes()
1669
        node = LeafNode()
1670
        node.set_maximum_size(10)
1671
        node.map(None, ("foo bar",), "baz quux")
1672
        expected_key = ("sha1:f89fac7edfc6bdb1b1b54a556012ff0c646ef5e0",)
1673
        self.assertEqual('foo bar', node._common_serialised_prefix)
1674
        self.assertEqual([expected_key],
1675
            list(node.serialise(store)))
1676
        self.assertEqual("chkleaf:\n10\n1\n1\nfoo bar\n\x001\nbaz quux\n",
1677
            self.read_bytes(store, expected_key))
1678
        self.assertEqual(expected_key, node.key())
1679
1680
    def test_unique_serialised_prefix_empty_new(self):
1681
        node = LeafNode()
1682
        self.assertIs(None, node._compute_search_prefix())
1683
1684
    def test_unique_serialised_prefix_one_item_new(self):
1685
        node = LeafNode()
1686
        node.map(None, ("foo bar", "baz"), "baz quux")
1687
        self.assertEqual("foo bar\x00baz", node._compute_search_prefix())
1688
1689
    def test_unmap_missing(self):
1690
        node = LeafNode()
1691
        self.assertRaises(KeyError, node.unmap, None, ("foo bar",))
1692
1693
    def test_unmap_present(self):
1694
        node = LeafNode()
1695
        node.map(None, ("foo bar",), "baz quux")
1696
        result = node.unmap(None, ("foo bar",))
1697
        self.assertEqual(node, result)
1698
        self.assertEqual({}, self.to_dict(node, None))
1699
        self.assertEqual(0, len(node))
1700
1701
    def test_map_maintains_common_prefixes(self):
1702
        node = LeafNode()
1703
        node._key_width = 2
1704
        node.map(None, ("foo bar", "baz"), "baz quux")
1705
        self.assertEqual('foo bar\x00baz', node._search_prefix)
1706
        self.assertEqual('foo bar\x00baz', node._common_serialised_prefix)
1707
        node.map(None, ("foo bar", "bing"), "baz quux")
1708
        self.assertEqual('foo bar\x00b', node._search_prefix)
1709
        self.assertEqual('foo bar\x00b', node._common_serialised_prefix)
1710
        node.map(None, ("fool", "baby"), "baz quux")
1711
        self.assertEqual('foo', node._search_prefix)
1712
        self.assertEqual('foo', node._common_serialised_prefix)
1713
        node.map(None, ("foo bar", "baz"), "replaced")
1714
        self.assertEqual('foo', node._search_prefix)
1715
        self.assertEqual('foo', node._common_serialised_prefix)
1716
        node.map(None, ("very", "different"), "value")
1717
        self.assertEqual('', node._search_prefix)
1718
        self.assertEqual('', node._common_serialised_prefix)
1719
1720
    def test_unmap_maintains_common_prefixes(self):
1721
        node = LeafNode()
1722
        node._key_width = 2
1723
        node.map(None, ("foo bar", "baz"), "baz quux")
1724
        node.map(None, ("foo bar", "bing"), "baz quux")
1725
        node.map(None, ("fool", "baby"), "baz quux")
1726
        node.map(None, ("very", "different"), "value")
1727
        self.assertEqual('', node._search_prefix)
1728
        self.assertEqual('', node._common_serialised_prefix)
1729
        node.unmap(None, ("very", "different"))
1730
        self.assertEqual("foo", node._search_prefix)
1731
        self.assertEqual("foo", node._common_serialised_prefix)
1732
        node.unmap(None, ("fool", "baby"))
1733
        self.assertEqual('foo bar\x00b', node._search_prefix)
1734
        self.assertEqual('foo bar\x00b', node._common_serialised_prefix)
1735
        node.unmap(None, ("foo bar", "baz"))
1736
        self.assertEqual('foo bar\x00bing', node._search_prefix)
1737
        self.assertEqual('foo bar\x00bing', node._common_serialised_prefix)
1738
        node.unmap(None, ("foo bar", "bing"))
1739
        self.assertEqual(None, node._search_prefix)
1740
        self.assertEqual(None, node._common_serialised_prefix)
1741
1742
1743
class TestInternalNode(TestCaseWithStore):
1744
1745
    def test_add_node_empty_new(self):
1746
        node = InternalNode('fo')
1747
        child = LeafNode()
1748
        child.set_maximum_size(100)
1749
        child.map(None, ("foo",), "bar")
1750
        node.add_node("foo", child)
1751
        # Note that node isn't strictly valid now as a tree (only one child),
1752
        # but thats ok for this test.
1753
        # The first child defines the node's width:
1754
        self.assertEqual(3, node._node_width)
1755
        # We should be able to iterate over the contents without doing IO.
1756
        self.assertEqual({('foo',): 'bar'}, self.to_dict(node, None))
1757
        # The length should be known:
1758
        self.assertEqual(1, len(node))
1759
        # serialising the node should serialise the child and the node.
1760
        chk_bytes = self.get_chk_bytes()
1761
        keys = list(node.serialise(chk_bytes))
1762
        child_key = child.serialise(chk_bytes)[0]
1763
        self.assertEqual(
1764
            [child_key, ('sha1:cf67e9997d8228a907c1f5bfb25a8bd9cd916fac',)],
1765
            keys)
1766
        # We should be able to access deserialised content.
1767
        bytes = self.read_bytes(chk_bytes, keys[1])
1768
        node = chk_map._deserialise(bytes, keys[1], None)
1769
        self.assertEqual(1, len(node))
1770
        self.assertEqual({('foo',): 'bar'}, self.to_dict(node, chk_bytes))
1771
        self.assertEqual(3, node._node_width)
1772
1773
    def test_add_node_resets_key_new(self):
1774
        node = InternalNode('fo')
1775
        child = LeafNode()
1776
        child.set_maximum_size(100)
1777
        child.map(None, ("foo",), "bar")
1778
        node.add_node("foo", child)
1779
        chk_bytes = self.get_chk_bytes()
1780
        keys = list(node.serialise(chk_bytes))
1781
        self.assertEqual(keys[1], node._key)
1782
        node.add_node("fos", child)
1783
        self.assertEqual(None, node._key)
1784
1785
#    def test_add_node_empty_oversized_one_ok_new(self):
1786
#    def test_add_node_one_oversized_second_kept_minimum_fan(self):
1787
#    def test_add_node_two_oversized_third_kept_minimum_fan(self):
1788
#    def test_add_node_one_oversized_second_splits_errors(self):
1789
1790
    def test__iter_nodes_no_key_filter(self):
1791
        node = InternalNode('')
1792
        child = LeafNode()
1793
        child.set_maximum_size(100)
1794
        child.map(None, ("foo",), "bar")
1795
        node.add_node("f", child)
1796
        child = LeafNode()
1797
        child.set_maximum_size(100)
1798
        child.map(None, ("bar",), "baz")
1799
        node.add_node("b", child)
1800
1801
        for child, node_key_filter in node._iter_nodes(None, key_filter=None):
1802
            self.assertEqual(None, node_key_filter)
1803
1804
    def test__iter_nodes_splits_key_filter(self):
1805
        node = InternalNode('')
1806
        child = LeafNode()
1807
        child.set_maximum_size(100)
1808
        child.map(None, ("foo",), "bar")
1809
        node.add_node("f", child)
1810
        child = LeafNode()
1811
        child.set_maximum_size(100)
1812
        child.map(None, ("bar",), "baz")
1813
        node.add_node("b", child)
1814
1815
        # foo and bar both match exactly one leaf node, but 'cat' should not
1816
        # match any, and should not be placed in one.
1817
        key_filter = (('foo',), ('bar',), ('cat',))
1818
        for child, node_key_filter in node._iter_nodes(None,
1819
                                                       key_filter=key_filter):
1820
            # each child could only match one key filter, so make sure it was
1821
            # properly filtered
1822
            self.assertEqual(1, len(node_key_filter))
1823
1824
    def test__iter_nodes_with_multiple_matches(self):
1825
        node = InternalNode('')
1826
        child = LeafNode()
1827
        child.set_maximum_size(100)
1828
        child.map(None, ("foo",), "val")
1829
        child.map(None, ("fob",), "val")
1830
        node.add_node("f", child)
1831
        child = LeafNode()
1832
        child.set_maximum_size(100)
1833
        child.map(None, ("bar",), "val")
1834
        child.map(None, ("baz",), "val")
1835
        node.add_node("b", child)
1836
4413.4.2 by John Arbash Meinel
Rewrite the shortcuts.
1837
        # Note that 'ram' doesn't match anything, so it should be freely
1838
        # ignored
1839
        key_filter = (('foo',), ('fob',), ('bar',), ('baz',), ('ram',))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
1840
        for child, node_key_filter in node._iter_nodes(None,
1841
                                                       key_filter=key_filter):
4413.4.2 by John Arbash Meinel
Rewrite the shortcuts.
1842
            # each child could match two key filters, so make sure they were
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
1843
            # both included.
1844
            self.assertEqual(2, len(node_key_filter))
1845
4413.4.2 by John Arbash Meinel
Rewrite the shortcuts.
1846
    def make_fo_fa_node(self):
1847
        node = InternalNode('f')
1848
        child = LeafNode()
1849
        child.set_maximum_size(100)
1850
        child.map(None, ("foo",), "val")
1851
        child.map(None, ("fob",), "val")
1852
        node.add_node('fo', child)
1853
        child = LeafNode()
1854
        child.set_maximum_size(100)
1855
        child.map(None, ("far",), "val")
1856
        child.map(None, ("faz",), "val")
1857
        node.add_node("fa", child)
1858
        return node
1859
1860
    def test__iter_nodes_single_entry(self):
1861
        node = self.make_fo_fa_node()
1862
        key_filter = [('foo',)]
1863
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1864
        self.assertEqual(1, len(nodes))
1865
        self.assertEqual(key_filter, nodes[0][1])
1866
1867
    def test__iter_nodes_single_entry_misses(self):
1868
        node = self.make_fo_fa_node()
1869
        key_filter = [('bar',)]
1870
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1871
        self.assertEqual(0, len(nodes))
1872
1873
    def test__iter_nodes_mixed_key_width(self):
1874
        node = self.make_fo_fa_node()
1875
        key_filter = [('foo', 'bar'), ('foo',), ('fo',), ('b',)]
1876
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1877
        self.assertEqual(1, len(nodes))
1878
        matches = key_filter[:]
1879
        matches.remove(('b',))
1880
        self.assertEqual(sorted(matches), sorted(nodes[0][1]))
1881
1882
    def test__iter_nodes_match_all(self):
1883
        node = self.make_fo_fa_node()
1884
        key_filter = [('foo', 'bar'), ('foo',), ('fo',), ('f',)]
1885
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1886
        self.assertEqual(2, len(nodes))
1887
1888
    def test__iter_nodes_fixed_widths_and_misses(self):
1889
        node = self.make_fo_fa_node()
1890
        # foo and faa should both match one child, baz should miss
1891
        key_filter = [('foo',), ('faa',), ('baz',)]
1892
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1893
        self.assertEqual(2, len(nodes))
1894
        for node, matches in nodes:
1895
            self.assertEqual(1, len(matches))
1896
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
1897
    def test_iteritems_empty_new(self):
1898
        node = InternalNode()
1899
        self.assertEqual([], sorted(node.iteritems(None)))
1900
1901
    def test_iteritems_two_children(self):
1902
        node = InternalNode()
1903
        leaf1 = LeafNode()
1904
        leaf1.map(None, ('foo bar',), 'quux')
1905
        leaf2 = LeafNode()
1906
        leaf2.map(None, ('strange',), 'beast')
1907
        node.add_node("f", leaf1)
1908
        node.add_node("s", leaf2)
1909
        self.assertEqual([(('foo bar',), 'quux'), (('strange',), 'beast')],
1910
            sorted(node.iteritems(None)))
1911
1912
    def test_iteritems_two_children_partial(self):
1913
        node = InternalNode()
1914
        leaf1 = LeafNode()
1915
        leaf1.map(None, ('foo bar',), 'quux')
1916
        leaf2 = LeafNode()
1917
        leaf2.map(None, ('strange',), 'beast')
1918
        node.add_node("f", leaf1)
1919
        # This sets up a path that should not be followed - it will error if
1920
        # the code tries to.
1921
        node._items['f'] = None
1922
        node.add_node("s", leaf2)
1923
        self.assertEqual([(('strange',), 'beast')],
1924
            sorted(node.iteritems(None, [('strange',), ('weird',)])))
1925
1926
    def test_iteritems_two_children_with_hash(self):
1927
        search_key_func = chk_map.search_key_registry.get('hash-255-way')
1928
        node = InternalNode(search_key_func=search_key_func)
1929
        leaf1 = LeafNode(search_key_func=search_key_func)
1930
        leaf1.map(None, ('foo bar',), 'quux')
1931
        leaf2 = LeafNode(search_key_func=search_key_func)
1932
        leaf2.map(None, ('strange',), 'beast')
1933
        self.assertEqual('\xbeF\x014', search_key_func(('foo bar',)))
1934
        self.assertEqual('\x85\xfa\xf7K', search_key_func(('strange',)))
1935
        node.add_node("\xbe", leaf1)
1936
        # This sets up a path that should not be followed - it will error if
1937
        # the code tries to.
1938
        node._items['\xbe'] = None
1939
        node.add_node("\x85", leaf2)
1940
        self.assertEqual([(('strange',), 'beast')],
1941
            sorted(node.iteritems(None, [('strange',), ('weird',)])))
1942
1943
    def test_iteritems_partial_empty(self):
1944
        node = InternalNode()
1945
        self.assertEqual([], sorted(node.iteritems([('missing',)])))
1946
1947
    def test_map_to_new_child_new(self):
1948
        chkmap = self._get_map({('k1',):'foo', ('k2',):'bar'}, maximum_size=10)
1949
        chkmap._ensure_root()
1950
        node = chkmap._root_node
1951
        # Ensure test validity: nothing paged in below the root.
1952
        self.assertEqual(2,
1953
            len([value for value in node._items.values()
1954
                if type(value) == tuple]))
1955
        # now, mapping to k3 should add a k3 leaf
1956
        prefix, nodes = node.map(None, ('k3',), 'quux')
1957
        self.assertEqual("k", prefix)
1958
        self.assertEqual([("", node)], nodes)
1959
        # check new child details
1960
        child = node._items['k3']
1961
        self.assertIsInstance(child, LeafNode)
1962
        self.assertEqual(1, len(child))
1963
        self.assertEqual({('k3',): 'quux'}, self.to_dict(child, None))
1964
        self.assertEqual(None, child._key)
1965
        self.assertEqual(10, child.maximum_size)
1966
        self.assertEqual(1, child._key_width)
1967
        # Check overall structure:
1968
        self.assertEqual(3, len(chkmap))
1969
        self.assertEqual({('k1',): 'foo', ('k2',): 'bar', ('k3',): 'quux'},
1970
            self.to_dict(chkmap))
1971
        # serialising should only serialise the new data - k3 and the internal
1972
        # node.
1973
        keys = list(node.serialise(chkmap._store))
1974
        child_key = child.serialise(chkmap._store)[0]
1975
        self.assertEqual([child_key, keys[1]], keys)
1976
1977
    def test_map_to_child_child_splits_new(self):
1978
        chkmap = self._get_map({('k1',):'foo', ('k22',):'bar'}, maximum_size=10)
1979
        # Check for the canonical root value for this tree:
1980
        self.assertEqualDiff("'' InternalNode\n"
1981
                             "  'k1' LeafNode\n"
1982
                             "      ('k1',) 'foo'\n"
1983
                             "  'k2' LeafNode\n"
1984
                             "      ('k22',) 'bar'\n"
1985
                             , chkmap._dump_tree())
1986
        # _dump_tree pages everything in, so reload using just the root
1987
        chkmap = CHKMap(chkmap._store, chkmap._root_node)
1988
        chkmap._ensure_root()
1989
        node = chkmap._root_node
1990
        # Ensure test validity: nothing paged in below the root.
1991
        self.assertEqual(2,
1992
            len([value for value in node._items.values()
1993
                if type(value) == tuple]))
1994
        # now, mapping to k23 causes k22 ('k2' in node) to split into k22 and
1995
        # k23, which for simplicity in the current implementation generates
1996
        # a new internal node between node, and k22/k23.
1997
        prefix, nodes = node.map(chkmap._store, ('k23',), 'quux')
1998
        self.assertEqual("k", prefix)
1999
        self.assertEqual([("", node)], nodes)
2000
        # check new child details
2001
        child = node._items['k2']
2002
        self.assertIsInstance(child, InternalNode)
2003
        self.assertEqual(2, len(child))
2004
        self.assertEqual({('k22',): 'bar', ('k23',): 'quux'},
2005
            self.to_dict(child, None))
2006
        self.assertEqual(None, child._key)
2007
        self.assertEqual(10, child.maximum_size)
2008
        self.assertEqual(1, child._key_width)
2009
        self.assertEqual(3, child._node_width)
2010
        # Check overall structure:
2011
        self.assertEqual(3, len(chkmap))
2012
        self.assertEqual({('k1',): 'foo', ('k22',): 'bar', ('k23',): 'quux'},
2013
            self.to_dict(chkmap))
2014
        # serialising should only serialise the new data - although k22 hasn't
2015
        # changed because its a special corner case (splitting on with only one
2016
        # key leaves one node unaltered), in general k22 is serialised, so we
2017
        # expect k22, k23, the new internal node, and node, to be serialised.
2018
        keys = list(node.serialise(chkmap._store))
2019
        child_key = child._key
2020
        k22_key = child._items['k22']._key
2021
        k23_key = child._items['k23']._key
2022
        self.assertEqual([k22_key, k23_key, child_key, node.key()], keys)
2023
        self.assertEqualDiff("'' InternalNode\n"
2024
                             "  'k1' LeafNode\n"
2025
                             "      ('k1',) 'foo'\n"
2026
                             "  'k2' InternalNode\n"
2027
                             "    'k22' LeafNode\n"
2028
                             "      ('k22',) 'bar'\n"
2029
                             "    'k23' LeafNode\n"
2030
                             "      ('k23',) 'quux'\n"
2031
                             , chkmap._dump_tree())
2032
2033
    def test__search_prefix_filter_with_hash(self):
2034
        search_key_func = chk_map.search_key_registry.get('hash-16-way')
2035
        node = InternalNode(search_key_func=search_key_func)
2036
        node._key_width = 2
2037
        node._node_width = 4
2038
        self.assertEqual('E8B7BE43\x0071BEEFF9', search_key_func(('a', 'b')))
2039
        self.assertEqual('E8B7', node._search_prefix_filter(('a', 'b')))
2040
        self.assertEqual('E8B7', node._search_prefix_filter(('a',)))
2041
2042
    def test_unmap_k23_from_k1_k22_k23_gives_k1_k22_tree_new(self):
2043
        chkmap = self._get_map(
2044
            {('k1',):'foo', ('k22',):'bar', ('k23',): 'quux'}, maximum_size=10)
2045
        # Check we have the expected tree.
2046
        self.assertEqualDiff("'' InternalNode\n"
2047
                             "  'k1' LeafNode\n"
2048
                             "      ('k1',) 'foo'\n"
2049
                             "  'k2' InternalNode\n"
2050
                             "    'k22' LeafNode\n"
2051
                             "      ('k22',) 'bar'\n"
2052
                             "    'k23' LeafNode\n"
2053
                             "      ('k23',) 'quux'\n"
2054
                             , chkmap._dump_tree())
2055
        chkmap = CHKMap(chkmap._store, chkmap._root_node)
2056
        chkmap._ensure_root()
2057
        node = chkmap._root_node
2058
        # unmapping k23 should give us a root, with k1 and k22 as direct
2059
        # children.
2060
        result = node.unmap(chkmap._store, ('k23',))
2061
        # check the pointed-at object within node - k2 should now point at the
2062
        # k22 leaf (which has been paged in to see if we can collapse the tree)
2063
        child = node._items['k2']
2064
        self.assertIsInstance(child, LeafNode)
2065
        self.assertEqual(1, len(child))
2066
        self.assertEqual({('k22',): 'bar'},
2067
            self.to_dict(child, None))
2068
        # Check overall structure is instact:
2069
        self.assertEqual(2, len(chkmap))
2070
        self.assertEqual({('k1',): 'foo', ('k22',): 'bar'},
2071
            self.to_dict(chkmap))
2072
        # serialising should only serialise the new data - the root node.
2073
        keys = list(node.serialise(chkmap._store))
2074
        self.assertEqual([keys[-1]], keys)
2075
        chkmap = CHKMap(chkmap._store, keys[-1])
2076
        self.assertEqualDiff("'' InternalNode\n"
2077
                             "  'k1' LeafNode\n"
2078
                             "      ('k1',) 'foo'\n"
2079
                             "  'k2' LeafNode\n"
2080
                             "      ('k22',) 'bar'\n"
2081
                             , chkmap._dump_tree())
2082
2083
    def test_unmap_k1_from_k1_k22_k23_gives_k22_k23_tree_new(self):
2084
        chkmap = self._get_map(
2085
            {('k1',):'foo', ('k22',):'bar', ('k23',): 'quux'}, maximum_size=10)
2086
        self.assertEqualDiff("'' InternalNode\n"
2087
                             "  'k1' LeafNode\n"
2088
                             "      ('k1',) 'foo'\n"
2089
                             "  'k2' InternalNode\n"
2090
                             "    'k22' LeafNode\n"
2091
                             "      ('k22',) 'bar'\n"
2092
                             "    'k23' LeafNode\n"
2093
                             "      ('k23',) 'quux'\n"
2094
                             , chkmap._dump_tree())
2095
        orig_root = chkmap._root_node
2096
        chkmap = CHKMap(chkmap._store, orig_root)
2097
        chkmap._ensure_root()
2098
        node = chkmap._root_node
2099
        k2_ptr = node._items['k2']
2100
        # unmapping k1 should give us a root, with k22 and k23 as direct
2101
        # children, and should not have needed to page in the subtree.
2102
        result = node.unmap(chkmap._store, ('k1',))
2103
        self.assertEqual(k2_ptr, result)
2104
        chkmap = CHKMap(chkmap._store, orig_root)
2105
        # Unmapping at the CHKMap level should switch to the new root
2106
        chkmap.unmap(('k1',))
2107
        self.assertEqual(k2_ptr, chkmap._root_node)
2108
        self.assertEqualDiff("'' InternalNode\n"
2109
                             "  'k22' LeafNode\n"
2110
                             "      ('k22',) 'bar'\n"
2111
                             "  'k23' LeafNode\n"
2112
                             "      ('k23',) 'quux'\n"
2113
                             , chkmap._dump_tree())
2114
2115
2116
# leaf:
2117
# map -> fits - done
2118
# map -> doesn't fit - shrink from left till fits
2119
#        key data to return: the common prefix, new nodes.
2120
2121
# unmap -> how to tell if siblings can be combined.
2122
#          combing leaf nodes means expanding the prefix to the left; so gather the size of
2123
#          all the leaf nodes addressed by expanding the prefix by 1; if any adjacent node
2124
#          is an internal node, we know that that is a dense subtree - can't combine.
2125
#          otherwise as soon as the sum of serialised values exceeds the split threshold
2126
#          we know we can't combine - stop.
2127
# unmap -> key return data - space in node, common prefix length? and key count
2128
# internal:
2129
# variable length prefixes? -> later start with fixed width to get something going
2130
# map -> fits - update pointer to leaf
2131
#        return [prefix and node] - seems sound.
2132
# map -> doesn't fit - find unique prefix and shift right
2133
#        create internal nodes for all the partitions, return list of unique
2134
#        prefixes and nodes.
2135
# map -> new prefix - create a leaf
2136
# unmap -> if child key count 0, remove
2137
# unmap -> return space in node, common prefix length? (why?), key count
2138
# map:
2139
# map, if 1 node returned, use it, otherwise make an internal and populate.
2140
# map - unmap - if empty, use empty leafnode (avoids special cases in driver
2141
# code)
2142
# map inits as empty leafnode.
2143
# tools:
2144
# visualiser
2145
2146
2147
# how to handle:
2148
# AA, AB, AC, AD, BA
2149
# packed internal node - ideal:
2150
# AA, AB, AC, AD, BA
2151
# single byte fanout - A,B,   AA,AB,AC,AD,     BA
2152
# build order's:
2153
# BA
2154
# AB - split, but we want to end up with AB, BA, in one node, with
2155
# 1-4K get0
2156
2157
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2158
class TestCHKMapDifference(TestCaseWithExampleMaps):
4476.1.12 by John Arbash Meinel
Start testing the new class.
2159
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2160
    def get_difference(self, new_roots, old_roots,
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2161
                       search_key_func=None):
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2162
        if search_key_func is None:
2163
            search_key_func = chk_map._search_key_plain
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2164
        return chk_map.CHKMapDifference(self.get_chk_bytes(),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2165
            new_roots, old_roots, search_key_func)
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2166
4476.1.12 by John Arbash Meinel
Start testing the new class.
2167
    def test__init__(self):
2168
        c_map = self.make_root_only_map()
2169
        key1 = c_map.key()
2170
        c_map.map(('aaa',), 'new aaa content')
2171
        key2 = c_map._save()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2172
        diff = self.get_difference([key2], [key1])
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2173
        self.assertEqual(set([key1]), diff._all_old_chks)
2174
        self.assertEqual([], diff._old_queue)
2175
        self.assertEqual([], diff._new_queue)
4476.1.12 by John Arbash Meinel
Start testing the new class.
2176
2177
    def help__read_all_roots(self, search_key_func):
2178
        c_map = self.make_root_only_map(search_key_func=search_key_func)
2179
        key1 = c_map.key()
2180
        c_map.map(('aaa',), 'new aaa content')
2181
        key2 = c_map._save()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2182
        diff = self.get_difference([key2], [key1], search_key_func)
2183
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2184
        self.assertEqual([key2], root_results)
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2185
        # We should have queued up only items that aren't in the old
4476.1.12 by John Arbash Meinel
Start testing the new class.
2186
        # set
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2187
        self.assertEqual([(('aaa',), 'new aaa content')],
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2188
                         diff._new_item_queue)
2189
        self.assertEqual([], diff._new_queue)
2190
        # And there are no old references, so that queue should be
4476.1.12 by John Arbash Meinel
Start testing the new class.
2191
        # empty
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2192
        self.assertEqual([], diff._old_queue)
4476.1.12 by John Arbash Meinel
Start testing the new class.
2193
2194
    def test__read_all_roots_plain(self):
2195
        self.help__read_all_roots(search_key_func=chk_map._search_key_plain)
2196
2197
    def test__read_all_roots_16(self):
2198
        self.help__read_all_roots(search_key_func=chk_map._search_key_16)
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
2199
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2200
    def test__read_all_roots_skips_known_old(self):
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2201
        c_map = self.make_one_deep_map(chk_map._search_key_plain)
2202
        key1 = c_map.key()
2203
        c_map2 = self.make_root_only_map(chk_map._search_key_plain)
2204
        key2 = c_map2.key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2205
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2206
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2207
        # We should have no results. key2 is completely contained within key1,
2208
        # and we should have seen that in the first pass
2209
        self.assertEqual([], root_results)
2210
2211
    def test__read_all_roots_prepares_queues(self):
2212
        c_map = self.make_one_deep_map(chk_map._search_key_plain)
2213
        key1 = c_map.key()
2214
        c_map._dump_tree() # load everything
2215
        key1_a = c_map._root_node._items['a'].key()
2216
        c_map.map(('abb',), 'new abb content')
2217
        key2 = c_map._save()
2218
        key2_a = c_map._root_node._items['a'].key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2219
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2220
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2221
        self.assertEqual([key2], root_results)
2222
        # At this point, we should have queued up only the 'a' Leaf on both
2223
        # sides, both 'c' and 'd' are known to not have changed on both sides
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2224
        self.assertEqual([key2_a], diff._new_queue)
2225
        self.assertEqual([], diff._new_item_queue)
2226
        self.assertEqual([key1_a], diff._old_queue)
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2227
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2228
    def test__read_all_roots_multi_new_prepares_queues(self):
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2229
        c_map = self.make_one_deep_map(chk_map._search_key_plain)
2230
        key1 = c_map.key()
2231
        c_map._dump_tree() # load everything
2232
        key1_a = c_map._root_node._items['a'].key()
2233
        key1_c = c_map._root_node._items['c'].key()
2234
        c_map.map(('abb',), 'new abb content')
2235
        key2 = c_map._save()
2236
        key2_a = c_map._root_node._items['a'].key()
2237
        key2_c = c_map._root_node._items['c'].key()
2238
        c_map = chk_map.CHKMap(self.get_chk_bytes(), key1,
2239
                               chk_map._search_key_plain)
2240
        c_map.map(('ccc',), 'new ccc content')
2241
        key3 = c_map._save()
2242
        key3_a = c_map._root_node._items['a'].key()
2243
        key3_c = c_map._root_node._items['c'].key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2244
        diff = self.get_difference([key2, key3], [key1],
2245
                                   chk_map._search_key_plain)
2246
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2247
        self.assertEqual(sorted([key2, key3]), sorted(root_results))
2248
        # We should have queued up key2_a, and key3_c, but not key2_c or key3_c
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2249
        self.assertEqual([key2_a, key3_c], diff._new_queue)
2250
        self.assertEqual([], diff._new_item_queue)
2251
        # And we should have queued up both a and c for the old set
2252
        self.assertEqual([key1_a, key1_c], diff._old_queue)
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2253
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
2254
    def test__read_all_roots_different_depths(self):
2255
        c_map = self.make_two_deep_map(chk_map._search_key_plain)
2256
        c_map._dump_tree() # load everything
2257
        key1 = c_map.key()
2258
        key1_a = c_map._root_node._items['a'].key()
4476.1.21 by John Arbash Meinel
When checking that a key prefix is present/missing
2259
        key1_c = c_map._root_node._items['c'].key()
2260
        key1_d = c_map._root_node._items['d'].key()
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
2261
2262
        c_map2 = self.make_one_deep_two_prefix_map(chk_map._search_key_plain)
2263
        c_map2._dump_tree()
2264
        key2 = c_map2.key()
2265
        key2_aa = c_map2._root_node._items['aa'].key()
2266
        key2_ad = c_map2._root_node._items['ad'].key()
2267
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2268
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2269
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.15 by John Arbash Meinel
Properly handle when the two sides potentially have the same chk pages,
2270
        self.assertEqual([key2], root_results)
2271
        # Only the 'a' subset should be queued up, since 'c' and 'd' cannot be
2272
        # present
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2273
        self.assertEqual([key1_a], diff._old_queue)
2274
        self.assertEqual([key2_aa, key2_ad], diff._new_queue)
2275
        self.assertEqual([], diff._new_item_queue)
4476.1.14 by John Arbash Meinel
Switch tactic a bit.
2276
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2277
        diff = self.get_difference([key1], [key2], chk_map._search_key_plain)
2278
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.21 by John Arbash Meinel
When checking that a key prefix is present/missing
2279
        self.assertEqual([key1], root_results)
2280
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2281
        self.assertEqual([key2_aa, key2_ad], diff._old_queue)
2282
        self.assertEqual([key1_a, key1_c, key1_d], diff._new_queue)
2283
        self.assertEqual([], diff._new_item_queue)
4476.1.21 by John Arbash Meinel
When checking that a key prefix is present/missing
2284
4476.1.23 by John Arbash Meinel
Assert that we still do the right thing even when using a search key.
2285
    def test__read_all_roots_different_depths_16(self):
2286
        c_map = self.make_two_deep_map(chk_map._search_key_16)
2287
        c_map._dump_tree() # load everything
2288
        key1 = c_map.key()
2289
        key1_2 = c_map._root_node._items['2'].key()
2290
        key1_4 = c_map._root_node._items['4'].key()
2291
        key1_C = c_map._root_node._items['C'].key()
2292
        key1_F = c_map._root_node._items['F'].key()
2293
2294
        c_map2 = self.make_one_deep_two_prefix_map(chk_map._search_key_16)
2295
        c_map2._dump_tree()
2296
        key2 = c_map2.key()
2297
        key2_F0 = c_map2._root_node._items['F0'].key()
2298
        key2_F3 = c_map2._root_node._items['F3'].key()
2299
        key2_F4 = c_map2._root_node._items['F4'].key()
2300
        key2_FD = c_map2._root_node._items['FD'].key()
2301
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2302
        diff = self.get_difference([key2], [key1], chk_map._search_key_16)
2303
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.23 by John Arbash Meinel
Assert that we still do the right thing even when using a search key.
2304
        self.assertEqual([key2], root_results)
2305
        # Only the subset of keys that may be present should be queued up.
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2306
        self.assertEqual([key1_F], diff._old_queue)
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2307
        self.assertEqual(sorted([key2_F0, key2_F3, key2_F4, key2_FD]),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2308
                         sorted(diff._new_queue))
2309
        self.assertEqual([], diff._new_item_queue)
4476.1.23 by John Arbash Meinel
Assert that we still do the right thing even when using a search key.
2310
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2311
        diff = self.get_difference([key1], [key2], chk_map._search_key_16)
2312
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.23 by John Arbash Meinel
Assert that we still do the right thing even when using a search key.
2313
        self.assertEqual([key1], root_results)
2314
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2315
        self.assertEqual(sorted([key2_F0, key2_F3, key2_F4, key2_FD]),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2316
                         sorted(diff._old_queue))
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2317
        self.assertEqual(sorted([key1_2, key1_4, key1_C, key1_F]),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2318
                         sorted(diff._new_queue))
2319
        self.assertEqual([], diff._new_item_queue)
4476.1.23 by John Arbash Meinel
Assert that we still do the right thing even when using a search key.
2320
4476.1.25 by John Arbash Meinel
A bit more testing.
2321
    def test__read_all_roots_mixed_depth(self):
2322
        c_map = self.make_one_deep_two_prefix_map(chk_map._search_key_plain)
2323
        c_map._dump_tree() # load everything
2324
        key1 = c_map.key()
2325
        key1_aa = c_map._root_node._items['aa'].key()
2326
        key1_ad = c_map._root_node._items['ad'].key()
2327
2328
        c_map2 = self.make_one_deep_one_prefix_map(chk_map._search_key_plain)
2329
        c_map2._dump_tree()
2330
        key2 = c_map2.key()
2331
        key2_a = c_map2._root_node._items['a'].key()
2332
        key2_b = c_map2._root_node._items['b'].key()
2333
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2334
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2335
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.25 by John Arbash Meinel
A bit more testing.
2336
        self.assertEqual([key2], root_results)
2337
        # 'ad' matches exactly 'a' on the other side, so it should be removed,
2338
        # and neither side should have it queued for walking
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2339
        self.assertEqual([], diff._old_queue)
2340
        self.assertEqual([key2_b], diff._new_queue)
2341
        self.assertEqual([], diff._new_item_queue)
4476.1.25 by John Arbash Meinel
A bit more testing.
2342
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2343
        diff = self.get_difference([key1], [key2], chk_map._search_key_plain)
2344
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.25 by John Arbash Meinel
A bit more testing.
2345
        self.assertEqual([key1], root_results)
4476.1.30 by John Arbash Meinel
Some comment updates
2346
        # Note: This is technically not the 'true minimal' set that we could
2347
        #       use The reason is that 'a' was matched exactly to 'ad' (by sha
2348
        #       sum).  However, the code gets complicated in the case of more
2349
        #       than one interesting key, so for now, we live with this
2350
        #       Consider revising, though benchmarking showing it to be a
2351
        #       real-world issue should be done
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2352
        self.assertEqual([key2_a], diff._old_queue)
2353
        # self.assertEqual([], diff._old_queue)
2354
        self.assertEqual([key1_aa], diff._new_queue)
2355
        self.assertEqual([], diff._new_item_queue)
4476.1.25 by John Arbash Meinel
A bit more testing.
2356
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2357
    def test__read_all_roots_yields_extra_deep_records(self):
4476.1.24 by John Arbash Meinel
some comment updates.
2358
        # This is slightly controversial, as we will yield a chk page that we
2359
        # might later on find out could be filtered out. (If a root node is
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2360
        # referenced deeper in the old set.)
4476.1.24 by John Arbash Meinel
some comment updates.
2361
        # However, even with stacking, we always have all chk pages that we
2362
        # will need. So as long as we filter out the referenced keys, we'll
2363
        # never run into problems.
2364
        # This allows us to yield a root node record immediately, without any
2365
        # buffering.
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2366
        c_map = self.make_two_deep_map(chk_map._search_key_plain)
4476.1.14 by John Arbash Meinel
Switch tactic a bit.
2367
        c_map._dump_tree() # load all keys
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2368
        key1 = c_map.key()
4476.1.14 by John Arbash Meinel
Switch tactic a bit.
2369
        key1_a = c_map._root_node._items['a'].key()
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2370
        c_map2 = self.get_map({
2371
            ('acc',): 'initial acc content',
2372
            ('ace',): 'initial ace content',
2373
        }, maximum_size=100)
2374
        self.assertEqualDiff(
2375
            "'' LeafNode\n"
2376
            "      ('acc',) 'initial acc content'\n"
2377
            "      ('ace',) 'initial ace content'\n",
2378
            c_map2._dump_tree())
2379
        key2 = c_map2.key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2380
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2381
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2382
        self.assertEqual([key2], root_results)
4476.1.14 by John Arbash Meinel
Switch tactic a bit.
2383
        # However, even though we have yielded the root node to be fetched,
2384
        # we should have enqued all of the chk pages to be walked, so that we
2385
        # can find the keys if they are present
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2386
        self.assertEqual([key1_a], diff._old_queue)
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2387
        self.assertEqual([(('acc',), 'initial acc content'),
2388
                          (('ace',), 'initial ace content'),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2389
                         ], diff._new_item_queue)
4476.1.13 by John Arbash Meinel
Test that _read_all_roots does what is expected
2390
4476.1.17 by John Arbash Meinel
Start running all of the iter_interesting_nodes tests
2391
    def test__read_all_roots_multiple_targets(self):
2392
        c_map = self.make_root_only_map()
2393
        key1 = c_map.key()
2394
        c_map = self.make_one_deep_map()
2395
        key2 = c_map.key()
2396
        c_map._dump_tree()
2397
        key2_c = c_map._root_node._items['c'].key()
2398
        key2_d = c_map._root_node._items['d'].key()
2399
        c_map.map(('ccc',), 'new ccc value')
2400
        key3 = c_map._save()
2401
        key3_c = c_map._root_node._items['c'].key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2402
        diff = self.get_difference([key2, key3], [key1],
4476.1.40 by John Arbash Meinel
cleanup indentation.
2403
                                   chk_map._search_key_plain)
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2404
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.17 by John Arbash Meinel
Start running all of the iter_interesting_nodes tests
2405
        self.assertEqual(sorted([key2, key3]), sorted(root_results))
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2406
        self.assertEqual([], diff._old_queue)
4476.1.17 by John Arbash Meinel
Start running all of the iter_interesting_nodes tests
2407
        # the key 'd' is interesting from key2 and key3, but should only be
2408
        # entered into the queue 1 time
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2409
        self.assertEqual(sorted([key2_c, key3_c, key2_d]),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2410
                         sorted(diff._new_queue))
2411
        self.assertEqual([], diff._new_item_queue)
4476.1.17 by John Arbash Meinel
Start running all of the iter_interesting_nodes tests
2412
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2413
    def test__read_all_roots_no_old(self):
2414
        # This is the 'initial branch' case. With nothing in the old
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2415
        # set, we can just queue up all root nodes into interesting queue, and
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2416
        # then have them fast-path flushed via _flush_new_queue
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2417
        c_map = self.make_two_deep_map()
2418
        key1 = c_map.key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2419
        diff = self.get_difference([key1], [], chk_map._search_key_plain)
2420
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2421
        self.assertEqual([], root_results)
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2422
        self.assertEqual([], diff._old_queue)
2423
        self.assertEqual([key1], diff._new_queue)
2424
        self.assertEqual([], diff._new_item_queue)
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2425
2426
        c_map2 = self.make_one_deep_map()
2427
        key2 = c_map2.key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2428
        diff = self.get_difference([key1, key2], [], chk_map._search_key_plain)
2429
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2430
        self.assertEqual([], root_results)
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2431
        self.assertEqual([], diff._old_queue)
2432
        self.assertEqual(sorted([key1, key2]), sorted(diff._new_queue))
2433
        self.assertEqual([], diff._new_item_queue)
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2434
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2435
    def test__read_all_roots_no_old_16(self):
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2436
        c_map = self.make_two_deep_map(chk_map._search_key_16)
2437
        key1 = c_map.key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2438
        diff = self.get_difference([key1], [], chk_map._search_key_16)
2439
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2440
        self.assertEqual([], root_results)
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2441
        self.assertEqual([], diff._old_queue)
2442
        self.assertEqual([key1], diff._new_queue)
2443
        self.assertEqual([], diff._new_item_queue)
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2444
2445
        c_map2 = self.make_one_deep_map(chk_map._search_key_16)
2446
        key2 = c_map2.key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2447
        diff = self.get_difference([key1, key2], [],
2448
                                   chk_map._search_key_16)
2449
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.28 by John Arbash Meinel
Add the shortcut path for _read_all_roots.
2450
        self.assertEqual([], root_results)
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2451
        self.assertEqual([], diff._old_queue)
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2452
        self.assertEqual(sorted([key1, key2]),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2453
                         sorted(diff._new_queue))
2454
        self.assertEqual([], diff._new_item_queue)
4476.1.17 by John Arbash Meinel
Start running all of the iter_interesting_nodes tests
2455
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2456
    def test__read_all_roots_multiple_old(self):
4476.1.29 by John Arbash Meinel
We should not enqueue the same uninteresting node when it is
2457
        c_map = self.make_two_deep_map()
2458
        key1 = c_map.key()
2459
        c_map._dump_tree() # load everything
2460
        key1_a = c_map._root_node._items['a'].key()
2461
        c_map.map(('ccc',), 'new ccc value')
2462
        key2 = c_map._save()
2463
        key2_a = c_map._root_node._items['a'].key()
2464
        c_map.map(('add',), 'new add value')
2465
        key3 = c_map._save()
2466
        key3_a = c_map._root_node._items['a'].key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2467
        diff = self.get_difference([key3], [key1, key2],
2468
                                   chk_map._search_key_plain)
2469
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.29 by John Arbash Meinel
We should not enqueue the same uninteresting node when it is
2470
        self.assertEqual([key3], root_results)
2471
        # the 'a' keys should not be queued up 2 times, since they are
2472
        # identical
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2473
        self.assertEqual([key1_a], diff._old_queue)
2474
        self.assertEqual([key3_a], diff._new_queue)
2475
        self.assertEqual([], diff._new_item_queue)
4476.1.29 by John Arbash Meinel
We should not enqueue the same uninteresting node when it is
2476
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2477
    def test__process_next_old_batched_no_dupes(self):
4476.1.32 by John Arbash Meinel
A few more updates.
2478
        c_map = self.make_two_deep_map()
2479
        key1 = c_map.key()
2480
        c_map._dump_tree() # load everything
2481
        key1_a = c_map._root_node._items['a'].key()
2482
        key1_aa = c_map._root_node._items['a']._items['aa'].key()
2483
        key1_ab = c_map._root_node._items['a']._items['ab'].key()
2484
        key1_ac = c_map._root_node._items['a']._items['ac'].key()
2485
        key1_ad = c_map._root_node._items['a']._items['ad'].key()
2486
        c_map.map(('aaa',), 'new aaa value')
2487
        key2 = c_map._save()
2488
        key2_a = c_map._root_node._items['a'].key()
2489
        key2_aa = c_map._root_node._items['a']._items['aa'].key()
2490
        c_map.map(('acc',), 'new acc content')
2491
        key3 = c_map._save()
2492
        key3_a = c_map._root_node._items['a'].key()
2493
        key3_ac = c_map._root_node._items['a']._items['ac'].key()
4476.1.38 by John Arbash Meinel
Rename InterestingNodeIterator => CHKMapDifference, update tests.
2494
        diff = self.get_difference([key3], [key1, key2],
2495
                                   chk_map._search_key_plain)
2496
        root_results = [record.key for record in diff._read_all_roots()]
4476.1.32 by John Arbash Meinel
A few more updates.
2497
        self.assertEqual([key3], root_results)
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2498
        self.assertEqual(sorted([key1_a, key2_a]),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2499
                         sorted(diff._old_queue))
2500
        self.assertEqual([key3_a], diff._new_queue)
2501
        self.assertEqual([], diff._new_item_queue)
2502
        diff._process_next_old()
2503
        # All of the old records should be brought in and queued up,
4476.1.32 by John Arbash Meinel
A few more updates.
2504
        # but we should not have any duplicates
4476.1.34 by John Arbash Meinel
Major rework, simplify what is put into the queues.
2505
        self.assertEqual(sorted([key1_aa, key1_ab, key1_ac, key1_ad, key2_aa]),
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2506
                         sorted(diff._old_queue))
4476.1.32 by John Arbash Meinel
A few more updates.
2507
4476.1.11 by John Arbash Meinel
Restore the iter_interesting_nodes implementation.
2508
4476.1.10 by John Arbash Meinel
refactor the helpful examples into separate classes
2509
class TestIterInterestingNodes(TestCaseWithExampleMaps):
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2510
4431.3.8 by Jonathan Lange
Cherrypick bzr.dev r4477.
2511
    def get_map_key(self, a_dict, maximum_size=10):
4476.1.11 by John Arbash Meinel
Restore the iter_interesting_nodes implementation.
2512
        c_map = self.get_map(a_dict, maximum_size=maximum_size)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2513
        return c_map.key()
2514
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2515
    def assertIterInteresting(self, records, items, interesting_keys,
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2516
                              old_keys):
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2517
        """Check the result of iter_interesting_nodes.
2518
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2519
        Note that we no longer care how many steps are taken, etc, just that
2520
        the right contents are returned.
2521
2522
        :param records: A list of record keys that should be yielded
2523
        :param items: A list of items (key,value) that should be yielded.
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2524
        """
2525
        store = self.get_chk_bytes()
4476.1.17 by John Arbash Meinel
Start running all of the iter_interesting_nodes tests
2526
        store._search_key_func = chk_map._search_key_plain
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2527
        iter_nodes = chk_map.iter_interesting_nodes(store, interesting_keys,
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2528
                                                    old_keys)
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2529
        record_keys = []
2530
        all_items = []
2531
        for record, new_items in iter_nodes:
2532
            if record is not None:
2533
                record_keys.append(record.key)
2534
            if new_items:
2535
                all_items.extend(new_items)
2536
        self.assertEqual(sorted(records), sorted(record_keys))
2537
        self.assertEqual(sorted(items), sorted(all_items))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2538
2539
    def test_empty_to_one_keys(self):
2540
        target = self.get_map_key({('a',): 'content'})
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2541
        self.assertIterInteresting([target],
2542
                                   [(('a',), 'content')],
2543
                                   [target], [])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2544
2545
    def test_none_to_one_key(self):
2546
        basis = self.get_map_key({})
2547
        target = self.get_map_key({('a',): 'content'})
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2548
        self.assertIterInteresting([target],
2549
                                   [(('a',), 'content')],
2550
                                   [target], [basis])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2551
2552
    def test_one_to_none_key(self):
2553
        basis = self.get_map_key({('a',): 'content'})
2554
        target = self.get_map_key({})
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2555
        self.assertIterInteresting([target],
2556
                                   [],
2557
                                   [target], [basis])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2558
2559
    def test_common_pages(self):
2560
        basis = self.get_map_key({('a',): 'content',
2561
                                  ('b',): 'content',
2562
                                  ('c',): 'content',
2563
                                 })
2564
        target = self.get_map_key({('a',): 'content',
2565
                                   ('b',): 'other content',
2566
                                   ('c',): 'content',
2567
                                  })
2568
        target_map = CHKMap(self.get_chk_bytes(), target)
2569
        self.assertEqualDiff(
2570
            "'' InternalNode\n"
2571
            "  'a' LeafNode\n"
2572
            "      ('a',) 'content'\n"
2573
            "  'b' LeafNode\n"
2574
            "      ('b',) 'other content'\n"
2575
            "  'c' LeafNode\n"
2576
            "      ('c',) 'content'\n",
2577
            target_map._dump_tree())
2578
        b_key = target_map._root_node._items['b'].key()
2579
        # This should return the root node, and the node for the 'b' key
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2580
        self.assertIterInteresting([target, b_key],
2581
                                   [(('b',), 'other content')],
2582
                                   [target], [basis])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2583
2584
    def test_common_sub_page(self):
2585
        basis = self.get_map_key({('aaa',): 'common',
2586
                                  ('c',): 'common',
2587
                                 })
2588
        target = self.get_map_key({('aaa',): 'common',
2589
                                   ('aab',): 'new',
2590
                                   ('c',): 'common',
2591
                                  })
2592
        target_map = CHKMap(self.get_chk_bytes(), target)
2593
        self.assertEqualDiff(
2594
            "'' InternalNode\n"
2595
            "  'a' InternalNode\n"
2596
            "    'aaa' LeafNode\n"
2597
            "      ('aaa',) 'common'\n"
2598
            "    'aab' LeafNode\n"
2599
            "      ('aab',) 'new'\n"
2600
            "  'c' LeafNode\n"
2601
            "      ('c',) 'common'\n",
2602
            target_map._dump_tree())
2603
        # The key for the internal aa node
2604
        a_key = target_map._root_node._items['a'].key()
2605
        # The key for the leaf aab node
4476.1.17 by John Arbash Meinel
Start running all of the iter_interesting_nodes tests
2606
        # aaa_key = target_map._root_node._items['a']._items['aaa'].key()
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2607
        aab_key = target_map._root_node._items['a']._items['aab'].key()
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2608
        self.assertIterInteresting([target, a_key, aab_key],
2609
                                   [(('aab',), 'new')],
2610
                                   [target], [basis])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2611
2612
    def test_common_leaf(self):
2613
        basis = self.get_map_key({})
2614
        target1 = self.get_map_key({('aaa',): 'common'})
2615
        target2 = self.get_map_key({('aaa',): 'common',
2616
                                    ('bbb',): 'new',
2617
                                   })
2618
        target3 = self.get_map_key({('aaa',): 'common',
2619
                                    ('aac',): 'other',
2620
                                    ('bbb',): 'new',
2621
                                   })
2622
        # The LeafNode containing 'aaa': 'common' occurs at 3 different levels.
2623
        # Once as a root node, once as a second layer, and once as a third
2624
        # layer. It should only be returned one time regardless
2625
        target1_map = CHKMap(self.get_chk_bytes(), target1)
2626
        self.assertEqualDiff(
2627
            "'' LeafNode\n"
2628
            "      ('aaa',) 'common'\n",
2629
            target1_map._dump_tree())
2630
        target2_map = CHKMap(self.get_chk_bytes(), target2)
2631
        self.assertEqualDiff(
2632
            "'' InternalNode\n"
2633
            "  'a' LeafNode\n"
2634
            "      ('aaa',) 'common'\n"
2635
            "  'b' LeafNode\n"
2636
            "      ('bbb',) 'new'\n",
2637
            target2_map._dump_tree())
2638
        target3_map = CHKMap(self.get_chk_bytes(), target3)
2639
        self.assertEqualDiff(
2640
            "'' InternalNode\n"
2641
            "  'a' InternalNode\n"
2642
            "    'aaa' LeafNode\n"
2643
            "      ('aaa',) 'common'\n"
2644
            "    'aac' LeafNode\n"
2645
            "      ('aac',) 'other'\n"
2646
            "  'b' LeafNode\n"
2647
            "      ('bbb',) 'new'\n",
2648
            target3_map._dump_tree())
2649
        aaa_key = target1_map._root_node.key()
2650
        b_key = target2_map._root_node._items['b'].key()
2651
        a_key = target3_map._root_node._items['a'].key()
2652
        aac_key = target3_map._root_node._items['a']._items['aac'].key()
2653
        self.assertIterInteresting(
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2654
            [target1, target2, target3, a_key, aac_key, b_key],
2655
            [(('aaa',), 'common'), (('bbb',), 'new'), (('aac',), 'other')],
2656
            [target1, target2, target3], [basis])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2657
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2658
        self.assertIterInteresting(
2659
            [target2, target3, a_key, aac_key, b_key],
2660
            [(('bbb',), 'new'), (('aac',), 'other')],
2661
            [target2, target3], [target1])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2662
4476.1.24 by John Arbash Meinel
some comment updates.
2663
        # Technically, target1 could be filtered out, but since it is a root
2664
        # node, we yield it immediately, rather than waiting to find out much
2665
        # later on.
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2666
        self.assertIterInteresting(
2667
            [target1],
2668
            [],
4476.1.19 by John Arbash Meinel
All test_chk_map cases are now passing. \o/
2669
            [target1], [target3])
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2670
2671
    def test_multiple_maps(self):
2672
        basis1 = self.get_map_key({('aaa',): 'common',
2673
                                   ('aab',): 'basis1',
2674
                                  })
2675
        basis2 = self.get_map_key({('bbb',): 'common',
2676
                                   ('bbc',): 'basis2',
2677
                                  })
2678
        target1 = self.get_map_key({('aaa',): 'common',
2679
                                    ('aac',): 'target1',
2680
                                    ('bbb',): 'common',
2681
                                   })
2682
        target2 = self.get_map_key({('aaa',): 'common',
2683
                                    ('bba',): 'target2',
2684
                                    ('bbb',): 'common',
2685
                                   })
2686
        target1_map = CHKMap(self.get_chk_bytes(), target1)
2687
        self.assertEqualDiff(
2688
            "'' InternalNode\n"
2689
            "  'a' InternalNode\n"
2690
            "    'aaa' LeafNode\n"
2691
            "      ('aaa',) 'common'\n"
2692
            "    'aac' LeafNode\n"
2693
            "      ('aac',) 'target1'\n"
2694
            "  'b' LeafNode\n"
2695
            "      ('bbb',) 'common'\n",
2696
            target1_map._dump_tree())
2697
        # The key for the target1 internal a node
2698
        a_key = target1_map._root_node._items['a'].key()
2699
        # The key for the leaf aac node
2700
        aac_key = target1_map._root_node._items['a']._items['aac'].key()
2701
2702
        target2_map = CHKMap(self.get_chk_bytes(), target2)
2703
        self.assertEqualDiff(
2704
            "'' InternalNode\n"
2705
            "  'a' LeafNode\n"
2706
            "      ('aaa',) 'common'\n"
2707
            "  'b' InternalNode\n"
2708
            "    'bba' LeafNode\n"
2709
            "      ('bba',) 'target2'\n"
2710
            "    'bbb' LeafNode\n"
2711
            "      ('bbb',) 'common'\n",
2712
            target2_map._dump_tree())
2713
        # The key for the target2 internal bb node
2714
        b_key = target2_map._root_node._items['b'].key()
2715
        # The key for the leaf bba node
2716
        bba_key = target2_map._root_node._items['b']._items['bba'].key()
2717
        self.assertIterInteresting(
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2718
            [target1, target2, a_key, aac_key, b_key, bba_key],
2719
            [(('aac',), 'target1'), (('bba',), 'target2')],
2720
            [target1, target2], [basis1, basis2])
4431.3.8 by Jonathan Lange
Cherrypick bzr.dev r4477.
2721
2722
    def test_multiple_maps_overlapping_common_new(self):
2723
        # Test that when a node found through the interesting_keys iteration
4476.1.39 by John Arbash Meinel
Rename interesting => new, uninteresting => old
2724
        # for *some roots* and also via the old keys iteration, that
2725
        # it is still scanned for old refs and items, because its
4431.3.8 by Jonathan Lange
Cherrypick bzr.dev r4477.
2726
        # not truely new. This requires 2 levels of InternalNodes to expose,
2727
        # because of the way the bootstrap in _find_children_info works.
2728
        # This suggests that the code is probably amenable to/benefit from
2729
        # consolidation.
2730
        # How does this test work?
2731
        # 1) We need a second level InternalNode present in a basis tree.
2732
        # 2) We need a left side new tree that uses that InternalNode
2733
        # 3) We need a right side new tree that does not use that InternalNode
2734
        #    at all but that has an unchanged *value* that was reachable inside
2735
        #    that InternalNode
2736
        basis = self.get_map_key({
2737
            # InternalNode, unchanged in left:
2738
            ('aaa',): 'left',
2739
            ('abb',): 'right',
2740
            # Forces an internalNode at 'a'
2741
            ('ccc',): 'common',
2742
            })
2743
        left = self.get_map_key({
2744
            # All of basis unchanged
2745
            ('aaa',): 'left',
2746
            ('abb',): 'right',
2747
            ('ccc',): 'common',
2748
            # And a new top level node so the root key is different
2749
            ('ddd',): 'change',
2750
            })
2751
        right = self.get_map_key({
2752
            # A value that is unchanged from basis and thus should be filtered
2753
            # out.
2754
            ('abb',): 'right'
2755
            })
2756
        basis_map = CHKMap(self.get_chk_bytes(), basis)
2757
        self.assertEqualDiff(
2758
            "'' InternalNode\n"
2759
            "  'a' InternalNode\n"
2760
            "    'aa' LeafNode\n"
2761
            "      ('aaa',) 'left'\n"
2762
            "    'ab' LeafNode\n"
2763
            "      ('abb',) 'right'\n"
2764
            "  'c' LeafNode\n"
2765
            "      ('ccc',) 'common'\n",
2766
            basis_map._dump_tree())
2767
        # Get left expected data
2768
        left_map = CHKMap(self.get_chk_bytes(), left)
2769
        self.assertEqualDiff(
2770
            "'' InternalNode\n"
2771
            "  'a' InternalNode\n"
2772
            "    'aa' LeafNode\n"
2773
            "      ('aaa',) 'left'\n"
2774
            "    'ab' LeafNode\n"
2775
            "      ('abb',) 'right'\n"
2776
            "  'c' LeafNode\n"
2777
            "      ('ccc',) 'common'\n"
2778
            "  'd' LeafNode\n"
2779
            "      ('ddd',) 'change'\n",
2780
            left_map._dump_tree())
2781
        # Keys from left side target
2782
        l_d_key = left_map._root_node._items['d'].key()
2783
        # Get right expected data
2784
        right_map = CHKMap(self.get_chk_bytes(), right)
2785
        self.assertEqualDiff(
2786
            "'' LeafNode\n"
2787
            "      ('abb',) 'right'\n",
2788
            right_map._dump_tree())
2789
        # Keys from the right side target - none, the root is enough.
2790
        # Test behaviour
2791
        self.assertIterInteresting(
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2792
            [right, left, l_d_key],
2793
            [(('ddd',), 'change')],
2794
            [left, right], [basis])
4431.3.8 by Jonathan Lange
Cherrypick bzr.dev r4477.
2795
2796
    def test_multiple_maps_similar(self):
2797
        # We want to have a depth=2 tree, with multiple entries in each leaf
2798
        # node
2799
        basis = self.get_map_key({
2800
            ('aaa',): 'unchanged',
2801
            ('abb',): 'will change left',
2802
            ('caa',): 'unchanged',
2803
            ('cbb',): 'will change right',
2804
            }, maximum_size=60)
2805
        left = self.get_map_key({
2806
            ('aaa',): 'unchanged',
2807
            ('abb',): 'changed left',
2808
            ('caa',): 'unchanged',
2809
            ('cbb',): 'will change right',
2810
            }, maximum_size=60)
2811
        right = self.get_map_key({
2812
            ('aaa',): 'unchanged',
2813
            ('abb',): 'will change left',
2814
            ('caa',): 'unchanged',
2815
            ('cbb',): 'changed right',
2816
            }, maximum_size=60)
2817
        basis_map = CHKMap(self.get_chk_bytes(), basis)
2818
        self.assertEqualDiff(
2819
            "'' InternalNode\n"
2820
            "  'a' LeafNode\n"
2821
            "      ('aaa',) 'unchanged'\n"
2822
            "      ('abb',) 'will change left'\n"
2823
            "  'c' LeafNode\n"
2824
            "      ('caa',) 'unchanged'\n"
2825
            "      ('cbb',) 'will change right'\n",
2826
            basis_map._dump_tree())
2827
        # Get left expected data
2828
        left_map = CHKMap(self.get_chk_bytes(), left)
2829
        self.assertEqualDiff(
2830
            "'' InternalNode\n"
2831
            "  'a' LeafNode\n"
2832
            "      ('aaa',) 'unchanged'\n"
2833
            "      ('abb',) 'changed left'\n"
2834
            "  'c' LeafNode\n"
2835
            "      ('caa',) 'unchanged'\n"
2836
            "      ('cbb',) 'will change right'\n",
2837
            left_map._dump_tree())
2838
        # Keys from left side target
2839
        l_a_key = left_map._root_node._items['a'].key()
2840
        l_c_key = left_map._root_node._items['c'].key()
2841
        # Get right expected data
2842
        right_map = CHKMap(self.get_chk_bytes(), right)
2843
        self.assertEqualDiff(
2844
            "'' InternalNode\n"
2845
            "  'a' LeafNode\n"
2846
            "      ('aaa',) 'unchanged'\n"
2847
            "      ('abb',) 'will change left'\n"
2848
            "  'c' LeafNode\n"
2849
            "      ('caa',) 'unchanged'\n"
2850
            "      ('cbb',) 'changed right'\n",
2851
            right_map._dump_tree())
2852
        r_a_key = right_map._root_node._items['a'].key()
2853
        r_c_key = right_map._root_node._items['c'].key()
2854
        self.assertIterInteresting(
4476.1.26 by John Arbash Meinel
Change how assertIterInteresting works.
2855
            [right, left, l_a_key, r_c_key],
2856
            [(('abb',), 'changed left'), (('cbb',), 'changed right')],
2857
            [left, right], [basis])