/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2007-2010 Canonical Ltd
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
16
17
"""Tests for indices."""
18
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from .. import (
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
20
    errors,
21
    tests,
22
    transport,
23
    )
6973.11.3 by Jelmer Vernooij
Fix some index tests.
24
from ..sixish import int2byte
6670.4.1 by Jelmer Vernooij
Update imports.
25
from ..bzr import (
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
26
    index as _mod_index,
6670.4.1 by Jelmer Vernooij
Update imports.
27
    )
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
28
29
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
30
class ErrorTests(tests.TestCase):
31
32
    def test_bad_index_format_signature(self):
33
        error = _mod_index.BadIndexFormatSignature("foo", "bar")
34
        self.assertEqual("foo is not an index of type bar.",
7143.15.2 by Jelmer Vernooij
Run autopep8.
35
                         str(error))
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
36
37
    def test_bad_index_data(self):
38
        error = _mod_index.BadIndexData("foo")
39
        self.assertEqual("Error in data for index foo.",
7143.15.2 by Jelmer Vernooij
Run autopep8.
40
                         str(error))
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
41
42
    def test_bad_index_duplicate_key(self):
43
        error = _mod_index.BadIndexDuplicateKey("foo", "bar")
44
        self.assertEqual("The key 'foo' is already in index 'bar'.",
7143.15.2 by Jelmer Vernooij
Run autopep8.
45
                         str(error))
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
46
47
    def test_bad_index_key(self):
48
        error = _mod_index.BadIndexKey("foo")
49
        self.assertEqual("The key 'foo' is not a valid key.",
7143.15.2 by Jelmer Vernooij
Run autopep8.
50
                         str(error))
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
51
52
    def test_bad_index_options(self):
53
        error = _mod_index.BadIndexOptions("foo")
54
        self.assertEqual("Could not parse options for index foo.",
7143.15.2 by Jelmer Vernooij
Run autopep8.
55
                         str(error))
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
56
57
    def test_bad_index_value(self):
58
        error = _mod_index.BadIndexValue("foo")
59
        self.assertEqual("The value 'foo' is not a valid value.",
7143.15.2 by Jelmer Vernooij
Run autopep8.
60
                         str(error))
6743 by Jelmer Vernooij
Merge lp:~jelmer/brz/move-errors-more.
61
62
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
63
class TestGraphIndexBuilder(tests.TestCaseWithMemoryTransport):
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
64
65
    def test_build_index_empty(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
66
        builder = _mod_index.GraphIndexBuilder()
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
67
        stream = builder.finish()
68
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
69
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
70
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=1\nlen=0\n\n",
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
71
            contents)
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
72
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
73
    def test_build_index_empty_two_element_keys(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
74
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
75
        stream = builder.finish()
76
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
77
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
78
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=2\nlen=0\n\n",
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
79
            contents)
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
80
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
81
    def test_build_index_one_reference_list_empty(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
82
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
2592.1.6 by Robert Collins
Record the number of node reference lists a particular index has.
83
        stream = builder.finish()
84
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
85
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
86
            b"Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=1\nlen=0\n\n",
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
87
            contents)
2592.1.4 by Robert Collins
Create a GraphIndexBuilder.
88
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
89
    def test_build_index_two_reference_list_empty(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
90
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
91
        stream = builder.finish()
92
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
93
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
94
            b"Bazaar Graph Index 1\nnode_ref_lists=2\nkey_elements=1\nlen=0\n\n",
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
95
            contents)
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
96
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
97
    def test_build_index_one_node_no_refs(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
98
        builder = _mod_index.GraphIndexBuilder()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
99
        builder.add_node((b'akey', ), b'data')
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
100
        stream = builder.finish()
101
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
102
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
103
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=1\nlen=1\n"
104
            b"akey\x00\x00\x00data\n\n", contents)
2592.1.46 by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method
105
106
    def test_build_index_one_node_no_refs_accepts_empty_reflist(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
107
        builder = _mod_index.GraphIndexBuilder()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
108
        builder.add_node((b'akey', ), b'data', ())
2592.1.12 by Robert Collins
Handle basic node adds.
109
        stream = builder.finish()
110
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
111
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
112
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=1\nlen=1\n"
113
            b"akey\x00\x00\x00data\n\n", contents)
2592.1.12 by Robert Collins
Handle basic node adds.
114
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
115
    def test_build_index_one_node_2_element_keys(self):
2624.2.11 by Robert Collins
Review comments.
116
        # multipart keys are separated by \x00 - because they are fixed length,
117
        # not variable this does not cause any issues, and seems clearer to the
118
        # author.
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
119
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
120
        builder.add_node((b'akey', b'secondpart'), b'data')
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
121
        stream = builder.finish()
122
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
123
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
124
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=2\nlen=1\n"
125
            b"akey\x00secondpart\x00\x00\x00data\n\n", contents)
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
126
2592.1.21 by Robert Collins
Empty values are ok.
127
    def test_add_node_empty_value(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
128
        builder = _mod_index.GraphIndexBuilder()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
129
        builder.add_node((b'akey', ), b'')
2592.1.21 by Robert Collins
Empty values are ok.
130
        stream = builder.finish()
131
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
132
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
133
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=1\nlen=1\n"
134
            b"akey\x00\x00\x00\n\n", contents)
2592.1.21 by Robert Collins
Empty values are ok.
135
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
136
    def test_build_index_nodes_sorted(self):
2592.1.17 by Robert Collins
Multi node sort order is defined.
137
        # the highest sorted node comes first.
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
138
        builder = _mod_index.GraphIndexBuilder()
2592.1.17 by Robert Collins
Multi node sort order is defined.
139
        # use three to have a good chance of glitching dictionary hash
140
        # lookups etc. Insert in randomish order that is not correct
141
        # and not the reverse of the correct order.
6973.11.3 by Jelmer Vernooij
Fix some index tests.
142
        builder.add_node((b'2002', ), b'data')
143
        builder.add_node((b'2000', ), b'data')
144
        builder.add_node((b'2001', ), b'data')
2592.1.17 by Robert Collins
Multi node sort order is defined.
145
        stream = builder.finish()
146
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
147
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
148
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=1\nlen=3\n"
149
            b"2000\x00\x00\x00data\n"
150
            b"2001\x00\x00\x00data\n"
151
            b"2002\x00\x00\x00data\n"
152
            b"\n", contents)
2592.1.17 by Robert Collins
Multi node sort order is defined.
153
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
154
    def test_build_index_2_element_key_nodes_sorted(self):
155
        # multiple element keys are sorted first-key, second-key.
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
156
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
157
        # use three values of each key element, to have a good chance of
158
        # glitching dictionary hash lookups etc. Insert in randomish order that
159
        # is not correct and not the reverse of the correct order.
6973.11.3 by Jelmer Vernooij
Fix some index tests.
160
        builder.add_node((b'2002', b'2002'), b'data')
161
        builder.add_node((b'2002', b'2000'), b'data')
162
        builder.add_node((b'2002', b'2001'), b'data')
163
        builder.add_node((b'2000', b'2002'), b'data')
164
        builder.add_node((b'2000', b'2000'), b'data')
165
        builder.add_node((b'2000', b'2001'), b'data')
166
        builder.add_node((b'2001', b'2002'), b'data')
167
        builder.add_node((b'2001', b'2000'), b'data')
168
        builder.add_node((b'2001', b'2001'), b'data')
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
169
        stream = builder.finish()
170
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
171
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
172
            b"Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=2\nlen=9\n"
173
            b"2000\x002000\x00\x00\x00data\n"
174
            b"2000\x002001\x00\x00\x00data\n"
175
            b"2000\x002002\x00\x00\x00data\n"
176
            b"2001\x002000\x00\x00\x00data\n"
177
            b"2001\x002001\x00\x00\x00data\n"
178
            b"2001\x002002\x00\x00\x00data\n"
179
            b"2002\x002000\x00\x00\x00data\n"
180
            b"2002\x002001\x00\x00\x00data\n"
181
            b"2002\x002002\x00\x00\x00data\n"
182
            b"\n", contents)
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
183
2592.1.19 by Robert Collins
Node references are tab separated.
184
    def test_build_index_reference_lists_are_included_one(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
185
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
186
        builder.add_node((b'key', ), b'data', ([], ))
2592.1.19 by Robert Collins
Node references are tab separated.
187
        stream = builder.finish()
188
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
189
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
190
            b"Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=1\nlen=1\n"
191
            b"key\x00\x00\x00data\n"
192
            b"\n", contents)
2592.1.19 by Robert Collins
Node references are tab separated.
193
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
194
    def test_build_index_reference_lists_with_2_element_keys(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
195
        builder = _mod_index.GraphIndexBuilder(
196
            reference_lists=1, key_elements=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
197
        builder.add_node((b'key', b'key2'), b'data', ([], ))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
198
        stream = builder.finish()
199
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
200
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
201
            b"Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=2\nlen=1\n"
202
            b"key\x00key2\x00\x00\x00data\n"
203
            b"\n", contents)
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
204
2592.1.19 by Robert Collins
Node references are tab separated.
205
    def test_build_index_reference_lists_are_included_two(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
206
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
207
        builder.add_node((b'key', ), b'data', ([], []))
2592.1.19 by Robert Collins
Node references are tab separated.
208
        stream = builder.finish()
209
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
210
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
211
            b"Bazaar Graph Index 1\nnode_ref_lists=2\nkey_elements=1\nlen=1\n"
212
            b"key\x00\x00\t\x00data\n"
213
            b"\n", contents)
2592.1.19 by Robert Collins
Node references are tab separated.
214
4744.2.7 by John Arbash Meinel
Add .clear_cache() members to GraphIndexBuilder and BTreeBuilder.
215
    def test_clear_cache(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
216
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
4744.2.7 by John Arbash Meinel
Add .clear_cache() members to GraphIndexBuilder and BTreeBuilder.
217
        # This is a no-op, but the api should exist
218
        builder.clear_cache()
219
2592.1.22 by Robert Collins
Node references are byte offsets.
220
    def test_node_references_are_byte_offsets(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
221
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
222
        builder.add_node((b'reference', ), b'data', ([], ))
223
        builder.add_node((b'key', ), b'data', ([(b'reference', )], ))
2592.1.22 by Robert Collins
Node references are byte offsets.
224
        stream = builder.finish()
225
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
226
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
227
            b"Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=1\nlen=2\n"
228
            b"key\x00\x0072\x00data\n"
229
            b"reference\x00\x00\x00data\n"
230
            b"\n", contents)
2592.1.22 by Robert Collins
Node references are byte offsets.
231
2592.1.23 by Robert Collins
node reference delimiting tested.
232
    def test_node_references_are_cr_delimited(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
233
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
234
        builder.add_node((b'reference', ), b'data', ([], ))
235
        builder.add_node((b'reference2', ), b'data', ([], ))
236
        builder.add_node((b'key', ), b'data',
237
                         ([(b'reference', ), (b'reference2', )], ))
2592.1.23 by Robert Collins
node reference delimiting tested.
238
        stream = builder.finish()
239
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
240
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
241
            b"Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=1\nlen=3\n"
242
            b"key\x00\x00077\r094\x00data\n"
243
            b"reference\x00\x00\x00data\n"
244
            b"reference2\x00\x00\x00data\n"
245
            b"\n", contents)
2592.1.23 by Robert Collins
node reference delimiting tested.
246
2592.1.24 by Robert Collins
Delimiting of multiple reference lists is by \t
247
    def test_multiple_reference_lists_are_tab_delimited(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
248
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
249
        builder.add_node((b'keference', ), b'data', ([], []))
250
        builder.add_node((b'rey', ), b'data',
251
                         ([(b'keference', )], [(b'keference', )]))
2592.1.24 by Robert Collins
Delimiting of multiple reference lists is by \t
252
        stream = builder.finish()
253
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
254
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
255
            b"Bazaar Graph Index 1\nnode_ref_lists=2\nkey_elements=1\nlen=2\n"
256
            b"keference\x00\x00\t\x00data\n"
257
            b"rey\x00\x0059\t59\x00data\n"
258
            b"\n", contents)
2592.1.24 by Robert Collins
Delimiting of multiple reference lists is by \t
259
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
260
    def test_add_node_referencing_missing_key_makes_absent(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
261
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
262
        builder.add_node((b'rey', ), b'data',
263
                         ([(b'beference', ), (b'aeference2', )], ))
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
264
        stream = builder.finish()
265
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
266
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
267
            b"Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=1\nlen=1\n"
268
            b"aeference2\x00a\x00\x00\n"
269
            b"beference\x00a\x00\x00\n"
270
            b"rey\x00\x00074\r059\x00data\n"
271
            b"\n", contents)
2592.1.25 by Robert Collins
Fix and tune node offset calculation.
272
2592.1.26 by Robert Collins
Test digit buffering is accurate.
273
    def test_node_references_three_digits(self):
274
        # test the node digit expands as needed.
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
275
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
276
        references = [((b"%d" % val), ) for val in range(8, -1, -1)]
277
        builder.add_node((b'2-key', ), b'', (references, ))
2592.1.26 by Robert Collins
Test digit buffering is accurate.
278
        stream = builder.finish()
279
        contents = stream.read()
4789.28.2 by John Arbash Meinel
Get rid of the GraphIndexBuilder/BTreeBuilder._keys attribute.
280
        self.assertEqualDiff(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
281
            b"Bazaar Graph Index 1\nnode_ref_lists=1\nkey_elements=1\nlen=1\n"
282
            b"0\x00a\x00\x00\n"
283
            b"1\x00a\x00\x00\n"
284
            b"2\x00a\x00\x00\n"
285
            b"2-key\x00\x00151\r145\r139\r133\r127\r121\r071\r065\r059\x00\n"
286
            b"3\x00a\x00\x00\n"
287
            b"4\x00a\x00\x00\n"
288
            b"5\x00a\x00\x00\n"
289
            b"6\x00a\x00\x00\n"
290
            b"7\x00a\x00\x00\n"
291
            b"8\x00a\x00\x00\n"
292
            b"\n", contents)
2592.1.40 by Robert Collins
Reverse index ordering - we do not have date prefixed revids.
293
294
    def test_absent_has_no_reference_overhead(self):
295
        # the offsets after an absent record should be correct when there are
296
        # >1 reference lists.
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
297
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
298
        builder.add_node((b'parent', ), b'', ([(b'aail', ), (b'zther', )], []))
2592.1.40 by Robert Collins
Reverse index ordering - we do not have date prefixed revids.
299
        stream = builder.finish()
300
        contents = stream.read()
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
301
        self.assertEqual(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
302
            b"Bazaar Graph Index 1\nnode_ref_lists=2\nkey_elements=1\nlen=1\n"
303
            b"aail\x00a\x00\x00\n"
304
            b"parent\x00\x0059\r84\t\x00\n"
305
            b"zther\x00a\x00\x00\n"
306
            b"\n", contents)
2592.1.26 by Robert Collins
Test digit buffering is accurate.
307
2592.1.13 by Robert Collins
Handle mismatched numbers of reference lists.
308
    def test_add_node_bad_key(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
309
        builder = _mod_index.GraphIndexBuilder()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
310
        for bad_char in bytearray(b'\t\n\x0b\x0c\r\x00 '):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
311
            self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
7143.15.2 by Jelmer Vernooij
Run autopep8.
312
                              (b'a%skey' % int2byte(bad_char), ), b'data')
313
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
314
                          (), b'data')
315
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
316
                          b'not-a-tuple', b'data')
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
317
        # not enough length
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
318
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
7143.15.2 by Jelmer Vernooij
Run autopep8.
319
                          (), b'data')
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
320
        # too long
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
321
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
7143.15.2 by Jelmer Vernooij
Run autopep8.
322
                          (b'primary', b'secondary'), b'data')
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
323
        # secondary key elements get checked too:
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
324
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
325
        for bad_char in bytearray(b'\t\n\x0b\x0c\r\x00 '):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
