144
144
def __init__(self):
145
145
self._should_strip_eol = False
148
"""Return a list of (origin, text) tuples."""
149
return list(self.annotate_iter())
151
147
def apply_delta(self, delta, new_version_id):
152
148
"""Apply delta to this object to become new_version_id."""
153
149
raise NotImplementedError(self.apply_delta)
206
202
KnitContent.__init__(self)
207
203
self._lines = lines
209
def annotate_iter(self):
210
"""Yield tuples of (origin, text) for each content line."""
211
return iter(self._lines)
206
"""Return a list of (origin, text) for each content line."""
207
return list(self._lines)
213
209
def apply_delta(self, delta, new_version_id):
214
210
"""Apply delta to this object to become new_version_id."""
256
252
self._lines = lines
257
253
self._version_id = version_id
259
def annotate_iter(self):
260
"""Yield tuples of (origin, text) for each content line."""
261
for line in self._lines:
262
yield self._version_id, line
256
"""Return a list of (origin, text) for each content line."""
257
return [(self._version_id, line) for line in self._lines]
264
259
def apply_delta(self, delta, new_version_id):
265
260
"""Apply delta to this object to become new_version_id."""
427
422
for origin, text in lines)
430
def annotate_iter(self, knit, version_id):
425
def annotate(self, knit, version_id):
431
426
content = knit._get_content(version_id)
432
return content.annotate_iter()
427
return content.annotate()
435
430
class KnitPlainFactory(_KnitFactory):
489
484
out.extend(lines)
492
def annotate_iter(self, knit, version_id):
487
def annotate(self, knit, version_id):
493
488
annotator = _KnitAnnotator(knit)
494
return iter(annotator.annotate(version_id))
489
return annotator.annotate(version_id)
497
492
def make_empty_knit(transport, relpath):
498
493
"""Construct a empty knit at the specified location."""
499
k = KnitVersionedFile(transport, relpath, 'w', KnitPlainFactory)
494
k = make_file_knit(transport, relpath, 'w', KnitPlainFactory)
497
def make_file_knit(name, transport, file_mode=None, access_mode='w',
498
factory=None, delta=True, create=False, create_parent_dir=False,
499
delay_create=False, dir_mode=None, get_scope=None):
500
"""Factory to create a KnitVersionedFile for a .knit/.kndx file pair."""
502
factory = KnitAnnotateFactory()
504
factory = KnitPlainFactory()
505
if get_scope is None:
506
get_scope = lambda:None
507
index = _KnitIndex(transport, name + INDEX_SUFFIX,
508
access_mode, create=create, file_mode=file_mode,
509
create_parent_dir=create_parent_dir, delay_create=delay_create,
510
dir_mode=dir_mode, get_scope=get_scope)
511
access = _KnitAccess(transport, name + DATA_SUFFIX, file_mode,
512
dir_mode, ((create and not len(index)) and delay_create),
514
return KnitVersionedFile(name, transport, factory=factory,
515
create=create, delay_create=delay_create, index=index,
516
access_method=access)
520
"""Return the suffixes used by file based knits."""
521
return [DATA_SUFFIX, INDEX_SUFFIX]
522
make_file_knit.get_suffixes = get_suffixes
502
525
class KnitVersionedFile(VersionedFile):
514
537
stored and retrieved.
517
def __init__(self, relpath, transport, file_mode=None, access_mode=None,
540
def __init__(self, relpath, transport, file_mode=None,
518
541
factory=None, delta=True, create=False, create_parent_dir=False,
519
542
delay_create=False, dir_mode=None, index=None, access_method=None):
520
543
"""Construct a knit at location specified by relpath.
527
550
actually be created until the first data is stored.
528
551
:param index: An index to use for the knit.
530
if access_mode is None:
532
super(KnitVersionedFile, self).__init__(access_mode)
533
assert access_mode in ('r', 'w'), "invalid mode specified %r" % access_mode
553
super(KnitVersionedFile, self).__init__()
534
554
self.transport = transport
535
555
self.filename = relpath
536
556
self.factory = factory or KnitAnnotateFactory()
537
self.writable = (access_mode == 'w')
538
557
self.delta = delta
540
559
self._max_delta_chain = 200
543
self._index = _KnitIndex(transport, relpath + INDEX_SUFFIX,
544
access_mode, create=create, file_mode=file_mode,
545
create_parent_dir=create_parent_dir, delay_create=delay_create,
549
if access_method is None:
550
_access = _KnitAccess(transport, relpath + DATA_SUFFIX, file_mode, dir_mode,
551
((create and not len(self)) and delay_create), create_parent_dir)
553
_access = access_method
561
if None in (access_method, index):
562
raise ValueError("No default access_method or index any more")
564
_access = access_method
554
565
if create and not len(self) and not delay_create:
556
567
self._data = _KnitData(_access)
589
600
return fulltext_size > delta_size
602
def _check_write_ok(self):
603
return self._index._check_write_ok()
591
605
def _add_raw_records(self, records, data):
592
606
"""Add all the records 'records' with data pre-joined in 'data'.
604
618
for (version_id, options, parents, size), access_memo in zip(
605
619
records, positions):
606
620
index_entries.append((version_id, options, access_memo, parents))
607
if self._data._do_cache:
608
self._data._cache[version_id] = data[offset:offset+size]
610
622
self._index.add_versions(index_entries)
612
def enable_cache(self):
613
"""Start caching data for this knit"""
614
self._data.enable_cache()
616
def clear_cache(self):
617
"""Clear the data cache only."""
618
self._data.clear_cache()
620
624
def copy_to(self, name, transport):
621
625
"""See VersionedFile.copy_to()."""
622
626
# copy the current index to a temp index to avoid racing with local
758
762
"""See VersionedFile.get_graph_with_ghosts()."""
759
763
return self.get_parent_map(self.versions())
761
def get_sha1(self, version_id):
762
return self.get_sha1s([version_id])[0]
764
765
def get_sha1s(self, version_ids):
765
"""See VersionedFile.get_sha1()."""
766
"""See VersionedFile.get_sha1s()."""
766
767
record_map = self._get_record_map(version_ids)
767
768
# record entry 2 is the 'digest'.
768
769
return [record_map[v][2] for v in version_ids]
772
"""See VersionedFile.get_suffixes()."""
773
return [DATA_SUFFIX, INDEX_SUFFIX]
775
771
@deprecated_method(one_four)
776
772
def has_ghost(self, version_id):
777
773
"""True if there is a ghost reference in the file to version_id."""
820
816
# Also check the SHA-1 of the fulltext this content will
822
818
raw_data = reader_callable(length)
823
my_fulltext_sha1 = self.get_sha1(version_id)
819
my_fulltext_sha1 = self.get_sha1s([version_id])[0]
824
820
df, rec = self._data._parse_record_header(version_id, raw_data)
825
821
stream_fulltext_sha1 = rec[3]
826
822
if my_fulltext_sha1 != stream_fulltext_sha1:
1090
1086
def check(self, progress_bar=None):
1091
1087
"""See VersionedFile.check()."""
1093
def _clone_text(self, new_version_id, old_version_id, parents):
1094
"""See VersionedFile.clone_text()."""
1095
# FIXME RBC 20060228 make fast by only inserting an index with null
1097
self.add_lines(new_version_id, parents, self.get_lines(old_version_id))
1099
1089
def get_lines(self, version_id):
1100
1090
"""See VersionedFile.get_lines()."""
1101
1091
return self.get_line_list([version_id])[0]
1240
1230
pb.update('Walking content.', total, total)
1242
def iter_parents(self, version_ids):
1243
"""Iterate through the parents for many version ids.
1245
:param version_ids: An iterable yielding version_ids.
1246
:return: An iterator that yields (version_id, parents). Requested
1247
version_ids not present in the versioned file are simply skipped.
1248
The order is undefined, allowing for different optimisations in
1249
the underlying implementation.
1251
return self._index.iter_parents(version_ids)
1253
1232
def num_versions(self):
1254
1233
"""See VersionedFile.num_versions()."""
1255
1234
return self._index.num_versions()
1257
1236
__len__ = num_versions
1259
def annotate_iter(self, version_id):
1260
"""See VersionedFile.annotate_iter."""
1261
return self.factory.annotate_iter(self, version_id)
1238
def annotate(self, version_id):
1239
"""See VersionedFile.annotate."""
1240
return self.factory.annotate(self, version_id)
1263
1242
def get_parent_map(self, version_ids):
1264
1243
"""See VersionedFile.get_parent_map."""
1382
def _check_write_ok(self):
1383
if self._get_scope() != self._scope:
1384
raise errors.OutSideTransaction()
1385
if self._mode != 'w':
1386
raise errors.ReadOnlyObjectDirtiedError(self)
1403
1388
def __init__(self, transport, filename, mode, create=False, file_mode=None,
1404
create_parent_dir=False, delay_create=False, dir_mode=None):
1389
create_parent_dir=False, delay_create=False, dir_mode=None,
1405
1391
_KnitComponentFile.__init__(self, transport, filename, mode,
1406
1392
file_mode=file_mode,
1407
1393
create_parent_dir=create_parent_dir,
1429
1415
self._transport.put_bytes_non_atomic(
1430
1416
self._filename, self.HEADER, mode=self._file_mode)
1417
self._scope = get_scope()
1418
self._get_scope = get_scope
1432
1420
def get_ancestry(self, versions, topo_sorted=True):
1433
1421
"""See VersionedFile.get_ancestry."""
1505
1493
parents, (method, noeol))
1508
def iter_parents(self, version_ids):
1509
"""Iterate through the parents for many version ids.
1511
:param version_ids: An iterable yielding version_ids.
1512
:return: An iterator that yields (version_id, parents). Requested
1513
version_ids not present in the versioned file are simply skipped.
1514
The order is undefined, allowing for different optimisations in
1515
the underlying implementation.
1517
parent_map = self.get_parent_map(version_ids)
1518
parent_map_set = set(parent_map)
1519
unknown_existence = set()
1520
for parents in parent_map.itervalues():
1521
unknown_existence.update(parents)
1522
unknown_existence.difference_update(parent_map_set)
1523
present_parents = set(self.get_parent_map(unknown_existence))
1524
present_parents.update(parent_map_set)
1525
for version_id, parents in parent_map.iteritems():
1526
parents = tuple(parent for parent in parents
1527
if parent in present_parents)
1528
yield version_id, parents
1530
1496
def num_versions(self):
1531
1497
return len(self._history)
1847
1816
return 'fulltext'
1849
def iter_parents(self, version_ids):
1850
"""Iterate through the parents for many version ids.
1852
:param version_ids: An iterable yielding version_ids.
1853
:return: An iterator that yields (version_id, parents). Requested
1854
version_ids not present in the versioned file are simply skipped.
1855
The order is undefined, allowing for different optimisations in
1856
the underlying implementation.
1859
all_nodes = set(self._get_entries(self._version_ids_to_keys(version_ids)))
1861
present_parents = set()
1862
for node in all_nodes:
1863
all_parents.update(node[3][0])
1864
# any node we are querying must be present
1865
present_parents.add(node[1])
1866
unknown_parents = all_parents.difference(present_parents)
1867
present_parents.update(self._present_keys(unknown_parents))
1868
for node in all_nodes:
1870
for parent in node[3][0]:
1871
if parent in present_parents:
1872
parents.append(parent[0])
1873
yield node[1][0], tuple(parents)
1875
for node in self._get_entries(self._version_ids_to_keys(version_ids)):
1876
yield node[1][0], ()
1878
1818
def num_versions(self):
1879
1819
return len(list(self._graph_index.iter_all_entries()))
2225
2165
def get_raw_records(self, memos_for_retrieval):
2226
2166
"""Get the raw bytes for a records.
2228
:param memos_for_retrieval: An iterable containing the (thunk_flag,
2229
index, start, end) memo for retrieving the bytes.
2230
:return: An iterator over the bytes of the records.
2168
:param memos_for_retrieval: An iterable of memos from the
2169
_StreamIndex object identifying bytes to read; for these classes
2170
they are (from_backing_knit, index, start, end) and can point to
2171
either the backing knit or streamed data.
2172
:return: An iterator yielding a byte string for each record in
2173
memos_for_retrieval.
2232
2175
# use a generator for memory friendliness
2233
for thunk_flag, version_id, start, end in memos_for_retrieval:
2234
if version_id is self.stream_index:
2176
for from_backing_knit, version_id, start, end in memos_for_retrieval:
2177
if not from_backing_knit:
2178
assert version_id is self.stream_index
2235
2179
yield self.data[start:end]
2237
2181
# we have been asked to thunk. This thunking only occurs when
2242
2186
# as desired. However, for now, this is sufficient.
2243
2187
if self.orig_factory.__class__ != KnitPlainFactory:
2244
2188
raise errors.KnitCorrupt(
2245
self, 'Bad thunk request %r' % version_id)
2189
self, 'Bad thunk request %r cannot be backed by %r' %
2190
(version_id, self.orig_factory))
2246
2191
lines = self.backing_knit.get_lines(version_id)
2247
2192
line_bytes = ''.join(lines)
2248
2193
digest = sha_string(line_bytes)
2194
# the packed form of the fulltext always has a trailing newline,
2195
# even if the actual text does not, unless the file is empty. the
2196
# record options including the noeol flag are passed through by
2197
# _StreamIndex, so this is safe.
2250
2199
if lines[-1][-1] != '\n':
2251
2200
lines[-1] = lines[-1] + '\n'
2252
2201
line_bytes += '\n'
2253
orig_options = list(self.backing_knit._index.get_options(version_id))
2254
if 'fulltext' not in orig_options:
2255
if 'line-delta' not in orig_options:
2256
raise errors.KnitCorrupt(self,
2257
'Unknown compression method %r' % orig_options)
2258
orig_options.remove('line-delta')
2259
orig_options.append('fulltext')
2260
2202
# We want plain data, because we expect to thunk only to allow text
2262
2204
size, bytes = self.backing_knit._data._record_to_data(version_id,
2314
2256
:return: A dict of version_id:(index_memo, compression_parent,
2315
2257
parents, record_details).
2317
opaque structure to pass to read_records to extract the raw
2259
opaque memo that can be passed to _StreamAccess.read_records
2260
to extract the raw data; for these classes it is
2261
(from_backing_knit, index, start, end)
2319
2262
compression_parent
2320
2263
Content that this record is built upon, may be None
2334
2277
parent_ids = self.get_parents_with_ghosts(version_id)
2335
2278
noeol = ('no-eol' in self.get_options(version_id))
2279
index_memo = self.get_position(version_id)
2280
from_backing_knit = index_memo[0]
2281
if from_backing_knit:
2282
# texts retrieved from the backing knit are always full texts
2336
2284
if method == 'fulltext':
2337
2285
compression_parent = None
2339
2287
compression_parent = parent_ids[0]
2340
index_memo = self.get_position(version_id)
2341
2288
result[version_id] = (index_memo, compression_parent,
2342
2289
parent_ids, (method, noeol))
2345
2292
def get_method(self, version_id):
2346
2293
"""Return compression method of specified version."""
2348
options = self._by_version[version_id][0]
2350
# Strictly speaking this should check in the backing knit, but
2351
# until we have a test to discriminate, this will do.
2352
return self.backing_index.get_method(version_id)
2294
options = self.get_options(version_id)
2353
2295
if 'fulltext' in options:
2354
2296
return 'fulltext'
2355
2297
elif 'line-delta' in options:
2366
2308
return self._by_version[version_id][0]
2367
2309
except KeyError:
2368
return self.backing_index.get_options(version_id)
2310
options = list(self.backing_index.get_options(version_id))
2311
if 'fulltext' in options:
2313
elif 'line-delta' in options:
2314
# Texts from the backing knit are always returned from the stream
2316
options.remove('line-delta')
2317
options.append('fulltext')
2319
raise errors.KnitIndexUnknownMethod(self, options)
2320
return tuple(options)
2370
2322
def get_parent_map(self, version_ids):
2371
2323
"""Passed through to by KnitVersionedFile.get_parent_map."""
2393
2345
coordinates into that (as index_memo's are opaque outside the
2394
2346
index and matching access class).
2396
:return: a tuple (thunk_flag, index, start, end). If thunk_flag is
2397
False, index will be self, otherwise it will be a version id.
2348
:return: a tuple (from_backing_knit, index, start, end) that can
2349
be passed e.g. to get_raw_records.
2350
If from_backing_knit is False, index will be self, otherwise it
2351
will be a version id.
2400
2354
start, end = self._by_version[version_id][1]
2407
2361
"""Get all the versions in the stream."""
2408
2362
return self._by_version.keys()
2410
def iter_parents(self, version_ids):
2411
"""Iterate through the parents for many version ids.
2413
:param version_ids: An iterable yielding version_ids.
2414
:return: An iterator that yields (version_id, parents). Requested
2415
version_ids not present in the versioned file are simply skipped.
2416
The order is undefined, allowing for different optimisations in
2417
the underlying implementation.
2420
for version in version_ids:
2422
result.append((version, self._by_version[version][2]))
2428
2365
class _KnitData(object):
2429
2366
"""Manage extraction of data from a KnitAccess, caching and decompressing.
2442
2379
self._access = access
2443
2380
self._checked = False
2444
# TODO: jam 20060713 conceptually, this could spill to disk
2445
# if the cached size gets larger than a certain amount
2446
# but it complicates the model a bit, so for now just use
2447
# a simple dictionary
2449
self._do_cache = False
2451
def enable_cache(self):
2452
"""Enable caching of reads."""
2453
self._do_cache = True
2455
def clear_cache(self):
2456
"""Clear the record cache."""
2457
self._do_cache = False
2460
2382
def _open_file(self):
2461
2383
return self._access.open_file()
2561
2483
# uses readv so nice and fast we hope.
2562
2484
if len(records):
2563
2485
# grab the disk data needed.
2565
# Don't check _cache if it is empty
2566
needed_offsets = [index_memo for version_id, index_memo
2568
if version_id not in self._cache]
2570
needed_offsets = [index_memo for version_id, index_memo
2486
needed_offsets = [index_memo for version_id, index_memo
2573
2488
raw_records = self._access.get_raw_records(needed_offsets)
2575
2490
for version_id, index_memo in records:
2576
if version_id in self._cache:
2577
# This data has already been validated
2578
data = self._cache[version_id]
2580
data = raw_records.next()
2582
self._cache[version_id] = data
2584
# validate the header
2585
df, rec = self._parse_record_header(version_id, data)
2491
data = raw_records.next()
2492
# validate the header
2493
df, rec = self._parse_record_header(version_id, data)
2587
2495
yield version_id, data
2589
2497
def read_records_iter(self, records):
2599
2507
if not records:
2603
# Skip records we have alread seen
2604
yielded_records = set()
2605
needed_records = set()
2606
for record in records:
2607
if record[0] in self._cache:
2608
if record[0] in yielded_records:
2610
yielded_records.add(record[0])
2611
data = self._cache[record[0]]
2612
content, digest = self._parse_record(record[0], data)
2613
yield (record[0], content, digest)
2615
needed_records.add(record)
2616
needed_records = sorted(needed_records, key=operator.itemgetter(1))
2618
needed_records = sorted(set(records), key=operator.itemgetter(1))
2510
needed_records = sorted(set(records), key=operator.itemgetter(1))
2620
2511
if not needed_records:
2628
2519
for (version_id, index_memo), data in \
2629
2520
izip(iter(needed_records), raw_data):
2630
2521
content, digest = self._parse_record(version_id, data)
2632
self._cache[version_id] = data
2633
2522
yield version_id, content, digest
2635
2524
def read_records(self, records):
2644
2533
class InterKnit(InterVersionedFile):
2645
2534
"""Optimised code paths for knit to knit operations."""
2647
_matching_file_from_factory = KnitVersionedFile
2648
_matching_file_to_factory = KnitVersionedFile
2536
_matching_file_from_factory = staticmethod(make_file_knit)
2537
_matching_file_to_factory = staticmethod(make_file_knit)
2651
2540
def is_compatible(source, target):