~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_chk_map.py

  • Committer: Jelmer Vernooij
  • Date: 2009-06-09 00:59:51 UTC
  • mto: (4443.1.1 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 4444.
  • Revision ID: jelmer@samba.org-20090609005951-apv900cdk35o2ygh
Move squashing of XML-invalid characters to XMLSerializer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Tests for maps built on a CHK versionedfiles facility."""
18
18
 
 
19
from itertools import izip
 
20
 
19
21
from bzrlib import (
20
22
    chk_map,
21
 
    errors,
22
 
    groupcompress,
23
23
    osutils,
24
24
    tests,
25
25
    )
29
29
    LeafNode,
30
30
    Node,
31
31
    )
32
 
from bzrlib.static_tuple import StaticTuple
33
32
 
34
33
 
35
34
class TestNode(tests.TestCase):
60
59
        self.assertCommonPrefix('', '', '')
61
60
 
62
61
 
63
 
class TestCaseWithStore(tests.TestCaseWithMemoryTransport):
 
62
class TestCaseWithStore(tests.TestCaseWithTransport):
64
63
 
65
64
    def get_chk_bytes(self):
66
 
        # This creates a standalone CHK store.
67
 
        factory = groupcompress.make_pack_factory(False, False, 1)
68
 
        self.chk_bytes = factory(self.get_transport())
69
 
        return self.chk_bytes
 
65
        # The easiest way to get a CHK store is a development6 repository and
 
66
        # then work with the chk_bytes attribute directly.
 
67
        repo = self.make_repository(".", format="development6-rich-root")
 
68
        repo.lock_write()
 
69
        self.addCleanup(repo.unlock)
 
70
        repo.start_write_group()
 
71
        self.addCleanup(repo.abort_write_group)
 
72
        return repo.chk_bytes
70
73
 
71
74
    def _get_map(self, a_dict, maximum_size=0, chk_bytes=None, key_width=1,
72
75
                 search_key_func=None):
75
78
        root_key = CHKMap.from_dict(chk_bytes, a_dict,
76
79
            maximum_size=maximum_size, key_width=key_width,
77
80
            search_key_func=search_key_func)
78
 
        root_key2 = CHKMap._create_via_map(chk_bytes, a_dict,
79
 
            maximum_size=maximum_size, key_width=key_width,
80
 
            search_key_func=search_key_func)
81
 
        self.assertEqual(root_key, root_key2, "CHKMap.from_dict() did not"
82
 
                         " match CHKMap._create_via_map")
83
81
        chkmap = CHKMap(chk_bytes, root_key, search_key_func=search_key_func)
84
82
        return chkmap
85
83
 
94
92
        return dict(node.iteritems(*args))
95
93
 
96
94
 
97
 
class TestCaseWithExampleMaps(TestCaseWithStore):
98
 
 
99
 
    def get_chk_bytes(self):
100
 
        if getattr(self, '_chk_bytes', None) is None:
101
 
            self._chk_bytes = super(TestCaseWithExampleMaps,
102
 
                                    self).get_chk_bytes()
103
 
        return self._chk_bytes
104
 
 
105
 
    def get_map(self, a_dict, maximum_size=100, search_key_func=None):
106
 
        c_map = self._get_map(a_dict, maximum_size=maximum_size,
107
 
                              chk_bytes=self.get_chk_bytes(),
108
 
                              search_key_func=search_key_func)
109
 
        return c_map
110
 
 
111
 
    def make_root_only_map(self, search_key_func=None):
112
 
        return self.get_map({
113
 
            ('aaa',): 'initial aaa content',
114
 
            ('abb',): 'initial abb content',
115
 
        }, search_key_func=search_key_func)
116
 
 
117
 
    def make_root_only_aaa_ddd_map(self, search_key_func=None):
118
 
        return self.get_map({
119
 
            ('aaa',): 'initial aaa content',
120
 
            ('ddd',): 'initial ddd content',
121
 
        }, search_key_func=search_key_func)
122
 
 
123
 
    def make_one_deep_map(self, search_key_func=None):
124
 
        # Same as root_only_map, except it forces an InternalNode at the root
125
 
        return self.get_map({
126
 
            ('aaa',): 'initial aaa content',
127
 
            ('abb',): 'initial abb content',
128
 
            ('ccc',): 'initial ccc content',
129
 
            ('ddd',): 'initial ddd content',
130
 
        }, search_key_func=search_key_func)
131
 
 
132
 
    def make_two_deep_map(self, search_key_func=None):
133
 
        # Carefully chosen so that it creates a 2-deep map for both
134
 
        # _search_key_plain and for _search_key_16
135
 
        # Also so that things line up with make_one_deep_two_prefix_map
136
 
        return self.get_map({
137
 
            ('aaa',): 'initial aaa content',
138
 
            ('abb',): 'initial abb content',
139
 
            ('acc',): 'initial acc content',
140
 
            ('ace',): 'initial ace content',
141
 
            ('add',): 'initial add content',
142
 
            ('adh',): 'initial adh content',
143
 
            ('adl',): 'initial adl content',
144
 
            ('ccc',): 'initial ccc content',
145
 
            ('ddd',): 'initial ddd content',
146
 
        }, search_key_func=search_key_func)
147
 
 
148
 
    def make_one_deep_two_prefix_map(self, search_key_func=None):
149
 
        """Create a map with one internal node, but references are extra long.
150
 
 
151
 
        Otherwise has similar content to make_two_deep_map.
152
 
        """
153
 
        return self.get_map({
154
 
            ('aaa',): 'initial aaa content',
155
 
            ('add',): 'initial add content',
156
 
            ('adh',): 'initial adh content',
157
 
            ('adl',): 'initial adl content',
158
 
        }, search_key_func=search_key_func)
159
 
 
160
 
    def make_one_deep_one_prefix_map(self, search_key_func=None):
161
 
        """Create a map with one internal node, but references are extra long.
162
 
 
163
 
        Similar to make_one_deep_two_prefix_map, except the split is at the
164
 
        first char, rather than the second.
165
 
        """
166
 
        return self.get_map({
167
 
            ('add',): 'initial add content',
168
 
            ('adh',): 'initial adh content',
169
 
            ('adl',): 'initial adl content',
170
 
            ('bbb',): 'initial bbb content',
171
 
        }, search_key_func=search_key_func)
172
 
 
173
 
 
174
 
class TestTestCaseWithExampleMaps(TestCaseWithExampleMaps):
175
 
    """Actual tests for the provided examples."""
176
 
 
177
 
    def test_root_only_map_plain(self):
178
 
        c_map = self.make_root_only_map()
179
 
        self.assertEqualDiff(
180
 
            "'' LeafNode\n"
181
 
            "      ('aaa',) 'initial aaa content'\n"
182
 
            "      ('abb',) 'initial abb content'\n",
183
 
            c_map._dump_tree())
184
 
 
185
 
    def test_root_only_map_16(self):
186
 
        c_map = self.make_root_only_map(search_key_func=chk_map._search_key_16)
187
 
        self.assertEqualDiff(
188
 
            "'' LeafNode\n"
189
 
            "      ('aaa',) 'initial aaa content'\n"
190
 
            "      ('abb',) 'initial abb content'\n",
191
 
            c_map._dump_tree())
192
 
 
193
 
    def test_one_deep_map_plain(self):
194
 
        c_map = self.make_one_deep_map()
195
 
        self.assertEqualDiff(
196
 
            "'' InternalNode\n"
197
 
            "  'a' LeafNode\n"
198
 
            "      ('aaa',) 'initial aaa content'\n"
199
 
            "      ('abb',) 'initial abb content'\n"
200
 
            "  'c' LeafNode\n"
201
 
            "      ('ccc',) 'initial ccc content'\n"
202
 
            "  'd' LeafNode\n"
203
 
            "      ('ddd',) 'initial ddd content'\n",
204
 
            c_map._dump_tree())
205
 
 
206
 
    def test_one_deep_map_16(self):
207
 
        c_map = self.make_one_deep_map(search_key_func=chk_map._search_key_16)
208
 
        self.assertEqualDiff(
209
 
            "'' InternalNode\n"
210
 
            "  '2' LeafNode\n"
211
 
            "      ('ccc',) 'initial ccc content'\n"
212
 
            "  '4' LeafNode\n"
213
 
            "      ('abb',) 'initial abb content'\n"
214
 
            "  'F' LeafNode\n"
215
 
            "      ('aaa',) 'initial aaa content'\n"
216
 
            "      ('ddd',) 'initial ddd content'\n",
217
 
            c_map._dump_tree())
218
 
 
219
 
    def test_root_only_aaa_ddd_plain(self):
220
 
        c_map = self.make_root_only_aaa_ddd_map()
221
 
        self.assertEqualDiff(
222
 
            "'' LeafNode\n"
223
 
            "      ('aaa',) 'initial aaa content'\n"
224
 
            "      ('ddd',) 'initial ddd content'\n",
225
 
            c_map._dump_tree())
226
 
 
227
 
    def test_root_only_aaa_ddd_16(self):
228
 
        c_map = self.make_root_only_aaa_ddd_map(
229
 
                search_key_func=chk_map._search_key_16)
230
 
        # We use 'aaa' and 'ddd' because they happen to map to 'F' when using
231
 
        # _search_key_16
232
 
        self.assertEqualDiff(
233
 
            "'' LeafNode\n"
234
 
            "      ('aaa',) 'initial aaa content'\n"
235
 
            "      ('ddd',) 'initial ddd content'\n",
236
 
            c_map._dump_tree())
237
 
 
238
 
    def test_two_deep_map_plain(self):
239
 
        c_map = self.make_two_deep_map()
240
 
        self.assertEqualDiff(
241
 
            "'' InternalNode\n"
242
 
            "  'a' InternalNode\n"
243
 
            "    'aa' LeafNode\n"
244
 
            "      ('aaa',) 'initial aaa content'\n"
245
 
            "    'ab' LeafNode\n"
246
 
            "      ('abb',) 'initial abb content'\n"
247
 
            "    'ac' LeafNode\n"
248
 
            "      ('acc',) 'initial acc content'\n"
249
 
            "      ('ace',) 'initial ace content'\n"
250
 
            "    'ad' LeafNode\n"
251
 
            "      ('add',) 'initial add content'\n"
252
 
            "      ('adh',) 'initial adh content'\n"
253
 
            "      ('adl',) 'initial adl content'\n"
254
 
            "  'c' LeafNode\n"
255
 
            "      ('ccc',) 'initial ccc content'\n"
256
 
            "  'd' LeafNode\n"
257
 
            "      ('ddd',) 'initial ddd content'\n",
258
 
            c_map._dump_tree())
259
 
 
260
 
    def test_two_deep_map_16(self):
261
 
        c_map = self.make_two_deep_map(search_key_func=chk_map._search_key_16)
262
 
        self.assertEqualDiff(
263
 
            "'' InternalNode\n"
264
 
            "  '2' LeafNode\n"
265
 
            "      ('acc',) 'initial acc content'\n"
266
 
            "      ('ccc',) 'initial ccc content'\n"
267
 
            "  '4' LeafNode\n"
268
 
            "      ('abb',) 'initial abb content'\n"
269
 
            "  'C' LeafNode\n"
270
 
            "      ('ace',) 'initial ace content'\n"
271
 
            "  'F' InternalNode\n"
272
 
            "    'F0' LeafNode\n"
273
 
            "      ('aaa',) 'initial aaa content'\n"
274
 
            "    'F3' LeafNode\n"
275
 
            "      ('adl',) 'initial adl content'\n"
276
 
            "    'F4' LeafNode\n"
277
 
            "      ('adh',) 'initial adh content'\n"
278
 
            "    'FB' LeafNode\n"
279
 
            "      ('ddd',) 'initial ddd content'\n"
280
 
            "    'FD' LeafNode\n"
281
 
            "      ('add',) 'initial add content'\n",
282
 
            c_map._dump_tree())
283
 
 
284
 
    def test_one_deep_two_prefix_map_plain(self):
285
 
        c_map = self.make_one_deep_two_prefix_map()
286
 
        self.assertEqualDiff(
287
 
            "'' InternalNode\n"
288
 
            "  'aa' LeafNode\n"
289
 
            "      ('aaa',) 'initial aaa content'\n"
290
 
            "  'ad' LeafNode\n"
291
 
            "      ('add',) 'initial add content'\n"
292
 
            "      ('adh',) 'initial adh content'\n"
293
 
            "      ('adl',) 'initial adl content'\n",
294
 
            c_map._dump_tree())
295
 
 
296
 
    def test_one_deep_two_prefix_map_16(self):
297
 
        c_map = self.make_one_deep_two_prefix_map(
298
 
            search_key_func=chk_map._search_key_16)
299
 
        self.assertEqualDiff(
300
 
            "'' InternalNode\n"
301
 
            "  'F0' LeafNode\n"
302
 
            "      ('aaa',) 'initial aaa content'\n"
303
 
            "  'F3' LeafNode\n"
304
 
            "      ('adl',) 'initial adl content'\n"
305
 
            "  'F4' LeafNode\n"
306
 
            "      ('adh',) 'initial adh content'\n"
307
 
            "  'FD' LeafNode\n"
308
 
            "      ('add',) 'initial add content'\n",
309
 
            c_map._dump_tree())
310
 
 
311
 
    def test_one_deep_one_prefix_map_plain(self):
312
 
        c_map = self.make_one_deep_one_prefix_map()
313
 
        self.assertEqualDiff(
314
 
            "'' InternalNode\n"
315
 
            "  'a' LeafNode\n"
316
 
            "      ('add',) 'initial add content'\n"
317
 
            "      ('adh',) 'initial adh content'\n"
318
 
            "      ('adl',) 'initial adl content'\n"
319
 
            "  'b' LeafNode\n"
320
 
            "      ('bbb',) 'initial bbb content'\n",
321
 
            c_map._dump_tree())
322
 
 
323
 
    def test_one_deep_one_prefix_map_16(self):
324
 
        c_map = self.make_one_deep_one_prefix_map(
325
 
            search_key_func=chk_map._search_key_16)
326
 
        self.assertEqualDiff(
327
 
            "'' InternalNode\n"
328
 
            "  '4' LeafNode\n"
329
 
            "      ('bbb',) 'initial bbb content'\n"
330
 
            "  'F' LeafNode\n"
331
 
            "      ('add',) 'initial add content'\n"
332
 
            "      ('adh',) 'initial adh content'\n"
333
 
            "      ('adl',) 'initial adl content'\n",
334
 
            c_map._dump_tree())
335
 
 
336
 
 
337
95
class TestMap(TestCaseWithStore):
338
96
 
339
97
    def assertHasABMap(self, chk_bytes):
465
223
        # updated key.
466
224
        self.assertEqual(new_root, chkmap._root_node._key)
467
225
 
468
 
    def test_apply_delete_to_internal_node(self):
469
 
        # applying a delta should be convert an internal root node to a leaf
470
 
        # node if the delta shrinks the map enough.
471
 
        store = self.get_chk_bytes()
472
 
        chkmap = CHKMap(store, None)
473
 
        # Add three items: 2 small enough to fit in one node, and one huge to
474
 
        # force multiple nodes.
475
 
        chkmap._root_node.set_maximum_size(100)
476
 
        chkmap.map(('small',), 'value')
477
 
        chkmap.map(('little',), 'value')
478
 
        chkmap.map(('very-big',), 'x' * 100)
479
 
        # (Check that we have constructed the scenario we want to test)
480
 
        self.assertIsInstance(chkmap._root_node, InternalNode)
481
 
        # Delete the huge item so that the map fits in one node again.
482
 
        delta = [(('very-big',), None, None)]
483
 
        chkmap.apply_delta(delta)
484
 
        self.assertCanonicalForm(chkmap)
485
 
        self.assertIsInstance(chkmap._root_node, LeafNode)
486
 
 
487
 
    def test_apply_new_keys_must_be_new(self):
488
 
        # applying a delta (None, "a", "b") to a map with 'a' in it generates
489
 
        # an error.
490
 
        chk_bytes = self.get_chk_bytes()
491
 
        root_key = CHKMap.from_dict(chk_bytes, {("a",):"b"})
492
 
        chkmap = CHKMap(chk_bytes, root_key)
493
 
        self.assertRaises(errors.InconsistentDelta, chkmap.apply_delta,
494
 
            [(None, ("a",), "b")])
495
 
        # As an error occured, the update should have left us without changing
496
 
        # anything (the root should be unchanged).
497
 
        self.assertEqual(root_key, chkmap._root_node._key)
498
 
 
499
226
    def test_apply_delta_is_deterministic(self):
500
227
        chk_bytes = self.get_chk_bytes()
501
228
        chkmap1 = CHKMap(chk_bytes, None)
849
576
        # 'ab' and 'ac' nodes
850
577
        chkmap.map(('aad',), 'v')
851
578
        self.assertIsInstance(chkmap._root_node._items['aa'], InternalNode)
852
 
        self.assertIsInstance(chkmap._root_node._items['ab'], StaticTuple)
853
 
        self.assertIsInstance(chkmap._root_node._items['ac'], StaticTuple)
 
579
        self.assertIsInstance(chkmap._root_node._items['ab'], tuple)
 
580
        self.assertIsInstance(chkmap._root_node._items['ac'], tuple)
854
581
        # Unmapping 'acd' can notice that 'aa' is an InternalNode and not have
855
582
        # to map in 'ab'
856
583
        chkmap.unmap(('acd',))
857
584
        self.assertIsInstance(chkmap._root_node._items['aa'], InternalNode)
858
 
        self.assertIsInstance(chkmap._root_node._items['ab'], StaticTuple)
 
585
        self.assertIsInstance(chkmap._root_node._items['ab'], tuple)
859
586
 
860
587
    def test_unmap_without_fitting_doesnt_page_in(self):
861
588
        store = self.get_chk_bytes()
878
605
        chkmap.map(('aaf',), 'v')
879
606
        # At this point, the previous nodes should not be paged in, but the
880
607
        # newly added nodes would be
881
 
        self.assertIsInstance(chkmap._root_node._items['aaa'], StaticTuple)
882
 
        self.assertIsInstance(chkmap._root_node._items['aab'], StaticTuple)
 
608
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
 
609
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
883
610
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
884
611
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
885
612
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
887
614
        # Now unmapping one of the new nodes will use only the already-paged-in
888
615
        # nodes to determine that we don't need to do more.
889
616
        chkmap.unmap(('aaf',))
890
 
        self.assertIsInstance(chkmap._root_node._items['aaa'], StaticTuple)
891
 
        self.assertIsInstance(chkmap._root_node._items['aab'], StaticTuple)
 
617
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
 
618
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
892
619
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
893
620
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
894
621
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
915
642
        chkmap.map(('aad',), 'v')
916
643
        # At this point, the previous nodes should not be paged in, but the
917
644
        # newly added node would be
918
 
        self.assertIsInstance(chkmap._root_node._items['aaa'], StaticTuple)
919
 
        self.assertIsInstance(chkmap._root_node._items['aab'], StaticTuple)
920
 
        self.assertIsInstance(chkmap._root_node._items['aac'], StaticTuple)
 
645
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
 
646
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
 
647
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
921
648
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
922
649
        # Unmapping the new node will check the existing nodes to see if they
923
650
        # would fit.
924
651
        # Clear the page cache so we ensure we have to read all the children
925
 
        chk_map.clear_cache()
 
652
        chk_map._page_cache.clear()
926
653
        chkmap.unmap(('aad',))
927
654
        self.assertIsInstance(chkmap._root_node._items['aaa'], LeafNode)
928
655
        self.assertIsInstance(chkmap._root_node._items['aab'], LeafNode)
955
682
        chkmap.map(('aad',), 'v')
956
683
        # At this point, the previous nodes should not be paged in, but the
957
684
        # newly added node would be
958
 
        self.assertIsInstance(chkmap._root_node._items['aaa'], StaticTuple)
959
 
        self.assertIsInstance(chkmap._root_node._items['aab'], StaticTuple)
960
 
        self.assertIsInstance(chkmap._root_node._items['aac'], StaticTuple)
 
685
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
 
686
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
 
687
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
961
688
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
962
689
        # Now clear the page cache, and only include 2 of the children in the
963
690
        # cache
964
691
        aab_key = chkmap._root_node._items['aab']
965
 
        aab_bytes = chk_map._get_cache()[aab_key]
 
692
        aab_bytes = chk_map._page_cache[aab_key]
966
693
        aac_key = chkmap._root_node._items['aac']
967
 
        aac_bytes = chk_map._get_cache()[aac_key]
968
 
        chk_map.clear_cache()
969
 
        chk_map._get_cache()[aab_key] = aab_bytes
970
 
        chk_map._get_cache()[aac_key] = aac_bytes
 
694
        aac_bytes = chk_map._page_cache[aac_key]
 
695
        chk_map._page_cache.clear()
 
696
        chk_map._page_cache[aab_key] = aab_bytes
 
697
        chk_map._page_cache[aac_key] = aac_bytes
971
698
 
972
699
        # Unmapping the new node will check the nodes from the page cache
973
700
        # first, and not have to read in 'aaa'
974
701
        chkmap.unmap(('aad',))
975
 
        self.assertIsInstance(chkmap._root_node._items['aaa'], StaticTuple)
 
702
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
976
703
        self.assertIsInstance(chkmap._root_node._items['aab'], LeafNode)
977
704
        self.assertIsInstance(chkmap._root_node._items['aac'], LeafNode)
978
705
 
992
719
        chkmap.map(('aaf',), 'val')
993
720
        # At this point, the previous nodes should not be paged in, but the
994
721
        # newly added node would be
995
 
        self.assertIsInstance(chkmap._root_node._items['aaa'], StaticTuple)
996
 
        self.assertIsInstance(chkmap._root_node._items['aab'], StaticTuple)
997
 
        self.assertIsInstance(chkmap._root_node._items['aac'], StaticTuple)
 
722
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
 
723
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
 
724
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
998
725
        self.assertIsInstance(chkmap._root_node._items['aad'], LeafNode)
999
726
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
1000
727
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
1002
729
        # Unmapping a new node will see the other nodes that are already in
1003
730
        # memory, and not need to page in anything else
1004
731
        chkmap.unmap(('aad',))
1005
 
        self.assertIsInstance(chkmap._root_node._items['aaa'], StaticTuple)
1006
 
        self.assertIsInstance(chkmap._root_node._items['aab'], StaticTuple)
1007
 
        self.assertIsInstance(chkmap._root_node._items['aac'], StaticTuple)
 
732
        self.assertIsInstance(chkmap._root_node._items['aaa'], tuple)
 
733
        self.assertIsInstance(chkmap._root_node._items['aab'], tuple)
 
734
        self.assertIsInstance(chkmap._root_node._items['aac'], tuple)
1008
735
        self.assertIsInstance(chkmap._root_node._items['aae'], LeafNode)
1009
736
        self.assertIsInstance(chkmap._root_node._items['aaf'], LeafNode)
1010
737
 
1049
776
            {('a',): 'content here', ('b',): 'more content'},
1050
777
            chk_bytes=basis._store, maximum_size=10)
1051
778
        list(target.iter_changes(basis))
1052
 
        self.assertIsInstance(target._root_node, StaticTuple)
1053
 
        self.assertIsInstance(basis._root_node, StaticTuple)
 
779
        self.assertIsInstance(target._root_node, tuple)
 
780
        self.assertIsInstance(basis._root_node, tuple)
1054
781
 
1055
782
    def test_iter_changes_ab_ab_changed_values_shown(self):
1056
783
        basis = self._get_map({('a',): 'content here', ('b',): 'more content'},
1108
835
        basis_get = basis._store.get_record_stream
1109
836
        def get_record_stream(keys, order, fulltext):
1110
837
            if ('sha1:1adf7c0d1b9140ab5f33bb64c6275fa78b1580b7',) in keys:
1111
 
                raise AssertionError("'aaa' pointer was followed %r" % keys)
 
838
                self.fail("'aaa' pointer was followed %r" % keys)
1112
839
            return basis_get(keys, order, fulltext)
1113
840
        basis._store.get_record_stream = get_record_stream
1114
841
        result = sorted(list(target.iter_changes(basis)))
1162
889
 
1163
890
    def test_iteritems_keys_prefixed_by_2_width_nodes_hashed(self):
1164
891
        search_key_func = chk_map.search_key_registry.get('hash-16-way')
1165
 
        self.assertEqual('E8B7BE43\x00E8B7BE43',
1166
 
                         search_key_func(StaticTuple('a', 'a')))
1167
 
        self.assertEqual('E8B7BE43\x0071BEEFF9',
1168
 
                         search_key_func(StaticTuple('a', 'b')))
1169
 
        self.assertEqual('71BEEFF9\x0000000000',
1170
 
                         search_key_func(StaticTuple('b', '')))
 
892
        self.assertEqual('E8B7BE43\x00E8B7BE43', search_key_func(('a', 'a')))
 
893
        self.assertEqual('E8B7BE43\x0071BEEFF9', search_key_func(('a', 'b')))
 
894
        self.assertEqual('71BEEFF9\x0000000000', search_key_func(('b', '')))
1171
895
        chkmap = self._get_map(
1172
896
            {("a","a"):"content here", ("a", "b",):"more content",
1173
897
             ("b", ""): 'boring content'},
1470
1194
                             , chkmap._dump_tree())
1471
1195
 
1472
1196
 
 
1197
class TestSearchKeyFuncs(tests.TestCase):
 
1198
 
 
1199
    def assertSearchKey16(self, expected, key):
 
1200
        self.assertEqual(expected, chk_map._search_key_16(key))
 
1201
 
 
1202
    def assertSearchKey255(self, expected, key):
 
1203
        actual = chk_map._search_key_255(key)
 
1204
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
 
1205
 
 
1206
    def test_simple_16(self):
 
1207
        self.assertSearchKey16('8C736521', ('foo',))
 
1208
        self.assertSearchKey16('8C736521\x008C736521', ('foo', 'foo'))
 
1209
        self.assertSearchKey16('8C736521\x0076FF8CAA', ('foo', 'bar'))
 
1210
        self.assertSearchKey16('ED82CD11', ('abcd',))
 
1211
 
 
1212
    def test_simple_255(self):
 
1213
        self.assertSearchKey255('\x8cse!', ('foo',))
 
1214
        self.assertSearchKey255('\x8cse!\x00\x8cse!', ('foo', 'foo'))
 
1215
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', ('foo', 'bar'))
 
1216
        # The standard mapping for these would include '\n', so it should be
 
1217
        # mapped to '_'
 
1218
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', ('<', 'V'))
 
1219
 
 
1220
    def test_255_does_not_include_newline(self):
 
1221
        # When mapping via _search_key_255, we should never have the '\n'
 
1222
        # character, but all other 255 values should be present
 
1223
        chars_used = set()
 
1224
        for char_in in range(256):
 
1225
            search_key = chk_map._search_key_255((chr(char_in),))
 
1226
            chars_used.update(search_key)
 
1227
        all_chars = set([chr(x) for x in range(256)])
 
1228
        unused_chars = all_chars.symmetric_difference(chars_used)
 
1229
        self.assertEqual(set('\n'), unused_chars)
 
1230
 
 
1231
 
1473
1232
class TestLeafNode(TestCaseWithStore):
1474
1233
 
1475
1234
    def test_current_size_empty(self):
1801
1560
        child.map(None, ("baz",), "val")
1802
1561
        node.add_node("b", child)
1803
1562
 
1804
 
        # Note that 'ram' doesn't match anything, so it should be freely
1805
 
        # ignored
1806
 
        key_filter = (('foo',), ('fob',), ('bar',), ('baz',), ('ram',))
 
1563
        key_filter = (('foo',), ('fob',), ('bar',), ('baz',))
1807
1564
        for child, node_key_filter in node._iter_nodes(None,
1808
1565
                                                       key_filter=key_filter):
1809
 
            # each child could match two key filters, so make sure they were
 
1566
            # each child could matches two key filters, so make sure they were
1810
1567
            # both included.
1811
1568
            self.assertEqual(2, len(node_key_filter))
1812
1569
 
1813
 
    def make_fo_fa_node(self):
1814
 
        node = InternalNode('f')
1815
 
        child = LeafNode()
1816
 
        child.set_maximum_size(100)
1817
 
        child.map(None, ("foo",), "val")
1818
 
        child.map(None, ("fob",), "val")
1819
 
        node.add_node('fo', child)
1820
 
        child = LeafNode()
1821
 
        child.set_maximum_size(100)
1822
 
        child.map(None, ("far",), "val")
1823
 
        child.map(None, ("faz",), "val")
1824
 
        node.add_node("fa", child)
1825
 
        return node
1826
 
 
1827
 
    def test__iter_nodes_single_entry(self):
1828
 
        node = self.make_fo_fa_node()
1829
 
        key_filter = [('foo',)]
1830
 
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1831
 
        self.assertEqual(1, len(nodes))
1832
 
        self.assertEqual(key_filter, nodes[0][1])
1833
 
 
1834
 
    def test__iter_nodes_single_entry_misses(self):
1835
 
        node = self.make_fo_fa_node()
1836
 
        key_filter = [('bar',)]
1837
 
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1838
 
        self.assertEqual(0, len(nodes))
1839
 
 
1840
 
    def test__iter_nodes_mixed_key_width(self):
1841
 
        node = self.make_fo_fa_node()
1842
 
        key_filter = [('foo', 'bar'), ('foo',), ('fo',), ('b',)]
1843
 
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1844
 
        self.assertEqual(1, len(nodes))
1845
 
        matches = key_filter[:]
1846
 
        matches.remove(('b',))
1847
 
        self.assertEqual(sorted(matches), sorted(nodes[0][1]))
1848
 
 
1849
 
    def test__iter_nodes_match_all(self):
1850
 
        node = self.make_fo_fa_node()
1851
 
        key_filter = [('foo', 'bar'), ('foo',), ('fo',), ('f',)]
1852
 
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1853
 
        self.assertEqual(2, len(nodes))
1854
 
 
1855
 
    def test__iter_nodes_fixed_widths_and_misses(self):
1856
 
        node = self.make_fo_fa_node()
1857
 
        # foo and faa should both match one child, baz should miss
1858
 
        key_filter = [('foo',), ('faa',), ('baz',)]
1859
 
        nodes = list(node._iter_nodes(None, key_filter=key_filter))
1860
 
        self.assertEqual(2, len(nodes))
1861
 
        for node, matches in nodes:
1862
 
            self.assertEqual(1, len(matches))
1863
 
 
1864
1570
    def test_iteritems_empty_new(self):
1865
1571
        node = InternalNode()
1866
1572
        self.assertEqual([], sorted(node.iteritems(None)))
1894
1600
        search_key_func = chk_map.search_key_registry.get('hash-255-way')
1895
1601
        node = InternalNode(search_key_func=search_key_func)
1896
1602
        leaf1 = LeafNode(search_key_func=search_key_func)
1897
 
        leaf1.map(None, StaticTuple('foo bar',), 'quux')
 
1603
        leaf1.map(None, ('foo bar',), 'quux')
1898
1604
        leaf2 = LeafNode(search_key_func=search_key_func)
1899
 
        leaf2.map(None, StaticTuple('strange',), 'beast')
1900
 
        self.assertEqual('\xbeF\x014', search_key_func(StaticTuple('foo bar',)))
1901
 
        self.assertEqual('\x85\xfa\xf7K', search_key_func(StaticTuple('strange',)))
 
1605
        leaf2.map(None, ('strange',), 'beast')
 
1606
        self.assertEqual('\xbeF\x014', search_key_func(('foo bar',)))
 
1607
        self.assertEqual('\x85\xfa\xf7K', search_key_func(('strange',)))
1902
1608
        node.add_node("\xbe", leaf1)
1903
1609
        # This sets up a path that should not be followed - it will error if
1904
1610
        # the code tries to.
1905
1611
        node._items['\xbe'] = None
1906
1612
        node.add_node("\x85", leaf2)
1907
1613
        self.assertEqual([(('strange',), 'beast')],
1908
 
            sorted(node.iteritems(None, [StaticTuple('strange',),
1909
 
                                         StaticTuple('weird',)])))
 
1614
            sorted(node.iteritems(None, [('strange',), ('weird',)])))
1910
1615
 
1911
1616
    def test_iteritems_partial_empty(self):
1912
1617
        node = InternalNode()
1919
1624
        # Ensure test validity: nothing paged in below the root.
1920
1625
        self.assertEqual(2,
1921
1626
            len([value for value in node._items.values()
1922
 
                if type(value) is StaticTuple]))
 
1627
                if type(value) == tuple]))
1923
1628
        # now, mapping to k3 should add a k3 leaf
1924
1629
        prefix, nodes = node.map(None, ('k3',), 'quux')
1925
1630
        self.assertEqual("k", prefix)
1958
1663
        # Ensure test validity: nothing paged in below the root.
1959
1664
        self.assertEqual(2,
1960
1665
            len([value for value in node._items.values()
1961
 
                if type(value) is StaticTuple]))
 
1666
                if type(value) == tuple]))
1962
1667
        # now, mapping to k23 causes k22 ('k2' in node) to split into k22 and
1963
1668
        # k23, which for simplicity in the current implementation generates
1964
1669
        # a new internal node between node, and k22/k23.
2003
1708
        node = InternalNode(search_key_func=search_key_func)
2004
1709
        node._key_width = 2
2005
1710
        node._node_width = 4
2006
 
        self.assertEqual('E8B7BE43\x0071BEEFF9', search_key_func(
2007
 
            StaticTuple('a', 'b')))
2008
 
        self.assertEqual('E8B7', node._search_prefix_filter(
2009
 
            StaticTuple('a', 'b')))
2010
 
        self.assertEqual('E8B7', node._search_prefix_filter(
2011
 
            StaticTuple('a',)))
 
1711
        self.assertEqual('E8B7BE43\x0071BEEFF9', search_key_func(('a', 'b')))
 
1712
        self.assertEqual('E8B7', node._search_prefix_filter(('a', 'b')))
 
1713
        self.assertEqual('E8B7', node._search_prefix_filter(('a',)))
2012
1714
 
2013
1715
    def test_unmap_k23_from_k1_k22_k23_gives_k1_k22_tree_new(self):
2014
1716
        chkmap = self._get_map(
2126
1828
# 1-4K get0
2127
1829
 
2128
1830
 
2129
 
class TestCHKMapDifference(TestCaseWithExampleMaps):
2130
 
 
2131
 
    def get_difference(self, new_roots, old_roots,
2132
 
                       search_key_func=None):
2133
 
        if search_key_func is None:
2134
 
            search_key_func = chk_map._search_key_plain
2135
 
        return chk_map.CHKMapDifference(self.get_chk_bytes(),
2136
 
            new_roots, old_roots, search_key_func)
2137
 
 
2138
 
    def test__init__(self):
2139
 
        c_map = self.make_root_only_map()
2140
 
        key1 = c_map.key()
2141
 
        c_map.map(('aaa',), 'new aaa content')
2142
 
        key2 = c_map._save()
2143
 
        diff = self.get_difference([key2], [key1])
2144
 
        self.assertEqual(set([key1]), diff._all_old_chks)
2145
 
        self.assertEqual([], diff._old_queue)
2146
 
        self.assertEqual([], diff._new_queue)
2147
 
 
2148
 
    def help__read_all_roots(self, search_key_func):
2149
 
        c_map = self.make_root_only_map(search_key_func=search_key_func)
2150
 
        key1 = c_map.key()
2151
 
        c_map.map(('aaa',), 'new aaa content')
2152
 
        key2 = c_map._save()
2153
 
        diff = self.get_difference([key2], [key1], search_key_func)
2154
 
        root_results = [record.key for record in diff._read_all_roots()]
2155
 
        self.assertEqual([key2], root_results)
2156
 
        # We should have queued up only items that aren't in the old
2157
 
        # set
2158
 
        self.assertEqual([(('aaa',), 'new aaa content')],
2159
 
                         diff._new_item_queue)
2160
 
        self.assertEqual([], diff._new_queue)
2161
 
        # And there are no old references, so that queue should be
2162
 
        # empty
2163
 
        self.assertEqual([], diff._old_queue)
2164
 
 
2165
 
    def test__read_all_roots_plain(self):
2166
 
        self.help__read_all_roots(search_key_func=chk_map._search_key_plain)
2167
 
 
2168
 
    def test__read_all_roots_16(self):
2169
 
        self.help__read_all_roots(search_key_func=chk_map._search_key_16)
2170
 
 
2171
 
    def test__read_all_roots_skips_known_old(self):
2172
 
        c_map = self.make_one_deep_map(chk_map._search_key_plain)
2173
 
        key1 = c_map.key()
2174
 
        c_map2 = self.make_root_only_map(chk_map._search_key_plain)
2175
 
        key2 = c_map2.key()
2176
 
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2177
 
        root_results = [record.key for record in diff._read_all_roots()]
2178
 
        # We should have no results. key2 is completely contained within key1,
2179
 
        # and we should have seen that in the first pass
2180
 
        self.assertEqual([], root_results)
2181
 
 
2182
 
    def test__read_all_roots_prepares_queues(self):
2183
 
        c_map = self.make_one_deep_map(chk_map._search_key_plain)
2184
 
        key1 = c_map.key()
2185
 
        c_map._dump_tree() # load everything
2186
 
        key1_a = c_map._root_node._items['a'].key()
2187
 
        c_map.map(('abb',), 'new abb content')
2188
 
        key2 = c_map._save()
2189
 
        key2_a = c_map._root_node._items['a'].key()
2190
 
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2191
 
        root_results = [record.key for record in diff._read_all_roots()]
2192
 
        self.assertEqual([key2], root_results)
2193
 
        # At this point, we should have queued up only the 'a' Leaf on both
2194
 
        # sides, both 'c' and 'd' are known to not have changed on both sides
2195
 
        self.assertEqual([key2_a], diff._new_queue)
2196
 
        self.assertEqual([], diff._new_item_queue)
2197
 
        self.assertEqual([key1_a], diff._old_queue)
2198
 
 
2199
 
    def test__read_all_roots_multi_new_prepares_queues(self):
2200
 
        c_map = self.make_one_deep_map(chk_map._search_key_plain)
2201
 
        key1 = c_map.key()
2202
 
        c_map._dump_tree() # load everything
2203
 
        key1_a = c_map._root_node._items['a'].key()
2204
 
        key1_c = c_map._root_node._items['c'].key()
2205
 
        c_map.map(('abb',), 'new abb content')
2206
 
        key2 = c_map._save()
2207
 
        key2_a = c_map._root_node._items['a'].key()
2208
 
        key2_c = c_map._root_node._items['c'].key()
2209
 
        c_map = chk_map.CHKMap(self.get_chk_bytes(), key1,
2210
 
                               chk_map._search_key_plain)
2211
 
        c_map.map(('ccc',), 'new ccc content')
2212
 
        key3 = c_map._save()
2213
 
        key3_a = c_map._root_node._items['a'].key()
2214
 
        key3_c = c_map._root_node._items['c'].key()
2215
 
        diff = self.get_difference([key2, key3], [key1],
2216
 
                                   chk_map._search_key_plain)
2217
 
        root_results = [record.key for record in diff._read_all_roots()]
2218
 
        self.assertEqual(sorted([key2, key3]), sorted(root_results))
2219
 
        # We should have queued up key2_a, and key3_c, but not key2_c or key3_c
2220
 
        self.assertEqual([key2_a, key3_c], diff._new_queue)
2221
 
        self.assertEqual([], diff._new_item_queue)
2222
 
        # And we should have queued up both a and c for the old set
2223
 
        self.assertEqual([key1_a, key1_c], diff._old_queue)
2224
 
 
2225
 
    def test__read_all_roots_different_depths(self):
2226
 
        c_map = self.make_two_deep_map(chk_map._search_key_plain)
2227
 
        c_map._dump_tree() # load everything
2228
 
        key1 = c_map.key()
2229
 
        key1_a = c_map._root_node._items['a'].key()
2230
 
        key1_c = c_map._root_node._items['c'].key()
2231
 
        key1_d = c_map._root_node._items['d'].key()
2232
 
 
2233
 
        c_map2 = self.make_one_deep_two_prefix_map(chk_map._search_key_plain)
2234
 
        c_map2._dump_tree()
2235
 
        key2 = c_map2.key()
2236
 
        key2_aa = c_map2._root_node._items['aa'].key()
2237
 
        key2_ad = c_map2._root_node._items['ad'].key()
2238
 
 
2239
 
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2240
 
        root_results = [record.key for record in diff._read_all_roots()]
2241
 
        self.assertEqual([key2], root_results)
2242
 
        # Only the 'a' subset should be queued up, since 'c' and 'd' cannot be
2243
 
        # present
2244
 
        self.assertEqual([key1_a], diff._old_queue)
2245
 
        self.assertEqual([key2_aa, key2_ad], diff._new_queue)
2246
 
        self.assertEqual([], diff._new_item_queue)
2247
 
 
2248
 
        diff = self.get_difference([key1], [key2], chk_map._search_key_plain)
2249
 
        root_results = [record.key for record in diff._read_all_roots()]
2250
 
        self.assertEqual([key1], root_results)
2251
 
 
2252
 
        self.assertEqual([key2_aa, key2_ad], diff._old_queue)
2253
 
        self.assertEqual([key1_a, key1_c, key1_d], diff._new_queue)
2254
 
        self.assertEqual([], diff._new_item_queue)
2255
 
 
2256
 
    def test__read_all_roots_different_depths_16(self):
2257
 
        c_map = self.make_two_deep_map(chk_map._search_key_16)
2258
 
        c_map._dump_tree() # load everything
2259
 
        key1 = c_map.key()
2260
 
        key1_2 = c_map._root_node._items['2'].key()
2261
 
        key1_4 = c_map._root_node._items['4'].key()
2262
 
        key1_C = c_map._root_node._items['C'].key()
2263
 
        key1_F = c_map._root_node._items['F'].key()
2264
 
 
2265
 
        c_map2 = self.make_one_deep_two_prefix_map(chk_map._search_key_16)
2266
 
        c_map2._dump_tree()
2267
 
        key2 = c_map2.key()
2268
 
        key2_F0 = c_map2._root_node._items['F0'].key()
2269
 
        key2_F3 = c_map2._root_node._items['F3'].key()
2270
 
        key2_F4 = c_map2._root_node._items['F4'].key()
2271
 
        key2_FD = c_map2._root_node._items['FD'].key()
2272
 
 
2273
 
        diff = self.get_difference([key2], [key1], chk_map._search_key_16)
2274
 
        root_results = [record.key for record in diff._read_all_roots()]
2275
 
        self.assertEqual([key2], root_results)
2276
 
        # Only the subset of keys that may be present should be queued up.
2277
 
        self.assertEqual([key1_F], diff._old_queue)
2278
 
        self.assertEqual(sorted([key2_F0, key2_F3, key2_F4, key2_FD]),
2279
 
                         sorted(diff._new_queue))
2280
 
        self.assertEqual([], diff._new_item_queue)
2281
 
 
2282
 
        diff = self.get_difference([key1], [key2], chk_map._search_key_16)
2283
 
        root_results = [record.key for record in diff._read_all_roots()]
2284
 
        self.assertEqual([key1], root_results)
2285
 
 
2286
 
        self.assertEqual(sorted([key2_F0, key2_F3, key2_F4, key2_FD]),
2287
 
                         sorted(diff._old_queue))
2288
 
        self.assertEqual(sorted([key1_2, key1_4, key1_C, key1_F]),
2289
 
                         sorted(diff._new_queue))
2290
 
        self.assertEqual([], diff._new_item_queue)
2291
 
 
2292
 
    def test__read_all_roots_mixed_depth(self):
2293
 
        c_map = self.make_one_deep_two_prefix_map(chk_map._search_key_plain)
2294
 
        c_map._dump_tree() # load everything
2295
 
        key1 = c_map.key()
2296
 
        key1_aa = c_map._root_node._items['aa'].key()
2297
 
        key1_ad = c_map._root_node._items['ad'].key()
2298
 
 
2299
 
        c_map2 = self.make_one_deep_one_prefix_map(chk_map._search_key_plain)
2300
 
        c_map2._dump_tree()
2301
 
        key2 = c_map2.key()
2302
 
        key2_a = c_map2._root_node._items['a'].key()
2303
 
        key2_b = c_map2._root_node._items['b'].key()
2304
 
 
2305
 
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2306
 
        root_results = [record.key for record in diff._read_all_roots()]
2307
 
        self.assertEqual([key2], root_results)
2308
 
        # 'ad' matches exactly 'a' on the other side, so it should be removed,
2309
 
        # and neither side should have it queued for walking
2310
 
        self.assertEqual([], diff._old_queue)
2311
 
        self.assertEqual([key2_b], diff._new_queue)
2312
 
        self.assertEqual([], diff._new_item_queue)
2313
 
 
2314
 
        diff = self.get_difference([key1], [key2], chk_map._search_key_plain)
2315
 
        root_results = [record.key for record in diff._read_all_roots()]
2316
 
        self.assertEqual([key1], root_results)
2317
 
        # Note: This is technically not the 'true minimal' set that we could
2318
 
        #       use The reason is that 'a' was matched exactly to 'ad' (by sha
2319
 
        #       sum).  However, the code gets complicated in the case of more
2320
 
        #       than one interesting key, so for now, we live with this
2321
 
        #       Consider revising, though benchmarking showing it to be a
2322
 
        #       real-world issue should be done
2323
 
        self.assertEqual([key2_a], diff._old_queue)
2324
 
        # self.assertEqual([], diff._old_queue)
2325
 
        self.assertEqual([key1_aa], diff._new_queue)
2326
 
        self.assertEqual([], diff._new_item_queue)
2327
 
 
2328
 
    def test__read_all_roots_yields_extra_deep_records(self):
2329
 
        # This is slightly controversial, as we will yield a chk page that we
2330
 
        # might later on find out could be filtered out. (If a root node is
2331
 
        # referenced deeper in the old set.)
2332
 
        # However, even with stacking, we always have all chk pages that we
2333
 
        # will need. So as long as we filter out the referenced keys, we'll
2334
 
        # never run into problems.
2335
 
        # This allows us to yield a root node record immediately, without any
2336
 
        # buffering.
2337
 
        c_map = self.make_two_deep_map(chk_map._search_key_plain)
2338
 
        c_map._dump_tree() # load all keys
2339
 
        key1 = c_map.key()
2340
 
        key1_a = c_map._root_node._items['a'].key()
2341
 
        c_map2 = self.get_map({
2342
 
            ('acc',): 'initial acc content',
2343
 
            ('ace',): 'initial ace content',
2344
 
        }, maximum_size=100)
2345
 
        self.assertEqualDiff(
2346
 
            "'' LeafNode\n"
2347
 
            "      ('acc',) 'initial acc content'\n"
2348
 
            "      ('ace',) 'initial ace content'\n",
2349
 
            c_map2._dump_tree())
2350
 
        key2 = c_map2.key()
2351
 
        diff = self.get_difference([key2], [key1], chk_map._search_key_plain)
2352
 
        root_results = [record.key for record in diff._read_all_roots()]
2353
 
        self.assertEqual([key2], root_results)
2354
 
        # However, even though we have yielded the root node to be fetched,
2355
 
        # we should have enqued all of the chk pages to be walked, so that we
2356
 
        # can find the keys if they are present
2357
 
        self.assertEqual([key1_a], diff._old_queue)
2358
 
        self.assertEqual([(('acc',), 'initial acc content'),
2359
 
                          (('ace',), 'initial ace content'),
2360
 
                         ], diff._new_item_queue)
2361
 
 
2362
 
    def test__read_all_roots_multiple_targets(self):
2363
 
        c_map = self.make_root_only_map()
2364
 
        key1 = c_map.key()
2365
 
        c_map = self.make_one_deep_map()
2366
 
        key2 = c_map.key()
2367
 
        c_map._dump_tree()
2368
 
        key2_c = c_map._root_node._items['c'].key()
2369
 
        key2_d = c_map._root_node._items['d'].key()
2370
 
        c_map.map(('ccc',), 'new ccc value')
2371
 
        key3 = c_map._save()
2372
 
        key3_c = c_map._root_node._items['c'].key()
2373
 
        diff = self.get_difference([key2, key3], [key1],
2374
 
                                   chk_map._search_key_plain)
2375
 
        root_results = [record.key for record in diff._read_all_roots()]
2376
 
        self.assertEqual(sorted([key2, key3]), sorted(root_results))
2377
 
        self.assertEqual([], diff._old_queue)
2378
 
        # the key 'd' is interesting from key2 and key3, but should only be
2379
 
        # entered into the queue 1 time
2380
 
        self.assertEqual(sorted([key2_c, key3_c, key2_d]),
2381
 
                         sorted(diff._new_queue))
2382
 
        self.assertEqual([], diff._new_item_queue)
2383
 
 
2384
 
    def test__read_all_roots_no_old(self):
2385
 
        # This is the 'initial branch' case. With nothing in the old
2386
 
        # set, we can just queue up all root nodes into interesting queue, and
2387
 
        # then have them fast-path flushed via _flush_new_queue
2388
 
        c_map = self.make_two_deep_map()
2389
 
        key1 = c_map.key()
2390
 
        diff = self.get_difference([key1], [], chk_map._search_key_plain)
2391
 
        root_results = [record.key for record in diff._read_all_roots()]
2392
 
        self.assertEqual([], root_results)
2393
 
        self.assertEqual([], diff._old_queue)
2394
 
        self.assertEqual([key1], diff._new_queue)
2395
 
        self.assertEqual([], diff._new_item_queue)
2396
 
 
2397
 
        c_map2 = self.make_one_deep_map()
2398
 
        key2 = c_map2.key()
2399
 
        diff = self.get_difference([key1, key2], [], chk_map._search_key_plain)
2400
 
        root_results = [record.key for record in diff._read_all_roots()]
2401
 
        self.assertEqual([], root_results)
2402
 
        self.assertEqual([], diff._old_queue)
2403
 
        self.assertEqual(sorted([key1, key2]), sorted(diff._new_queue))
2404
 
        self.assertEqual([], diff._new_item_queue)
2405
 
 
2406
 
    def test__read_all_roots_no_old_16(self):
2407
 
        c_map = self.make_two_deep_map(chk_map._search_key_16)
2408
 
        key1 = c_map.key()
2409
 
        diff = self.get_difference([key1], [], chk_map._search_key_16)
2410
 
        root_results = [record.key for record in diff._read_all_roots()]
2411
 
        self.assertEqual([], root_results)
2412
 
        self.assertEqual([], diff._old_queue)
2413
 
        self.assertEqual([key1], diff._new_queue)
2414
 
        self.assertEqual([], diff._new_item_queue)
2415
 
 
2416
 
        c_map2 = self.make_one_deep_map(chk_map._search_key_16)
2417
 
        key2 = c_map2.key()
2418
 
        diff = self.get_difference([key1, key2], [],
2419
 
                                   chk_map._search_key_16)
2420
 
        root_results = [record.key for record in diff._read_all_roots()]
2421
 
        self.assertEqual([], root_results)
2422
 
        self.assertEqual([], diff._old_queue)
2423
 
        self.assertEqual(sorted([key1, key2]),
2424
 
                         sorted(diff._new_queue))
2425
 
        self.assertEqual([], diff._new_item_queue)
2426
 
 
2427
 
    def test__read_all_roots_multiple_old(self):
2428
 
        c_map = self.make_two_deep_map()
2429
 
        key1 = c_map.key()
2430
 
        c_map._dump_tree() # load everything
2431
 
        key1_a = c_map._root_node._items['a'].key()
2432
 
        c_map.map(('ccc',), 'new ccc value')
2433
 
        key2 = c_map._save()
2434
 
        key2_a = c_map._root_node._items['a'].key()
2435
 
        c_map.map(('add',), 'new add value')
2436
 
        key3 = c_map._save()
2437
 
        key3_a = c_map._root_node._items['a'].key()
2438
 
        diff = self.get_difference([key3], [key1, key2],
2439
 
                                   chk_map._search_key_plain)
2440
 
        root_results = [record.key for record in diff._read_all_roots()]
2441
 
        self.assertEqual([key3], root_results)
2442
 
        # the 'a' keys should not be queued up 2 times, since they are
2443
 
        # identical
2444
 
        self.assertEqual([key1_a], diff._old_queue)
2445
 
        self.assertEqual([key3_a], diff._new_queue)
2446
 
        self.assertEqual([], diff._new_item_queue)
2447
 
 
2448
 
    def test__process_next_old_batched_no_dupes(self):
2449
 
        c_map = self.make_two_deep_map()
2450
 
        key1 = c_map.key()
2451
 
        c_map._dump_tree() # load everything
2452
 
        key1_a = c_map._root_node._items['a'].key()
2453
 
        key1_aa = c_map._root_node._items['a']._items['aa'].key()
2454
 
        key1_ab = c_map._root_node._items['a']._items['ab'].key()
2455
 
        key1_ac = c_map._root_node._items['a']._items['ac'].key()
2456
 
        key1_ad = c_map._root_node._items['a']._items['ad'].key()
2457
 
        c_map.map(('aaa',), 'new aaa value')
2458
 
        key2 = c_map._save()
2459
 
        key2_a = c_map._root_node._items['a'].key()
2460
 
        key2_aa = c_map._root_node._items['a']._items['aa'].key()
2461
 
        c_map.map(('acc',), 'new acc content')
2462
 
        key3 = c_map._save()
2463
 
        key3_a = c_map._root_node._items['a'].key()
2464
 
        key3_ac = c_map._root_node._items['a']._items['ac'].key()
2465
 
        diff = self.get_difference([key3], [key1, key2],
2466
 
                                   chk_map._search_key_plain)
2467
 
        root_results = [record.key for record in diff._read_all_roots()]
2468
 
        self.assertEqual([key3], root_results)
2469
 
        self.assertEqual(sorted([key1_a, key2_a]),
2470
 
                         sorted(diff._old_queue))
2471
 
        self.assertEqual([key3_a], diff._new_queue)
2472
 
        self.assertEqual([], diff._new_item_queue)
2473
 
        diff._process_next_old()
2474
 
        # All of the old records should be brought in and queued up,
2475
 
        # but we should not have any duplicates
2476
 
        self.assertEqual(sorted([key1_aa, key1_ab, key1_ac, key1_ad, key2_aa]),
2477
 
                         sorted(diff._old_queue))
2478
 
 
2479
 
 
2480
 
class TestIterInterestingNodes(TestCaseWithExampleMaps):
2481
 
 
2482
 
    def get_map_key(self, a_dict, maximum_size=10):
2483
 
        c_map = self.get_map(a_dict, maximum_size=maximum_size)
 
1831
class TestIterInterestingNodes(TestCaseWithStore):
 
1832
 
 
1833
    def get_chk_bytes(self):
 
1834
        if getattr(self, '_chk_bytes', None) is None:
 
1835
            self._chk_bytes = super(TestIterInterestingNodes,
 
1836
                                    self).get_chk_bytes()
 
1837
        return self._chk_bytes
 
1838
 
 
1839
    def get_map_key(self, a_dict):
 
1840
        c_map = self._get_map(a_dict, maximum_size=10,
 
1841
                              chk_bytes=self.get_chk_bytes())
2484
1842
        return c_map.key()
2485
1843
 
2486
 
    def assertIterInteresting(self, records, items, interesting_keys,
2487
 
                              old_keys):
 
1844
    def assertIterInteresting(self, expected, interesting_keys,
 
1845
                              uninteresting_keys):
2488
1846
        """Check the result of iter_interesting_nodes.
2489
1847
 
2490
 
        Note that we no longer care how many steps are taken, etc, just that
2491
 
        the right contents are returned.
2492
 
 
2493
 
        :param records: A list of record keys that should be yielded
2494
 
        :param items: A list of items (key,value) that should be yielded.
 
1848
        :param expected: A list of (record_keys, interesting_chk_pages,
 
1849
                                    interesting key value pairs)
2495
1850
        """
2496
1851
        store = self.get_chk_bytes()
2497
 
        store._search_key_func = chk_map._search_key_plain
2498
1852
        iter_nodes = chk_map.iter_interesting_nodes(store, interesting_keys,
2499
 
                                                    old_keys)
2500
 
        record_keys = []
2501
 
        all_items = []
2502
 
        for record, new_items in iter_nodes:
2503
 
            if record is not None:
2504
 
                record_keys.append(record.key)
2505
 
            if new_items:
2506
 
                all_items.extend(new_items)
2507
 
        self.assertEqual(sorted(records), sorted(record_keys))
2508
 
        self.assertEqual(sorted(items), sorted(all_items))
 
1853
                                                    uninteresting_keys)
 
1854
        nodes = list(iter_nodes)
 
1855
        for count, (exp, act) in enumerate(izip(expected, nodes)):
 
1856
            exp_record, exp_items = exp
 
1857
            record, items = act
 
1858
            exp_tuple = (exp_record, sorted(exp_items))
 
1859
            if record is None:
 
1860
                act_tuple = (None, sorted(items))
 
1861
            else:
 
1862
                act_tuple = (record.key, sorted(items))
 
1863
            self.assertEqual(exp_tuple, act_tuple,
 
1864
                             'entry %d did not match expected' % count)
 
1865
        self.assertEqual(len(expected), len(nodes))
2509
1866
 
2510
1867
    def test_empty_to_one_keys(self):
2511
1868
        target = self.get_map_key({('a',): 'content'})
2512
 
        self.assertIterInteresting([target],
2513
 
                                   [(('a',), 'content')],
2514
 
                                   [target], [])
 
1869
        self.assertIterInteresting(
 
1870
            [(target, [(('a',), 'content')]),
 
1871
            ], [target], [])
2515
1872
 
2516
1873
    def test_none_to_one_key(self):
2517
1874
        basis = self.get_map_key({})
2518
1875
        target = self.get_map_key({('a',): 'content'})
2519
 
        self.assertIterInteresting([target],
2520
 
                                   [(('a',), 'content')],
2521
 
                                   [target], [basis])
 
1876
        self.assertIterInteresting(
 
1877
            [(None, [(('a',), 'content')]),
 
1878
             (target, []),
 
1879
            ], [target], [basis])
2522
1880
 
2523
1881
    def test_one_to_none_key(self):
2524
1882
        basis = self.get_map_key({('a',): 'content'})
2525
1883
        target = self.get_map_key({})
2526
 
        self.assertIterInteresting([target],
2527
 
                                   [],
2528
 
                                   [target], [basis])
 
1884
        self.assertIterInteresting(
 
1885
            [(target, [])],
 
1886
            [target], [basis])
2529
1887
 
2530
1888
    def test_common_pages(self):
2531
1889
        basis = self.get_map_key({('a',): 'content',
2548
1906
            target_map._dump_tree())
2549
1907
        b_key = target_map._root_node._items['b'].key()
2550
1908
        # This should return the root node, and the node for the 'b' key
2551
 
        self.assertIterInteresting([target, b_key],
2552
 
                                   [(('b',), 'other content')],
2553
 
                                   [target], [basis])
 
1909
        self.assertIterInteresting(
 
1910
            [(target, []),
 
1911
             (b_key, [(('b',), 'other content')])],
 
1912
            [target], [basis])
2554
1913
 
2555
1914
    def test_common_sub_page(self):
2556
1915
        basis = self.get_map_key({('aaa',): 'common',
2574
1933
        # The key for the internal aa node
2575
1934
        a_key = target_map._root_node._items['a'].key()
2576
1935
        # The key for the leaf aab node
2577
 
        # aaa_key = target_map._root_node._items['a']._items['aaa'].key()
2578
1936
        aab_key = target_map._root_node._items['a']._items['aab'].key()
2579
 
        self.assertIterInteresting([target, a_key, aab_key],
2580
 
                                   [(('aab',), 'new')],
2581
 
                                   [target], [basis])
 
1937
        self.assertIterInteresting(
 
1938
            [(target, []),
 
1939
             (a_key, []),
 
1940
             (aab_key, [(('aab',), 'new')])],
 
1941
            [target], [basis])
2582
1942
 
2583
1943
    def test_common_leaf(self):
2584
1944
        basis = self.get_map_key({})
2622
1982
        a_key = target3_map._root_node._items['a'].key()
2623
1983
        aac_key = target3_map._root_node._items['a']._items['aac'].key()
2624
1984
        self.assertIterInteresting(
2625
 
            [target1, target2, target3, a_key, aac_key, b_key],
2626
 
            [(('aaa',), 'common'), (('bbb',), 'new'), (('aac',), 'other')],
2627
 
            [target1, target2, target3], [basis])
2628
 
 
2629
 
        self.assertIterInteresting(
2630
 
            [target2, target3, a_key, aac_key, b_key],
2631
 
            [(('bbb',), 'new'), (('aac',), 'other')],
2632
 
            [target2, target3], [target1])
2633
 
 
2634
 
        # Technically, target1 could be filtered out, but since it is a root
2635
 
        # node, we yield it immediately, rather than waiting to find out much
2636
 
        # later on.
2637
 
        self.assertIterInteresting(
2638
 
            [target1],
2639
 
            [],
2640
 
            [target1], [target3])
 
1985
            [(None, [(('aaa',), 'common')]),
 
1986
             (target1, []),
 
1987
             (target2, []),
 
1988
             (target3, []),
 
1989
             (b_key, [(('bbb',), 'new')]),
 
1990
             (a_key, []),
 
1991
             (aac_key, [(('aac',), 'other')]),
 
1992
            ], [target1, target2, target3], [basis])
 
1993
 
 
1994
        self.assertIterInteresting(
 
1995
            [(target2, []),
 
1996
             (target3, []),
 
1997
             (b_key, [(('bbb',), 'new')]),
 
1998
             (a_key, []),
 
1999
             (aac_key, [(('aac',), 'other')]),
 
2000
            ], [target2, target3], [target1])
 
2001
 
 
2002
        # This may be a case that we relax. A root node is a deep child of the
 
2003
        # excluded set. The cost is buffering root nodes until we have
 
2004
        # determined all possible exclusions. (Because a prefix of '', cannot
 
2005
        # be excluded.)
 
2006
        self.assertIterInteresting(
 
2007
            [], [target1], [target3])
2641
2008
 
2642
2009
    def test_multiple_maps(self):
2643
2010
        basis1 = self.get_map_key({('aaa',): 'common',
2686
2053
        # The key for the leaf bba node
2687
2054
        bba_key = target2_map._root_node._items['b']._items['bba'].key()
2688
2055
        self.assertIterInteresting(
2689
 
            [target1, target2, a_key, aac_key, b_key, bba_key],
2690
 
            [(('aac',), 'target1'), (('bba',), 'target2')],
2691
 
            [target1, target2], [basis1, basis2])
2692
 
 
2693
 
    def test_multiple_maps_overlapping_common_new(self):
2694
 
        # Test that when a node found through the interesting_keys iteration
2695
 
        # for *some roots* and also via the old keys iteration, that
2696
 
        # it is still scanned for old refs and items, because its
2697
 
        # not truely new. This requires 2 levels of InternalNodes to expose,
2698
 
        # because of the way the bootstrap in _find_children_info works.
2699
 
        # This suggests that the code is probably amenable to/benefit from
2700
 
        # consolidation.
2701
 
        # How does this test work?
2702
 
        # 1) We need a second level InternalNode present in a basis tree.
2703
 
        # 2) We need a left side new tree that uses that InternalNode
2704
 
        # 3) We need a right side new tree that does not use that InternalNode
2705
 
        #    at all but that has an unchanged *value* that was reachable inside
2706
 
        #    that InternalNode
2707
 
        basis = self.get_map_key({
2708
 
            # InternalNode, unchanged in left:
2709
 
            ('aaa',): 'left',
2710
 
            ('abb',): 'right',
2711
 
            # Forces an internalNode at 'a'
2712
 
            ('ccc',): 'common',
2713
 
            })
2714
 
        left = self.get_map_key({
2715
 
            # All of basis unchanged
2716
 
            ('aaa',): 'left',
2717
 
            ('abb',): 'right',
2718
 
            ('ccc',): 'common',
2719
 
            # And a new top level node so the root key is different
2720
 
            ('ddd',): 'change',
2721
 
            })
2722
 
        right = self.get_map_key({
2723
 
            # A value that is unchanged from basis and thus should be filtered
2724
 
            # out.
2725
 
            ('abb',): 'right'
2726
 
            })
2727
 
        basis_map = CHKMap(self.get_chk_bytes(), basis)
2728
 
        self.assertEqualDiff(
2729
 
            "'' InternalNode\n"
2730
 
            "  'a' InternalNode\n"
2731
 
            "    'aa' LeafNode\n"
2732
 
            "      ('aaa',) 'left'\n"
2733
 
            "    'ab' LeafNode\n"
2734
 
            "      ('abb',) 'right'\n"
2735
 
            "  'c' LeafNode\n"
2736
 
            "      ('ccc',) 'common'\n",
2737
 
            basis_map._dump_tree())
2738
 
        # Get left expected data
2739
 
        left_map = CHKMap(self.get_chk_bytes(), left)
2740
 
        self.assertEqualDiff(
2741
 
            "'' InternalNode\n"
2742
 
            "  'a' InternalNode\n"
2743
 
            "    'aa' LeafNode\n"
2744
 
            "      ('aaa',) 'left'\n"
2745
 
            "    'ab' LeafNode\n"
2746
 
            "      ('abb',) 'right'\n"
2747
 
            "  'c' LeafNode\n"
2748
 
            "      ('ccc',) 'common'\n"
2749
 
            "  'd' LeafNode\n"
2750
 
            "      ('ddd',) 'change'\n",
2751
 
            left_map._dump_tree())
2752
 
        # Keys from left side target
2753
 
        l_d_key = left_map._root_node._items['d'].key()
2754
 
        # Get right expected data
2755
 
        right_map = CHKMap(self.get_chk_bytes(), right)
2756
 
        self.assertEqualDiff(
2757
 
            "'' LeafNode\n"
2758
 
            "      ('abb',) 'right'\n",
2759
 
            right_map._dump_tree())
2760
 
        # Keys from the right side target - none, the root is enough.
2761
 
        # Test behaviour
2762
 
        self.assertIterInteresting(
2763
 
            [right, left, l_d_key],
2764
 
            [(('ddd',), 'change')],
2765
 
            [left, right], [basis])
2766
 
 
2767
 
    def test_multiple_maps_similar(self):
2768
 
        # We want to have a depth=2 tree, with multiple entries in each leaf
2769
 
        # node
2770
 
        basis = self.get_map_key({
2771
 
            ('aaa',): 'unchanged',
2772
 
            ('abb',): 'will change left',
2773
 
            ('caa',): 'unchanged',
2774
 
            ('cbb',): 'will change right',
2775
 
            }, maximum_size=60)
2776
 
        left = self.get_map_key({
2777
 
            ('aaa',): 'unchanged',
2778
 
            ('abb',): 'changed left',
2779
 
            ('caa',): 'unchanged',
2780
 
            ('cbb',): 'will change right',
2781
 
            }, maximum_size=60)
2782
 
        right = self.get_map_key({
2783
 
            ('aaa',): 'unchanged',
2784
 
            ('abb',): 'will change left',
2785
 
            ('caa',): 'unchanged',
2786
 
            ('cbb',): 'changed right',
2787
 
            }, maximum_size=60)
2788
 
        basis_map = CHKMap(self.get_chk_bytes(), basis)
2789
 
        self.assertEqualDiff(
2790
 
            "'' InternalNode\n"
2791
 
            "  'a' LeafNode\n"
2792
 
            "      ('aaa',) 'unchanged'\n"
2793
 
            "      ('abb',) 'will change left'\n"
2794
 
            "  'c' LeafNode\n"
2795
 
            "      ('caa',) 'unchanged'\n"
2796
 
            "      ('cbb',) 'will change right'\n",
2797
 
            basis_map._dump_tree())
2798
 
        # Get left expected data
2799
 
        left_map = CHKMap(self.get_chk_bytes(), left)
2800
 
        self.assertEqualDiff(
2801
 
            "'' InternalNode\n"
2802
 
            "  'a' LeafNode\n"
2803
 
            "      ('aaa',) 'unchanged'\n"
2804
 
            "      ('abb',) 'changed left'\n"
2805
 
            "  'c' LeafNode\n"
2806
 
            "      ('caa',) 'unchanged'\n"
2807
 
            "      ('cbb',) 'will change right'\n",
2808
 
            left_map._dump_tree())
2809
 
        # Keys from left side target
2810
 
        l_a_key = left_map._root_node._items['a'].key()
2811
 
        l_c_key = left_map._root_node._items['c'].key()
2812
 
        # Get right expected data
2813
 
        right_map = CHKMap(self.get_chk_bytes(), right)
2814
 
        self.assertEqualDiff(
2815
 
            "'' InternalNode\n"
2816
 
            "  'a' LeafNode\n"
2817
 
            "      ('aaa',) 'unchanged'\n"
2818
 
            "      ('abb',) 'will change left'\n"
2819
 
            "  'c' LeafNode\n"
2820
 
            "      ('caa',) 'unchanged'\n"
2821
 
            "      ('cbb',) 'changed right'\n",
2822
 
            right_map._dump_tree())
2823
 
        r_a_key = right_map._root_node._items['a'].key()
2824
 
        r_c_key = right_map._root_node._items['c'].key()
2825
 
        self.assertIterInteresting(
2826
 
            [right, left, l_a_key, r_c_key],
2827
 
            [(('abb',), 'changed left'), (('cbb',), 'changed right')],
2828
 
            [left, right], [basis])
 
2056
            [(target1, []),
 
2057
             (target2, []),
 
2058
             (a_key, []),
 
2059
             (b_key, []),
 
2060
             (aac_key, [(('aac',), 'target1')]),
 
2061
             (bba_key, [(('bba',), 'target2')]),
 
2062
            ], [target1, target2], [basis1, basis2])