326
            self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
7143.15.2 by Jelmer Vernooij
Run autopep8.
327
                              (b'prefix', b'a%skey' % int2byte(bad_char)), b'data')
2592.1.12 by Robert Collins
Handle basic node adds.
328
2592.1.13 by Robert Collins
Handle mismatched numbers of reference lists.
329
    def test_add_node_bad_data(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
330
        builder = _mod_index.GraphIndexBuilder()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
331
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
332
                          b'data\naa')
6973.11.3 by Jelmer Vernooij
Fix some index tests.
333
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
334
                          b'data\x00aa')
2592.1.12 by Robert Collins
Handle basic node adds.
335
2592.1.13 by Robert Collins
Handle mismatched numbers of reference lists.
336
    def test_add_node_bad_mismatched_ref_lists_length(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
337
        builder = _mod_index.GraphIndexBuilder()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
338
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
339
                          b'data aa', ([], ))
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
340
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
341
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
342
                          b'data aa')
343
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
344
                          b'data aa', (), )
345
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
346
                          b'data aa', ([], []))
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
347
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
348
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
349
                          b'data aa')
350
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
351
                          b'data aa', ([], ))
352
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, (b'akey', ),
353
                          b'data aa', ([], [], []))
2592.1.13 by Robert Collins
Handle mismatched numbers of reference lists.
354
2592.1.14 by Robert Collins
Detect bad reference key values.
355
    def test_add_node_bad_key_in_reference_lists(self):
356
        # first list, first key - trivial
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
357
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
358
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
359
                          b'data aa', ([(b'a key', )], ))
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
360
        # references keys must be tuples too
6973.11.3 by Jelmer Vernooij
Fix some index tests.
361
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
362
                          b'data aa', (['not-a-tuple'], ))
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
363
        # not enough length
6973.11.3 by Jelmer Vernooij
Fix some index tests.
364
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
365
                          b'data aa', ([()], ))
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
366
        # too long
6973.11.3 by Jelmer Vernooij
Fix some index tests.
367
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
368
                          b'data aa', ([(b'primary', b'secondary')], ))
2592.1.14 by Robert Collins
Detect bad reference key values.
369
        # need to check more than the first key in the list
6973.11.3 by Jelmer Vernooij
Fix some index tests.
370
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
371
                          b'data aa', ([(b'agoodkey', ), (b'that is a bad key', )], ))
2592.1.14 by Robert Collins
Detect bad reference key values.
372
        # and if there is more than one list it should be getting checked
373
        # too
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
374
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
375
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, (b'akey', ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
376
                          b'data aa', ([], ['a bad key']))
