52
52
is that sequence numbers are stable references. But not every
53
53
repository in the world will assign the same sequence numbers,
54
54
therefore the SHA-1 is the only universally unique reference.
56
This is meant to scale to hold 100,000 revisions of a single file, by
57
which time the index file will be ~4.8MB and a bit big to read
60
Some of the reserved fields could be used to implement a (semi?)
61
balanced tree indexed by SHA1 so we can much more efficiently find the
62
index associated with a particular hash. For 100,000 revs we would be
63
able to find it in about 17 random reads, which is not too bad.
65
This performs pretty well except when trying to calculate deltas of
66
really large files. For that the main thing would be to plug in
67
something faster than difflib, which is after all pure Python.
68
Another approach is to just store the gzipped full text of big files,
69
though perhaps that's too perverse?
71
55
The iter method here will generally read through the whole index file
72
56
in one go. With readahead in the kernel and python/libc (typically
73
57
128kB) this means that there should be no seeks and often only one
89
73
# if there are gaps and that can happen if we're interrupted while
90
74
# writing to the datafile. Overlapping would be very bad though.
92
# TODO: Shouldn't need to lock if we always write in append mode and
93
# then ftell after writing to see where it went. In any case we
94
# assume the whole branch is protected by a lock.
78
import sys, zlib, struct, mdiff, stat, os, sha
102
79
from binascii import hexlify, unhexlify
104
import bzrlib.mdiff as mdiff
250
227
return self._add_compressed(text_sha, text, _NO_RECORD, compress)
254
def _choose_base(self, seed, base):
256
if base == _NO_RECORD:
259
if idxrec[I_BASE] == _NO_RECORD:
262
base = idxrec[I_BASE]
265
return base # relative to this full text
269
230
def _add_delta(self, text, text_sha, base, compress):
270
231
"""Add a text stored relative to a previous text."""
271
232
self._check_index(base)
274
base_text = self.get(base, CHAIN_LIMIT)
235
base_text = self.get(base, recursion_limit=CHAIN_LIMIT)
275
236
except LimitHitException:
276
237
return self._add_full_text(text, text_sha, compress)
278
239
data = mdiff.bdiff(base_text, text)
281
if True: # paranoid early check for bad diff
282
result = mdiff.bpatch(base_text, data)
283
assert result == text
286
241
# If the delta is larger than the text, we might as well just
287
242
# store the text. (OK, the delta might be more compressible,
293
248
return self._add_compressed(text_sha, data, base, compress)
296
def add(self, text, base=None, compress=True):
251
def add(self, text, base=_NO_RECORD, compress=True):
297
252
"""Add a new text to the revfile.
299
254
If the text is already present them its existing id is
320
272
# it's the same, in case someone ever breaks SHA-1.
321
273
return idx # already present
323
# base = self._choose_base(ord(text_sha[0]), base)
325
275
if base == _NO_RECORD:
326
276
return self._add_full_text(text, text_sha, compress)
346
296
text = self._get_patched(idx, idxrec, recursion_limit)
348
298
if sha.new(text).digest() != idxrec[I_SHA]:
349
raise RevfileError("corrupt SHA-1 digest on record %d in %s"
350
% (idx, self.basename))
299
raise RevfileError("corrupt SHA-1 digest on record %d"
438
388
"""Read back all index records.
440
390
Do not seek the index file while this is underway!"""
441
## sys.stderr.write(" ** iter called ** \n")
391
sys.stderr.write(" ** iter called ** \n")
442
392
self._seek_index(0)
444
394
idxrec = self._read_next_index()
486
436
for idx in range(len(self)):
487
437
t += len(self.get(idx))
491
def check(self, pb=None):
492
"""Extract every version and check its hash."""
494
for i in range(total):
496
pb.update("check revision", i, total)
497
# the get method implicitly checks the SHA-1
508
445
except IndexError:
509
sys.stderr.write("usage: revfile dump REVFILE\n"
510
" revfile add REVFILE < INPUT\n"
511
" revfile add-delta REVFILE BASE < INPUT\n"
512
" revfile add-series REVFILE BASE FILE...\n"
513
" revfile get REVFILE IDX\n"
514
" revfile find-sha REVFILE HEX\n"
515
" revfile total-text-size REVFILE\n"
516
" revfile last REVFILE\n")
446
sys.stderr.write("usage: revfile dump\n"
448
" revfile add-delta BASE\n"
450
" revfile find-sha HEX\n"
451
" revfile total-text-size\n"
519
if filename.endswith('.drev') or filename.endswith('.irev'):
520
filename = filename[:-5]
523
return Revfile(filename, 'w')
456
return Revfile('testrev', 'w')
526
return Revfile(filename, 'r')
459
return Revfile('testrev', 'r')
529
462
print rw().add(sys.stdin.read())
530
463
elif cmd == 'add-delta':
531
print rw().add(sys.stdin.read(), int(argv[3]))
532
elif cmd == 'add-series':
537
rev = r.add(file(fn).read(), rev)
464
print rw().add(sys.stdin.read(), int(argv[2]))
538
465
elif cmd == 'dump':
540
467
elif cmd == 'get':
543
470
except IndexError:
544
sys.stderr.write("usage: revfile get FILE IDX\n")
471
sys.stderr.write("usage: revfile get IDX\n")
549
474
if idx < 0 or idx >= len(r):
550
475
sys.stderr.write("invalid index %r\n" % idx)
553
sys.stdout.write(r.get(idx))
478
sys.stdout.write(ro().get(idx))
554
479
elif cmd == 'find-sha':
556
s = unhexlify(argv[3])
481
s = unhexlify(argv[2])
557
482
except IndexError:
558
sys.stderr.write("usage: revfile find-sha FILE HEX\n")
483
sys.stderr.write("usage: revfile find-sha HEX\n")
561
486
idx = ro().find_sha(s)