bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
1 |
# groupcompress, a bzr plugin providing new compression logic.
|
2 |
# Copyright (C) 2008 Canonical Limited.
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License version 2 as published
|
|
6 |
# by the Free Software Foundation.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
#
|
|
17 |
||
18 |
"""Core compression logic for compressing streams of related files."""
|
|
19 |
||
|
0.17.13
by Robert Collins
Do not output copy instructions which take more to encode than a fresh insert. (But do not refer to those insertions when finding ranges to copy: they are not interesting). |
20 |
from itertools import izip |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
21 |
from cStringIO import StringIO |
22 |
import zlib |
|
23 |
||
|
0.17.4
by Robert Collins
Annotate. |
24 |
from bzrlib import ( |
25 |
annotate, |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
26 |
debug, |
|
0.17.4
by Robert Collins
Annotate. |
27 |
diff, |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
28 |
errors, |
|
0.17.4
by Robert Collins
Annotate. |
29 |
graph as _mod_graph, |
30 |
pack, |
|
31 |
patiencediff, |
|
32 |
)
|
|
33 |
from bzrlib.graph import Graph |
|
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
34 |
from bzrlib.knit import _DirectPackAccess |
|
0.17.2
by Robert Collins
Core proof of concept working. |
35 |
from bzrlib.osutils import ( |
36 |
contains_whitespace, |
|
37 |
contains_linebreaks, |
|
38 |
sha_string, |
|
39 |
sha_strings, |
|
40 |
split_lines, |
|
41 |
)
|
|
|
0.17.21
by Robert Collins
Update groupcompress to bzrlib 1.10. |
42 |
from bzrlib.btree_index import BTreeBuilder |
|
0.17.24
by Robert Collins
Add a group cache to decompression, 5 times faster than knit at decompression when accessing everything in a group. |
43 |
from bzrlib.lru_cache import LRUSizeCache |
|
0.18.6
by John Arbash Meinel
Use the new EquivalenceTable to track the lines. |
44 |
from bzrlib.plugins.groupcompress import equivalence_table |
|
0.17.9
by Robert Collins
Initial stab at repository format support. |
45 |
from bzrlib.tsort import topo_sort |
|
0.17.2
by Robert Collins
Core proof of concept working. |
46 |
from bzrlib.versionedfile import ( |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
47 |
adapter_registry, |
48 |
AbsentContentFactory, |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
49 |
FulltextContentFactory, |
50 |
VersionedFiles, |
|
51 |
)
|
|
52 |
||
53 |
||
|
0.17.5
by Robert Collins
nograph tests completely passing. |
54 |
def parse(line_list): |
|
0.17.2
by Robert Collins
Core proof of concept working. |
55 |
result = [] |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
56 |
lines = iter(line_list) |
|
0.17.2
by Robert Collins
Core proof of concept working. |
57 |
next = lines.next |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
58 |
label_line = lines.next() |
59 |
sha1_line = lines.next() |
|
60 |
if (not label_line.startswith('label: ') or |
|
61 |
not sha1_line.startswith('sha1: ')): |
|
62 |
raise AssertionError("bad text record %r" % lines) |
|
63 |
label = tuple(label_line[7:-1].split('\x00')) |
|
64 |
sha1 = sha1_line[6:-1] |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
65 |
for header in lines: |
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
66 |
op = header[0] |
67 |
numbers = header[2:] |
|
68 |
numbers = [int(n) for n in header[2:].split(',')] |
|
69 |
if op == 'c': |
|
70 |
result.append((op, numbers[0], numbers[1], None)) |
|
71 |
else: |
|
72 |
contents = [next() for i in xrange(numbers[0])] |
|
73 |
result.append((op, None, numbers[0], contents)) |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
74 |
return label, sha1, result |
|
0.17.2
by Robert Collins
Core proof of concept working. |
75 |
|
76 |
def apply_delta(basis, delta): |
|
77 |
"""Apply delta to this object to become new_version_id.""" |
|
78 |
lines = [] |
|
79 |
last_offset = 0 |
|
80 |
# eq ranges occur where gaps occur
|
|
81 |
# start, end refer to offsets in basis
|
|
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
82 |
for op, start, count, delta_lines in delta: |
83 |
if op == 'c': |
|
|
0.17.12
by Robert Collins
Encode copy ranges as bytes not lines, halves decode overhead. |
84 |
lines.append(basis[start:start+count]) |
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
85 |
else: |
86 |
lines.extend(delta_lines) |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
87 |
trim_encoding_newline(lines) |
88 |
return lines |
|
89 |
||
90 |
||
91 |
def trim_encoding_newline(lines): |
|
92 |
if lines[-1] == '\n': |
|
93 |
del lines[-1] |
|
94 |
else: |
|
95 |
lines[-1] = lines[-1][:-1] |
|
96 |
||
97 |
||
98 |
class GroupCompressor(object): |
|
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
99 |
"""Produce a serialised group of compressed texts. |
100 |
|
|
101 |
It contains code very similar to SequenceMatcher because of having a similar
|
|
102 |
task. However some key differences apply:
|
|
103 |
- there is no junk, we want a minimal edit not a human readable diff.
|
|
104 |
- we don't filter very common lines (because we don't know where a good
|
|
105 |
range will start, and after the first text we want to be emitting minmal
|
|
106 |
edits only.
|
|
107 |
- we chain the left side, not the right side
|
|
108 |
- we incrementally update the adjacency matrix as new lines are provided.
|
|
109 |
- we look for matches in all of the left side, so the routine which does
|
|
110 |
the analagous task of find_longest_match does not need to filter on the
|
|
111 |
left side.
|
|
112 |
"""
|
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
113 |
|
|
0.18.14
by John Arbash Meinel
A bit more work, not really usable yet. |
114 |
_equivalence_table_class = equivalence_table.EquivalenceTable |
115 |
||
|
0.17.2
by Robert Collins
Core proof of concept working. |
116 |
def __init__(self, delta=True): |
117 |
"""Create a GroupCompressor. |
|
118 |
||
119 |
:paeam delta: If False, do not compress records.
|
|
120 |
"""
|
|
121 |
self._delta = delta |
|
|
0.17.12
by Robert Collins
Encode copy ranges as bytes not lines, halves decode overhead. |
122 |
self.line_offsets = [] |
|
0.17.2
by Robert Collins
Core proof of concept working. |
123 |
self.endpoint = 0 |
124 |
self.input_bytes = 0 |
|
|
0.18.14
by John Arbash Meinel
A bit more work, not really usable yet. |
125 |
self.line_locations = self._equivalence_table_class([]) |
|
0.18.9
by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right |
126 |
self.lines = self.line_locations.lines |
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
127 |
self.labels_deltas = {} |
|
0.17.2
by Robert Collins
Core proof of concept working. |
128 |
|
|
0.17.15
by Robert Collins
Factor out a get_matching_blocks style function. |
129 |
def get_matching_blocks(self, lines): |
130 |
"""Return an the ranges in lines which match self.lines. |
|
131 |
||
132 |
:param lines: lines to compress
|
|
133 |
:return: A list of (old_start, new_start, length) tuples which reflect
|
|
134 |
a region in self.lines that is present in lines. The last element
|
|
135 |
of the list is always (old_len, new_len, 0) to provide a end point
|
|
136 |
for generating instructions from the matching blocks list.
|
|
137 |
"""
|
|
138 |
result = [] |
|
139 |
pos = 0 |
|
140 |
line_locations = self.line_locations |
|
|
0.18.11
by John Arbash Meinel
Convert back into grabbing a right-lines ahead of time. |
141 |
line_locations.set_right_lines(lines) |
|
0.17.15
by Robert Collins
Factor out a get_matching_blocks style function. |
142 |
# We either copy a range (while there are reusable lines) or we
|
143 |
# insert new lines. To find reusable lines we traverse
|
|
|
0.18.24
by John Arbash Meinel
Factor out the most compute intensive portion, with plans to turn it into a compiled func. |
144 |
locations = None |
|
0.18.26
by John Arbash Meinel
Start with a copy implementation of the _get_longest_match function. |
145 |
max_pos = len(lines) |
|
0.18.31
by John Arbash Meinel
We had a small bug when we had to rebuild the hash, as we would forget about the non-indexed entries. |
146 |
max_time = 0.0 |
147 |
max_info = None |
|
|
0.18.36
by John Arbash Meinel
Small tweak makes a big difference on inventory.py, minor otherwise. |
148 |
result_append = result.append |
|
0.18.26
by John Arbash Meinel
Start with a copy implementation of the _get_longest_match function. |
149 |
while pos < max_pos: |
|
0.18.35
by John Arbash Meinel
remove the timing calls |
150 |
block, pos, locations = _get_longest_match(line_locations, pos, |
151 |
max_pos, locations) |
|
|
0.18.25
by John Arbash Meinel
Factor the get_longest_match into a helper func |
152 |
if block is not None: |
|
0.18.36
by John Arbash Meinel
Small tweak makes a big difference on inventory.py, minor otherwise. |
153 |
result_append(block) |
154 |
result_append((len(self.lines), len(lines), 0)) |
|
|
0.17.15
by Robert Collins
Factor out a get_matching_blocks style function. |
155 |
return result |
156 |
||
|
0.17.2
by Robert Collins
Core proof of concept working. |
157 |
def compress(self, key, lines, expected_sha): |
158 |
"""Compress lines with label key. |
|
159 |
||
160 |
:param key: A key tuple. It is stored in the output
|
|
|
0.17.26
by Robert Collins
Working better --gc-plain-chk. |
161 |
for identification of the text during decompression. If the last
|
162 |
element is 'None' it is replaced with the sha1 of the text -
|
|
163 |
e.g. sha1:xxxxxxx.
|
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
164 |
:param lines: The lines to be compressed. Must be split
|
165 |
on \n, with the \n preserved.'
|
|
166 |
:param expected_sha: If non-None, the sha the lines are blieved to
|
|
167 |
have. During compression the sha is calculated; a mismatch will
|
|
168 |
cause an error.
|
|
169 |
:return: The sha1 of lines, and the number of bytes accumulated in
|
|
170 |
the group output so far.
|
|
171 |
"""
|
|
172 |
sha1 = sha_strings(lines) |
|
|
0.17.26
by Robert Collins
Working better --gc-plain-chk. |
173 |
if key[-1] is None: |
174 |
key = key[:-1] + ('sha1:' + sha1,) |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
175 |
label = '\x00'.join(key) |
176 |
# setup good encoding for trailing \n support.
|
|
177 |
if not lines or lines[-1].endswith('\n'): |
|
178 |
lines.append('\n') |
|
179 |
else: |
|
180 |
lines[-1] = lines[-1] + '\n' |
|
181 |
new_lines = [] |
|
182 |
new_lines.append('label: %s\n' % label) |
|
183 |
new_lines.append('sha1: %s\n' % sha1) |
|
|
0.17.13
by Robert Collins
Do not output copy instructions which take more to encode than a fresh insert. (But do not refer to those insertions when finding ranges to copy: they are not interesting). |
184 |
index_lines = [False, False] |
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
185 |
pos = 0 |
|
0.17.14
by Robert Collins
Cleaner code. |
186 |
range_len = 0 |
187 |
range_start = 0 |
|
188 |
flush_range = self.flush_range |
|
189 |
copy_ends = None |
|
|
0.17.15
by Robert Collins
Factor out a get_matching_blocks style function. |
190 |
blocks = self.get_matching_blocks(lines) |
191 |
current_pos = 0 |
|
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
192 |
# We either copy a range (while there are reusable lines) or we
|
193 |
# insert new lines. To find reusable lines we traverse
|
|
|
0.17.15
by Robert Collins
Factor out a get_matching_blocks style function. |
194 |
for old_start, new_start, range_len in blocks: |
195 |
if new_start != current_pos: |
|
196 |
# non-matching region
|
|
|
0.19.1
by Robert Collins
Start to simplify flush_range. |
197 |
flush_range(current_pos, None, new_start - current_pos, |
|
0.17.15
by Robert Collins
Factor out a get_matching_blocks style function. |
198 |
lines, new_lines, index_lines) |
199 |
current_pos = new_start + range_len |
|
200 |
if not range_len: |
|
201 |
continue
|
|
|
0.19.1
by Robert Collins
Start to simplify flush_range. |
202 |
flush_range(new_start, old_start, range_len, lines, |
203 |
new_lines, index_lines) |
|
|
0.18.9
by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right |
204 |
delta_start = (self.endpoint, len(self.lines)) |
|
0.17.13
by Robert Collins
Do not output copy instructions which take more to encode than a fresh insert. (But do not refer to those insertions when finding ranges to copy: they are not interesting). |
205 |
self.output_lines(new_lines, index_lines) |
|
0.17.2
by Robert Collins
Core proof of concept working. |
206 |
trim_encoding_newline(lines) |
207 |
self.input_bytes += sum(map(len, lines)) |
|
|
0.18.9
by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right |
208 |
delta_end = (self.endpoint, len(self.lines)) |
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
209 |
self.labels_deltas[key] = (delta_start, delta_end) |
|
0.17.2
by Robert Collins
Core proof of concept working. |
210 |
return sha1, self.endpoint |
211 |
||
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
212 |
def extract(self, key): |
|
0.17.12
by Robert Collins
Encode copy ranges as bytes not lines, halves decode overhead. |
213 |
"""Extract a key previously added to the compressor. |
214 |
|
|
215 |
:param key: The key to extract.
|
|
216 |
:return: An iterable over bytes and the sha1.
|
|
217 |
"""
|
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
218 |
delta_details = self.labels_deltas[key] |
219 |
delta_lines = self.lines[delta_details[0][1]:delta_details[1][1]] |
|
220 |
label, sha1, delta = parse(delta_lines) |
|
221 |
if label != key: |
|
222 |
raise AssertionError("wrong key: %r, wanted %r" % (label, key)) |
|
|
0.17.12
by Robert Collins
Encode copy ranges as bytes not lines, halves decode overhead. |
223 |
# Perhaps we want to keep the line offsets too in memory at least?
|
224 |
lines = apply_delta(''.join(self.lines), delta) |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
225 |
sha1 = sha_strings(lines) |
226 |
return lines, sha1 |
|
227 |
||
|
0.19.1
by Robert Collins
Start to simplify flush_range. |
228 |
def flush_range(self, range_start, copy_start, range_len, lines, new_lines, index_lines): |
|
0.17.14
by Robert Collins
Cleaner code. |
229 |
insert_instruction = "i,%d\n" % range_len |
|
0.19.1
by Robert Collins
Start to simplify flush_range. |
230 |
if copy_start is not None: |
|
0.17.14
by Robert Collins
Cleaner code. |
231 |
# range stops, flush and start a new copy range
|
232 |
stop_byte = self.line_offsets[copy_start + range_len - 1] |
|
233 |
if copy_start == 0: |
|
234 |
start_byte = 0 |
|
235 |
else: |
|
236 |
start_byte = self.line_offsets[copy_start - 1] |
|
237 |
bytes = stop_byte - start_byte |
|
238 |
copy_control_instruction = "c,%d,%d\n" % (start_byte, bytes) |
|
239 |
if (bytes + len(insert_instruction) > |
|
240 |
len(copy_control_instruction)): |
|
241 |
new_lines.append(copy_control_instruction) |
|
242 |
index_lines.append(False) |
|
243 |
return
|
|
244 |
# not copying, or inserting is shorter than copying, so insert.
|
|
245 |
new_lines.append(insert_instruction) |
|
246 |
new_lines.extend(lines[range_start:range_start+range_len]) |
|
247 |
index_lines.append(False) |
|
|
0.19.1
by Robert Collins
Start to simplify flush_range. |
248 |
index_lines.extend([copy_start is None]*range_len) |
|
0.17.14
by Robert Collins
Cleaner code. |
249 |
|
|
0.17.13
by Robert Collins
Do not output copy instructions which take more to encode than a fresh insert. (But do not refer to those insertions when finding ranges to copy: they are not interesting). |
250 |
def output_lines(self, new_lines, index_lines): |
251 |
"""Output some lines. |
|
252 |
||
253 |
:param new_lines: The lines to output.
|
|
254 |
:param index_lines: A boolean flag for each line - when True, index
|
|
255 |
that line.
|
|
256 |
"""
|
|
|
0.18.31
by John Arbash Meinel
We had a small bug when we had to rebuild the hash, as we would forget about the non-indexed entries. |
257 |
# indexed_newlines = [idx for idx, val in enumerate(index_lines)
|
258 |
# if val and new_lines[idx] == '\n']
|
|
259 |
# if indexed_newlines:
|
|
260 |
# import pdb; pdb.set_trace()
|
|
|
0.17.12
by Robert Collins
Encode copy ranges as bytes not lines, halves decode overhead. |
261 |
endpoint = self.endpoint |
|
0.18.9
by John Arbash Meinel
If we are going to do it this way, we don't need to explicitly distinguish left and right |
262 |
self.line_locations.extend_lines(new_lines, index_lines) |
|
0.18.6
by John Arbash Meinel
Use the new EquivalenceTable to track the lines. |
263 |
for line in new_lines: |
|
0.17.12
by Robert Collins
Encode copy ranges as bytes not lines, halves decode overhead. |
264 |
endpoint += len(line) |
265 |
self.line_offsets.append(endpoint) |
|
266 |
self.endpoint = endpoint |
|
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
267 |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
268 |
def ratio(self): |
269 |
"""Return the overall compression ratio.""" |
|
270 |
return float(self.input_bytes) / float(self.endpoint) |
|
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
271 |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
272 |
|
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
273 |
def make_pack_factory(graph, delta, keylength): |
274 |
"""Create a factory for creating a pack based groupcompress. |
|
275 |
||
276 |
This is only functional enough to run interface tests, it doesn't try to
|
|
277 |
provide a full pack environment.
|
|
278 |
|
|
279 |
:param graph: Store a graph.
|
|
280 |
:param delta: Delta compress contents.
|
|
281 |
:param keylength: How long should keys be.
|
|
282 |
"""
|
|
283 |
def factory(transport): |
|
284 |
parents = graph or delta |
|
285 |
ref_length = 0 |
|
286 |
if graph: |
|
287 |
ref_length += 1 |
|
|
0.17.7
by Robert Collins
Update for current index2 changes. |
288 |
graph_index = BTreeBuilder(reference_lists=ref_length, |
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
289 |
key_elements=keylength) |
290 |
stream = transport.open_write_stream('newpack') |
|
291 |
writer = pack.ContainerWriter(stream.write) |
|
292 |
writer.begin() |
|
293 |
index = _GCGraphIndex(graph_index, lambda:True, parents=parents, |
|
|
0.17.9
by Robert Collins
Initial stab at repository format support. |
294 |
add_callback=graph_index.add_nodes) |
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
295 |
access = _DirectPackAccess({}) |
296 |
access.set_writer(writer, graph_index, (transport, 'newpack')) |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
297 |
result = GroupCompressVersionedFiles(index, access, delta) |
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
298 |
result.stream = stream |
299 |
result.writer = writer |
|
300 |
return result |
|
301 |
return factory |
|
302 |
||
303 |
||
304 |
def cleanup_pack_group(versioned_files): |
|
|
0.17.23
by Robert Collins
Only decompress as much of the zlib data as is needed to read the text recipe. |
305 |
versioned_files.writer.end() |
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
306 |
versioned_files.stream.close() |
307 |
||
308 |
||
309 |
class GroupCompressVersionedFiles(VersionedFiles): |
|
310 |
"""A group-compress based VersionedFiles implementation.""" |
|
311 |
||
|
0.17.2
by Robert Collins
Core proof of concept working. |
312 |
def __init__(self, index, access, delta=True): |
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
313 |
"""Create a GroupCompressVersionedFiles object. |
314 |
||
315 |
:param index: The index object storing access and graph data.
|
|
316 |
:param access: The access object storing raw data.
|
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
317 |
:param delta: Whether to delta compress or just entropy compress.
|
318 |
"""
|
|
319 |
self._index = index |
|
320 |
self._access = access |
|
321 |
self._delta = delta |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
322 |
self._unadded_refs = {} |
|
0.17.24
by Robert Collins
Add a group cache to decompression, 5 times faster than knit at decompression when accessing everything in a group. |
323 |
self._group_cache = LRUSizeCache(max_size=50*1024*1024) |
|
0.17.2
by Robert Collins
Core proof of concept working. |
324 |
|
325 |
def add_lines(self, key, parents, lines, parent_texts=None, |
|
326 |
left_matching_blocks=None, nostore_sha=None, random_id=False, |
|
327 |
check_content=True): |
|
328 |
"""Add a text to the store. |
|
329 |
||
330 |
:param key: The key tuple of the text to add.
|
|
331 |
:param parents: The parents key tuples of the text to add.
|
|
332 |
:param lines: A list of lines. Each line must be a bytestring. And all
|
|
333 |
of them except the last must be terminated with \n and contain no
|
|
334 |
other \n's. The last line may either contain no \n's or a single
|
|
335 |
terminating \n. If the lines list does meet this constraint the add
|
|
336 |
routine may error or may succeed - but you will be unable to read
|
|
337 |
the data back accurately. (Checking the lines have been split
|
|
338 |
correctly is expensive and extremely unlikely to catch bugs so it
|
|
339 |
is not done at runtime unless check_content is True.)
|
|
340 |
:param parent_texts: An optional dictionary containing the opaque
|
|
341 |
representations of some or all of the parents of version_id to
|
|
342 |
allow delta optimisations. VERY IMPORTANT: the texts must be those
|
|
343 |
returned by add_lines or data corruption can be caused.
|
|
344 |
:param left_matching_blocks: a hint about which areas are common
|
|
345 |
between the text and its left-hand-parent. The format is
|
|
346 |
the SequenceMatcher.get_matching_blocks format.
|
|
347 |
:param nostore_sha: Raise ExistingContent and do not add the lines to
|
|
348 |
the versioned file if the digest of the lines matches this.
|
|
349 |
:param random_id: If True a random id has been selected rather than
|
|
350 |
an id determined by some deterministic process such as a converter
|
|
351 |
from a foreign VCS. When True the backend may choose not to check
|
|
352 |
for uniqueness of the resulting key within the versioned file, so
|
|
353 |
this should only be done when the result is expected to be unique
|
|
354 |
anyway.
|
|
355 |
:param check_content: If True, the lines supplied are verified to be
|
|
356 |
bytestrings that are correctly formed lines.
|
|
357 |
:return: The text sha1, the number of bytes in the text, and an opaque
|
|
358 |
representation of the inserted version which can be provided
|
|
359 |
back to future add_lines calls in the parent_texts dictionary.
|
|
360 |
"""
|
|
361 |
self._index._check_write_ok() |
|
362 |
self._check_add(key, lines, random_id, check_content) |
|
363 |
if parents is None: |
|
364 |
# The caller might pass None if there is no graph data, but kndx
|
|
365 |
# indexes can't directly store that, so we give them
|
|
366 |
# an empty tuple instead.
|
|
367 |
parents = () |
|
368 |
# double handling for now. Make it work until then.
|
|
369 |
bytes = ''.join(lines) |
|
370 |
record = FulltextContentFactory(key, parents, None, bytes) |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
371 |
sha1 = list(self._insert_record_stream([record], random_id=random_id))[0] |
|
0.17.2
by Robert Collins
Core proof of concept working. |
372 |
return sha1, len(bytes), None |
373 |
||
|
0.17.4
by Robert Collins
Annotate. |
374 |
def annotate(self, key): |
375 |
"""See VersionedFiles.annotate.""" |
|
376 |
graph = Graph(self) |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
377 |
parent_map = self.get_parent_map([key]) |
378 |
if not parent_map: |
|
379 |
raise errors.RevisionNotPresent(key, self) |
|
380 |
if parent_map[key] is not None: |
|
381 |
search = graph._make_breadth_first_searcher([key]) |
|
382 |
keys = set() |
|
383 |
while True: |
|
384 |
try: |
|
385 |
present, ghosts = search.next_with_ghosts() |
|
386 |
except StopIteration: |
|
387 |
break
|
|
388 |
keys.update(present) |
|
389 |
parent_map = self.get_parent_map(keys) |
|
390 |
else: |
|
391 |
keys = [key] |
|
392 |
parent_map = {key:()} |
|
|
0.17.4
by Robert Collins
Annotate. |
393 |
head_cache = _mod_graph.FrozenHeadsCache(graph) |
394 |
parent_cache = {} |
|
395 |
reannotate = annotate.reannotate |
|
396 |
for record in self.get_record_stream(keys, 'topological', True): |
|
397 |
key = record.key |
|
398 |
fulltext = split_lines(record.get_bytes_as('fulltext')) |
|
399 |
parent_lines = [parent_cache[parent] for parent in parent_map[key]] |
|
400 |
parent_cache[key] = list( |
|
401 |
reannotate(parent_lines, fulltext, key, None, head_cache)) |
|
402 |
return parent_cache[key] |
|
403 |
||
|
0.17.5
by Robert Collins
nograph tests completely passing. |
404 |
def check(self, progress_bar=None): |
405 |
"""See VersionedFiles.check().""" |
|
406 |
keys = self.keys() |
|
407 |
for record in self.get_record_stream(keys, 'unordered', True): |
|
408 |
record.get_bytes_as('fulltext') |
|
409 |
||
|
0.17.2
by Robert Collins
Core proof of concept working. |
410 |
def _check_add(self, key, lines, random_id, check_content): |
411 |
"""check that version_id and lines are safe to add.""" |
|
412 |
version_id = key[-1] |
|
|
0.17.26
by Robert Collins
Working better --gc-plain-chk. |
413 |
if version_id is not None: |
414 |
if contains_whitespace(version_id): |
|
415 |
raise InvalidRevisionId(version_id, self) |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
416 |
self.check_not_reserved_id(version_id) |
417 |
# TODO: If random_id==False and the key is already present, we should
|
|
418 |
# probably check that the existing content is identical to what is
|
|
419 |
# being inserted, and otherwise raise an exception. This would make
|
|
420 |
# the bundle code simpler.
|
|
421 |
if check_content: |
|
422 |
self._check_lines_not_unicode(lines) |
|
423 |
self._check_lines_are_lines(lines) |
|
424 |
||
|
0.17.5
by Robert Collins
nograph tests completely passing. |
425 |
def get_parent_map(self, keys): |
426 |
"""Get a map of the parents of keys. |
|
427 |
||
428 |
:param keys: The keys to look up parents for.
|
|
429 |
:return: A mapping from keys to parents. Absent keys are absent from
|
|
430 |
the mapping.
|
|
431 |
"""
|
|
432 |
result = {} |
|
433 |
sources = [self._index] |
|
434 |
source_results = [] |
|
435 |
missing = set(keys) |
|
436 |
for source in sources: |
|
437 |
if not missing: |
|
438 |
break
|
|
439 |
new_result = source.get_parent_map(missing) |
|
440 |
source_results.append(new_result) |
|
441 |
result.update(new_result) |
|
442 |
missing.difference_update(set(new_result)) |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
443 |
if self._unadded_refs: |
444 |
for key in missing: |
|
445 |
if key in self._unadded_refs: |
|
446 |
result[key] = self._unadded_refs[key] |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
447 |
return result |
448 |
||
449 |
def get_record_stream(self, keys, ordering, include_delta_closure): |
|
450 |
"""Get a stream of records for keys. |
|
451 |
||
452 |
:param keys: The keys to include.
|
|
453 |
:param ordering: Either 'unordered' or 'topological'. A topologically
|
|
454 |
sorted stream has compression parents strictly before their
|
|
455 |
children.
|
|
456 |
:param include_delta_closure: If True then the closure across any
|
|
457 |
compression parents will be included (in the opaque data).
|
|
458 |
:return: An iterator of ContentFactory objects, each of which is only
|
|
459 |
valid until the iterator is advanced.
|
|
460 |
"""
|
|
461 |
# keys might be a generator
|
|
462 |
keys = set(keys) |
|
463 |
if not keys: |
|
464 |
return
|
|
465 |
if not self._index.has_graph: |
|
466 |
# Cannot topological order when no graph has been stored.
|
|
467 |
ordering = 'unordered' |
|
468 |
# Cheap: iterate
|
|
469 |
locations = self._index.get_build_details(keys) |
|
470 |
if ordering == 'topological': |
|
471 |
# would be better to not globally sort initially but instead
|
|
472 |
# start with one key, recurse to its oldest parent, then grab
|
|
473 |
# everything in the same group, etc.
|
|
474 |
parent_map = dict((key, details[2]) for key, details in |
|
475 |
locations.iteritems()) |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
476 |
local = frozenset(keys).intersection(set(self._unadded_refs)) |
477 |
for key in local: |
|
478 |
parent_map[key] = self._unadded_refs[key] |
|
479 |
locations[key] = None |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
480 |
present_keys = topo_sort(parent_map) |
481 |
# Now group by source:
|
|
482 |
else: |
|
483 |
present_keys = locations.keys() |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
484 |
local = frozenset(keys).intersection(set(self._unadded_refs)) |
485 |
for key in local: |
|
486 |
present_keys.append(key) |
|
487 |
locations[key] = None |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
488 |
absent_keys = keys.difference(set(locations)) |
489 |
for key in absent_keys: |
|
490 |
yield AbsentContentFactory(key) |
|
491 |
for key in present_keys: |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
492 |
if key in self._unadded_refs: |
493 |
lines, sha1 = self._compressor.extract(key) |
|
494 |
parents = self._unadded_refs[key] |
|
495 |
else: |
|
496 |
index_memo, _, parents, (method, _) = locations[key] |
|
497 |
read_memo = index_memo[0:3] |
|
|
0.17.24
by Robert Collins
Add a group cache to decompression, 5 times faster than knit at decompression when accessing everything in a group. |
498 |
# get the group:
|
499 |
try: |
|
500 |
plain = self._group_cache[read_memo] |
|
501 |
except KeyError: |
|
502 |
# read the group
|
|
503 |
zdata = self._access.get_raw_records([read_memo]).next() |
|
504 |
# decompress - whole thing - this is not a bug, as it
|
|
505 |
# permits caching. We might want to store the partially
|
|
506 |
# decompresed group and decompress object, so that recent
|
|
507 |
# texts are not penalised by big groups.
|
|
508 |
decomp = zlib.decompressobj() |
|
509 |
plain = decomp.decompress(zdata) #, index_memo[4]) |
|
510 |
self._group_cache[read_memo] = plain |
|
|
0.17.23
by Robert Collins
Only decompress as much of the zlib data as is needed to read the text recipe. |
511 |
# cheapo debugging:
|
512 |
# print len(zdata), len(plain)
|
|
|
0.17.24
by Robert Collins
Add a group cache to decompression, 5 times faster than knit at decompression when accessing everything in a group. |
513 |
# parse - requires split_lines, better to have byte offsets
|
514 |
# here (but not by much - we only split the region for the
|
|
515 |
# recipe, and we often want to end up with lines anyway.
|
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
516 |
delta_lines = split_lines(plain[index_memo[3]:index_memo[4]]) |
517 |
label, sha1, delta = parse(delta_lines) |
|
518 |
if label != key: |
|
519 |
raise AssertionError("wrong key: %r, wanted %r" % (label, key)) |
|
|
0.17.24
by Robert Collins
Add a group cache to decompression, 5 times faster than knit at decompression when accessing everything in a group. |
520 |
lines = apply_delta(plain, delta) |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
521 |
bytes = ''.join(lines) |
522 |
yield FulltextContentFactory(key, parents, sha1, bytes) |
|
523 |
||
524 |
def get_sha1s(self, keys): |
|
525 |
"""See VersionedFiles.get_sha1s().""" |
|
526 |
result = {} |
|
527 |
for record in self.get_record_stream(keys, 'unordered', True): |
|
528 |
if record.sha1 != None: |
|
529 |
result[record.key] = record.sha1 |
|
530 |
else: |
|
531 |
if record.storage_kind != 'absent': |
|
532 |
result[record.key] == sha_string(record.get_bytes_as( |
|
533 |
'fulltext')) |
|
534 |
return result |
|
535 |
||
|
0.17.2
by Robert Collins
Core proof of concept working. |
536 |
def insert_record_stream(self, stream): |
537 |
"""Insert a record stream into this container. |
|
538 |
||
539 |
:param stream: A stream of records to insert.
|
|
540 |
:return: None
|
|
541 |
:seealso VersionedFiles.get_record_stream:
|
|
542 |
"""
|
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
543 |
for _ in self._insert_record_stream(stream): |
544 |
pass
|
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
545 |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
546 |
def _insert_record_stream(self, stream, random_id=False): |
|
0.17.2
by Robert Collins
Core proof of concept working. |
547 |
"""Internal core to insert a record stream into this container. |
548 |
||
549 |
This helper function has a different interface than insert_record_stream
|
|
550 |
to allow add_lines to be minimal, but still return the needed data.
|
|
551 |
||
552 |
:param stream: A stream of records to insert.
|
|
553 |
:return: An iterator over the sha1 of the inserted records.
|
|
554 |
:seealso insert_record_stream:
|
|
555 |
:seealso add_lines:
|
|
556 |
"""
|
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
557 |
def get_adapter(adapter_key): |
558 |
try: |
|
559 |
return adapters[adapter_key] |
|
560 |
except KeyError: |
|
561 |
adapter_factory = adapter_registry.get(adapter_key) |
|
562 |
adapter = adapter_factory(self) |
|
563 |
adapters[adapter_key] = adapter |
|
564 |
return adapter |
|
565 |
adapters = {} |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
566 |
# This will go up to fulltexts for gc to gc fetching, which isn't
|
567 |
# ideal.
|
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
568 |
self._compressor = GroupCompressor(self._delta) |
569 |
self._unadded_refs = {} |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
570 |
keys_to_add = [] |
571 |
basis_end = 0 |
|
|
0.17.6
by Robert Collins
Cap group size at 20MB internal buffer. (Probably way too big). |
572 |
groups = 1 |
573 |
def flush(): |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
574 |
compressed = zlib.compress(''.join(self._compressor.lines)) |
|
0.17.6
by Robert Collins
Cap group size at 20MB internal buffer. (Probably way too big). |
575 |
index, start, length = self._access.add_raw_records( |
576 |
[(None, len(compressed))], compressed)[0] |
|
577 |
nodes = [] |
|
578 |
for key, reads, refs in keys_to_add: |
|
579 |
nodes.append((key, "%d %d %s" % (start, length, reads), refs)) |
|
580 |
self._index.add_records(nodes, random_id=random_id) |
|
|
0.17.2
by Robert Collins
Core proof of concept working. |
581 |
for record in stream: |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
582 |
# Raise an error when a record is missing.
|
583 |
if record.storage_kind == 'absent': |
|
584 |
raise errors.RevisionNotPresent([record.key], self) |
|
585 |
elif record.storage_kind == 'fulltext': |
|
586 |
bytes = record.get_bytes_as('fulltext') |
|
587 |
else: |
|
588 |
adapter_key = record.storage_kind, 'fulltext' |
|
589 |
adapter = get_adapter(adapter_key) |
|
590 |
bytes = adapter.get_bytes(record, |
|
591 |
record.get_bytes_as(record.storage_kind)) |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
592 |
found_sha1, end_point = self._compressor.compress(record.key, |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
593 |
split_lines(bytes), record.sha1) |
|
0.17.26
by Robert Collins
Working better --gc-plain-chk. |
594 |
if record.key[-1] is None: |
595 |
key = record.key[:-1] + ('sha1:' + found_sha1,) |
|
596 |
else: |
|
597 |
key = record.key |
|
598 |
self._unadded_refs[key] = record.parents |
|
|
0.17.3
by Robert Collins
new encoder, allows non monotonically increasing sequence matches for moar compression. |
599 |
yield found_sha1 |
|
0.17.26
by Robert Collins
Working better --gc-plain-chk. |
600 |
keys_to_add.append((key, '%d %d' % (basis_end, end_point), |
|
0.17.5
by Robert Collins
nograph tests completely passing. |
601 |
(record.parents,))) |
602 |
basis_end = end_point |
|
|
0.17.6
by Robert Collins
Cap group size at 20MB internal buffer. (Probably way too big). |
603 |
if basis_end > 1024 * 1024 * 20: |
604 |
flush() |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
605 |
self._compressor = GroupCompressor(self._delta) |
606 |
self._unadded_refs = {} |
|
|
0.17.6
by Robert Collins
Cap group size at 20MB internal buffer. (Probably way too big). |
607 |
keys_to_add = [] |
608 |
basis_end = 0 |
|
609 |
groups += 1 |
|
|
0.17.8
by Robert Collins
Flush pending updates at the end of _insert_record_stream |
610 |
if len(keys_to_add): |
611 |
flush() |
|
|
0.17.11
by Robert Collins
Add extraction of just-compressed texts to support converting from knits. |
612 |
self._compressor = None |
613 |
self._unadded_refs = {} |
|
|
0.17.5
by Robert Collins
nograph tests completely passing. |
614 |
|
615 |
def iter_lines_added_or_present_in_keys(self, keys, pb=None): |
|
616 |
"""Iterate over the lines in the versioned files from keys. |
|
617 |
||
618 |
This may return lines from other keys. Each item the returned
|
|
619 |
iterator yields is a tuple of a line and a text version that that line
|
|
620 |
is present in (not introduced in).
|
|
621 |
||
622 |
Ordering of results is in whatever order is most suitable for the
|
|
623 |
underlying storage format.
|
|
624 |
||
625 |
If a progress bar is supplied, it may be used to indicate progress.
|
|
626 |
The caller is responsible for cleaning up progress bars (because this
|
|
627 |
is an iterator).
|
|
628 |
||
629 |
NOTES:
|
|
630 |
* Lines are normalised by the underlying store: they will all have \n
|
|
631 |
terminators.
|
|
632 |
* Lines are returned in arbitrary order.
|
|
633 |
||
634 |
:return: An iterator over (line, key).
|
|
635 |
"""
|
|
636 |
if pb is None: |
|
637 |
pb = progress.DummyProgress() |
|
638 |
keys = set(keys) |
|
639 |
total = len(keys) |
|
640 |
# we don't care about inclusions, the caller cares.
|
|
641 |
# but we need to setup a list of records to visit.
|
|
642 |
# we need key, position, length
|
|
643 |
for key_idx, record in enumerate(self.get_record_stream(keys, |
|
644 |
'unordered', True)): |
|
645 |
# XXX: todo - optimise to use less than full texts.
|
|
646 |
key = record.key |
|
647 |
pb.update('Walking content.', key_idx, total) |
|
648 |
if record.storage_kind == 'absent': |
|
649 |
raise errors.RevisionNotPresent(record.key, self) |
|
650 |
lines = split_lines(record.get_bytes_as('fulltext')) |
|
651 |
for line in lines: |
|
652 |
yield line, key |
|
653 |
pb.update('Walking content.', total, total) |
|
654 |
||
655 |
def keys(self): |
|
656 |
"""See VersionedFiles.keys.""" |
|
657 |
if 'evil' in debug.debug_flags: |
|
658 |
trace.mutter_callsite(2, "keys scales with size of history") |
|
659 |
sources = [self._index] |
|
660 |
result = set() |
|
661 |
for source in sources: |
|
662 |
result.update(source.keys()) |
|
663 |
return result |
|
664 |
||
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
665 |
|
666 |
class _GCGraphIndex(object): |
|
667 |
"""Mapper from GroupCompressVersionedFiles needs into GraphIndex storage.""" |
|
668 |
||
|
0.17.9
by Robert Collins
Initial stab at repository format support. |
669 |
def __init__(self, graph_index, is_locked, parents=True, |
|
0.17.1
by Robert Collins
Starting point. Interface tests hooked up and failing. |
670 |
add_callback=None): |
671 |
"""Construct a _GCGraphIndex on a graph_index. |
|
672 |
||
673 |
:param graph_index: An implementation of bzrlib.index.GraphIndex.
|
|
674 |
:param is_locked: A callback to check whether the object should answer
|
|
675 |
queries.
|
|
676 |
:param parents: If True, record knits parents, if not do not record
|
|
677 |
parents.
|
|
678 |
:param add_callback: If not None, allow additions to the index and call
|
|
679 |
this callback with a list of added GraphIndex nodes:
|
|
680 |
[(node, value, node_refs), ...]
|
|
681 |
:param is_locked: A callback, returns True if the index is locked and
|
|
682 |
thus usable.
|
|
683 |
"""
|
|
684 |
self._add_callback = add_callback |
|
685 |
self._graph_index = graph_index |
|
686 |
self._parents = parents |
|
687 |
self.has_graph = parents |
|
688 |
self._is_locked = is_locked |
|
689 |
||
|
0.17.5
by Robert Collins
nograph tests completely passing. |
690 |
def add_records(self, records, random_id=False): |
691 |
"""Add multiple records to the index. |
|
692 |
|
|
693 |
This function does not insert data into the Immutable GraphIndex
|
|
694 |
backing the KnitGraphIndex, instead it prepares data for insertion by
|
|
695 |
the caller and checks that it is safe to insert then calls
|
|
696 |
self._add_callback with the prepared GraphIndex nodes.
|
|
697 |
||
698 |
:param records: a list of tuples:
|
|
699 |
(key, options, access_memo, parents).
|
|
700 |
:param random_id: If True the ids being added were randomly generated
|
|
701 |
and no check for existence will be performed.
|
|
702 |
"""
|
|
703 |
if not self._add_callback: |
|
704 |
raise errors.ReadOnlyError(self) |
|
705 |
# we hope there are no repositories with inconsistent parentage
|
|
706 |
# anymore.
|
|
707 |
||
708 |
changed = False |
|
709 |
keys = {} |
|
710 |
for (key, value, refs) in records: |
|
711 |
if not self._parents: |
|
712 |
if refs: |
|
713 |
for ref in refs: |
|
714 |
if ref: |
|
715 |
raise KnitCorrupt(self, |
|
716 |
"attempt to add node with parents "
|
|
717 |
"in parentless index.") |
|
718 |
refs = () |
|
719 |
changed = True |
|
720 |
keys[key] = (value, refs) |
|
721 |
# check for dups
|
|
722 |
if not random_id: |
|
723 |
present_nodes = self._get_entries(keys) |
|
724 |
for (index, key, value, node_refs) in present_nodes: |
|
725 |
if node_refs != keys[key][1]: |
|
726 |
raise errors.KnitCorrupt(self, "inconsistent details in add_records" |
|
727 |
": %s %s" % ((value, node_refs), keys[key])) |
|
728 |
del keys[key] |
|
729 |
changed = True |
|
730 |
if changed: |
|
731 |
result = [] |
|
732 |
if self._parents: |
|
733 |
for key, (value, node_refs) in keys.iteritems(): |
|
734 |
result.append((key, value, node_refs)) |
|
735 |
else: |
|
736 |
for key, (value, node_refs) in keys.iteritems(): |
|
737 |
result.append((key, value)) |
|
738 |
records = result |
|
739 |
self._add_callback(records) |
|
740 |
||
741 |
def _check_read(self): |
|
742 |
"""raise if reads are not permitted.""" |
|
743 |
if not self._is_locked(): |
|
744 |
raise errors.ObjectNotLocked(self) |
|
745 |
||
|
0.17.2
by Robert Collins
Core proof of concept working. |
746 |
def _check_write_ok(self): |
747 |
"""Assert if writes are not permitted.""" |
|
748 |
if not self._is_locked(): |
|
749 |
raise errors.ObjectNotLocked(self) |
|
750 |
||
|
0.17.5
by Robert Collins
nograph tests completely passing. |
751 |
def _get_entries(self, keys, check_present=False): |
752 |
"""Get the entries for keys. |
|
753 |
|
|
754 |
:param keys: An iterable of index key tuples.
|
|
755 |
"""
|
|
756 |
keys = set(keys) |
|
757 |
found_keys = set() |
|
758 |
if self._parents: |
|
759 |
for node in self._graph_index.iter_entries(keys): |
|
760 |
yield node |
|
761 |
found_keys.add(node[1]) |
|
762 |
else: |
|
763 |
# adapt parentless index to the rest of the code.
|
|
764 |
for node in self._graph_index.iter_entries(keys): |
|
765 |
yield node[0], node[1], node[2], () |
|
766 |
found_keys.add(node[1]) |
|
767 |
if check_present: |
|
768 |
missing_keys = keys.difference(found_keys) |
|
769 |
if missing_keys: |
|
770 |
raise RevisionNotPresent(missing_keys.pop(), self) |
|
771 |
||
772 |
def get_parent_map(self, keys): |
|
773 |
"""Get a map of the parents of keys. |
|
774 |
||
775 |
:param keys: The keys to look up parents for.
|
|
776 |
:return: A mapping from keys to parents. Absent keys are absent from
|
|
777 |
the mapping.
|
|
778 |
"""
|
|
779 |
self._check_read() |
|
780 |
nodes = self._get_entries(keys) |
|
781 |
result = {} |
|
782 |
if self._parents: |
|
783 |
for node in nodes: |
|
784 |
result[node[1]] = node[3][0] |
|
785 |
else: |
|
786 |
for node in nodes: |
|
787 |
result[node[1]] = None |
|
788 |
return result |
|
789 |
||
790 |
def get_build_details(self, keys): |
|
791 |
"""Get the various build details for keys. |
|
792 |
||
793 |
Ghosts are omitted from the result.
|
|
794 |
||
795 |
:param keys: An iterable of keys.
|
|
796 |
:return: A dict of key:
|
|
797 |
(index_memo, compression_parent, parents, record_details).
|
|
798 |
index_memo
|
|
799 |
opaque structure to pass to read_records to extract the raw
|
|
800 |
data
|
|
801 |
compression_parent
|
|
802 |
Content that this record is built upon, may be None
|
|
803 |
parents
|
|
804 |
Logical parents of this node
|
|
805 |
record_details
|
|
806 |
extra information about the content which needs to be passed to
|
|
807 |
Factory.parse_record
|
|
808 |
"""
|
|
809 |
self._check_read() |
|
810 |
result = {} |
|
811 |
entries = self._get_entries(keys, False) |
|
812 |
for entry in entries: |
|
813 |
key = entry[1] |
|
814 |
if not self._parents: |
|
815 |
parents = None |
|
816 |
else: |
|
817 |
parents = entry[3][0] |
|
818 |
value = entry[2] |
|
819 |
method = 'group' |
|
820 |
result[key] = (self._node_to_position(entry), |
|
821 |
None, parents, (method, None)) |
|
822 |
return result |
|
823 |
||
824 |
def keys(self): |
|
825 |
"""Get all the keys in the collection. |
|
826 |
|
|
827 |
The keys are not ordered.
|
|
828 |
"""
|
|
829 |
self._check_read() |
|
830 |
return [node[1] for node in self._graph_index.iter_all_entries()] |
|
831 |
||
832 |
def _node_to_position(self, node): |
|
833 |
"""Convert an index value to position details.""" |
|
834 |
bits = node[2].split(' ') |
|
835 |
# It would be nice not to read the entire gzip.
|
|
836 |
start = int(bits[0]) |
|
837 |
stop = int(bits[1]) |
|
838 |
basis_end = int(bits[2]) |
|
839 |
delta_end = int(bits[3]) |
|
840 |
return node[0], start, stop, basis_end, delta_end |
|
|
0.18.14
by John Arbash Meinel
A bit more work, not really usable yet. |
841 |
|
842 |
||
|
0.18.26
by John Arbash Meinel
Start with a copy implementation of the _get_longest_match function. |
843 |
def _get_longest_match(equivalence_table, pos, max_pos, locations): |
|
0.18.25
by John Arbash Meinel
Factor the get_longest_match into a helper func |
844 |
"""Get the longest possible match for the current position.""" |
845 |
range_start = pos |
|
846 |
range_len = 0 |
|
847 |
copy_ends = None |
|
|
0.18.26
by John Arbash Meinel
Start with a copy implementation of the _get_longest_match function. |
848 |
while pos < max_pos: |
|
0.18.25
by John Arbash Meinel
Factor the get_longest_match into a helper func |
849 |
if locations is None: |
850 |
locations = equivalence_table.get_idx_matches(pos) |
|
851 |
if locations is None: |
|
852 |
# No more matches, just return whatever we have, but we know that
|
|
853 |
# this last position is not going to match anything
|
|
854 |
pos += 1 |
|
855 |
break
|
|
856 |
else: |
|
857 |
if copy_ends is None: |
|
858 |
# We are starting a new range
|
|
859 |
copy_ends = [loc + 1 for loc in locations] |
|
860 |
range_len = 1 |
|
861 |
locations = None # Consumed |
|
862 |
else: |
|
863 |
# We are currently in the middle of a match
|
|
864 |
next_locations = set(copy_ends).intersection(locations) |
|
865 |
if len(next_locations): |
|
866 |
# range continues
|
|
867 |
copy_ends = [loc + 1 for loc in next_locations] |
|
868 |
range_len += 1 |
|
869 |
locations = None # Consumed |
|
870 |
else: |
|
871 |
# But we are done with this match, we should be
|
|
872 |
# starting a new one, though. We will pass back 'locations'
|
|
873 |
# so that we don't have to do another lookup.
|
|
874 |
break
|
|
875 |
pos += 1 |
|
876 |
if copy_ends is None: |
|
877 |
return None, pos, locations |
|
878 |
return ((min(copy_ends) - range_len, range_start, range_len)), pos, locations |
|
879 |
||
880 |
||
|
0.18.14
by John Arbash Meinel
A bit more work, not really usable yet. |
881 |
try: |
882 |
from bzrlib.plugins.groupcompress import _groupcompress_c |
|
883 |
except ImportError: |
|
884 |
pass
|
|
885 |
else: |
|
886 |
GroupCompressor._equivalence_table_class = _groupcompress_c.EquivalenceTable |
|
|
0.18.29
by John Arbash Meinel
Implement _get_longest_match in Pyrex. |
887 |
_get_longest_match = _groupcompress_c._get_longest_match |