2592.1.14 by Robert Collins
Detect bad reference key values.
377
2592.1.15 by Robert Collins
Detect duplicate key insertion.
378
    def test_add_duplicate_key(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
379
        builder = _mod_index.GraphIndexBuilder()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
380
        builder.add_node((b'key', ), b'data')
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
381
        self.assertRaises(_mod_index.BadIndexDuplicateKey,
6973.11.3 by Jelmer Vernooij
Fix some index tests.
382
                          builder.add_node, (b'key', ), b'data')
2592.1.15 by Robert Collins
Detect duplicate key insertion.
383
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
384
    def test_add_duplicate_key_2_elements(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
385
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
386
        builder.add_node((b'key', b'key'), b'data')
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
387
        self.assertRaises(_mod_index.BadIndexDuplicateKey, builder.add_node,
7143.15.2 by Jelmer Vernooij
Run autopep8.
388
                          (b'key', b'key'), b'data')
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
389
2592.1.16 by Robert Collins
Can add keys after referencing them.
390
    def test_add_key_after_referencing_key(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
391
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
392
        builder.add_node((b'key', ), b'data', ([(b'reference', )], ))
393
        builder.add_node((b'reference', ), b'data', ([],))
2592.1.16 by Robert Collins
Can add keys after referencing them.
394
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
395
    def test_add_key_after_referencing_key_2_elements(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
396
        builder = _mod_index.GraphIndexBuilder(
397
            reference_lists=1, key_elements=2)
398
        builder.add_node((b'k', b'ey'), b'data',
399
                         ([(b'reference', b'tokey')], ))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
400
        builder.add_node((b'reference', b'tokey'), b'data', ([],))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
401
3777.5.3 by John Arbash Meinel
Add Builder.set_optimize(for_size=True) for GraphIndexBuilder and BTreeBuilder.
402
    def test_set_optimize(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
403
        builder = _mod_index.GraphIndexBuilder(
404
            reference_lists=1, key_elements=2)
3777.5.3 by John Arbash Meinel
Add Builder.set_optimize(for_size=True) for GraphIndexBuilder and BTreeBuilder.
405
        builder.set_optimize(for_size=True)
406
        self.assertTrue(builder._optimize_for_size)
407
        builder.set_optimize(for_size=False)
408
        self.assertFalse(builder._optimize_for_size)
409
2592.1.5 by Robert Collins
Trivial index reading.
410
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
411
class TestGraphIndex(tests.TestCaseWithMemoryTransport):
2592.1.5 by Robert Collins
Trivial index reading.
412
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
413
    def make_key(self, number):
7143.15.2 by Jelmer Vernooij
Run autopep8.
414
        return ((b'%d' % number) + b'X' * 100,)
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
415
416
    def make_value(self, number):
7143.15.2 by Jelmer Vernooij
Run autopep8.
417
        return (b'%d' % number) + b'Y' * 100
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
418
419
    def make_nodes(self, count=64):
420
        # generate a big enough index that we only read some of it on a typical
421
        # bisection lookup.
422
        nodes = []
423
        for counter in range(count):
7143.15.2 by Jelmer Vernooij
Run autopep8.
424
            nodes.append(
425
                (self.make_key(counter), self.make_value(counter), ()))
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
426
        return nodes
427
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
428
    def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
7143.15.2 by Jelmer Vernooij
Run autopep8.
429
        builder = _mod_index.GraphIndexBuilder(
430
            ref_lists, key_elements=key_elements)
2624.2.17 by Robert Collins
Review feedback.
431
        for key, value, references in nodes:
432
            builder.add_node(key, value, references)
2592.1.5 by Robert Collins
Trivial index reading.
433
        stream = builder.finish()
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
434
        trans = transport.get_transport_from_url('trace+' + self.get_url())
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
435
        size = trans.put_file('index', stream)
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
436
        return _mod_index.GraphIndex(trans, 'index', size)
2592.1.5 by Robert Collins
Trivial index reading.
437
5074.4.3 by John Arbash Meinel
Actually implement offset support for GraphIndex.
438
    def make_index_with_offset(self, ref_lists=0, key_elements=1, nodes=[],
439
                               offset=0):
7143.15.2 by Jelmer Vernooij
Run autopep8.
440
        builder = _mod_index.GraphIndexBuilder(
441
            ref_lists, key_elements=key_elements)
5074.4.3 by John Arbash Meinel
Actually implement offset support for GraphIndex.
442
        for key, value, references in nodes:
443
            builder.add_node(key, value, references)
444
        content = builder.finish().read()
445
        size = len(content)
446
        trans = self.get_transport()
7143.15.2 by Jelmer Vernooij
Run autopep8.
447
        trans.put_bytes('index', (b' ' * offset) + content)
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
448
        return _mod_index.GraphIndex(trans, 'index', size, offset=offset)
5074.4.3 by John Arbash Meinel
Actually implement offset support for GraphIndex.
449
4744.2.6 by John Arbash Meinel
Start exposing an GraphIndex.clear_cache() member.
450
    def test_clear_cache(self):
451
        index = self.make_index()
452
        # For now, we just want to make sure the api is available. As this is
453
        # old code, we don't really worry if it *does* anything.
454
        index.clear_cache()
455
2592.1.7 by Robert Collins
A validate that goes boom.
456
    def test_open_bad_index_no_error(self):
457
        trans = self.get_transport()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
458
        trans.put_bytes('name', b"not an index\n")
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
459
        idx = _mod_index.GraphIndex(trans, 'name', 13)
2592.1.7 by Robert Collins
A validate that goes boom.
460
5074.4.3 by John Arbash Meinel
Actually implement offset support for GraphIndex.
461
    def test_with_offset(self):
462
        nodes = self.make_nodes(200)
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
463
        idx = self.make_index_with_offset(offset=1234567, nodes=nodes)
464
        self.assertEqual(200, idx.key_count())
5074.4.3 by John Arbash Meinel
Actually implement offset support for GraphIndex.
465
466
    def test_buffer_all_with_offset(self):
467
        nodes = self.make_nodes(200)
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
468
        idx = self.make_index_with_offset(offset=1234567, nodes=nodes)
469
        idx._buffer_all()
470
        self.assertEqual(200, idx.key_count())
5074.4.3 by John Arbash Meinel
Actually implement offset support for GraphIndex.
471
472
    def test_side_effect_buffering_with_offset(self):
473
        nodes = self.make_nodes(20)
474
        index = self.make_index_with_offset(offset=1234567, nodes=nodes)
7143.15.2 by Jelmer Vernooij
Run autopep8.
475
        index._transport.recommended_page_size = lambda: 64 * 1024
5074.4.3 by John Arbash Meinel
Actually implement offset support for GraphIndex.
476
        subset_nodes = [nodes[0][0], nodes[10][0], nodes[19][0]]
477
        entries = [n[1] for n in index.iter_entries(subset_nodes)]
478
        self.assertEqual(sorted(subset_nodes), sorted(entries))
479
        self.assertEqual(20, index.key_count())
5074.4.2 by John Arbash Meinel
Add 'offset=' to the GraphIndex api, but refuse to let it be nonzero for now.
480
2890.2.2 by Robert Collins
Opening an index creates a map for the parsed bytes.
481
    def test_open_sets_parsed_map_empty(self):
482
        index = self.make_index()
483
        self.assertEqual([], index._parsed_byte_map)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
484
        self.assertEqual([], index._parsed_key_map)
485
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
486
    def test_key_count_buffers(self):
487
        index = self.make_index(nodes=self.make_nodes(2))
488
        # reset the transport log
489
        del index._transport._activity[:]
490
        self.assertEqual(2, index.key_count())
491
        # We should have requested reading the header bytes
492
        self.assertEqual([
493
            ('readv', 'index', [(0, 200)], True, index._size),
494
            ],
495
            index._transport._activity)
496
        # And that should have been enough to trigger reading the whole index
497
        # with buffering
498
        self.assertIsNot(None, index._nodes)
499
500
    def test_lookup_key_via_location_buffers(self):
501
        index = self.make_index()
502
        # reset the transport log
503
        del index._transport._activity[:]
504
        # do a _lookup_keys_via_location call for the middle of the file, which
505
        # is what bisection uses.
506
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
507
            [(index._size // 2, (b'missing', ))])
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
508
        # this should have asked for a readv request, with adjust_for_latency,
509
        # and two regions: the header, and half-way into the file.
510
        self.assertEqual([
511
            ('readv', 'index', [(30, 30), (0, 200)], True, 60),
512
            ],
513
            index._transport._activity)
514
        # and the result should be that the key cannot be present, because this
515
        # is a trivial index.
6973.11.3 by Jelmer Vernooij
Fix some index tests.
516
        self.assertEqual([((index._size // 2, (b'missing', )), False)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
517
                         result)
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
518
        # And this should have caused the file to be fully buffered
519
        self.assertIsNot(None, index._nodes)
520
        self.assertEqual([], index._parsed_byte_map)
521
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
522
    def test_first_lookup_key_via_location(self):
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
523
        # We need enough data so that the _HEADER_READV doesn't consume the
524
        # whole file. We always read 800 bytes for every key, and the local
525
        # transport natural expansion is 4096 bytes. So we have to have >8192
526
        # bytes or we will trigger "buffer_all".
527
        # We also want the 'missing' key to fall within the range that *did*
528
        # read
529
        nodes = []
530
        index = self.make_index(nodes=self.make_nodes(64))
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
531
        # reset the transport log
532
        del index._transport._activity[:]
2890.2.18 by Robert Collins
Review feedback.
533
        # do a _lookup_keys_via_location call for the middle of the file, which
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
534
        # is what bisection uses.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
535
        start_lookup = index._size // 2
2890.2.18 by Robert Collins
Review feedback.
536
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
537
            [(start_lookup, (b'40missing', ))])
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
538
        # this should have asked for a readv request, with adjust_for_latency,
539
        # and two regions: the header, and half-way into the file.
540
        self.assertEqual([
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
541
            ('readv', 'index',
542
             [(start_lookup, 800), (0, 200)], True, index._size),
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
543
            ],
544
            index._transport._activity)
545
        # and the result should be that the key cannot be present, because this
546
        # is a trivial index.
6973.11.3 by Jelmer Vernooij
Fix some index tests.
547
        self.assertEqual([((start_lookup, (b'40missing', )), False)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
548
                         result)
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
549
        # And this should not have caused the file to be fully buffered
550
        self.assertIs(None, index._nodes)
551
        # And the regions of the file that have been parsed should be in the
552
        # parsed_byte_map and the parsed_key_map
553
        self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
554
        self.assertEqual([((), self.make_key(26)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
555
                          (self.make_key(31), self.make_key(48))],
556
                         index._parsed_key_map)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
557
558
    def test_parsing_non_adjacent_data_trims(self):
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
559
        index = self.make_index(nodes=self.make_nodes(64))
2890.2.18 by Robert Collins
Review feedback.
560
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
561
            [(index._size // 2, (b'40', ))])
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
562
        # and the result should be that the key cannot be present, because key is
563
        # in the middle of the observed data from a 4K read - the smallest transport
564
        # will do today with this api.
6973.11.3 by Jelmer Vernooij
Fix some index tests.
565
        self.assertEqual([((index._size // 2, (b'40', )), False)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
566
                         result)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
567
        # and we should have a parse map that includes the header and the
568
        # region that was parsed after trimming.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
569
        self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
570
        self.assertEqual([((), self.make_key(26)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
571
                          (self.make_key(31), self.make_key(48))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
572
                         index._parsed_key_map)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
573
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
574
    def test_parsing_data_handles_parsed_contained_regions(self):
575
        # the following patten creates a parsed region that is wholly within a
576
        # single result from the readv layer:
577
        # .... single-read (readv-minimum-size) ...
578
        # which then trims the start and end so the parsed size is < readv
579
        # miniumum.
580
        # then a dual lookup (or a reference lookup for that matter) which
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
581
        # abuts or overlaps the parsed region on both sides will need to
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
582
        # discard the data in the middle, but parse the end as well.
583
        #
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
584
        # we test this by doing a single lookup to seed the data, then
585
        # a lookup for two keys that are present, and adjacent -
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
586
        # we except both to be found, and the parsed byte map to include the
587
        # locations of both keys.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
588
        index = self.make_index(nodes=self.make_nodes(128))
2890.2.18 by Robert Collins
Review feedback.
589
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
590
            [(index._size // 2, (b'40', ))])
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
591
        # and we should have a parse map that includes the header and the
592
        # region that was parsed after trimming.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
593
        self.assertEqual([(0, 4045), (11759, 15707)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
594
        self.assertEqual([((), self.make_key(116)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
595
                          (self.make_key(35), self.make_key(51))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
596
                         index._parsed_key_map)
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
597
        # now ask for two keys, right before and after the parsed region
2890.2.18 by Robert Collins
Review feedback.
598
        result = index._lookup_keys_via_location(
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
599
            [(11450, self.make_key(34)), (15707, self.make_key(52))])
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
600
        self.assertEqual([
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
601
            ((11450, self.make_key(34)),
602
             (index, self.make_key(34), self.make_value(34))),
603
            ((15707, self.make_key(52)),
604
             (index, self.make_key(52), self.make_value(52))),
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
605
            ],
606
            result)
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
607
        self.assertEqual([(0, 4045), (9889, 17993)], index._parsed_byte_map)
2890.2.14 by Robert Collins
Parse more than one segment of data from a single readv response if needed.
608
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
609
    def test_lookup_missing_key_answers_without_io_when_map_permits(self):
610
        # generate a big enough index that we only read some of it on a typical
611
        # bisection lookup.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
612
        index = self.make_index(nodes=self.make_nodes(64))
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
613
        # lookup the keys in the middle of the file
7143.15.2 by Jelmer Vernooij
Run autopep8.
614
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
615
            [(index._size // 2, (b'40', ))])
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
616
        # check the parse map, this determines the test validity
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
617
        self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
618
        self.assertEqual([((), self.make_key(26)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
619
                          (self.make_key(31), self.make_key(48))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
620
                         index._parsed_key_map)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
621
        # reset the transport log
622
        del index._transport._activity[:]
623
        # now looking up a key in the portion of the file already parsed should
624
        # not create a new transport request, and should return False (cannot
625
        # be in the index) - even when the byte location we ask for is outside
626
        # the parsed region
2890.2.18 by Robert Collins
Review feedback.
627
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
628
            [(4000, (b'40', ))])
629
        self.assertEqual([((4000, (b'40', )), False)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
630
                         result)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
631
        self.assertEqual([], index._transport._activity)
632
633
    def test_lookup_present_key_answers_without_io_when_map_permits(self):
634
        # generate a big enough index that we only read some of it on a typical
635
        # bisection lookup.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
636
        index = self.make_index(nodes=self.make_nodes(64))
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
637
        # lookup the keys in the middle of the file
7143.15.2 by Jelmer Vernooij
Run autopep8.
638
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
639
            [(index._size // 2, (b'40', ))])
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
640
        # check the parse map, this determines the test validity
641
        self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
642
        self.assertEqual([((), self.make_key(26)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
643
                          (self.make_key(31), self.make_key(48))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
644
                         index._parsed_key_map)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
645
        # reset the transport log
646
        del index._transport._activity[:]
647
        # now looking up a key in the portion of the file already parsed should
648
        # not create a new transport request, and should return False (cannot
649
        # be in the index) - even when the byte location we ask for is outside
650
        # the parsed region
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
651
        #
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
652
        result = index._lookup_keys_via_location([(4000, self.make_key(40))])
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
653
        self.assertEqual(
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
654
            [((4000, self.make_key(40)),
655
              (index, self.make_key(40), self.make_value(40)))],
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
656
            result)
657
        self.assertEqual([], index._transport._activity)
658
659
    def test_lookup_key_below_probed_area(self):
660
        # generate a big enough index that we only read some of it on a typical
661
        # bisection lookup.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
662
        index = self.make_index(nodes=self.make_nodes(64))
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
663
        # ask for the key in the middle, but a key that is located in the
664
        # unparsed region before the middle.
7143.15.2 by Jelmer Vernooij
Run autopep8.
665
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
666
            [(index._size // 2, (b'30', ))])
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
667
        # check the parse map, this determines the test validity
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
668
        self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
669
        self.assertEqual([((), self.make_key(26)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
670
                          (self.make_key(31), self.make_key(48))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
671
                         index._parsed_key_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
672
        self.assertEqual([((index._size // 2, (b'30', )), -1)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
673
                         result)
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
674
675
    def test_lookup_key_above_probed_area(self):
676
        # generate a big enough index that we only read some of it on a typical
677
        # bisection lookup.
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
678
        index = self.make_index(nodes=self.make_nodes(64))
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
679
        # ask for the key in the middle, but a key that is located in the
680
        # unparsed region after the middle.
7143.15.2 by Jelmer Vernooij
Run autopep8.
681
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
682
            [(index._size // 2, (b'50', ))])
2890.2.5 by Robert Collins
Create a content lookup function for bisection in GraphIndex.
683
        # check the parse map, this determines the test validity
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
684
        self.assertEqual([(0, 4008), (5046, 8996)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
685
        self.assertEqual([((), self.make_key(26)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
686
                          (self.make_key(31), self.make_key(48))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
687
                         index._parsed_key_map)
6973.11.5 by Jelmer Vernooij
Update python3.passing.
688
        self.assertEqual([((index._size // 2, (b'50', )), +1)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
689
                         result)
2890.2.2 by Robert Collins
Opening an index creates a map for the parsed bytes.
690
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
691
    def test_lookup_key_resolves_references(self):
692
        # generate a big enough index that we only read some of it on a typical
693
        # bisection lookup.
694
        nodes = []
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
695
        for counter in range(99):
696
            nodes.append((self.make_key(counter), self.make_value(counter),
7143.15.2 by Jelmer Vernooij
Run autopep8.
697
                          ((self.make_key(counter + 20),),)))
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
698
        index = self.make_index(ref_lists=1, nodes=nodes)
699
        # lookup a key in the middle that does not exist, so that when we can
700
        # check that the referred-to-keys are not accessed automatically.
701
        index_size = index._size
702
        index_center = index_size // 2
703
        result = index._lookup_keys_via_location(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
704
            [(index_center, (b'40', ))])
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
705
        # check the parse map - only the start and middle should have been
706
        # parsed.
707
        self.assertEqual([(0, 4027), (10198, 14028)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
708
        self.assertEqual([((), self.make_key(17)),
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
709
                          (self.make_key(44), self.make_key(5))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
710
                         index._parsed_key_map)
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
711
        # and check the transport activity likewise.
712
        self.assertEqual(
713
            [('readv', 'index', [(index_center, 800), (0, 200)], True,
7143.15.2 by Jelmer Vernooij
Run autopep8.
714
              index_size)],
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
715
            index._transport._activity)
716
        # reset the transport log for testing the reference lookup
717
        del index._transport._activity[:]
718
        # now looking up a key in the portion of the file already parsed should
719
        # only perform IO to resolve its key references.
720
        result = index._lookup_keys_via_location([(11000, self.make_key(45))])
721
        self.assertEqual(
722
            [((11000, self.make_key(45)),
723
              (index, self.make_key(45), self.make_value(45),
724
               ((self.make_key(65),),)))],
725
            result)
726
        self.assertEqual([('readv', 'index', [(15093, 800)], True, index_size)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
727
                         index._transport._activity)
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
728
729
    def test_lookup_key_can_buffer_all(self):
730
        nodes = []
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
731
        for counter in range(64):
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
732
            nodes.append((self.make_key(counter), self.make_value(counter),
7143.15.2 by Jelmer Vernooij
Run autopep8.
733
                          ((self.make_key(counter + 20),),)))
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
734
        index = self.make_index(ref_lists=1, nodes=nodes)
735
        # lookup a key in the middle that does not exist, so that when we can
736
        # check that the referred-to-keys are not accessed automatically.
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
737
        index_size = index._size
738
        index_center = index_size // 2
6973.11.3 by Jelmer Vernooij
Fix some index tests.
739
        result = index._lookup_keys_via_location([(index_center, (b'40', ))])
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
740
        # check the parse map - only the start and middle should have been
741
        # parsed.
742
        self.assertEqual([(0, 3890), (6444, 10274)], index._parsed_byte_map)
7067.4.1 by Jelmer Vernooij
Fix bisect on index on Python 3.
743
        self.assertEqual([((), self.make_key(25)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
744
                          (self.make_key(37), self.make_key(52))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
745
                         index._parsed_key_map)
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
746
        # and check the transport activity likewise.
747
        self.assertEqual(
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
748
            [('readv', 'index', [(index_center, 800), (0, 200)], True,
7143.15.2 by Jelmer Vernooij
Run autopep8.
749
              index_size)],
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
750
            index._transport._activity)
751
        # reset the transport log for testing the reference lookup
752
        del index._transport._activity[:]
753
        # now looking up a key in the portion of the file already parsed should
754
        # only perform IO to resolve its key references.
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
755
        result = index._lookup_keys_via_location([(7000, self.make_key(40))])
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
756
        self.assertEqual(
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
757
            [((7000, self.make_key(40)),
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
758
              (index, self.make_key(40), self.make_value(40),
759
               ((self.make_key(60),),)))],
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
760
            result)
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
761
        # Resolving the references would have required more data read, and we
762
        # are already above the 50% threshold, so it triggered a _buffer_all
763
        self.assertEqual([('get', 'index')], index._transport._activity)
2890.2.6 by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface.
764
2592.1.5 by Robert Collins
Trivial index reading.
765
    def test_iter_all_entries_empty(self):
766
        index = self.make_index()
767
        self.assertEqual([], list(index.iter_all_entries()))
768
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
769
    def test_iter_all_entries_simple(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
770
        index = self.make_index(nodes=[((b'name', ), b'data', ())])
771
        self.assertEqual([(index, (b'name', ), b'data')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
772
                         list(index.iter_all_entries()))
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
773
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
774
    def test_iter_all_entries_simple_2_elements(self):
775
        index = self.make_index(key_elements=2,
7143.15.2 by Jelmer Vernooij
Run autopep8.
776
                                nodes=[((b'name', b'surname'), b'data', ())])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
777
        self.assertEqual([(index, (b'name', b'surname'), b'data')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
778
                         list(index.iter_all_entries()))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
779
2592.1.28 by Robert Collins
Basic two pass iter_all_entries.
780
    def test_iter_all_entries_references_resolved(self):
781
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
782
            ((b'name', ), b'data', ([(b'ref', )], )),
783
            ((b'ref', ), b'refdata', ([], ))])
784
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
785
                          (index, (b'ref', ), b'refdata', ((), ))},
786
                         set(index.iter_all_entries()))
2592.1.28 by Robert Collins
Basic two pass iter_all_entries.
787
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
788
    def test_iter_entries_buffers_once(self):
789
        index = self.make_index(nodes=self.make_nodes(2))
790
        # reset the transport log
791
        del index._transport._activity[:]
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
792
        self.assertEqual({(index, self.make_key(1), self.make_value(1))},
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
793
                         set(index.iter_entries([self.make_key(1)])))
794
        # We should have requested reading the header bytes
795
        # But not needed any more than that because it would have triggered a
796
        # buffer all
797
        self.assertEqual([
798
            ('readv', 'index', [(0, 200)], True, index._size),
799
            ],
800
            index._transport._activity)
801
        # And that should have been enough to trigger reading the whole index
802
        # with buffering
803
        self.assertIsNot(None, index._nodes)
804
3665.3.3 by John Arbash Meinel
If we read more than 50% of the whole index,
805
    def test_iter_entries_buffers_by_bytes_read(self):
806
        index = self.make_index(nodes=self.make_nodes(64))
807
        list(index.iter_entries([self.make_key(10)]))
808
        # The first time through isn't enough to trigger a buffer all
809
        self.assertIs(None, index._nodes)
810
        self.assertEqual(4096, index._bytes_read)
811
        # Grabbing a key in that same page won't trigger a buffer all, as we
812
        # still haven't read 50% of the file
813
        list(index.iter_entries([self.make_key(11)]))
814
        self.assertIs(None, index._nodes)
815
        self.assertEqual(4096, index._bytes_read)
816
        # We haven't read more data, so reading outside the range won't trigger
817
        # a buffer all right away
818
        list(index.iter_entries([self.make_key(40)]))
819
        self.assertIs(None, index._nodes)
820
        self.assertEqual(8192, index._bytes_read)
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
821
        # On the next pass, we will not trigger buffer all if the key is
822
        # available without reading more
3665.3.3 by John Arbash Meinel
If we read more than 50% of the whole index,
823
        list(index.iter_entries([self.make_key(32)]))
3665.3.5 by John Arbash Meinel
Move the point at which we 'buffer_all' if we've read >50% of the index.
824
        self.assertIs(None, index._nodes)
825
        # But if we *would* need to read more to resolve it, then we will
826
        # buffer all.
827
        list(index.iter_entries([self.make_key(60)]))
3665.3.3 by John Arbash Meinel
If we read more than 50% of the whole index,
828
        self.assertIsNot(None, index._nodes)
829
2890.2.10 by Robert Collins
Add test coverage to ensure \r's are not mangled by bisection parsing.
830
    def test_iter_entries_references_resolved(self):
831
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
832
            ((b'name', ), b'data', ([(b'ref', ), (b'ref', )], )),
833
            ((b'ref', ), b'refdata', ([], ))])
834
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',), (b'ref',)),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
835
                          (index, (b'ref', ), b'refdata', ((), ))},
836
                         set(index.iter_entries([(b'name',), (b'ref',)])))
2890.2.10 by Robert Collins
Add test coverage to ensure \r's are not mangled by bisection parsing.
837
838
    def test_iter_entries_references_2_refs_resolved(self):
839
        index = self.make_index(2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
840
            ((b'name', ), b'data', ([(b'ref', )], [(b'ref', )])),
841
            ((b'ref', ), b'refdata', ([], []))])
842
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),), ((b'ref',),))),
7143.15.2 by Jelmer Vernooij
Run autopep8.
843
                          (index, (b'ref', ), b'refdata', ((), ()))},
844
                         set(index.iter_entries([(b'name',), (b'ref',)])))
2890.2.10 by Robert Collins
Add test coverage to ensure \r's are not mangled by bisection parsing.
845
2592.1.30 by Robert Collins
Absent entries are not yeilded.
846
    def test_iteration_absent_skipped(self):
847
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
848
            ((b'name', ), b'data', ([(b'ref', )], ))])
849
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),))},
7143.15.2 by Jelmer Vernooij
Run autopep8.
850
                         set(index.iter_all_entries()))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
851
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),))},
7143.15.2 by Jelmer Vernooij
Run autopep8.
852
                         set(index.iter_entries([(b'name', )])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
853
        self.assertEqual([], list(index.iter_entries([(b'ref', )])))
2592.1.30 by Robert Collins
Absent entries are not yeilded.
854
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
855
    def test_iteration_absent_skipped_2_element_keys(self):
856
        index = self.make_index(1, key_elements=2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
857
            ((b'name', b'fin'), b'data', ([(b'ref', b'erence')], ))])
6973.11.4 by Jelmer Vernooij
use context managers.
858
        self.assertEqual([(index, (b'name', b'fin'), b'data', (((b'ref', b'erence'),),))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
859
                         list(index.iter_all_entries()))
6973.11.4 by Jelmer Vernooij
use context managers.
860
        self.assertEqual([(index, (b'name', b'fin'), b'data', (((b'ref', b'erence'),),))],
7143.15.2 by Jelmer Vernooij
Run autopep8.
861
                         list(index.iter_entries([(b'name', b'fin')])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
862
        self.assertEqual([], list(index.iter_entries([(b'ref', b'erence')])))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
863
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
864
    def test_iter_all_keys(self):
2592.1.29 by Robert Collins
Basic iter_entries working.
865
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
866
            ((b'name', ), b'data', ([(b'ref', )], )),
867
            ((b'ref', ), b'refdata', ([], ))])
868
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
869
                          (index, (b'ref', ), b'refdata', ((), ))},
870
                         set(index.iter_entries([(b'name', ), (b'ref', )])))
2592.1.29 by Robert Collins
Basic iter_entries working.
871
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
872
    def test_iter_nothing_empty(self):
2592.1.9 by Robert Collins
Iterating no keys should work too.
873
        index = self.make_index()
874
        self.assertEqual([], list(index.iter_entries([])))
875
2592.1.5 by Robert Collins
Trivial index reading.
876
    def test_iter_missing_entry_empty(self):
877
        index = self.make_index()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
878
        self.assertEqual([], list(index.iter_entries([(b'a', )])))
2592.1.7 by Robert Collins
A validate that goes boom.
879
2890.2.8 by Robert Collins
Make the size of the index optionally None for the pack-names index.
880
    def test_iter_missing_entry_empty_no_size(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
881
        idx = self.make_index()
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
882
        idx = _mod_index.GraphIndex(idx._transport, 'index', None)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
883
        self.assertEqual([], list(idx.iter_entries([(b'a', )])))
2890.2.8 by Robert Collins
Make the size of the index optionally None for the pack-names index.
884
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
885
    def test_iter_key_prefix_1_element_key_None(self):
886
        index = self.make_index()
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
887
        self.assertRaises(_mod_index.BadIndexKey, list,
7143.15.2 by Jelmer Vernooij
Run autopep8.
888
                          index.iter_entries_prefix([(None, )]))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
889
890
    def test_iter_key_prefix_wrong_length(self):
891
        index = self.make_index()
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
892
        self.assertRaises(_mod_index.BadIndexKey, list,
7143.15.2 by Jelmer Vernooij
Run autopep8.
893
                          index.iter_entries_prefix([(b'foo', None)]))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
894
        index = self.make_index(key_elements=2)
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
895
        self.assertRaises(_mod_index.BadIndexKey, list,
7143.15.2 by Jelmer Vernooij
Run autopep8.
896
                          index.iter_entries_prefix([(b'foo', )]))
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
897
        self.assertRaises(_mod_index.BadIndexKey, list,
7143.15.2 by Jelmer Vernooij
Run autopep8.
898
                          index.iter_entries_prefix([(b'foo', None, None)]))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
899
900
    def test_iter_key_prefix_1_key_element_no_refs(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
901
        index = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
902
            ((b'name', ), b'data', ()),
903
            ((b'ref', ), b'refdata', ())])
904
        self.assertEqual({(index, (b'name', ), b'data'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
905
                          (index, (b'ref', ), b'refdata')},
906
                         set(index.iter_entries_prefix([(b'name', ), (b'ref', )])))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
907
908
    def test_iter_key_prefix_1_key_element_refs(self):
909
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
910
            ((b'name', ), b'data', ([(b'ref', )], )),
911
            ((b'ref', ), b'refdata', ([], ))])
912
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
913
                          (index, (b'ref', ), b'refdata', ((), ))},
914
                         set(index.iter_entries_prefix([(b'name', ), (b'ref', )])))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
915
916
    def test_iter_key_prefix_2_key_element_no_refs(self):
917
        index = self.make_index(key_elements=2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
918
            ((b'name', b'fin1'), b'data', ()),
919
            ((b'name', b'fin2'), b'beta', ()),
920
            ((b'ref', b'erence'), b'refdata', ())])
921
        self.assertEqual({(index, (b'name', b'fin1'), b'data'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
922
                          (index, (b'ref', b'erence'), b'refdata')},
923
                         set(index.iter_entries_prefix([(b'name', b'fin1'), (b'ref', b'erence')])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
924
        self.assertEqual({(index, (b'name', b'fin1'), b'data'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
925
                          (index, (b'name', b'fin2'), b'beta')},
926
                         set(index.iter_entries_prefix([(b'name', None)])))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
927
928
    def test_iter_key_prefix_2_key_element_refs(self):
929
        index = self.make_index(1, key_elements=2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
930
            ((b'name', b'fin1'), b'data', ([(b'ref', b'erence')], )),
931
            ((b'name', b'fin2'), b'beta', ([], )),
932
            ((b'ref', b'erence'), b'refdata', ([], ))])
933
        self.assertEqual({(index, (b'name', b'fin1'), b'data', (((b'ref', b'erence'),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
934
                          (index, (b'ref', b'erence'), b'refdata', ((), ))},
935
                         set(index.iter_entries_prefix([(b'name', b'fin1'), (b'ref', b'erence')])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
936
        self.assertEqual({(index, (b'name', b'fin1'), b'data', (((b'ref', b'erence'),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
937
                          (index, (b'name', b'fin2'), b'beta', ((), ))},
938
                         set(index.iter_entries_prefix([(b'name', None)])))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
939
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
940
    def test_key_count_empty(self):
941
        index = self.make_index()
942
        self.assertEqual(0, index.key_count())
943
944
    def test_key_count_one(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
945
        index = self.make_index(nodes=[((b'name', ), b'', ())])
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
946
        self.assertEqual(1, index.key_count())
947
948
    def test_key_count_two(self):
949
        index = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
950
            ((b'name', ), b'', ()), ((b'foo', ), b'', ())])
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
951
        self.assertEqual(2, index.key_count())
952
3665.3.3 by John Arbash Meinel
If we read more than 50% of the whole index,
953
    def test_read_and_parse_tracks_real_read_value(self):
954
        index = self.make_index(nodes=self.make_nodes(10))
955
        del index._transport._activity[:]
956
        index._read_and_parse([(0, 200)])
957
        self.assertEqual([
958
            ('readv', 'index', [(0, 200)], True, index._size),
959
            ],
960
            index._transport._activity)
961
        # The readv expansion code will expand the initial request to 4096
962
        # bytes, which is more than enough to read the entire index, and we
963
        # will track the fact that we read that many bytes.
964
        self.assertEqual(index._size, index._bytes_read)
965
966
    def test_read_and_parse_triggers_buffer_all(self):
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
967
        index = self.make_index(key_elements=2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
968
            ((b'name', b'fin1'), b'data', ()),
969
            ((b'name', b'fin2'), b'beta', ()),
970
            ((b'ref', b'erence'), b'refdata', ())])
3665.3.1 by John Arbash Meinel
Updates to GraphIndex processing.
971
        self.assertTrue(index._size > 0)
972
        self.assertIs(None, index._nodes)
973
        index._read_and_parse([(0, index._size)])
974
        self.assertIsNot(None, index._nodes)
975
2592.1.7 by Robert Collins
A validate that goes boom.
976
    def test_validate_bad_index_errors(self):
977
        trans = self.get_transport()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
978
        trans.put_bytes('name', b"not an index\n")
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
979
        idx = _mod_index.GraphIndex(trans, 'name', 13)
980
        self.assertRaises(_mod_index.BadIndexFormatSignature, idx.validate)
2592.1.8 by Robert Collins
Empty files should validate ok.
981
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
982
    def test_validate_bad_node_refs(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
983
        idx = self.make_index(2)
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
984
        trans = self.get_transport()
985
        content = trans.get_bytes('index')
986
        # change the options line to end with a rather than a parseable number
6973.11.3 by Jelmer Vernooij
Fix some index tests.
987
        new_content = content[:-2] + b'a\n\n'
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
988
        trans.put_bytes('index', new_content)
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
989
        self.assertRaises(_mod_index.BadIndexOptions, idx.validate)
2592.1.10 by Robert Collins
Make validate detect node reference parsing errors.
990
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
991
    def test_validate_missing_end_line_empty(self):
2592.1.11 by Robert Collins
Detect truncated indices.
992
        index = self.make_index(2)
993
        trans = self.get_transport()
994
        content = trans.get_bytes('index')
995
        # truncate the last byte
996
        trans.put_bytes('index', content[:-1])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
997
        self.assertRaises(_mod_index.BadIndexData, index.validate)
2592.1.11 by Robert Collins
Detect truncated indices.
998
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
999
    def test_validate_missing_end_line_nonempty(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1000
        index = self.make_index(2, nodes=[((b'key', ), b'', ([], []))])
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
1001
        trans = self.get_transport()
1002
        content = trans.get_bytes('index')
1003
        # truncate the last byte
1004
        trans.put_bytes('index', content[:-1])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1005
        self.assertRaises(_mod_index.BadIndexData, index.validate)
2592.1.27 by Robert Collins
Test missing end lines with non-empty indices.
1006
2592.1.8 by Robert Collins
Empty files should validate ok.
1007
    def test_validate_empty(self):
1008
        index = self.make_index()
1009
        index.validate()
2592.1.12 by Robert Collins
Handle basic node adds.
1010
1011
    def test_validate_no_refs_content(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1012
        index = self.make_index(nodes=[((b'key', ), b'value', ())])
2592.1.12 by Robert Collins
Handle basic node adds.
1013
        index.validate()
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1014
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1015
    # XXX: external_references tests are duplicated in test_btree_index.  We
1016
    # probably should have per_graph_index tests...
1017
    def test_external_references_no_refs(self):
1018
        index = self.make_index(ref_lists=0, nodes=[])
1019
        self.assertRaises(ValueError, index.external_references, 0)
1020
1021
    def test_external_references_no_results(self):
1022
        index = self.make_index(ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1023
            ((b'key',), b'value', ([],))])
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1024
        self.assertEqual(set(), index.external_references(0))
1025
1026
    def test_external_references_missing_ref(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1027
        missing_key = (b'missing',)
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1028
        index = self.make_index(ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1029
            ((b'key',), b'value', ([missing_key],))])
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1030
        self.assertEqual({missing_key}, index.external_references(0))
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1031
1032
    def test_external_references_multiple_ref_lists(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1033
        missing_key = (b'missing',)
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1034
        index = self.make_index(ref_lists=2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1035
            ((b'key',), b'value', ([], [missing_key]))])
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1036
        self.assertEqual(set([]), index.external_references(0))
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1037
        self.assertEqual({missing_key}, index.external_references(1))
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1038
1039
    def test_external_references_two_records(self):
1040
        index = self.make_index(ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1041
            ((b'key-1',), b'value', ([(b'key-2',)],)),
1042
            ((b'key-2',), b'value', ([],)),
4011.5.3 by Andrew Bennetts
Implement and test external_references on GraphIndex and BTreeGraphIndex.
1043
            ])
1044
        self.assertEqual(set([]), index.external_references(0))
1045
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1046
    def test__find_ancestors(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1047
        key1 = (b'key-1',)
1048
        key2 = (b'key-2',)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1049
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1050
            (key1, b'value', ([key2],)),
1051
            (key2, b'value', ([],)),
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1052
            ])
1053
        parent_map = {}
1054
        missing_keys = set()
7143.15.2 by Jelmer Vernooij
Run autopep8.
1055
        search_keys = index._find_ancestors(
1056
            [key1], 0, parent_map, missing_keys)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1057
        self.assertEqual({key1: (key2,)}, parent_map)
1058
        self.assertEqual(set(), missing_keys)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1059
        self.assertEqual({key2}, search_keys)
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1060
        search_keys = index._find_ancestors(search_keys, 0, parent_map,
1061
                                            missing_keys)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1062
        self.assertEqual({key1: (key2,), key2: ()}, parent_map)
1063
        self.assertEqual(set(), missing_keys)
1064
        self.assertEqual(set(), search_keys)
1065
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1066
    def test__find_ancestors_w_missing(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1067
        key1 = (b'key-1',)
1068
        key2 = (b'key-2',)
1069
        key3 = (b'key-3',)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1070
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1071
            (key1, b'value', ([key2],)),
1072
            (key2, b'value', ([],)),
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1073
            ])
1074
        parent_map = {}
1075
        missing_keys = set()
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1076
        search_keys = index._find_ancestors([key2, key3], 0, parent_map,
1077
                                            missing_keys)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1078
        self.assertEqual({key2: ()}, parent_map)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1079
        self.assertEqual({key3}, missing_keys)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1080
        self.assertEqual(set(), search_keys)
1081
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1082
    def test__find_ancestors_dont_search_known(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1083
        key1 = (b'key-1',)
1084
        key2 = (b'key-2',)
1085
        key3 = (b'key-3',)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1086
        index = self.make_index(ref_lists=1, key_elements=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1087
            (key1, b'value', ([key2],)),
1088
            (key2, b'value', ([key3],)),
1089
            (key3, b'value', ([],)),
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1090
            ])
1091
        # We already know about key2, so we won't try to search for key3
1092
        parent_map = {key2: (key3,)}
1093
        missing_keys = set()
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1094
        search_keys = index._find_ancestors([key1], 0, parent_map,
1095
                                            missing_keys)
4593.4.7 by John Arbash Meinel
Basic implementation of a conforming interface for GraphIndex.
1096
        self.assertEqual({key1: (key2,), key2: (key3,)}, parent_map)
1097
        self.assertEqual(set(), missing_keys)
1098
        self.assertEqual(set(), search_keys)
1099
4634.71.1 by John Arbash Meinel
Work around bug #402623 by allowing BTreeGraphIndex(...,unlimited_cache=True).
1100
    def test_supports_unlimited_cache(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1101
        builder = _mod_index.GraphIndexBuilder(0, key_elements=1)
4634.71.1 by John Arbash Meinel
Work around bug #402623 by allowing BTreeGraphIndex(...,unlimited_cache=True).
1102
        stream = builder.finish()
5609.9.4 by Vincent Ladeuil
Use self.get_transport instead of transport.get_transport where possible.
1103
        trans = self.get_transport()
4634.71.1 by John Arbash Meinel
Work around bug #402623 by allowing BTreeGraphIndex(...,unlimited_cache=True).
1104
        size = trans.put_file('index', stream)
1105
        # It doesn't matter what unlimited_cache does here, just that it can be
1106
        # passed
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1107
        idx = _mod_index.GraphIndex(trans, 'index', size, unlimited_cache=True)
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1108
1109
1110
class TestCombinedGraphIndex(tests.TestCaseWithMemoryTransport):
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1111
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
1112
    def make_index(self, name, ref_lists=0, key_elements=1, nodes=[]):
7143.15.2 by Jelmer Vernooij
Run autopep8.
1113
        builder = _mod_index.GraphIndexBuilder(
1114
            ref_lists, key_elements=key_elements)
2624.2.17 by Robert Collins
Review feedback.
1115
        for key, value, references in nodes:
1116
            builder.add_node(key, value, references)
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1117
        stream = builder.finish()
1118
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
1119
        size = trans.put_file(name, stream)
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1120
        return _mod_index.GraphIndex(trans, name, size)
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1121
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
1122
    def make_combined_index_with_missing(self, missing=['1', '2']):
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1123
        """Create a CombinedGraphIndex which will have missing indexes.
1124
1125
        This creates a CGI which thinks it has 2 indexes, however they have
1126
        been deleted. If CGI._reload_func() is called, then it will repopulate
1127
        with a new index.
1128
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1129
        :param missing: The underlying indexes to delete
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1130
        :return: (CombinedGraphIndex, reload_counter)
1131
        """
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1132
        idx1 = self.make_index('1', nodes=[((b'1',), b'', ())])
1133
        idx2 = self.make_index('2', nodes=[((b'2',), b'', ())])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1134
        idx3 = self.make_index('3', nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1135
            ((b'1',), b'', ()),
1136
            ((b'2',), b'', ())])
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1137
1138
        # total_reloads, num_changed, num_unchanged
1139
        reload_counter = [0, 0, 0]
7143.15.2 by Jelmer Vernooij
Run autopep8.
1140
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1141
        def reload():
1142
            reload_counter[0] += 1
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1143
            new_indices = [idx3]
1144
            if idx._indices == new_indices:
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1145
                reload_counter[2] += 1
1146
                return False
1147
            reload_counter[1] += 1
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1148
            idx._indices[:] = new_indices
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1149
            return True
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1150
        idx = _mod_index.CombinedGraphIndex([idx1, idx2], reload_func=reload)
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1151
        trans = self.get_transport()
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1152
        for fname in missing:
1153
            trans.delete(fname)
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1154
        return idx, reload_counter
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1155
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1156
    def test_open_missing_index_no_error(self):
1157
        trans = self.get_transport()
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1158
        idx1 = _mod_index.GraphIndex(trans, 'missing', 100)
1159
        idx = _mod_index.CombinedGraphIndex([idx1])
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1160
2592.1.37 by Robert Collins
Add CombinedGraphIndex.insert_index.
1161
    def test_add_index(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1162
        idx = _mod_index.CombinedGraphIndex([])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1163
        idx1 = self.make_index('name', 0, nodes=[((b'key', ), b'', ())])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1164
        idx.insert_index(0, idx1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1165
        self.assertEqual([(idx1, (b'key', ), b'')],
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1166
                         list(idx.iter_all_entries()))
2592.1.37 by Robert Collins
Add CombinedGraphIndex.insert_index.
1167
4744.2.6 by John Arbash Meinel
Start exposing an GraphIndex.clear_cache() member.
1168
    def test_clear_cache(self):
1169
        log = []
1170
1171
        class ClearCacheProxy(object):
1172
1173
            def __init__(self, index):
1174
                self._index = index
1175
1176
            def __getattr__(self, name):
1177
                return getattr(self._index)
1178
1179
            def clear_cache(self):
1180
                log.append(self._index)
1181
                return self._index.clear_cache()
1182
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1183
        idx = _mod_index.CombinedGraphIndex([])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1184
        idx1 = self.make_index('name', 0, nodes=[((b'key', ), b'', ())])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1185
        idx.insert_index(0, ClearCacheProxy(idx1))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1186
        idx2 = self.make_index('name', 0, nodes=[((b'key', ), b'', ())])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1187
        idx.insert_index(1, ClearCacheProxy(idx2))
4744.2.6 by John Arbash Meinel
Start exposing an GraphIndex.clear_cache() member.
1188
        # CombinedGraphIndex should call 'clear_cache()' on all children
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1189
        idx.clear_cache()
1190
        self.assertEqual(sorted([idx1, idx2]), sorted(log))
4744.2.6 by John Arbash Meinel
Start exposing an GraphIndex.clear_cache() member.
1191
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1192
    def test_iter_all_entries_empty(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1193
        idx = _mod_index.CombinedGraphIndex([])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1194
        self.assertEqual([], list(idx.iter_all_entries()))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1195
1196
    def test_iter_all_entries_children_empty(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1197
        idx1 = self.make_index('name')
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1198
        idx = _mod_index.CombinedGraphIndex([idx1])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1199
        self.assertEqual([], list(idx.iter_all_entries()))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1200
1201
    def test_iter_all_entries_simple(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1202
        idx1 = self.make_index('name', nodes=[((b'name', ), b'data', ())])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1203
        idx = _mod_index.CombinedGraphIndex([idx1])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1204
        self.assertEqual([(idx1, (b'name', ), b'data')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
1205
                         list(idx.iter_all_entries()))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1206
1207
    def test_iter_all_entries_two_indices(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1208
        idx1 = self.make_index('name1', nodes=[((b'name', ), b'data', ())])
1209
        idx2 = self.make_index('name2', nodes=[((b'2', ), b'', ())])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1210
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1211
        self.assertEqual([(idx1, (b'name', ), b'data'),
1212
                          (idx2, (b'2', ), b'')],
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1213
                         list(idx.iter_all_entries()))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1214
2592.1.39 by Robert Collins
CombinedGraphIndex.iter_entries does not need to see all entries.
1215
    def test_iter_entries_two_indices_dup_key(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1216
        idx1 = self.make_index('name1', nodes=[((b'name', ), b'data', ())])
1217
        idx2 = self.make_index('name2', nodes=[((b'name', ), b'data', ())])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1218
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1219
        self.assertEqual([(idx1, (b'name', ), b'data')],
1220
                         list(idx.iter_entries([(b'name', )])))
2592.1.39 by Robert Collins
CombinedGraphIndex.iter_entries does not need to see all entries.
1221
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1222
    def test_iter_all_entries_two_indices_dup_key(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1223
        idx1 = self.make_index('name1', nodes=[((b'name', ), b'data', ())])
1224
        idx2 = self.make_index('name2', nodes=[((b'name', ), b'data', ())])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1225
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1226
        self.assertEqual([(idx1, (b'name', ), b'data')],
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1227
                         list(idx.iter_all_entries()))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1228
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
1229
    def test_iter_key_prefix_2_key_element_refs(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1230
        idx1 = self.make_index('1', 1, key_elements=2, nodes=[
7143.15.2 by Jelmer Vernooij
Run autopep8.
1231
            ((b'name', b'fin1'), b'data', ([(b'ref', b'erence')], ))])
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
1232
        idx2 = self.make_index('2', 1, key_elements=2, nodes=[
7143.15.2 by Jelmer Vernooij
Run autopep8.
1233
            ((b'name', b'fin2'), b'beta', ([], )),
1234
            ((b'ref', b'erence'), b'refdata', ([], ))])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1235
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1236
        self.assertEqual({(idx1, (b'name', b'fin1'), b'data',
7143.15.2 by Jelmer Vernooij
Run autopep8.
1237
                           (((b'ref', b'erence'),),)),
1238
                          (idx2, (b'ref', b'erence'), b'refdata', ((), ))},
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1239
                         set(idx.iter_entries_prefix([(b'name', b'fin1'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1240
                                                      (b'ref', b'erence')])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1241
        self.assertEqual({(idx1, (b'name', b'fin1'), b'data',
7143.15.2 by Jelmer Vernooij
Run autopep8.
1242
                           (((b'ref', b'erence'),),)),
1243
                          (idx2, (b'name', b'fin2'), b'beta', ((), ))},
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1244
                         set(idx.iter_entries_prefix([(b'name', None)])))
2624.2.9 by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one.
1245
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1246
    def test_iter_nothing_empty(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1247
        idx = _mod_index.CombinedGraphIndex([])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1248
        self.assertEqual([], list(idx.iter_entries([])))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1249
1250
    def test_iter_nothing_children_empty(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1251
        idx1 = self.make_index('name')
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1252
        idx = _mod_index.CombinedGraphIndex([idx1])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1253
        self.assertEqual([], list(idx.iter_entries([])))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1254
1255
    def test_iter_all_keys(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1256
        idx1 = self.make_index('1', 1, nodes=[((b'name', ), b'data',
1257
                                               ([(b'ref', )], ))])
7143.15.2 by Jelmer Vernooij
Run autopep8.
1258
        idx2 = self.make_index(
1259
            '2', 1, nodes=[((b'ref', ), b'refdata', ((), ))])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1260
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1261
        self.assertEqual({(idx1, (b'name', ), b'data', (((b'ref', ), ), )),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1262
                          (idx2, (b'ref', ), b'refdata', ((), ))},
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1263
                         set(idx.iter_entries([(b'name', ), (b'ref', )])))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1264
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1265
    def test_iter_all_keys_dup_entry(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1266
        idx1 = self.make_index('1', 1, nodes=[((b'name', ), b'data',
7143.15.2 by Jelmer Vernooij
Run autopep8.
1267
                                               ([(b'ref', )], )),
1268
                                              ((b'ref', ), b'refdata', ([], ))])
1269
        idx2 = self.make_index(
1270
            '2', 1, nodes=[((b'ref', ), b'refdata', ([], ))])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1271
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1272
        self.assertEqual({(idx1, (b'name', ), b'data', (((b'ref',),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1273
                          (idx1, (b'ref', ), b'refdata', ((), ))},
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1274
                         set(idx.iter_entries([(b'name', ), (b'ref', )])))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1275
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1276
    def test_iter_missing_entry_empty(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1277
        idx = _mod_index.CombinedGraphIndex([])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1278
        self.assertEqual([], list(idx.iter_entries([('a', )])))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1279
1280
    def test_iter_missing_entry_one_index(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1281
        idx1 = self.make_index('1')
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1282
        idx = _mod_index.CombinedGraphIndex([idx1])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1283
        self.assertEqual([], list(idx.iter_entries([(b'a', )])))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1284
1285
    def test_iter_missing_entry_two_index(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1286
        idx1 = self.make_index('1')
1287
        idx2 = self.make_index('2')
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1288
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1289
        self.assertEqual([], list(idx.iter_entries([('a', )])))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1290
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1291
    def test_iter_entry_present_one_index_only(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1292
        idx1 = self.make_index('1', nodes=[((b'key', ), b'', ())])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1293
        idx2 = self.make_index('2', nodes=[])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1294
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1295
        self.assertEqual([(idx1, (b'key', ), b'')],
1296
                         list(idx.iter_entries([(b'key', )])))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1297
        # and in the other direction
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1298
        idx = _mod_index.CombinedGraphIndex([idx2, idx1])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1299
        self.assertEqual([(idx1, (b'key', ), b'')],
1300
                         list(idx.iter_entries([(b'key', )])))
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1301
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1302
    def test_key_count_empty(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1303
        idx1 = self.make_index('1', nodes=[])
1304
        idx2 = self.make_index('2', nodes=[])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1305
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1306
        self.assertEqual(0, idx.key_count())
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1307
1308
    def test_key_count_sums_index_keys(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1309
        idx1 = self.make_index('1', nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1310
            ((b'1',), b'', ()),
1311
            ((b'2',), b'', ())])
1312
        idx2 = self.make_index('2', nodes=[((b'1',), b'', ())])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1313
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1314
        self.assertEqual(3, idx.key_count())
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1315
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1316
    def test_validate_bad_child_index_errors(self):
1317
        trans = self.get_transport()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1318
        trans.put_bytes('name', b"not an index\n")
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1319
        idx1 = _mod_index.GraphIndex(trans, 'name', 13)
1320
        idx = _mod_index.CombinedGraphIndex([idx1])
1321
        self.assertRaises(_mod_index.BadIndexFormatSignature, idx.validate)
2592.1.31 by Robert Collins
Build a combined graph index to use multiple indices at once.
1322
1323
    def test_validate_empty(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1324
        idx = _mod_index.CombinedGraphIndex([])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1325
        idx.validate()
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1326
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1327
    def test_key_count_reloads(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1328
        idx, reload_counter = self.make_combined_index_with_missing()
1329
        self.assertEqual(2, idx.key_count())
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1330
        self.assertEqual([1, 1, 0], reload_counter)
1331
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1332
    def test_key_count_no_reload(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1333
        idx, reload_counter = self.make_combined_index_with_missing()
1334
        idx._reload_func = None
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1335
        # Without a _reload_func we just raise the exception
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1336
        self.assertRaises(errors.NoSuchFile, idx.key_count)
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1337
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1338
    def test_key_count_reloads_and_fails(self):
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1339
        # We have deleted all underlying indexes, so we will try to reload, but
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1340
        # still fail. This is mostly to test we don't get stuck in an infinite
1341
        # loop trying to reload
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1342
        idx, reload_counter = self.make_combined_index_with_missing(
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
1343
            ['1', '2', '3'])
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1344
        self.assertRaises(errors.NoSuchFile, idx.key_count)
3789.1.3 by John Arbash Meinel
CombinedGraphIndex can now reload when calling key_count().
1345
        self.assertEqual([2, 1, 1], reload_counter)
1346
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1347
    def test_iter_entries_reloads(self):
1348
        index, reload_counter = self.make_combined_index_with_missing()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1349
        result = list(index.iter_entries([(b'1',), (b'2',), (b'3',)]))
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1350
        index3 = index._indices[0]
7058.3.4 by Jelmer Vernooij
Allow iter_entries to return entries in either order.
1351
        self.assertEqual({(index3, (b'1',), b''), (index3, (b'2',), b'')},
1352
                         set(result))
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1353
        self.assertEqual([1, 1, 0], reload_counter)
1354
1355
    def test_iter_entries_reloads_midway(self):
1356
        # The first index still looks present, so we get interrupted mid-way
1357
        # through
1358
        index, reload_counter = self.make_combined_index_with_missing(['2'])
1359
        index1, index2 = index._indices
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1360
        result = list(index.iter_entries([(b'1',), (b'2',), (b'3',)]))
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1361
        index3 = index._indices[0]
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1362
        # We had already yielded b'1', so we just go on to the next, we should
1363
        # not yield b'1' twice.
1364
        self.assertEqual([(index1, (b'1',), b''), (index3, (b'2',), b'')],
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1365
                         result)
1366
        self.assertEqual([1, 1, 0], reload_counter)
1367
1368
    def test_iter_entries_no_reload(self):
1369
        index, reload_counter = self.make_combined_index_with_missing()
1370
        index._reload_func = None
1371
        # Without a _reload_func we just raise the exception
1372
        self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
1373
1374
    def test_iter_entries_reloads_and_fails(self):
1375
        index, reload_counter = self.make_combined_index_with_missing(
7143.15.2 by Jelmer Vernooij
Run autopep8.
1376
            ['1', '2', '3'])
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1377
        self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
1378
        self.assertEqual([2, 1, 1], reload_counter)
1379
3789.1.5 by John Arbash Meinel
CombinedGraphIndex.iter_all_entries() can now reload when needed.
1380
    def test_iter_all_entries_reloads(self):
1381
        index, reload_counter = self.make_combined_index_with_missing()
1382
        result = list(index.iter_all_entries())
1383
        index3 = index._indices[0]
7058.3.4 by Jelmer Vernooij
Allow iter_entries to return entries in either order.
1384
        self.assertEqual({(index3, (b'1',), b''), (index3, (b'2',), b'')},
1385
                         set(result))
3789.1.5 by John Arbash Meinel
CombinedGraphIndex.iter_all_entries() can now reload when needed.
1386
        self.assertEqual([1, 1, 0], reload_counter)
1387
1388
    def test_iter_all_entries_reloads_midway(self):
1389
        index, reload_counter = self.make_combined_index_with_missing(['2'])
1390
        index1, index2 = index._indices
1391
        result = list(index.iter_all_entries())
1392
        index3 = index._indices[0]
1393
        # We had already yielded '1', so we just go on to the next, we should
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1394
        # not yield '1' twice.
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1395
        self.assertEqual([(index1, (b'1',), b''), (index3, (b'2',), b'')],
3789.1.5 by John Arbash Meinel
CombinedGraphIndex.iter_all_entries() can now reload when needed.
1396
                         result)
1397
        self.assertEqual([1, 1, 0], reload_counter)
1398
1399
    def test_iter_all_entries_no_reload(self):
1400
        index, reload_counter = self.make_combined_index_with_missing()
1401
        index._reload_func = None
1402
        self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
1403
1404
    def test_iter_all_entries_reloads_and_fails(self):
1405
        index, reload_counter = self.make_combined_index_with_missing(
7143.15.2 by Jelmer Vernooij
Run autopep8.
1406
            ['1', '2', '3'])
3789.1.5 by John Arbash Meinel
CombinedGraphIndex.iter_all_entries() can now reload when needed.
1407
        self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
3789.1.4 by John Arbash Meinel
CombinedGraphIndex.iter_entries() is now able to reload on request.
1408
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1409
    def test_iter_entries_prefix_reloads(self):
1410
        index, reload_counter = self.make_combined_index_with_missing()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1411
        result = list(index.iter_entries_prefix([(b'1',)]))
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1412
        index3 = index._indices[0]
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1413
        self.assertEqual([(index3, (b'1',), b'')], result)
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1414
        self.assertEqual([1, 1, 0], reload_counter)
1415
1416
    def test_iter_entries_prefix_reloads_midway(self):
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
1417
        index, reload_counter = self.make_combined_index_with_missing(['2'])
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1418
        index1, index2 = index._indices
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1419
        result = list(index.iter_entries_prefix([(b'1',)]))
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1420
        index3 = index._indices[0]
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1421
        # We had already yielded b'1', so we just go on to the next, we should
1422
        # not yield b'1' twice.
1423
        self.assertEqual([(index1, (b'1',), b'')], result)
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1424
        self.assertEqual([1, 1, 0], reload_counter)
1425
1426
    def test_iter_entries_prefix_no_reload(self):
1427
        index, reload_counter = self.make_combined_index_with_missing()
1428
        index._reload_func = None
1429
        self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1430
                              [(b'1',)])
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1431
1432
    def test_iter_entries_prefix_reloads_and_fails(self):
1433
        index, reload_counter = self.make_combined_index_with_missing(
7143.15.2 by Jelmer Vernooij
Run autopep8.
1434
            ['1', '2', '3'])
3789.1.6 by John Arbash Meinel
CombinedGraphIndex.iter_entries_prefix can now reload when needed.
1435
        self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1436
                              [(b'1',)])
5086.7.7 by Andrew Bennetts
Add another test for propagating reorderings.
1437
1438
    def make_index_with_simple_nodes(self, name, num_nodes=1):
1439
        """Make an index named after 'name', with keys named after 'name' too.
1440
1441
        Nodes will have a value of '' and no references.
1442
        """
1443
        nodes = [
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1444
            ((('index-%s-key-%s' % (name, n)).encode('ascii'),), b'', ())
7143.15.2 by Jelmer Vernooij
Run autopep8.
1445
            for n in range(1, num_nodes + 1)]
5086.7.7 by Andrew Bennetts
Add another test for propagating reorderings.
1446
        return self.make_index('index-%s' % name, 0, nodes=nodes)
1447
5086.7.5 by Andrew Bennetts
Add test for CombinedGraphIndex's auto-reordering behaviour.
1448
    def test_reorder_after_iter_entries(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1449
        # Four indices: [key1] in idx1, [key2,key3] in idx2, [] in idx3,
1450
        # [key4] in idx4.
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1451
        idx = _mod_index.CombinedGraphIndex([])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1452
        idx.insert_index(0, self.make_index_with_simple_nodes('1'), b'1')
1453
        idx.insert_index(1, self.make_index_with_simple_nodes('2'), b'2')
1454
        idx.insert_index(2, self.make_index_with_simple_nodes('3'), b'3')
1455
        idx.insert_index(3, self.make_index_with_simple_nodes('4'), b'4')
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1456
        idx1, idx2, idx3, idx4 = idx._indices
1457
        # Query a key from idx4 and idx2.
1458
        self.assertLength(2, list(idx.iter_entries(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1459
            [(b'index-4-key-1',), (b'index-2-key-1',)])))
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1460
        # Now idx2 and idx4 should be moved to the front (and idx1 should
1461
        # still be before idx3).
1462
        self.assertEqual([idx2, idx4, idx1, idx3], idx._indices)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1463
        self.assertEqual([b'2', b'4', b'1', b'3'], idx._index_names)
5086.7.5 by Andrew Bennetts
Add test for CombinedGraphIndex's auto-reordering behaviour.
1464
5086.7.7 by Andrew Bennetts
Add another test for propagating reorderings.
1465
    def test_reorder_propagates_to_siblings(self):
1466
        # Two CombinedGraphIndex objects, with the same number of indicies with
1467
        # matching names.
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1468
        cgi1 = _mod_index.CombinedGraphIndex([])
1469
        cgi2 = _mod_index.CombinedGraphIndex([])
5086.7.7 by Andrew Bennetts
Add another test for propagating reorderings.
1470
        cgi1.insert_index(0, self.make_index_with_simple_nodes('1-1'), 'one')
1471
        cgi1.insert_index(1, self.make_index_with_simple_nodes('1-2'), 'two')
1472
        cgi2.insert_index(0, self.make_index_with_simple_nodes('2-1'), 'one')
1473
        cgi2.insert_index(1, self.make_index_with_simple_nodes('2-2'), 'two')
1474
        index2_1, index2_2 = cgi2._indices
1475
        cgi1.set_sibling_indices([cgi2])
1476
        # Trigger a reordering in cgi1.  cgi2 will be reordered as well.
7067.4.2 by Jelmer Vernooij
Actually look for a key that exists.
1477
        list(cgi1.iter_entries([(b'index-1-2-key-1',)]))
5086.7.7 by Andrew Bennetts
Add another test for propagating reorderings.
1478
        self.assertEqual([index2_2, index2_1], cgi2._indices)
1479
        self.assertEqual(['two', 'one'], cgi2._index_names)
1480
3789.1.7 by John Arbash Meinel
CombinedGraphIndex.validate() will now reload.
1481
    def test_validate_reloads(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1482
        idx, reload_counter = self.make_combined_index_with_missing()
1483
        idx.validate()
3789.1.7 by John Arbash Meinel
CombinedGraphIndex.validate() will now reload.
1484
        self.assertEqual([1, 1, 0], reload_counter)
1485
1486
    def test_validate_reloads_midway(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1487
        idx, reload_counter = self.make_combined_index_with_missing(['2'])
1488
        idx.validate()
3789.1.7 by John Arbash Meinel
CombinedGraphIndex.validate() will now reload.
1489
1490
    def test_validate_no_reload(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1491
        idx, reload_counter = self.make_combined_index_with_missing()
1492
        idx._reload_func = None
1493
        self.assertRaises(errors.NoSuchFile, idx.validate)
3789.1.7 by John Arbash Meinel
CombinedGraphIndex.validate() will now reload.
1494
1495
    def test_validate_reloads_and_fails(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1496
        idx, reload_counter = self.make_combined_index_with_missing(
1497
            ['1', '2', '3'])
1498
        self.assertRaises(errors.NoSuchFile, idx.validate)
3789.1.7 by John Arbash Meinel
CombinedGraphIndex.validate() will now reload.
1499
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1500
    def test_find_ancestors_across_indexes(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1501
        key1 = (b'key-1',)
1502
        key2 = (b'key-2',)
1503
        key3 = (b'key-3',)
1504
        key4 = (b'key-4',)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1505
        index1 = self.make_index('12', ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1506
            (key1, b'value', ([],)),
1507
            (key2, b'value', ([key1],)),
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1508
            ])
1509
        index2 = self.make_index('34', ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1510
            (key3, b'value', ([key2],)),
1511
            (key4, b'value', ([key3],)),
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1512
            ])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1513
        c_index = _mod_index.CombinedGraphIndex([index1, index2])
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1514
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1515
        self.assertEqual({key1: ()}, parent_map)
1516
        self.assertEqual(set(), missing_keys)
1517
        # Now look for a key from index2 which requires us to find the key in
1518
        # the second index, and then continue searching for parents in the
1519
        # first index
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1520
        parent_map, missing_keys = c_index.find_ancestry([key3], 0)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1521
        self.assertEqual({key1: (), key2: (key1,), key3: (key2,)}, parent_map)
1522
        self.assertEqual(set(), missing_keys)
1523
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1524
    def test_find_ancestors_missing_keys(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1525
        key1 = (b'key-1',)
1526
        key2 = (b'key-2',)
1527
        key3 = (b'key-3',)
1528
        key4 = (b'key-4',)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1529
        index1 = self.make_index('12', ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1530
            (key1, b'value', ([],)),
1531
            (key2, b'value', ([key1],)),
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1532
            ])
1533
        index2 = self.make_index('34', ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1534
            (key3, b'value', ([key2],)),
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1535
            ])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1536
        c_index = _mod_index.CombinedGraphIndex([index1, index2])
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1537
        # Searching for a key which is actually not present at all should
1538
        # eventually converge
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1539
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1540
        self.assertEqual({}, parent_map)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1541
        self.assertEqual({key4}, missing_keys)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1542
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1543
    def test_find_ancestors_no_indexes(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1544
        c_index = _mod_index.CombinedGraphIndex([])
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1545
        key1 = (b'key-1',)
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1546
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1547
        self.assertEqual({}, parent_map)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1548
        self.assertEqual({key1}, missing_keys)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1549
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1550
    def test_find_ancestors_ghost_parent(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1551
        key1 = (b'key-1',)
1552
        key2 = (b'key-2',)
1553
        key3 = (b'key-3',)
1554
        key4 = (b'key-4',)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1555
        index1 = self.make_index('12', ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1556
            (key1, b'value', ([],)),
1557
            (key2, b'value', ([key1],)),
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1558
            ])
1559
        index2 = self.make_index('34', ref_lists=1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1560
            (key4, b'value', ([key2, key3],)),
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1561
            ])
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1562
        c_index = _mod_index.CombinedGraphIndex([index1, index2])
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1563
        # Searching for a key which is actually not present at all should
1564
        # eventually converge
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1565
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1566
        self.assertEqual({key4: (key2, key3), key2: (key1,), key1: ()},
1567
                         parent_map)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1568
        self.assertEqual({key3}, missing_keys)
4593.4.8 by John Arbash Meinel
Implement CombinedGraphIndex.get_ancestry()
1569
4593.4.12 by John Arbash Meinel
Name the specific index api _find_ancestors, and the public CombinedGraphIndex api find_ancestry()
1570
    def test__find_ancestors_empty_index(self):
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1571
        idx = self.make_index('test', ref_lists=1, key_elements=1, nodes=[])
4593.4.11 by John Arbash Meinel
Snapshot the work in progress.
1572
        parent_map = {}
1573
        missing_keys = set()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1574
        search_keys = idx._find_ancestors([(b'one',), (b'two',)], 0, parent_map,
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1575
                                          missing_keys)
4593.4.11 by John Arbash Meinel
Snapshot the work in progress.
1576
        self.assertEqual(set(), search_keys)
1577
        self.assertEqual({}, parent_map)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1578
        self.assertEqual({(b'one',), (b'two',)}, missing_keys)
4593.4.11 by John Arbash Meinel
Snapshot the work in progress.
1579
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1580
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1581
class TestInMemoryGraphIndex(tests.TestCaseWithMemoryTransport):
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1582
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
1583
    def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
7143.15.2 by Jelmer Vernooij
Run autopep8.
1584
        result = _mod_index.InMemoryGraphIndex(
1585
            ref_lists, key_elements=key_elements)
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1586
        result.add_nodes(nodes)
1587
        return result
1588
2624.2.1 by Robert Collins
InMemoryGraphIndex.add_nodes was inconsistent with other metods for non-node-reference indices.
1589
    def test_add_nodes_no_refs(self):
1590
        index = self.make_index(0)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1591
        index.add_nodes([((b'name', ), b'data')])
1592
        index.add_nodes([((b'name2', ), b''), ((b'name3', ), b'')])
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1593
        self.assertEqual({
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1594
            (index, (b'name', ), b'data'),
1595
            (index, (b'name2', ), b''),
1596
            (index, (b'name3', ), b''),
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1597
            }, set(index.iter_all_entries()))
2624.2.1 by Robert Collins
InMemoryGraphIndex.add_nodes was inconsistent with other metods for non-node-reference indices.
1598
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1599
    def test_add_nodes(self):
1600
        index = self.make_index(1)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1601
        index.add_nodes([((b'name', ), b'data', ([],))])
7143.15.2 by Jelmer Vernooij
Run autopep8.
1602
        index.add_nodes([((b'name2', ), b'', ([],)),
1603
                         ((b'name3', ), b'', ([(b'r', )],))])
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1604
        self.assertEqual({
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1605
            (index, (b'name', ), b'data', ((),)),
1606
            (index, (b'name2', ), b'', ((),)),
1607
            (index, (b'name3', ), b'', (((b'r', ), ), )),
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1608
            }, set(index.iter_all_entries()))
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1609
1610
    def test_iter_all_entries_empty(self):
1611
        index = self.make_index()
1612
        self.assertEqual([], list(index.iter_all_entries()))
1613
1614
    def test_iter_all_entries_simple(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1615
        index = self.make_index(nodes=[((b'name', ), b'data')])
1616
        self.assertEqual([(index, (b'name', ), b'data')],
7143.15.2 by Jelmer Vernooij
Run autopep8.
1617
                         list(index.iter_all_entries()))
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1618
1619
    def test_iter_all_entries_references(self):
1620
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1621
            ((b'name', ), b'data', ([(b'ref', )], )),
1622
            ((b'ref', ), b'refdata', ([], ))])
1623
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref', ),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1624
                          (index, (b'ref', ), b'refdata', ((), ))},
1625
                         set(index.iter_all_entries()))
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1626
1627
    def test_iteration_absent_skipped(self):
1628
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1629
            ((b'name', ), b'data', ([(b'ref', )], ))])
1630
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),))},
7143.15.2 by Jelmer Vernooij
Run autopep8.
1631
                         set(index.iter_all_entries()))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1632
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),))},
7143.15.2 by Jelmer Vernooij
Run autopep8.
1633
                         set(index.iter_entries([(b'name', )])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1634
        self.assertEqual([], list(index.iter_entries([(b'ref', )])))
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1635
1636
    def test_iter_all_keys(self):
1637
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1638
            ((b'name', ), b'data', ([(b'ref', )], )),
1639
            ((b'ref', ), b'refdata', ([], ))])
1640
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1641
                          (index, (b'ref', ), b'refdata', ((), ))},
1642
                         set(index.iter_entries([(b'name', ), (b'ref', )])))
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1643
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
1644
    def test_iter_key_prefix_1_key_element_no_refs(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
1645
        index = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1646
            ((b'name', ), b'data'),
1647
            ((b'ref', ), b'refdata')])
1648
        self.assertEqual({(index, (b'name', ), b'data'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1649
                          (index, (b'ref', ), b'refdata')},
1650
                         set(index.iter_entries_prefix([(b'name', ), (b'ref', )])))
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
1651
1652
    def test_iter_key_prefix_1_key_element_refs(self):
1653
        index = self.make_index(1, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1654
            ((b'name', ), b'data', ([(b'ref', )], )),
1655
            ((b'ref', ), b'refdata', ([], ))])
1656
        self.assertEqual({(index, (b'name', ), b'data', (((b'ref',),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1657
                          (index, (b'ref', ), b'refdata', ((), ))},
1658
                         set(index.iter_entries_prefix([(b'name', ), (b'ref', )])))
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
1659
1660
    def test_iter_key_prefix_2_key_element_no_refs(self):
1661
        index = self.make_index(key_elements=2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1662
            ((b'name', b'fin1'), b'data'),
1663
            ((b'name', b'fin2'), b'beta'),
1664
            ((b'ref', b'erence'), b'refdata')])
1665
        self.assertEqual({(index, (b'name', b'fin1'), b'data'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1666
                          (index, (b'ref', b'erence'), b'refdata')},
1667
                         set(index.iter_entries_prefix([(b'name', b'fin1'), (b'ref', b'erence')])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1668
        self.assertEqual({(index, (b'name', b'fin1'), b'data'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1669
                          (index, (b'name', b'fin2'), b'beta')},
1670
                         set(index.iter_entries_prefix([(b'name', None)])))
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
1671
1672
    def test_iter_key_prefix_2_key_element_refs(self):
1673
        index = self.make_index(1, key_elements=2, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1674
            ((b'name', b'fin1'), b'data', ([(b'ref', b'erence')], )),
1675
            ((b'name', b'fin2'), b'beta', ([], )),
1676
            ((b'ref', b'erence'), b'refdata', ([], ))])
1677
        self.assertEqual({(index, (b'name', b'fin1'), b'data', (((b'ref', b'erence'),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1678
                          (index, (b'ref', b'erence'), b'refdata', ((), ))},
1679
                         set(index.iter_entries_prefix([(b'name', b'fin1'), (b'ref', b'erence')])))
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1680
        self.assertEqual({(index, (b'name', b'fin1'), b'data', (((b'ref', b'erence'),),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1681
                          (index, (b'name', b'fin2'), b'beta', ((), ))},
1682
                         set(index.iter_entries_prefix([(b'name', None)])))
2624.2.10 by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex.
1683
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1684
    def test_iter_nothing_empty(self):
1685
        index = self.make_index()
1686
        self.assertEqual([], list(index.iter_entries([])))
1687
1688
    def test_iter_missing_entry_empty(self):
1689
        index = self.make_index()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1690
        self.assertEqual([], list(index.iter_entries([b'a'])))
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1691
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1692
    def test_key_count_empty(self):
1693
        index = self.make_index()
1694
        self.assertEqual(0, index.key_count())
1695
1696
    def test_key_count_one(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1697
        index = self.make_index(nodes=[((b'name', ), b'')])
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1698
        self.assertEqual(1, index.key_count())
1699
1700
    def test_key_count_two(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1701
        index = self.make_index(nodes=[((b'name', ), b''), ((b'foo', ), b'')])
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1702
        self.assertEqual(2, index.key_count())
1703
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1704
    def test_validate_empty(self):
1705
        index = self.make_index()
1706
        index.validate()
1707
1708
    def test_validate_no_refs_content(self):
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1709
        index = self.make_index(nodes=[((b'key', ), b'value')])
2592.1.38 by Robert Collins
Create an InMemoryGraphIndex for temporary indexing.
1710
        index.validate()
1711
1712
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1713
class TestGraphIndexPrefixAdapter(tests.TestCaseWithMemoryTransport):
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1714
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
1715
    def make_index(self, ref_lists=1, key_elements=2, nodes=[],
1716
                   add_callback=False):
7143.15.2 by Jelmer Vernooij
Run autopep8.
1717
        result = _mod_index.InMemoryGraphIndex(
1718
            ref_lists, key_elements=key_elements)
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1719
        result.add_nodes(nodes)
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
1720
        if add_callback:
2624.2.17 by Robert Collins
Review feedback.
1721
            add_nodes_callback = result.add_nodes
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
1722
        else:
2624.2.17 by Robert Collins
Review feedback.
1723
            add_nodes_callback = None
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1724
        adapter = _mod_index.GraphIndexPrefixAdapter(
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1725
            result, (b'prefix', ), key_elements - 1,
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
1726
            add_nodes_callback=add_nodes_callback)
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1727
        return result, adapter
1728
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
1729
    def test_add_node(self):
1730
        index, adapter = self.make_index(add_callback=True)
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1731
        adapter.add_node((b'key',), b'value', (((b'ref',),),))
1732
        self.assertEqual({(index, (b'prefix', b'key'), b'value',
7143.15.2 by Jelmer Vernooij
Run autopep8.
1733
                           (((b'prefix', b'ref'),),))},
1734
                         set(index.iter_all_entries()))
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
1735
1736
    def test_add_nodes(self):
1737
        index, adapter = self.make_index(add_callback=True)
1738
        adapter.add_nodes((
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1739
            ((b'key',), b'value', (((b'ref',),),)),
1740
            ((b'key2',), b'value2', ((),)),
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
1741
            ))
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1742
        self.assertEqual({
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1743
            (index, (b'prefix', b'key2'), b'value2', ((),)),
1744
            (index, (b'prefix', b'key'), b'value', (((b'prefix', b'ref'),),))
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1745
            },
2624.2.13 by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter.
1746
            set(index.iter_all_entries()))
1747
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1748
    def test_construct(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1749
        idx = _mod_index.InMemoryGraphIndex()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1750
        adapter = _mod_index.GraphIndexPrefixAdapter(idx, (b'prefix', ), 1)
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1751
1752
    def test_construct_with_callback(self):
6734.1.18 by Jelmer Vernooij
Move BadIndex* errors out of breezy.errors.
1753
        idx = _mod_index.InMemoryGraphIndex()
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1754
        adapter = _mod_index.GraphIndexPrefixAdapter(idx, (b'prefix', ), 1,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1755
                                                     idx.add_nodes)
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1756
1757
    def test_iter_all_entries_cross_prefix_map_errors(self):
1758
        index, adapter = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1759
            ((b'prefix', b'key1'), b'data1', (((b'prefixaltered', b'key2'),),))])
7143.15.2 by Jelmer Vernooij
Run autopep8.
1760
        self.assertRaises(_mod_index.BadIndexData, list,
1761
                          adapter.iter_all_entries())
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1762
1763
    def test_iter_all_entries(self):
1764
        index, adapter = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1765
            ((b'notprefix', b'key1'), b'data', ((), )),
1766
            ((b'prefix', b'key1'), b'data1', ((), )),
1767
            ((b'prefix', b'key2'), b'data2', (((b'prefix', b'key1'),),))])
1768
        self.assertEqual({(index, (b'key1', ), b'data1', ((),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1769
                          (index, (b'key2', ), b'data2', (((b'key1',),),))},
1770
                         set(adapter.iter_all_entries()))
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1771
1772
    def test_iter_entries(self):
1773
        index, adapter = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1774
            ((b'notprefix', b'key1'), b'data', ((), )),
1775
            ((b'prefix', b'key1'), b'data1', ((), )),
1776
            ((b'prefix', b'key2'), b'data2', (((b'prefix', b'key1'),),))])
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1777
        # ask for many - get all
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1778
        self.assertEqual({(index, (b'key1', ), b'data1', ((),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1779
                          (index, (b'key2', ), b'data2', (((b'key1', ),),))},
1780
                         set(adapter.iter_entries([(b'key1', ), (b'key2', )])))
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1781
        # ask for one, get one
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1782
        self.assertEqual({(index, (b'key1', ), b'data1', ((),))},
7143.15.2 by Jelmer Vernooij
Run autopep8.
1783
                         set(adapter.iter_entries([(b'key1', )])))
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1784
        # ask for missing, get none
1785
        self.assertEqual(set(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1786
                         set(adapter.iter_entries([(b'key3', )])))
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1787
1788
    def test_iter_entries_prefix(self):
1789
        index, adapter = self.make_index(key_elements=3, nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1790
            ((b'notprefix', b'foo', b'key1'), b'data', ((), )),
1791
            ((b'prefix', b'prefix2', b'key1'), b'data1', ((), )),
1792
            ((b'prefix', b'prefix2', b'key2'), b'data2', (((b'prefix', b'prefix2', b'key1'),),))])
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1793
        # ask for a prefix, get the results for just that prefix, adjusted.
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1794
        self.assertEqual({(index, (b'prefix2', b'key1', ), b'data1', ((),)),
7143.15.2 by Jelmer Vernooij
Run autopep8.
1795
                          (index, (b'prefix2', b'key2', ), b'data2', (((b'prefix2', b'key1', ),),))},
1796
                         set(adapter.iter_entries_prefix([(b'prefix2', None)])))
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1797
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1798
    def test_key_count_no_matching_keys(self):
1799
        index, adapter = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1800
            ((b'notprefix', b'key1'), b'data', ((), ))])
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1801
        self.assertEqual(0, adapter.key_count())
1802
1803
    def test_key_count_some_keys(self):
1804
        index, adapter = self.make_index(nodes=[
6973.11.3 by Jelmer Vernooij
Fix some index tests.
1805
            ((b'notprefix', b'key1'), b'data', ((), )),
1806
            ((b'prefix', b'key1'), b'data1', ((), )),
1807
            ((b'prefix', b'key2'), b'data2', (((b'prefix', b'key1'),),))])
2624.2.16 by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index.
1808
        self.assertEqual(2, adapter.key_count())
1809
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1810
    def test_validate(self):
1811
        index, adapter = self.make_index()
1812
        calls = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
1813
2624.2.12 by Robert Collins
Create an adapter between indices with differing key lengths.
1814
        def validate():
1815
            calls.append('called')
1816
        index.validate = validate
1817
        adapter.validate()
1818
        self.assertEqual(['called'], calls)