2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
1 |
# Copyright (C) 2007 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Indexing facilities."""
|
|
18 |
||
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
19 |
__all__ = [ |
20 |
'CombinedGraphIndex', |
|
21 |
'GraphIndex', |
|
22 |
'GraphIndexBuilder', |
|
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
23 |
'GraphIndexPrefixAdapter', |
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
24 |
'InMemoryGraphIndex', |
25 |
]
|
|
2592.1.32
by Robert Collins
Add __all__ to index. |
26 |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
27 |
from bisect import bisect_right |
2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
28 |
from cStringIO import StringIO |
2592.1.12
by Robert Collins
Handle basic node adds. |
29 |
import re |
2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
30 |
|
2624.2.15
by Robert Collins
Add useful -Dindex flag. |
31 |
from bzrlib.lazy_import import lazy_import |
32 |
lazy_import(globals(), """ |
|
2745.1.2
by Robert Collins
Ensure mutter_callsite is not directly called on a lazy_load object, to make the stacklevel parameter work correctly. |
33 |
from bzrlib import trace
|
2890.2.7
by Robert Collins
* Pack indices are now partially parsed for specific key lookup using a |
34 |
from bzrlib.bisect_multi import bisect_multi_bytes
|
2979.2.2
by Robert Collins
Per-file graph heads detection during commit for pack repositories. |
35 |
from bzrlib.revision import NULL_REVISION
|
2745.1.2
by Robert Collins
Ensure mutter_callsite is not directly called on a lazy_load object, to make the stacklevel parameter work correctly. |
36 |
from bzrlib.trace import mutter
|
2624.2.15
by Robert Collins
Add useful -Dindex flag. |
37 |
""") |
3099.3.3
by John Arbash Meinel
Deprecate get_parents() in favor of get_parent_map() |
38 |
from bzrlib import ( |
39 |
debug, |
|
40 |
errors, |
|
41 |
symbol_versioning, |
|
42 |
)
|
|
2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
43 |
|
2979.1.1
by Robert Collins
Use the GraphIndex header to answer key_count queries rather than parsing the entire index unnecessarily. |
44 |
_HEADER_READV = (0, 200) |
2624.2.8
by Robert Collins
Explicitly mark the number of keys elements in use in GraphIndex files. |
45 |
_OPTION_KEY_ELEMENTS = "key_elements=" |
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
46 |
_OPTION_LEN = "len=" |
2592.1.6
by Robert Collins
Record the number of node reference lists a particular index has. |
47 |
_OPTION_NODE_REFS = "node_ref_lists=" |
2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
48 |
_SIGNATURE = "Bazaar Graph Index 1\n" |
49 |
||
50 |
||
2592.1.14
by Robert Collins
Detect bad reference key values. |
51 |
_whitespace_re = re.compile('[\t\n\x0b\x0c\r\x00 ]') |
2592.1.12
by Robert Collins
Handle basic node adds. |
52 |
_newline_null_re = re.compile('[\n\0]') |
53 |
||
54 |
||
2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
55 |
class GraphIndexBuilder(object): |
2592.1.18
by Robert Collins
Add space to mark absent nodes. |
56 |
"""A builder that can build a GraphIndex.
|
57 |
|
|
58 |
The resulting graph has the structure:
|
|
59 |
|
|
60 |
_SIGNATURE OPTIONS NODES NEWLINE
|
|
61 |
_SIGNATURE := 'Bazaar Graph Index 1' NEWLINE
|
|
62 |
OPTIONS := 'node_ref_lists=' DIGITS NEWLINE
|
|
63 |
NODES := NODE*
|
|
64 |
NODE := KEY NULL ABSENT? NULL REFERENCES NULL VALUE NEWLINE
|
|
65 |
KEY := Not-whitespace-utf8
|
|
66 |
ABSENT := 'a'
|
|
2592.1.19
by Robert Collins
Node references are tab separated. |
67 |
REFERENCES := REFERENCE_LIST (TAB REFERENCE_LIST){node_ref_lists - 1}
|
68 |
REFERENCE_LIST := (REFERENCE (CR REFERENCE)*)?
|
|
69 |
REFERENCE := DIGITS ; digits is the byte offset in the index of the
|
|
70 |
; referenced key.
|
|
2592.1.18
by Robert Collins
Add space to mark absent nodes. |
71 |
VALUE := no-newline-no-null-bytes
|
72 |
"""
|
|
2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
73 |
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
74 |
def __init__(self, reference_lists=0, key_elements=1): |
2592.1.6
by Robert Collins
Record the number of node reference lists a particular index has. |
75 |
"""Create a GraphIndex builder.
|
76 |
||
77 |
:param reference_lists: The number of node references lists for each
|
|
78 |
entry.
|
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
79 |
:param key_elements: The number of bytestrings in each key.
|
2592.1.6
by Robert Collins
Record the number of node reference lists a particular index has. |
80 |
"""
|
81 |
self.reference_lists = reference_lists |
|
2592.3.62
by Robert Collins
Performance tweak - use a set for InMemoryGraph key iteration. |
82 |
self._keys = set() |
2592.1.15
by Robert Collins
Detect duplicate key insertion. |
83 |
self._nodes = {} |
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
84 |
self._nodes_by_key = {} |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
85 |
self._key_length = key_elements |
2624.2.5
by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings. |
86 |
|
87 |
def _check_key(self, key): |
|
88 |
"""Raise BadIndexKey if key is not a valid key for this index."""
|
|
89 |
if type(key) != tuple: |
|
90 |
raise errors.BadIndexKey(key) |
|
91 |
if self._key_length != len(key): |
|
92 |
raise errors.BadIndexKey(key) |
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
93 |
for element in key: |
94 |
if not element or _whitespace_re.search(element) is not None: |
|
95 |
raise errors.BadIndexKey(element) |
|
2592.1.12
by Robert Collins
Handle basic node adds. |
96 |
|
2592.1.46
by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method |
97 |
def add_node(self, key, value, references=()): |
2592.1.12
by Robert Collins
Handle basic node adds. |
98 |
"""Add a node to the index.
|
99 |
||
2624.2.5
by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings. |
100 |
:param key: The key. keys are non-empty tuples containing
|
101 |
as many whitespace-free utf8 bytestrings as the key length
|
|
102 |
defined for this index.
|
|
2592.1.12
by Robert Collins
Handle basic node adds. |
103 |
:param references: An iterable of iterables of keys. Each is a
|
104 |
reference to another key.
|
|
105 |
:param value: The value to associate with the key. It may be any
|
|
106 |
bytes as long as it does not contain \0 or \n.
|
|
107 |
"""
|
|
2624.2.5
by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings. |
108 |
self._check_key(key) |
2592.1.12
by Robert Collins
Handle basic node adds. |
109 |
if _newline_null_re.search(value) is not None: |
110 |
raise errors.BadIndexValue(value) |
|
2592.1.13
by Robert Collins
Handle mismatched numbers of reference lists. |
111 |
if len(references) != self.reference_lists: |
112 |
raise errors.BadIndexValue(references) |
|
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
113 |
node_refs = [] |
2592.1.14
by Robert Collins
Detect bad reference key values. |
114 |
for reference_list in references: |
115 |
for reference in reference_list: |
|
2624.2.5
by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings. |
116 |
self._check_key(reference) |
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
117 |
if reference not in self._nodes: |
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
118 |
self._nodes[reference] = ('a', (), '') |
119 |
node_refs.append(tuple(reference_list)) |
|
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
120 |
if key in self._nodes and self._nodes[key][0] == '': |
2592.1.15
by Robert Collins
Detect duplicate key insertion. |
121 |
raise errors.BadIndexDuplicateKey(key, self) |
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
122 |
self._nodes[key] = ('', tuple(node_refs), value) |
2592.3.62
by Robert Collins
Performance tweak - use a set for InMemoryGraph key iteration. |
123 |
self._keys.add(key) |
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
124 |
if self._key_length > 1: |
125 |
key_dict = self._nodes_by_key |
|
126 |
if self.reference_lists: |
|
127 |
key_value = key, value, tuple(node_refs) |
|
128 |
else: |
|
129 |
key_value = key, value |
|
130 |
# possibly should do this on-demand, but it seems likely it is
|
|
131 |
# always wanted
|
|
2624.2.11
by Robert Collins
Review comments. |
132 |
# For a key of (foo, bar, baz) create
|
133 |
# _nodes_by_key[foo][bar][baz] = key_value
|
|
134 |
for subkey in key[:-1]: |
|
135 |
key_dict = key_dict.setdefault(subkey, {}) |
|
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
136 |
key_dict[key[-1]] = key_value |
2592.1.6
by Robert Collins
Record the number of node reference lists a particular index has. |
137 |
|
2592.1.4
by Robert Collins
Create a GraphIndexBuilder. |
138 |
def finish(self): |
2592.1.6
by Robert Collins
Record the number of node reference lists a particular index has. |
139 |
lines = [_SIGNATURE] |
140 |
lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n') |
|
2624.2.8
by Robert Collins
Explicitly mark the number of keys elements in use in GraphIndex files. |
141 |
lines.append(_OPTION_KEY_ELEMENTS + str(self._key_length) + '\n') |
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
142 |
lines.append(_OPTION_LEN + str(len(self._keys)) + '\n') |
2624.2.11
by Robert Collins
Review comments. |
143 |
prefix_length = sum(len(x) for x in lines) |
2592.1.22
by Robert Collins
Node references are byte offsets. |
144 |
# references are byte offsets. To avoid having to do nasty
|
145 |
# polynomial work to resolve offsets (references to later in the
|
|
146 |
# file cannot be determined until all the inbetween references have
|
|
147 |
# been calculated too) we pad the offsets with 0's to make them be
|
|
148 |
# of consistent length. Using binary offsets would break the trivial
|
|
149 |
# file parsing.
|
|
150 |
# to calculate the width of zero's needed we do three passes:
|
|
151 |
# one to gather all the non-reference data and the number of references.
|
|
152 |
# one to pad all the data with reference-length and determine entry
|
|
153 |
# addresses.
|
|
154 |
# One to serialise.
|
|
2592.1.40
by Robert Collins
Reverse index ordering - we do not have date prefixed revids. |
155 |
|
156 |
# forward sorted by key. In future we may consider topological sorting,
|
|
157 |
# at the cost of table scans for direct lookup, or a second index for
|
|
158 |
# direct lookup
|
|
159 |
nodes = sorted(self._nodes.items()) |
|
2592.1.42
by Robert Collins
Check the index length is as expected, when we have done preprocessing. |
160 |
# if we do not prepass, we don't know how long it will be up front.
|
161 |
expected_bytes = None |
|
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
162 |
# we only need to pre-pass if we have reference lists at all.
|
163 |
if self.reference_lists: |
|
2592.1.41
by Robert Collins
Remove duplication in the index serialisation logic with John's suggestion. |
164 |
key_offset_info = [] |
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
165 |
non_ref_bytes = prefix_length |
166 |
total_references = 0 |
|
167 |
# TODO use simple multiplication for the constants in this loop.
|
|
168 |
for key, (absent, references, value) in nodes: |
|
2592.1.41
by Robert Collins
Remove duplication in the index serialisation logic with John's suggestion. |
169 |
# record the offset known *so far* for this key:
|
170 |
# the non reference bytes to date, and the total references to
|
|
171 |
# date - saves reaccumulating on the second pass
|
|
172 |
key_offset_info.append((key, non_ref_bytes, total_references)) |
|
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
173 |
# key is literal, value is literal, there are 3 null's, 1 NL
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
174 |
# key is variable length tuple, \x00 between elements
|
2624.2.5
by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings. |
175 |
non_ref_bytes += sum(len(element) for element in key) |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
176 |
if self._key_length > 1: |
177 |
non_ref_bytes += self._key_length - 1 |
|
2624.2.5
by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings. |
178 |
# value is literal bytes, there are 3 null's, 1 NL.
|
179 |
non_ref_bytes += len(value) + 3 + 1 |
|
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
180 |
# one byte for absent if set.
|
181 |
if absent: |
|
182 |
non_ref_bytes += 1 |
|
2592.1.36
by Robert Collins
Bugfix incorrect offset generation when an absent record is before a referenced record. |
183 |
elif self.reference_lists: |
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
184 |
# (ref_lists -1) tabs
|
185 |
non_ref_bytes += self.reference_lists - 1 |
|
186 |
# (ref-1 cr's per ref_list)
|
|
187 |
for ref_list in references: |
|
188 |
# how many references across the whole file?
|
|
189 |
total_references += len(ref_list) |
|
190 |
# accrue reference separators
|
|
191 |
if ref_list: |
|
192 |
non_ref_bytes += len(ref_list) - 1 |
|
193 |
# how many digits are needed to represent the total byte count?
|
|
194 |
digits = 1 |
|
2592.1.22
by Robert Collins
Node references are byte offsets. |
195 |
possible_total_bytes = non_ref_bytes + total_references*digits |
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
196 |
while 10 ** digits < possible_total_bytes: |
197 |
digits += 1 |
|
198 |
possible_total_bytes = non_ref_bytes + total_references*digits |
|
2592.1.42
by Robert Collins
Check the index length is as expected, when we have done preprocessing. |
199 |
expected_bytes = possible_total_bytes + 1 # terminating newline |
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
200 |
# resolve key addresses.
|
201 |
key_addresses = {} |
|
2592.1.41
by Robert Collins
Remove duplication in the index serialisation logic with John's suggestion. |
202 |
for key, non_ref_bytes, total_references in key_offset_info: |
203 |
key_addresses[key] = non_ref_bytes + total_references*digits |
|
2592.1.25
by Robert Collins
Fix and tune node offset calculation. |
204 |
# serialise
|
205 |
format_string = '%%0%sd' % digits |
|
206 |
for key, (absent, references, value) in nodes: |
|
2592.1.19
by Robert Collins
Node references are tab separated. |
207 |
flattened_references = [] |
208 |
for ref_list in references: |
|
2592.1.22
by Robert Collins
Node references are byte offsets. |
209 |
ref_addresses = [] |
210 |
for reference in ref_list: |
|
211 |
ref_addresses.append(format_string % key_addresses[reference]) |
|
212 |
flattened_references.append('\r'.join(ref_addresses)) |
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
213 |
string_key = '\x00'.join(key) |
2624.2.11
by Robert Collins
Review comments. |
214 |
lines.append("%s\x00%s\x00%s\x00%s\n" % (string_key, absent, |
2592.1.19
by Robert Collins
Node references are tab separated. |
215 |
'\t'.join(flattened_references), value)) |
2592.1.6
by Robert Collins
Record the number of node reference lists a particular index has. |
216 |
lines.append('\n') |
2592.1.42
by Robert Collins
Check the index length is as expected, when we have done preprocessing. |
217 |
result = StringIO(''.join(lines)) |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
218 |
if expected_bytes and len(result.getvalue()) != expected_bytes: |
219 |
raise errors.BzrError('Failed index creation. Internal error:' |
|
220 |
' mismatched output length and expected length: %d %d' % |
|
221 |
(len(result.getvalue()), expected_bytes)) |
|
3498.1.1
by James Westby
Don't join the lines of the index twice. |
222 |
return result |
2592.1.5
by Robert Collins
Trivial index reading. |
223 |
|
224 |
||
225 |
class GraphIndex(object): |
|
226 |
"""An index for data with embedded graphs.
|
|
2592.1.10
by Robert Collins
Make validate detect node reference parsing errors. |
227 |
|
228 |
The index maps keys to a list of key reference lists, and a value.
|
|
229 |
Each node has the same number of key reference lists. Each key reference
|
|
230 |
list can be empty or an arbitrary length. The value is an opaque NULL
|
|
2592.1.45
by Robert Collins
Tweak documentation as per Aaron's review. |
231 |
terminated string without any newlines. The storage of the index is
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
232 |
hidden in the interface: keys and key references are always tuples of
|
233 |
bytestrings, never the internal representation (e.g. dictionary offsets).
|
|
2592.1.30
by Robert Collins
Absent entries are not yeilded. |
234 |
|
235 |
It is presumed that the index will not be mutated - it is static data.
|
|
2592.1.34
by Robert Collins
Cleanup docs. |
236 |
|
2592.1.44
by Robert Collins
Remove some unneeded index iteration by checking if we have found all keys, and grammar improvements from Aaron's review. |
237 |
Successive iter_all_entries calls will read the entire index each time.
|
238 |
Additionally, iter_entries calls will read the index linearly until the
|
|
239 |
desired keys are found. XXX: This must be fixed before the index is
|
|
2592.1.34
by Robert Collins
Cleanup docs. |
240 |
suitable for production use. :XXX
|
2592.1.5
by Robert Collins
Trivial index reading. |
241 |
"""
|
242 |
||
2890.2.1
by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the |
243 |
def __init__(self, transport, name, size): |
2592.1.5
by Robert Collins
Trivial index reading. |
244 |
"""Open an index called name on transport.
|
245 |
||
246 |
:param transport: A bzrlib.transport.Transport.
|
|
247 |
:param name: A path to provide to transport API calls.
|
|
2890.2.1
by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the |
248 |
:param size: The size of the index in bytes. This is used for bisection
|
249 |
logic to perform partial index reads. While the size could be
|
|
250 |
obtained by statting the file this introduced an additional round
|
|
2890.2.8
by Robert Collins
Make the size of the index optionally None for the pack-names index. |
251 |
trip as well as requiring stat'able transports, both of which are
|
252 |
avoided by having it supplied. If size is None, then bisection
|
|
253 |
support will be disabled and accessing the index will just stream
|
|
254 |
all the data.
|
|
2592.1.5
by Robert Collins
Trivial index reading. |
255 |
"""
|
256 |
self._transport = transport |
|
257 |
self._name = name |
|
2890.2.16
by Robert Collins
Review feedback. |
258 |
# Becomes a dict of key:(value, reference-list-byte-locations) used by
|
259 |
# the bisection interface to store parsed but not resolved keys.
|
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
260 |
self._bisect_nodes = None |
2890.2.16
by Robert Collins
Review feedback. |
261 |
# Becomes a dict of key:(value, reference-list-keys) which are ready to
|
262 |
# be returned directly to callers.
|
|
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
263 |
self._nodes = None |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
264 |
# a sorted list of slice-addresses for the parsed bytes of the file.
|
265 |
# e.g. (0,1) would mean that byte 0 is parsed.
|
|
2890.2.2
by Robert Collins
Opening an index creates a map for the parsed bytes. |
266 |
self._parsed_byte_map = [] |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
267 |
# a sorted list of keys matching each slice address for parsed bytes
|
268 |
# e.g. (None, 'foo@bar') would mean that the first byte contained no
|
|
269 |
# key, and the end byte of the slice is the of the data for 'foo@bar'
|
|
270 |
self._parsed_key_map = [] |
|
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
271 |
self._key_count = None |
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
272 |
self._keys_by_offset = None |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
273 |
self._nodes_by_key = None |
2890.2.1
by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the |
274 |
self._size = size |
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
275 |
|
2592.3.176
by Robert Collins
Various pack refactorings. |
276 |
def __eq__(self, other): |
2592.3.215
by Robert Collins
Review feedback. |
277 |
"""Equal when self and other were created with the same parameters."""
|
2592.3.176
by Robert Collins
Various pack refactorings. |
278 |
return ( |
279 |
type(self) == type(other) and |
|
280 |
self._transport == other._transport and |
|
281 |
self._name == other._name and |
|
282 |
self._size == other._size) |
|
283 |
||
284 |
def __ne__(self, other): |
|
285 |
return not self.__eq__(other) |
|
286 |
||
3517.4.13
by Martin Pool
Add repr methods |
287 |
def __repr__(self): |
288 |
return "%s(%r)" % (self.__class__.__name__, |
|
289 |
self._transport.abspath(self._name)) |
|
290 |
||
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
291 |
def _buffer_all(self): |
292 |
"""Buffer all the index data.
|
|
293 |
||
294 |
Mutates self._nodes and self.keys_by_offset.
|
|
2592.1.5
by Robert Collins
Trivial index reading. |
295 |
"""
|
2624.2.15
by Robert Collins
Add useful -Dindex flag. |
296 |
if 'index' in debug.debug_flags: |
297 |
mutter('Reading entire index %s', self._transport.abspath(self._name)) |
|
2592.1.27
by Robert Collins
Test missing end lines with non-empty indices. |
298 |
stream = self._transport.get(self._name) |
299 |
self._read_prefix(stream) |
|
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
300 |
self._expected_elements = 3 + self._key_length |
2592.1.27
by Robert Collins
Test missing end lines with non-empty indices. |
301 |
line_count = 0 |
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
302 |
# raw data keyed by offset
|
303 |
self._keys_by_offset = {} |
|
304 |
# ready-to-return key:value or key:value, node_ref_lists
|
|
305 |
self._nodes = {} |
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
306 |
self._nodes_by_key = {} |
2592.1.27
by Robert Collins
Test missing end lines with non-empty indices. |
307 |
trailers = 0 |
308 |
pos = stream.tell() |
|
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
309 |
lines = stream.read().split('\n') |
310 |
del lines[-1] |
|
311 |
_, _, _, trailers = self._parse_lines(lines, pos) |
|
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
312 |
for key, absent, references, value in self._keys_by_offset.itervalues(): |
2592.1.30
by Robert Collins
Absent entries are not yeilded. |
313 |
if absent: |
314 |
continue
|
|
2592.1.28
by Robert Collins
Basic two pass iter_all_entries. |
315 |
# resolve references:
|
316 |
if self.node_ref_lists: |
|
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
317 |
node_value = (value, self._resolve_references(references)) |
2592.1.28
by Robert Collins
Basic two pass iter_all_entries. |
318 |
else: |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
319 |
node_value = value |
320 |
self._nodes[key] = node_value |
|
321 |
if self._key_length > 1: |
|
322 |
subkey = list(reversed(key[:-1])) |
|
323 |
key_dict = self._nodes_by_key |
|
324 |
if self.node_ref_lists: |
|
325 |
key_value = key, node_value[0], node_value[1] |
|
326 |
else: |
|
327 |
key_value = key, node_value |
|
328 |
# possibly should do this on-demand, but it seems likely it is
|
|
329 |
# always wanted
|
|
2624.2.11
by Robert Collins
Review comments. |
330 |
# For a key of (foo, bar, baz) create
|
331 |
# _nodes_by_key[foo][bar][baz] = key_value
|
|
332 |
for subkey in key[:-1]: |
|
333 |
key_dict = key_dict.setdefault(subkey, {}) |
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
334 |
key_dict[key[-1]] = key_value |
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
335 |
# cache the keys for quick set intersections
|
2592.3.54
by Robert Collins
Fix remaining performance discrepancy with regular repositories. |
336 |
self._keys = set(self._nodes) |
2592.1.27
by Robert Collins
Test missing end lines with non-empty indices. |
337 |
if trailers != 1: |
338 |
# there must be one line - the empty trailer line.
|
|
339 |
raise errors.BadIndexData(self) |
|
340 |
||
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
341 |
def iter_all_entries(self): |
342 |
"""Iterate over all keys within the index.
|
|
343 |
||
2592.5.1
by Martin Pool
Fix docstrings for Index.iter_entries etc |
344 |
:return: An iterable of (index, key, value) or (index, key, value, reference_lists).
|
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
345 |
The former tuple is used when there are no reference lists in the
|
346 |
index, making the API compatible with simple key:value index types.
|
|
347 |
There is no defined order for the result iteration - it will be in
|
|
348 |
the most efficient order for the index.
|
|
349 |
"""
|
|
2745.1.1
by Robert Collins
Add a number of -Devil checkpoints. |
350 |
if 'evil' in debug.debug_flags: |
2592.3.112
by Robert Collins
Various fixups found dogfooding. |
351 |
trace.mutter_callsite(3, |
2745.1.2
by Robert Collins
Ensure mutter_callsite is not directly called on a lazy_load object, to make the stacklevel parameter work correctly. |
352 |
"iter_all_entries scales with size of history.") |
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
353 |
if self._nodes is None: |
354 |
self._buffer_all() |
|
355 |
if self.node_ref_lists: |
|
356 |
for key, (value, node_ref_lists) in self._nodes.iteritems(): |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
357 |
yield self, key, value, node_ref_lists |
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
358 |
else: |
359 |
for key, value in self._nodes.iteritems(): |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
360 |
yield self, key, value |
2624.2.2
by Robert Collins
Temporary performance hack for GraphIndex : load the entire index once and only once into ram. |
361 |
|
2592.1.27
by Robert Collins
Test missing end lines with non-empty indices. |
362 |
def _read_prefix(self, stream): |
363 |
signature = stream.read(len(self._signature())) |
|
364 |
if not signature == self._signature(): |
|
365 |
raise errors.BadIndexFormatSignature(self._name, GraphIndex) |
|
366 |
options_line = stream.readline() |
|
367 |
if not options_line.startswith(_OPTION_NODE_REFS): |
|
368 |
raise errors.BadIndexOptions(self) |
|
369 |
try: |
|
370 |
self.node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):-1]) |
|
371 |
except ValueError: |
|
372 |
raise errors.BadIndexOptions(self) |
|
2624.2.8
by Robert Collins
Explicitly mark the number of keys elements in use in GraphIndex files. |
373 |
options_line = stream.readline() |
374 |
if not options_line.startswith(_OPTION_KEY_ELEMENTS): |
|
375 |
raise errors.BadIndexOptions(self) |
|
376 |
try: |
|
377 |
self._key_length = int(options_line[len(_OPTION_KEY_ELEMENTS):-1]) |
|
378 |
except ValueError: |
|
379 |
raise errors.BadIndexOptions(self) |
|
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
380 |
options_line = stream.readline() |
381 |
if not options_line.startswith(_OPTION_LEN): |
|
382 |
raise errors.BadIndexOptions(self) |
|
383 |
try: |
|
384 |
self._key_count = int(options_line[len(_OPTION_LEN):-1]) |
|
385 |
except ValueError: |
|
386 |
raise errors.BadIndexOptions(self) |
|
2592.1.5
by Robert Collins
Trivial index reading. |
387 |
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
388 |
def _resolve_references(self, references): |
2890.2.16
by Robert Collins
Review feedback. |
389 |
"""Return the resolved key references for references.
|
390 |
|
|
391 |
References are resolved by looking up the location of the key in the
|
|
392 |
_keys_by_offset map and substituting the key name, preserving ordering.
|
|
393 |
||
394 |
:param references: An iterable of iterables of key locations. e.g.
|
|
395 |
[[123, 456], [123]]
|
|
396 |
:return: A tuple of tuples of keys.
|
|
397 |
"""
|
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
398 |
node_refs = [] |
399 |
for ref_list in references: |
|
400 |
node_refs.append(tuple([self._keys_by_offset[ref][0] for ref in ref_list])) |
|
401 |
return tuple(node_refs) |
|
402 |
||
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
403 |
def _find_index(self, range_map, key): |
404 |
"""Helper for the _parsed_*_index calls.
|
|
405 |
||
406 |
Given a range map - [(start, end), ...], finds the index of the range
|
|
407 |
in the map for key if it is in the map, and if it is not there, the
|
|
408 |
immediately preceeding range in the map.
|
|
409 |
"""
|
|
410 |
result = bisect_right(range_map, key) - 1 |
|
411 |
if result + 1 < len(range_map): |
|
412 |
# check the border condition, it may be in result + 1
|
|
413 |
if range_map[result + 1][0] == key[0]: |
|
414 |
return result + 1 |
|
415 |
return result |
|
416 |
||
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
417 |
def _parsed_byte_index(self, offset): |
418 |
"""Return the index of the entry immediately before offset.
|
|
419 |
||
420 |
e.g. if the parsed map has regions 0,10 and 11,12 parsed, meaning that
|
|
421 |
there is one unparsed byte (the 11th, addressed as[10]). then:
|
|
422 |
asking for 0 will return 0
|
|
423 |
asking for 10 will return 0
|
|
424 |
asking for 11 will return 1
|
|
425 |
asking for 12 will return 1
|
|
426 |
"""
|
|
427 |
key = (offset, 0) |
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
428 |
return self._find_index(self._parsed_byte_map, key) |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
429 |
|
430 |
def _parsed_key_index(self, key): |
|
431 |
"""Return the index of the entry immediately before key.
|
|
432 |
||
433 |
e.g. if the parsed map has regions (None, 'a') and ('b','c') parsed,
|
|
434 |
meaning that keys from None to 'a' inclusive, and 'b' to 'c' inclusive
|
|
435 |
have been parsed, then:
|
|
436 |
asking for '' will return 0
|
|
437 |
asking for 'a' will return 0
|
|
438 |
asking for 'b' will return 1
|
|
439 |
asking for 'e' will return 1
|
|
440 |
"""
|
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
441 |
search_key = (key, None) |
442 |
return self._find_index(self._parsed_key_map, search_key) |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
443 |
|
444 |
def _is_parsed(self, offset): |
|
445 |
"""Returns True if offset has been parsed."""
|
|
446 |
index = self._parsed_byte_index(offset) |
|
447 |
if index == len(self._parsed_byte_map): |
|
448 |
return offset < self._parsed_byte_map[index - 1][1] |
|
449 |
start, end = self._parsed_byte_map[index] |
|
450 |
return offset >= start and offset < end |
|
451 |
||
2890.2.7
by Robert Collins
* Pack indices are now partially parsed for specific key lookup using a |
452 |
def _iter_entries_from_total_buffer(self, keys): |
453 |
"""Iterate over keys when the entire index is parsed."""
|
|
2592.3.54
by Robert Collins
Fix remaining performance discrepancy with regular repositories. |
454 |
keys = keys.intersection(self._keys) |
2624.2.3
by Robert Collins
Make GraphIndex.iter_entries do hash lookups rather than table scans. |
455 |
if self.node_ref_lists: |
456 |
for key in keys: |
|
457 |
value, node_refs = self._nodes[key] |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
458 |
yield self, key, value, node_refs |
2624.2.3
by Robert Collins
Make GraphIndex.iter_entries do hash lookups rather than table scans. |
459 |
else: |
460 |
for key in keys: |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
461 |
yield self, key, self._nodes[key] |
2592.1.7
by Robert Collins
A validate that goes boom. |
462 |
|
2890.2.7
by Robert Collins
* Pack indices are now partially parsed for specific key lookup using a |
463 |
def iter_entries(self, keys): |
464 |
"""Iterate over keys within the index.
|
|
465 |
||
466 |
:param keys: An iterable providing the keys to be retrieved.
|
|
467 |
:return: An iterable as per iter_all_entries, but restricted to the
|
|
468 |
keys supplied. No additional keys will be returned, and every
|
|
469 |
key supplied that is in the index will be returned.
|
|
470 |
"""
|
|
471 |
keys = set(keys) |
|
472 |
if not keys: |
|
473 |
return [] |
|
2890.2.8
by Robert Collins
Make the size of the index optionally None for the pack-names index. |
474 |
if self._size is None and self._nodes is None: |
475 |
self._buffer_all() |
|
3606.6.1
by Robert Collins
Cherry-pick Robert's index buffering. |
476 |
# We fit about 20 keys per minimum-read (4K), so if we are looking for
|
477 |
# more than 1/20th of the index its likely (assuming homogenous key
|
|
478 |
# spread) that we'll read the entire index. If we're going to do that,
|
|
479 |
# buffer the whole thing. A better analysis might take key spread into
|
|
480 |
# account - but B+Tree indices are better anyway.
|
|
481 |
# We could look at all data read, and use a threshold there, which will
|
|
482 |
# trigger on ancestry walks, but that is not yet fully mapped out.
|
|
483 |
if self._nodes is None and len(keys) * 20 > self.key_count(): |
|
484 |
self._buffer_all() |
|
2890.2.7
by Robert Collins
* Pack indices are now partially parsed for specific key lookup using a |
485 |
if self._nodes is not None: |
486 |
return self._iter_entries_from_total_buffer(keys) |
|
487 |
else: |
|
488 |
return (result[1] for result in bisect_multi_bytes( |
|
2890.2.18
by Robert Collins
Review feedback. |
489 |
self._lookup_keys_via_location, self._size, keys)) |
2890.2.7
by Robert Collins
* Pack indices are now partially parsed for specific key lookup using a |
490 |
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
491 |
def iter_entries_prefix(self, keys): |
492 |
"""Iterate over keys within the index using prefix matching.
|
|
493 |
||
494 |
Prefix matching is applied within the tuple of a key, not to within
|
|
495 |
the bytestring of each key element. e.g. if you have the keys ('foo',
|
|
496 |
'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
|
|
497 |
only the former key is returned.
|
|
498 |
||
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
499 |
WARNING: Note that this method currently causes a full index parse
|
500 |
unconditionally (which is reasonably appropriate as it is a means for
|
|
501 |
thunking many small indices into one larger one and still supplies
|
|
502 |
iter_all_entries at the thunk layer).
|
|
503 |
||
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
504 |
:param keys: An iterable providing the key prefixes to be retrieved.
|
505 |
Each key prefix takes the form of a tuple the length of a key, but
|
|
506 |
with the last N elements 'None' rather than a regular bytestring.
|
|
507 |
The first element cannot be 'None'.
|
|
508 |
:return: An iterable as per iter_all_entries, but restricted to the
|
|
509 |
keys with a matching prefix to those supplied. No additional keys
|
|
510 |
will be returned, and every match that is in the index will be
|
|
511 |
returned.
|
|
512 |
"""
|
|
513 |
keys = set(keys) |
|
514 |
if not keys: |
|
515 |
return
|
|
516 |
# load data - also finds key lengths
|
|
517 |
if self._nodes is None: |
|
518 |
self._buffer_all() |
|
519 |
if self._key_length == 1: |
|
520 |
for key in keys: |
|
521 |
# sanity check
|
|
522 |
if key[0] is None: |
|
523 |
raise errors.BadIndexKey(key) |
|
524 |
if len(key) != self._key_length: |
|
525 |
raise errors.BadIndexKey(key) |
|
526 |
if self.node_ref_lists: |
|
527 |
value, node_refs = self._nodes[key] |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
528 |
yield self, key, value, node_refs |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
529 |
else: |
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
530 |
yield self, key, self._nodes[key] |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
531 |
return
|
532 |
for key in keys: |
|
533 |
# sanity check
|
|
534 |
if key[0] is None: |
|
535 |
raise errors.BadIndexKey(key) |
|
536 |
if len(key) != self._key_length: |
|
537 |
raise errors.BadIndexKey(key) |
|
538 |
# find what it refers to:
|
|
539 |
key_dict = self._nodes_by_key |
|
540 |
elements = list(key) |
|
2624.2.11
by Robert Collins
Review comments. |
541 |
# find the subdict whose contents should be returned.
|
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
542 |
try: |
543 |
while len(elements) and elements[0] is not None: |
|
544 |
key_dict = key_dict[elements[0]] |
|
545 |
elements.pop(0) |
|
546 |
except KeyError: |
|
547 |
# a non-existant lookup.
|
|
548 |
continue
|
|
549 |
if len(elements): |
|
550 |
dicts = [key_dict] |
|
551 |
while dicts: |
|
552 |
key_dict = dicts.pop(-1) |
|
553 |
# can't be empty or would not exist
|
|
554 |
item, value = key_dict.iteritems().next() |
|
555 |
if type(value) == dict: |
|
556 |
# push keys
|
|
557 |
dicts.extend(key_dict.itervalues()) |
|
558 |
else: |
|
559 |
# yield keys
|
|
560 |
for value in key_dict.itervalues(): |
|
2624.2.11
by Robert Collins
Review comments. |
561 |
# each value is the key:value:node refs tuple
|
562 |
# ready to yield.
|
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
563 |
yield (self, ) + value |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
564 |
else: |
2624.2.11
by Robert Collins
Review comments. |
565 |
# the last thing looked up was a terminal element
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
566 |
yield (self, ) + key_dict |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
567 |
|
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
568 |
def key_count(self): |
569 |
"""Return an estimate of the number of keys in this index.
|
|
570 |
|
|
571 |
For GraphIndex the estimate is exact.
|
|
572 |
"""
|
|
573 |
if self._key_count is None: |
|
2979.1.1
by Robert Collins
Use the GraphIndex header to answer key_count queries rather than parsing the entire index unnecessarily. |
574 |
self._read_and_parse([_HEADER_READV]) |
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
575 |
return self._key_count |
576 |
||
2890.2.18
by Robert Collins
Review feedback. |
577 |
def _lookup_keys_via_location(self, location_keys): |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
578 |
"""Public interface for implementing bisection.
|
579 |
||
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
580 |
If _buffer_all has been called, then all the data for the index is in
|
581 |
memory, and this method should not be called, as it uses a separate
|
|
582 |
cache because it cannot pre-resolve all indices, which buffer_all does
|
|
583 |
for performance.
|
|
584 |
||
2890.2.16
by Robert Collins
Review feedback. |
585 |
:param location_keys: A list of location(byte offset), key tuples.
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
586 |
:return: A list of (location_key, result) tuples as expected by
|
587 |
bzrlib.bisect_multi.bisect_multi_bytes.
|
|
588 |
"""
|
|
589 |
# Possible improvements:
|
|
590 |
# - only bisect lookup each key once
|
|
591 |
# - sort the keys first, and use that to reduce the bisection window
|
|
592 |
# -----
|
|
593 |
# this progresses in three parts:
|
|
594 |
# read data
|
|
595 |
# parse it
|
|
596 |
# attempt to answer the question from the now in memory data.
|
|
597 |
# build the readv request
|
|
598 |
# for each location, ask for 800 bytes - much more than rows we've seen
|
|
599 |
# anywhere.
|
|
600 |
readv_ranges = [] |
|
601 |
for location, key in location_keys: |
|
602 |
# can we answer from cache?
|
|
2911.3.1
by Robert Collins
(robertc) Improve index bisection lookup performance looking for keys in the parsed dict before doing bisection searches in the parsed ranges. (Robert Collins). |
603 |
if self._bisect_nodes and key in self._bisect_nodes: |
604 |
# We have the key parsed.
|
|
605 |
continue
|
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
606 |
index = self._parsed_key_index(key) |
607 |
if (len(self._parsed_key_map) and |
|
608 |
self._parsed_key_map[index][0] <= key and |
|
2911.3.1
by Robert Collins
(robertc) Improve index bisection lookup performance looking for keys in the parsed dict before doing bisection searches in the parsed ranges. (Robert Collins). |
609 |
(self._parsed_key_map[index][1] >= key or |
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
610 |
# end of the file has been parsed
|
611 |
self._parsed_byte_map[index][1] == self._size)): |
|
2911.3.1
by Robert Collins
(robertc) Improve index bisection lookup performance looking for keys in the parsed dict before doing bisection searches in the parsed ranges. (Robert Collins). |
612 |
# the key has been parsed, so no lookup is needed even if its
|
613 |
# not present.
|
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
614 |
continue
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
615 |
# - if we have examined this part of the file already - yes
|
616 |
index = self._parsed_byte_index(location) |
|
617 |
if (len(self._parsed_byte_map) and |
|
618 |
self._parsed_byte_map[index][0] <= location and |
|
619 |
self._parsed_byte_map[index][1] > location): |
|
620 |
# the byte region has been parsed, so no read is needed.
|
|
621 |
continue
|
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
622 |
length = 800 |
623 |
if location + length > self._size: |
|
624 |
length = self._size - location |
|
625 |
# todo, trim out parsed locations.
|
|
626 |
if length > 0: |
|
627 |
readv_ranges.append((location, length)) |
|
628 |
# read the header if needed
|
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
629 |
if self._bisect_nodes is None: |
2979.1.1
by Robert Collins
Use the GraphIndex header to answer key_count queries rather than parsing the entire index unnecessarily. |
630 |
readv_ranges.append(_HEADER_READV) |
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
631 |
self._read_and_parse(readv_ranges) |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
632 |
# generate results:
|
633 |
# - figure out <, >, missing, present
|
|
634 |
# - result present references so we can return them.
|
|
635 |
result = [] |
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
636 |
# keys that we cannot answer until we resolve references
|
637 |
pending_references = [] |
|
638 |
pending_locations = set() |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
639 |
for location, key in location_keys: |
640 |
# can we answer from cache?
|
|
2911.3.1
by Robert Collins
(robertc) Improve index bisection lookup performance looking for keys in the parsed dict before doing bisection searches in the parsed ranges. (Robert Collins). |
641 |
if key in self._bisect_nodes: |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
642 |
# the key has been parsed, so no lookup is needed
|
2911.3.1
by Robert Collins
(robertc) Improve index bisection lookup performance looking for keys in the parsed dict before doing bisection searches in the parsed ranges. (Robert Collins). |
643 |
if self.node_ref_lists: |
644 |
# the references may not have been all parsed.
|
|
645 |
value, refs = self._bisect_nodes[key] |
|
646 |
wanted_locations = [] |
|
647 |
for ref_list in refs: |
|
648 |
for ref in ref_list: |
|
649 |
if ref not in self._keys_by_offset: |
|
650 |
wanted_locations.append(ref) |
|
651 |
if wanted_locations: |
|
652 |
pending_locations.update(wanted_locations) |
|
653 |
pending_references.append((location, key)) |
|
654 |
continue
|
|
655 |
result.append(((location, key), (self, key, |
|
656 |
value, self._resolve_references(refs)))) |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
657 |
else: |
2911.3.1
by Robert Collins
(robertc) Improve index bisection lookup performance looking for keys in the parsed dict before doing bisection searches in the parsed ranges. (Robert Collins). |
658 |
result.append(((location, key), |
659 |
(self, key, self._bisect_nodes[key]))) |
|
660 |
continue
|
|
661 |
else: |
|
662 |
# has the region the key should be in, been parsed?
|
|
663 |
index = self._parsed_key_index(key) |
|
664 |
if (self._parsed_key_map[index][0] <= key and |
|
665 |
(self._parsed_key_map[index][1] >= key or |
|
666 |
# end of the file has been parsed
|
|
667 |
self._parsed_byte_map[index][1] == self._size)): |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
668 |
result.append(((location, key), False)) |
2911.3.1
by Robert Collins
(robertc) Improve index bisection lookup performance looking for keys in the parsed dict before doing bisection searches in the parsed ranges. (Robert Collins). |
669 |
continue
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
670 |
# no, is the key above or below the probed location:
|
671 |
# get the range of the probed & parsed location
|
|
672 |
index = self._parsed_byte_index(location) |
|
673 |
# if the key is below the start of the range, its below
|
|
674 |
if key < self._parsed_key_map[index][0]: |
|
675 |
direction = -1 |
|
676 |
else: |
|
677 |
direction = +1 |
|
678 |
result.append(((location, key), direction)) |
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
679 |
readv_ranges = [] |
680 |
# lookup data to resolve references
|
|
681 |
for location in pending_locations: |
|
682 |
length = 800 |
|
683 |
if location + length > self._size: |
|
684 |
length = self._size - location |
|
685 |
# TODO: trim out parsed locations (e.g. if the 800 is into the
|
|
2890.2.16
by Robert Collins
Review feedback. |
686 |
# parsed region trim it, and dont use the adjust_for_latency
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
687 |
# facility)
|
688 |
if length > 0: |
|
689 |
readv_ranges.append((location, length)) |
|
690 |
self._read_and_parse(readv_ranges) |
|
691 |
for location, key in pending_references: |
|
692 |
# answer key references we had to look-up-late.
|
|
693 |
index = self._parsed_key_index(key) |
|
694 |
value, refs = self._bisect_nodes[key] |
|
695 |
result.append(((location, key), (self, key, |
|
696 |
value, self._resolve_references(refs)))) |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
697 |
return result |
698 |
||
699 |
def _parse_header_from_bytes(self, bytes): |
|
700 |
"""Parse the header from a region of bytes.
|
|
701 |
||
702 |
:param bytes: The data to parse.
|
|
703 |
:return: An offset, data tuple such as readv yields, for the unparsed
|
|
704 |
data. (which may length 0).
|
|
705 |
"""
|
|
706 |
signature = bytes[0:len(self._signature())] |
|
707 |
if not signature == self._signature(): |
|
708 |
raise errors.BadIndexFormatSignature(self._name, GraphIndex) |
|
709 |
lines = bytes[len(self._signature()):].splitlines() |
|
710 |
options_line = lines[0] |
|
711 |
if not options_line.startswith(_OPTION_NODE_REFS): |
|
712 |
raise errors.BadIndexOptions(self) |
|
713 |
try: |
|
714 |
self.node_ref_lists = int(options_line[len(_OPTION_NODE_REFS):]) |
|
715 |
except ValueError: |
|
716 |
raise errors.BadIndexOptions(self) |
|
717 |
options_line = lines[1] |
|
718 |
if not options_line.startswith(_OPTION_KEY_ELEMENTS): |
|
719 |
raise errors.BadIndexOptions(self) |
|
720 |
try: |
|
721 |
self._key_length = int(options_line[len(_OPTION_KEY_ELEMENTS):]) |
|
722 |
except ValueError: |
|
723 |
raise errors.BadIndexOptions(self) |
|
724 |
options_line = lines[2] |
|
725 |
if not options_line.startswith(_OPTION_LEN): |
|
726 |
raise errors.BadIndexOptions(self) |
|
727 |
try: |
|
728 |
self._key_count = int(options_line[len(_OPTION_LEN):]) |
|
729 |
except ValueError: |
|
730 |
raise errors.BadIndexOptions(self) |
|
731 |
# calculate the bytes we have processed
|
|
732 |
header_end = (len(signature) + len(lines[0]) + len(lines[1]) + |
|
733 |
len(lines[2]) + 3) |
|
734 |
self._parsed_bytes(0, None, header_end, None) |
|
735 |
# setup parsing state
|
|
736 |
self._expected_elements = 3 + self._key_length |
|
737 |
# raw data keyed by offset
|
|
738 |
self._keys_by_offset = {} |
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
739 |
# keys with the value and node references
|
740 |
self._bisect_nodes = {} |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
741 |
return header_end, bytes[header_end:] |
742 |
||
743 |
def _parse_region(self, offset, data): |
|
744 |
"""Parse node data returned from a readv operation.
|
|
745 |
||
746 |
:param offset: The byte offset the data starts at.
|
|
747 |
:param data: The data to parse.
|
|
748 |
"""
|
|
749 |
# trim the data.
|
|
750 |
# end first:
|
|
751 |
end = offset + len(data) |
|
2890.2.15
by Robert Collins
Corner case when parsing repeated sections - the bottom section of a region may not be parsed, so we need to manually advance past that. |
752 |
high_parsed = offset |
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
753 |
while True: |
754 |
# Trivial test - if the current index's end is within the
|
|
755 |
# low-matching parsed range, we're done.
|
|
2890.2.15
by Robert Collins
Corner case when parsing repeated sections - the bottom section of a region may not be parsed, so we need to manually advance past that. |
756 |
index = self._parsed_byte_index(high_parsed) |
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
757 |
if end < self._parsed_byte_map[index][1]: |
758 |
return
|
|
2890.2.15
by Robert Collins
Corner case when parsing repeated sections - the bottom section of a region may not be parsed, so we need to manually advance past that. |
759 |
# print "[%d:%d]" % (offset, end), \
|
760 |
# self._parsed_byte_map[index:index + 2]
|
|
761 |
high_parsed, last_segment = self._parse_segment( |
|
762 |
offset, data, end, index) |
|
763 |
if last_segment: |
|
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
764 |
return
|
765 |
||
766 |
def _parse_segment(self, offset, data, end, index): |
|
767 |
"""Parse one segment of data.
|
|
768 |
||
769 |
:param offset: Where 'data' begins in the file.
|
|
770 |
:param data: Some data to parse a segment of.
|
|
771 |
:param end: Where data ends
|
|
772 |
:param index: The current index into the parsed bytes map.
|
|
773 |
:return: True if the parsed segment is the last possible one in the
|
|
774 |
range of data.
|
|
2890.2.15
by Robert Collins
Corner case when parsing repeated sections - the bottom section of a region may not be parsed, so we need to manually advance past that. |
775 |
:return: high_parsed_byte, last_segment.
|
776 |
high_parsed_byte is the location of the highest parsed byte in this
|
|
777 |
segment, last_segment is True if the parsed segment is the last
|
|
778 |
possible one in the data block.
|
|
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
779 |
"""
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
780 |
# default is to use all data
|
781 |
trim_end = None |
|
782 |
# accomodate overlap with data before this.
|
|
783 |
if offset < self._parsed_byte_map[index][1]: |
|
784 |
# overlaps the lower parsed region
|
|
785 |
# skip the parsed data
|
|
786 |
trim_start = self._parsed_byte_map[index][1] - offset |
|
787 |
# don't trim the start for \n
|
|
788 |
start_adjacent = True |
|
789 |
elif offset == self._parsed_byte_map[index][1]: |
|
790 |
# abuts the lower parsed region
|
|
791 |
# use all data
|
|
792 |
trim_start = None |
|
793 |
# do not trim anything
|
|
794 |
start_adjacent = True |
|
795 |
else: |
|
796 |
# does not overlap the lower parsed region
|
|
797 |
# use all data
|
|
798 |
trim_start = None |
|
799 |
# but trim the leading \n
|
|
800 |
start_adjacent = False |
|
801 |
if end == self._size: |
|
802 |
# lines up to the end of all data:
|
|
803 |
# use it all
|
|
804 |
trim_end = None |
|
805 |
# do not strip to the last \n
|
|
806 |
end_adjacent = True |
|
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
807 |
last_segment = True |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
808 |
elif index + 1 == len(self._parsed_byte_map): |
809 |
# at the end of the parsed data
|
|
810 |
# use it all
|
|
811 |
trim_end = None |
|
812 |
# but strip to the last \n
|
|
813 |
end_adjacent = False |
|
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
814 |
last_segment = True |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
815 |
elif end == self._parsed_byte_map[index + 1][0]: |
816 |
# buts up against the next parsed region
|
|
817 |
# use it all
|
|
818 |
trim_end = None |
|
819 |
# do not strip to the last \n
|
|
820 |
end_adjacent = True |
|
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
821 |
last_segment = True |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
822 |
elif end > self._parsed_byte_map[index + 1][0]: |
823 |
# overlaps into the next parsed region
|
|
824 |
# only consider the unparsed data
|
|
825 |
trim_end = self._parsed_byte_map[index + 1][0] - offset |
|
826 |
# do not strip to the last \n as we know its an entire record
|
|
827 |
end_adjacent = True |
|
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
828 |
last_segment = end < self._parsed_byte_map[index + 1][1] |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
829 |
else: |
830 |
# does not overlap into the next region
|
|
831 |
# use it all
|
|
832 |
trim_end = None |
|
833 |
# but strip to the last \n
|
|
834 |
end_adjacent = False |
|
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
835 |
last_segment = True |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
836 |
# now find bytes to discard if needed
|
837 |
if not start_adjacent: |
|
838 |
# work around python bug in rfind
|
|
839 |
if trim_start is None: |
|
840 |
trim_start = data.find('\n') + 1 |
|
841 |
else: |
|
842 |
trim_start = data.find('\n', trim_start) + 1 |
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
843 |
if not (trim_start != 0): |
844 |
raise AssertionError('no \n was present') |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
845 |
# print 'removing start', offset, trim_start, repr(data[:trim_start])
|
846 |
if not end_adjacent: |
|
847 |
# work around python bug in rfind
|
|
848 |
if trim_end is None: |
|
849 |
trim_end = data.rfind('\n') + 1 |
|
850 |
else: |
|
851 |
trim_end = data.rfind('\n', None, trim_end) + 1 |
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
852 |
if not (trim_end != 0): |
853 |
raise AssertionError('no \n was present') |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
854 |
# print 'removing end', offset, trim_end, repr(data[trim_end:])
|
855 |
# adjust offset and data to the parseable data.
|
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
856 |
trimmed_data = data[trim_start:trim_end] |
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
857 |
if not (trimmed_data): |
858 |
raise AssertionError('read unneeded data [%d:%d] from [%d:%d]' |
|
859 |
% (trim_start, trim_end, offset, offset + len(data))) |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
860 |
if trim_start: |
861 |
offset += trim_start |
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
862 |
# print "parsing", repr(trimmed_data)
|
2890.2.10
by Robert Collins
Add test coverage to ensure \r's are not mangled by bisection parsing. |
863 |
# splitlines mangles the \r delimiters.. don't use it.
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
864 |
lines = trimmed_data.split('\n') |
2890.2.9
by Robert Collins
Don't use splitlines for index data parsing, we embed \r. |
865 |
del lines[-1] |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
866 |
pos = offset |
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
867 |
first_key, last_key, nodes, _ = self._parse_lines(lines, pos) |
868 |
for key, value in nodes: |
|
869 |
self._bisect_nodes[key] = value |
|
870 |
self._parsed_bytes(offset, first_key, |
|
871 |
offset + len(trimmed_data), last_key) |
|
872 |
return offset + len(trimmed_data), last_segment |
|
873 |
||
874 |
def _parse_lines(self, lines, pos): |
|
875 |
key = None |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
876 |
first_key = None |
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
877 |
trailers = 0 |
878 |
nodes = [] |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
879 |
for line in lines: |
880 |
if line == '': |
|
881 |
# must be at the end
|
|
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
882 |
if self._size: |
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
883 |
if not (self._size == pos + 1): |
884 |
raise AssertionError("%s %s" % (self._size, pos)) |
|
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
885 |
trailers += 1 |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
886 |
continue
|
887 |
elements = line.split('\0') |
|
888 |
if len(elements) != self._expected_elements: |
|
889 |
raise errors.BadIndexData(self) |
|
3530.3.3
by Robert Collins
Credit and explanation for interning. |
890 |
# keys are tuples. Each element is a string that may occur many
|
891 |
# times, so we intern them to save space. AB, RC, 200807
|
|
3530.3.1
by Robert Collins
Intern keys to reduce duplicated memory in GraphIndex parsing. |
892 |
key = tuple(intern(element) for element in elements[:self._key_length]) |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
893 |
if first_key is None: |
894 |
first_key = key |
|
895 |
absent, references, value = elements[-3:] |
|
896 |
ref_lists = [] |
|
897 |
for ref_string in references.split('\t'): |
|
898 |
ref_lists.append(tuple([ |
|
899 |
int(ref) for ref in ref_string.split('\r') if ref |
|
900 |
]))
|
|
901 |
ref_lists = tuple(ref_lists) |
|
902 |
self._keys_by_offset[pos] = (key, absent, ref_lists, value) |
|
903 |
pos += len(line) + 1 # +1 for the \n |
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
904 |
if absent: |
905 |
continue
|
|
906 |
if self.node_ref_lists: |
|
907 |
node_value = (value, ref_lists) |
|
908 |
else: |
|
909 |
node_value = value |
|
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
910 |
nodes.append((key, node_value)) |
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
911 |
# print "parsed ", key
|
2890.2.17
by Robert Collins
Split _parse_segment out into a _parse_lines helper, reducing duplication with full index parsing. |
912 |
return first_key, key, nodes, trailers |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
913 |
|
914 |
def _parsed_bytes(self, start, start_key, end, end_key): |
|
915 |
"""Mark the bytes from start to end as parsed.
|
|
916 |
||
917 |
Calling self._parsed_bytes(1,2) will mark one byte (the one at offset
|
|
918 |
1) as parsed.
|
|
919 |
||
920 |
:param start: The start of the parsed region.
|
|
921 |
:param end: The end of the parsed region.
|
|
922 |
"""
|
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
923 |
index = self._parsed_byte_index(start) |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
924 |
new_value = (start, end) |
925 |
new_key = (start_key, end_key) |
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
926 |
if index == -1: |
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
927 |
# first range parsed is always the beginning.
|
928 |
self._parsed_byte_map.insert(index, new_value) |
|
929 |
self._parsed_key_map.insert(index, new_key) |
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
930 |
return
|
931 |
# four cases:
|
|
932 |
# new region
|
|
933 |
# extend lower region
|
|
934 |
# extend higher region
|
|
935 |
# combine two regions
|
|
936 |
if (index + 1 < len(self._parsed_byte_map) and |
|
937 |
self._parsed_byte_map[index][1] == start and |
|
938 |
self._parsed_byte_map[index + 1][0] == end): |
|
939 |
# combine two regions
|
|
940 |
self._parsed_byte_map[index] = (self._parsed_byte_map[index][0], |
|
941 |
self._parsed_byte_map[index + 1][1]) |
|
942 |
self._parsed_key_map[index] = (self._parsed_key_map[index][0], |
|
943 |
self._parsed_key_map[index + 1][1]) |
|
2890.2.12
by Robert Collins
More index tweaks. |
944 |
del self._parsed_byte_map[index + 1] |
945 |
del self._parsed_key_map[index + 1] |
|
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
946 |
elif self._parsed_byte_map[index][1] == start: |
947 |
# extend the lower entry
|
|
948 |
self._parsed_byte_map[index] = ( |
|
949 |
self._parsed_byte_map[index][0], end) |
|
950 |
self._parsed_key_map[index] = ( |
|
951 |
self._parsed_key_map[index][0], end_key) |
|
952 |
elif (index + 1 < len(self._parsed_byte_map) and |
|
953 |
self._parsed_byte_map[index + 1][0] == end): |
|
954 |
# extend the higher entry
|
|
955 |
self._parsed_byte_map[index + 1] = ( |
|
956 |
start, self._parsed_byte_map[index + 1][1]) |
|
957 |
self._parsed_key_map[index + 1] = ( |
|
958 |
start_key, self._parsed_key_map[index + 1][1]) |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
959 |
else: |
2890.2.11
by Robert Collins
Bisection improvements after integrating with packs. |
960 |
# new entry
|
961 |
self._parsed_byte_map.insert(index + 1, new_value) |
|
962 |
self._parsed_key_map.insert(index + 1, new_key) |
|
2890.2.5
by Robert Collins
Create a content lookup function for bisection in GraphIndex. |
963 |
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
964 |
def _read_and_parse(self, readv_ranges): |
965 |
"""Read the the ranges and parse the resulting data.
|
|
966 |
||
967 |
:param readv_ranges: A prepared readv range list.
|
|
968 |
"""
|
|
969 |
if readv_ranges: |
|
970 |
readv_data = self._transport.readv(self._name, readv_ranges, True, |
|
971 |
self._size) |
|
972 |
# parse
|
|
973 |
for offset, data in readv_data: |
|
974 |
if self._bisect_nodes is None: |
|
975 |
# this must be the start
|
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
976 |
if not (offset == 0): |
977 |
raise AssertionError() |
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
978 |
offset, data = self._parse_header_from_bytes(data) |
2890.2.14
by Robert Collins
Parse more than one segment of data from a single readv response if needed. |
979 |
# print readv_ranges, "[%d:%d]" % (offset, offset + len(data))
|
2890.2.6
by Robert Collins
Add support for key references to the index lookup_keys_via_location bisection interface. |
980 |
self._parse_region(offset, data) |
981 |
||
2592.1.8
by Robert Collins
Empty files should validate ok. |
982 |
def _signature(self): |
983 |
"""The file signature for this index type."""
|
|
984 |
return _SIGNATURE |
|
985 |
||
2592.1.7
by Robert Collins
A validate that goes boom. |
986 |
def validate(self): |
987 |
"""Validate that everything in the index can be accessed."""
|
|
2592.1.27
by Robert Collins
Test missing end lines with non-empty indices. |
988 |
# iter_all validates completely at the moment, so just do that.
|
989 |
for node in self.iter_all_entries(): |
|
990 |
pass
|
|
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
991 |
|
992 |
||
993 |
class CombinedGraphIndex(object): |
|
994 |
"""A GraphIndex made up from smaller GraphIndices.
|
|
995 |
|
|
996 |
The backing indices must implement GraphIndex, and are presumed to be
|
|
997 |
static data.
|
|
2592.1.45
by Robert Collins
Tweak documentation as per Aaron's review. |
998 |
|
999 |
Queries against the combined index will be made against the first index,
|
|
1000 |
and then the second and so on. The order of index's can thus influence
|
|
1001 |
performance significantly. For example, if one index is on local disk and a
|
|
1002 |
second on a remote server, the local disk index should be before the other
|
|
1003 |
in the index list.
|
|
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1004 |
"""
|
1005 |
||
1006 |
def __init__(self, indices): |
|
1007 |
"""Create a CombinedGraphIndex backed by indices.
|
|
1008 |
||
2592.1.45
by Robert Collins
Tweak documentation as per Aaron's review. |
1009 |
:param indices: An ordered list of indices to query for data.
|
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1010 |
"""
|
1011 |
self._indices = indices |
|
2592.1.37
by Robert Collins
Add CombinedGraphIndex.insert_index. |
1012 |
|
2592.5.4
by Martin Pool
Add CombinedGraphIndex repr |
1013 |
def __repr__(self): |
1014 |
return "%s(%s)" % ( |
|
1015 |
self.__class__.__name__, |
|
1016 |
', '.join(map(repr, self._indices))) |
|
1017 |
||
3099.3.3
by John Arbash Meinel
Deprecate get_parents() in favor of get_parent_map() |
1018 |
@symbol_versioning.deprecated_method(symbol_versioning.one_one) |
2979.2.2
by Robert Collins
Per-file graph heads detection during commit for pack repositories. |
1019 |
def get_parents(self, revision_ids): |
3099.3.1
by John Arbash Meinel
Implement get_parent_map for ParentProviders |
1020 |
"""See graph._StackedParentsProvider.get_parents.
|
2979.2.2
by Robert Collins
Per-file graph heads detection during commit for pack repositories. |
1021 |
|
1022 |
This implementation thunks the graph.Graph.get_parents api across to
|
|
1023 |
GraphIndex.
|
|
1024 |
||
1025 |
:param revision_ids: An iterable of graph keys for this graph.
|
|
1026 |
:return: A list of parent details for each key in revision_ids.
|
|
1027 |
Each parent details will be one of:
|
|
1028 |
* None when the key was missing
|
|
1029 |
* (NULL_REVISION,) when the key has no parents.
|
|
1030 |
* (parent_key, parent_key...) otherwise.
|
|
1031 |
"""
|
|
3099.3.1
by John Arbash Meinel
Implement get_parent_map for ParentProviders |
1032 |
parent_map = self.get_parent_map(revision_ids) |
1033 |
return [parent_map.get(r, None) for r in revision_ids] |
|
1034 |
||
1035 |
def get_parent_map(self, keys): |
|
1036 |
"""See graph._StackedParentsProvider.get_parent_map"""
|
|
1037 |
search_keys = set(keys) |
|
1038 |
if NULL_REVISION in search_keys: |
|
1039 |
search_keys.discard(NULL_REVISION) |
|
1040 |
found_parents = {NULL_REVISION:[]} |
|
1041 |
else: |
|
1042 |
found_parents = {} |
|
2979.2.2
by Robert Collins
Per-file graph heads detection during commit for pack repositories. |
1043 |
for index, key, value, refs in self.iter_entries(search_keys): |
1044 |
parents = refs[0] |
|
1045 |
if not parents: |
|
1046 |
parents = (NULL_REVISION,) |
|
1047 |
found_parents[key] = parents |
|
3099.3.1
by John Arbash Meinel
Implement get_parent_map for ParentProviders |
1048 |
return found_parents |
2979.2.2
by Robert Collins
Per-file graph heads detection during commit for pack repositories. |
1049 |
|
2592.1.37
by Robert Collins
Add CombinedGraphIndex.insert_index. |
1050 |
def insert_index(self, pos, index): |
1051 |
"""Insert a new index in the list of indices to query.
|
|
1052 |
||
1053 |
:param pos: The position to insert the index.
|
|
1054 |
:param index: The index to insert.
|
|
1055 |
"""
|
|
1056 |
self._indices.insert(pos, index) |
|
1057 |
||
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1058 |
def iter_all_entries(self): |
1059 |
"""Iterate over all keys within the index
|
|
1060 |
||
2592.1.44
by Robert Collins
Remove some unneeded index iteration by checking if we have found all keys, and grammar improvements from Aaron's review. |
1061 |
Duplicate keys across child indices are presumed to have the same
|
1062 |
value and are only reported once.
|
|
1063 |
||
2592.5.1
by Martin Pool
Fix docstrings for Index.iter_entries etc |
1064 |
:return: An iterable of (index, key, reference_lists, value).
|
1065 |
There is no defined order for the result iteration - it will be in
|
|
1066 |
the most efficient order for the index.
|
|
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1067 |
"""
|
1068 |
seen_keys = set() |
|
1069 |
for index in self._indices: |
|
1070 |
for node in index.iter_all_entries(): |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1071 |
if node[1] not in seen_keys: |
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1072 |
yield node |
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1073 |
seen_keys.add(node[1]) |
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1074 |
|
1075 |
def iter_entries(self, keys): |
|
1076 |
"""Iterate over keys within the index.
|
|
1077 |
||
2592.1.44
by Robert Collins
Remove some unneeded index iteration by checking if we have found all keys, and grammar improvements from Aaron's review. |
1078 |
Duplicate keys across child indices are presumed to have the same
|
1079 |
value and are only reported once.
|
|
1080 |
||
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1081 |
:param keys: An iterable providing the keys to be retrieved.
|
2592.5.1
by Martin Pool
Fix docstrings for Index.iter_entries etc |
1082 |
:return: An iterable of (index, key, reference_lists, value). There is no
|
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1083 |
defined order for the result iteration - it will be in the most
|
1084 |
efficient order for the index.
|
|
1085 |
"""
|
|
1086 |
keys = set(keys) |
|
2592.1.39
by Robert Collins
CombinedGraphIndex.iter_entries does not need to see all entries. |
1087 |
for index in self._indices: |
2592.1.44
by Robert Collins
Remove some unneeded index iteration by checking if we have found all keys, and grammar improvements from Aaron's review. |
1088 |
if not keys: |
1089 |
return
|
|
2592.1.39
by Robert Collins
CombinedGraphIndex.iter_entries does not need to see all entries. |
1090 |
for node in index.iter_entries(keys): |
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1091 |
keys.remove(node[1]) |
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1092 |
yield node |
1093 |
||
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
1094 |
def iter_entries_prefix(self, keys): |
1095 |
"""Iterate over keys within the index using prefix matching.
|
|
1096 |
||
1097 |
Duplicate keys across child indices are presumed to have the same
|
|
1098 |
value and are only reported once.
|
|
1099 |
||
1100 |
Prefix matching is applied within the tuple of a key, not to within
|
|
1101 |
the bytestring of each key element. e.g. if you have the keys ('foo',
|
|
1102 |
'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
|
|
1103 |
only the former key is returned.
|
|
1104 |
||
1105 |
:param keys: An iterable providing the key prefixes to be retrieved.
|
|
1106 |
Each key prefix takes the form of a tuple the length of a key, but
|
|
1107 |
with the last N elements 'None' rather than a regular bytestring.
|
|
1108 |
The first element cannot be 'None'.
|
|
1109 |
:return: An iterable as per iter_all_entries, but restricted to the
|
|
1110 |
keys with a matching prefix to those supplied. No additional keys
|
|
1111 |
will be returned, and every match that is in the index will be
|
|
1112 |
returned.
|
|
1113 |
"""
|
|
1114 |
keys = set(keys) |
|
1115 |
if not keys: |
|
1116 |
return
|
|
1117 |
seen_keys = set() |
|
1118 |
for index in self._indices: |
|
1119 |
for node in index.iter_entries_prefix(keys): |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1120 |
if node[1] in seen_keys: |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
1121 |
continue
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1122 |
seen_keys.add(node[1]) |
2624.2.9
by Robert Collins
Introduce multiple component keys, which is what is needed to combine multiple knit indices into one. |
1123 |
yield node |
1124 |
||
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
1125 |
def key_count(self): |
1126 |
"""Return an estimate of the number of keys in this index.
|
|
1127 |
|
|
1128 |
For CombinedGraphIndex this is approximated by the sum of the keys of
|
|
1129 |
the child indices. As child indices may have duplicate keys this can
|
|
1130 |
have a maximum error of the number of child indices * largest number of
|
|
1131 |
keys in any index.
|
|
1132 |
"""
|
|
1133 |
return sum((index.key_count() for index in self._indices), 0) |
|
1134 |
||
2592.1.31
by Robert Collins
Build a combined graph index to use multiple indices at once. |
1135 |
def validate(self): |
1136 |
"""Validate that everything in the index can be accessed."""
|
|
1137 |
for index in self._indices: |
|
1138 |
index.validate() |
|
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
1139 |
|
1140 |
||
1141 |
class InMemoryGraphIndex(GraphIndexBuilder): |
|
1142 |
"""A GraphIndex which operates entirely out of memory and is mutable.
|
|
1143 |
||
1144 |
This is designed to allow the accumulation of GraphIndex entries during a
|
|
1145 |
single write operation, where the accumulated entries need to be immediately
|
|
1146 |
available - for example via a CombinedGraphIndex.
|
|
1147 |
"""
|
|
1148 |
||
1149 |
def add_nodes(self, nodes): |
|
1150 |
"""Add nodes to the index.
|
|
1151 |
||
1152 |
:param nodes: An iterable of (key, node_refs, value) entries to add.
|
|
1153 |
"""
|
|
2592.3.39
by Robert Collins
Fugly version to remove signatures.kndx |
1154 |
if self.reference_lists: |
1155 |
for (key, value, node_refs) in nodes: |
|
1156 |
self.add_node(key, value, node_refs) |
|
1157 |
else: |
|
1158 |
for (key, value) in nodes: |
|
1159 |
self.add_node(key, value) |
|
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
1160 |
|
1161 |
def iter_all_entries(self): |
|
1162 |
"""Iterate over all keys within the index
|
|
1163 |
||
2592.5.1
by Martin Pool
Fix docstrings for Index.iter_entries etc |
1164 |
:return: An iterable of (index, key, reference_lists, value). There is no
|
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
1165 |
defined order for the result iteration - it will be in the most
|
1166 |
efficient order for the index (in this case dictionary hash order).
|
|
1167 |
"""
|
|
2745.1.1
by Robert Collins
Add a number of -Devil checkpoints. |
1168 |
if 'evil' in debug.debug_flags: |
2592.3.112
by Robert Collins
Various fixups found dogfooding. |
1169 |
trace.mutter_callsite(3, |
2745.1.2
by Robert Collins
Ensure mutter_callsite is not directly called on a lazy_load object, to make the stacklevel parameter work correctly. |
1170 |
"iter_all_entries scales with size of history.") |
2592.1.46
by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method |
1171 |
if self.reference_lists: |
1172 |
for key, (absent, references, value) in self._nodes.iteritems(): |
|
1173 |
if not absent: |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1174 |
yield self, key, value, references |
2592.1.46
by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method |
1175 |
else: |
1176 |
for key, (absent, references, value) in self._nodes.iteritems(): |
|
1177 |
if not absent: |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1178 |
yield self, key, value |
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
1179 |
|
1180 |
def iter_entries(self, keys): |
|
1181 |
"""Iterate over keys within the index.
|
|
1182 |
||
1183 |
:param keys: An iterable providing the keys to be retrieved.
|
|
2979.2.4
by Robert Collins
Docstring fixes from review. |
1184 |
:return: An iterable of (index, key, value, reference_lists). There is no
|
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
1185 |
defined order for the result iteration - it will be in the most
|
1186 |
efficient order for the index (keys iteration order in this case).
|
|
1187 |
"""
|
|
1188 |
keys = set(keys) |
|
2592.1.46
by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method |
1189 |
if self.reference_lists: |
2592.3.62
by Robert Collins
Performance tweak - use a set for InMemoryGraph key iteration. |
1190 |
for key in keys.intersection(self._keys): |
2592.1.46
by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method |
1191 |
node = self._nodes[key] |
1192 |
if not node[0]: |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1193 |
yield self, key, node[2], node[1] |
2592.1.46
by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method |
1194 |
else: |
2592.3.62
by Robert Collins
Performance tweak - use a set for InMemoryGraph key iteration. |
1195 |
for key in keys.intersection(self._keys): |
2592.1.46
by Robert Collins
Make GraphIndex accept nodes as key, value, references, so that the method |
1196 |
node = self._nodes[key] |
1197 |
if not node[0]: |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1198 |
yield self, key, node[2] |
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
1199 |
|
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
1200 |
def iter_entries_prefix(self, keys): |
1201 |
"""Iterate over keys within the index using prefix matching.
|
|
1202 |
||
1203 |
Prefix matching is applied within the tuple of a key, not to within
|
|
1204 |
the bytestring of each key element. e.g. if you have the keys ('foo',
|
|
1205 |
'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
|
|
1206 |
only the former key is returned.
|
|
1207 |
||
1208 |
:param keys: An iterable providing the key prefixes to be retrieved.
|
|
1209 |
Each key prefix takes the form of a tuple the length of a key, but
|
|
1210 |
with the last N elements 'None' rather than a regular bytestring.
|
|
1211 |
The first element cannot be 'None'.
|
|
1212 |
:return: An iterable as per iter_all_entries, but restricted to the
|
|
1213 |
keys with a matching prefix to those supplied. No additional keys
|
|
1214 |
will be returned, and every match that is in the index will be
|
|
1215 |
returned.
|
|
1216 |
"""
|
|
1217 |
# XXX: To much duplication with the GraphIndex class; consider finding
|
|
1218 |
# a good place to pull out the actual common logic.
|
|
1219 |
keys = set(keys) |
|
1220 |
if not keys: |
|
1221 |
return
|
|
1222 |
if self._key_length == 1: |
|
1223 |
for key in keys: |
|
1224 |
# sanity check
|
|
1225 |
if key[0] is None: |
|
1226 |
raise errors.BadIndexKey(key) |
|
1227 |
if len(key) != self._key_length: |
|
1228 |
raise errors.BadIndexKey(key) |
|
1229 |
node = self._nodes[key] |
|
1230 |
if node[0]: |
|
1231 |
continue
|
|
1232 |
if self.reference_lists: |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1233 |
yield self, key, node[2], node[1] |
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
1234 |
else: |
2624.2.17
by Robert Collins
Review feedback. |
1235 |
yield self, key, node[2] |
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
1236 |
return
|
1237 |
for key in keys: |
|
1238 |
# sanity check
|
|
1239 |
if key[0] is None: |
|
1240 |
raise errors.BadIndexKey(key) |
|
1241 |
if len(key) != self._key_length: |
|
1242 |
raise errors.BadIndexKey(key) |
|
1243 |
# find what it refers to:
|
|
1244 |
key_dict = self._nodes_by_key |
|
1245 |
elements = list(key) |
|
1246 |
# find the subdict to return
|
|
1247 |
try: |
|
1248 |
while len(elements) and elements[0] is not None: |
|
1249 |
key_dict = key_dict[elements[0]] |
|
1250 |
elements.pop(0) |
|
1251 |
except KeyError: |
|
1252 |
# a non-existant lookup.
|
|
1253 |
continue
|
|
1254 |
if len(elements): |
|
1255 |
dicts = [key_dict] |
|
1256 |
while dicts: |
|
1257 |
key_dict = dicts.pop(-1) |
|
1258 |
# can't be empty or would not exist
|
|
1259 |
item, value = key_dict.iteritems().next() |
|
1260 |
if type(value) == dict: |
|
1261 |
# push keys
|
|
1262 |
dicts.extend(key_dict.itervalues()) |
|
1263 |
else: |
|
1264 |
# yield keys
|
|
1265 |
for value in key_dict.itervalues(): |
|
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1266 |
yield (self, ) + value |
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
1267 |
else: |
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1268 |
yield (self, ) + key_dict |
2624.2.10
by Robert Collins
Also add iter_key_prefix support to InMemoryGraphIndex. |
1269 |
|
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
1270 |
def key_count(self): |
1271 |
"""Return an estimate of the number of keys in this index.
|
|
1272 |
|
|
1273 |
For InMemoryGraphIndex the estimate is exact.
|
|
1274 |
"""
|
|
1275 |
return len(self._keys) |
|
1276 |
||
2592.1.38
by Robert Collins
Create an InMemoryGraphIndex for temporary indexing. |
1277 |
def validate(self): |
1278 |
"""In memory index's have no known corruption at the moment."""
|
|
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1279 |
|
1280 |
||
1281 |
class GraphIndexPrefixAdapter(object): |
|
1282 |
"""An adapter between GraphIndex with different key lengths.
|
|
1283 |
||
1284 |
Queries against this will emit queries against the adapted Graph with the
|
|
1285 |
prefix added, queries for all items use iter_entries_prefix. The returned
|
|
1286 |
nodes will have their keys and node references adjusted to remove the
|
|
1287 |
prefix. Finally, an add_nodes_callback can be supplied - when called the
|
|
1288 |
nodes and references being added will have prefix prepended.
|
|
1289 |
"""
|
|
1290 |
||
2624.2.17
by Robert Collins
Review feedback. |
1291 |
def __init__(self, adapted, prefix, missing_key_length, |
1292 |
add_nodes_callback=None): |
|
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1293 |
"""Construct an adapter against adapted with prefix."""
|
1294 |
self.adapted = adapted |
|
2624.2.19
by Robert Collins
Why we should always test before committing. |
1295 |
self.prefix_key = prefix + (None,)*missing_key_length |
2624.2.17
by Robert Collins
Review feedback. |
1296 |
self.prefix = prefix |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1297 |
self.prefix_len = len(prefix) |
1298 |
self.add_nodes_callback = add_nodes_callback |
|
1299 |
||
2624.2.13
by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter. |
1300 |
def add_nodes(self, nodes): |
1301 |
"""Add nodes to the index.
|
|
1302 |
||
1303 |
:param nodes: An iterable of (key, node_refs, value) entries to add.
|
|
1304 |
"""
|
|
1305 |
# save nodes in case its an iterator
|
|
1306 |
nodes = tuple(nodes) |
|
1307 |
translated_nodes = [] |
|
1308 |
try: |
|
2624.2.17
by Robert Collins
Review feedback. |
1309 |
# Add prefix_key to each reference node_refs is a tuple of tuples,
|
1310 |
# so split it apart, and add prefix_key to the internal reference
|
|
2624.2.13
by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter. |
1311 |
for (key, value, node_refs) in nodes: |
1312 |
adjusted_references = ( |
|
2624.2.17
by Robert Collins
Review feedback. |
1313 |
tuple(tuple(self.prefix + ref_node for ref_node in ref_list) |
2624.2.13
by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter. |
1314 |
for ref_list in node_refs)) |
2624.2.17
by Robert Collins
Review feedback. |
1315 |
translated_nodes.append((self.prefix + key, value, |
2624.2.13
by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter. |
1316 |
adjusted_references)) |
1317 |
except ValueError: |
|
1318 |
# XXX: TODO add an explicit interface for getting the reference list
|
|
1319 |
# status, to handle this bit of user-friendliness in the API more
|
|
1320 |
# explicitly.
|
|
1321 |
for (key, value) in nodes: |
|
2624.2.17
by Robert Collins
Review feedback. |
1322 |
translated_nodes.append((self.prefix + key, value)) |
2624.2.13
by Robert Collins
Implement add_node/add_nodes to the GraphIndexPrefixAdapter. |
1323 |
self.add_nodes_callback(translated_nodes) |
1324 |
||
1325 |
def add_node(self, key, value, references=()): |
|
1326 |
"""Add a node to the index.
|
|
1327 |
||
1328 |
:param key: The key. keys are non-empty tuples containing
|
|
1329 |
as many whitespace-free utf8 bytestrings as the key length
|
|
1330 |
defined for this index.
|
|
1331 |
:param references: An iterable of iterables of keys. Each is a
|
|
1332 |
reference to another key.
|
|
1333 |
:param value: The value to associate with the key. It may be any
|
|
1334 |
bytes as long as it does not contain \0 or \n.
|
|
1335 |
"""
|
|
1336 |
self.add_nodes(((key, value, references), )) |
|
1337 |
||
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1338 |
def _strip_prefix(self, an_iter): |
1339 |
"""Strip prefix data from nodes and return it."""
|
|
1340 |
for node in an_iter: |
|
1341 |
# cross checks
|
|
2624.2.17
by Robert Collins
Review feedback. |
1342 |
if node[1][:self.prefix_len] != self.prefix: |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1343 |
raise errors.BadIndexData(self) |
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1344 |
for ref_list in node[3]: |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1345 |
for ref_node in ref_list: |
2624.2.17
by Robert Collins
Review feedback. |
1346 |
if ref_node[:self.prefix_len] != self.prefix: |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1347 |
raise errors.BadIndexData(self) |
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1348 |
yield node[0], node[1][self.prefix_len:], node[2], ( |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1349 |
tuple(tuple(ref_node[self.prefix_len:] for ref_node in ref_list) |
2624.2.14
by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data. |
1350 |
for ref_list in node[3])) |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1351 |
|
1352 |
def iter_all_entries(self): |
|
1353 |
"""Iterate over all keys within the index
|
|
1354 |
||
1355 |
iter_all_entries is implemented against the adapted index using
|
|
1356 |
iter_entries_prefix.
|
|
1357 |
||
2592.5.1
by Martin Pool
Fix docstrings for Index.iter_entries etc |
1358 |
:return: An iterable of (index, key, reference_lists, value). There is no
|
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1359 |
defined order for the result iteration - it will be in the most
|
1360 |
efficient order for the index (in this case dictionary hash order).
|
|
1361 |
"""
|
|
2624.2.19
by Robert Collins
Why we should always test before committing. |
1362 |
return self._strip_prefix(self.adapted.iter_entries_prefix([self.prefix_key])) |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1363 |
|
1364 |
def iter_entries(self, keys): |
|
1365 |
"""Iterate over keys within the index.
|
|
1366 |
||
1367 |
:param keys: An iterable providing the keys to be retrieved.
|
|
2979.2.4
by Robert Collins
Docstring fixes from review. |
1368 |
:return: An iterable of (index, key, value, reference_lists). There is no
|
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1369 |
defined order for the result iteration - it will be in the most
|
1370 |
efficient order for the index (keys iteration order in this case).
|
|
1371 |
"""
|
|
1372 |
return self._strip_prefix(self.adapted.iter_entries( |
|
2624.2.17
by Robert Collins
Review feedback. |
1373 |
self.prefix + key for key in keys)) |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1374 |
|
1375 |
def iter_entries_prefix(self, keys): |
|
1376 |
"""Iterate over keys within the index using prefix matching.
|
|
1377 |
||
1378 |
Prefix matching is applied within the tuple of a key, not to within
|
|
1379 |
the bytestring of each key element. e.g. if you have the keys ('foo',
|
|
1380 |
'bar'), ('foobar', 'gam') and do a prefix search for ('foo', None) then
|
|
1381 |
only the former key is returned.
|
|
1382 |
||
1383 |
:param keys: An iterable providing the key prefixes to be retrieved.
|
|
1384 |
Each key prefix takes the form of a tuple the length of a key, but
|
|
1385 |
with the last N elements 'None' rather than a regular bytestring.
|
|
1386 |
The first element cannot be 'None'.
|
|
1387 |
:return: An iterable as per iter_all_entries, but restricted to the
|
|
1388 |
keys with a matching prefix to those supplied. No additional keys
|
|
1389 |
will be returned, and every match that is in the index will be
|
|
1390 |
returned.
|
|
1391 |
"""
|
|
1392 |
return self._strip_prefix(self.adapted.iter_entries_prefix( |
|
2624.2.17
by Robert Collins
Review feedback. |
1393 |
self.prefix + key for key in keys)) |
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1394 |
|
2624.2.16
by Robert Collins
Add a key_count method to GraphIndex and friends, allowing optimisation of length calculations by the index. |
1395 |
def key_count(self): |
1396 |
"""Return an estimate of the number of keys in this index.
|
|
1397 |
|
|
1398 |
For GraphIndexPrefixAdapter this is relatively expensive - key
|
|
1399 |
iteration with the prefix is done.
|
|
1400 |
"""
|
|
1401 |
return len(list(self.iter_all_entries())) |
|
1402 |
||
2624.2.12
by Robert Collins
Create an adapter between indices with differing key lengths. |
1403 |
def validate(self): |
1404 |
"""Call the adapted's validate."""
|
|
1405 |
self.adapted.validate() |