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?
55
71
The iter method here will generally read through the whole index file
56
72
in one go. With readahead in the kernel and python/libc (typically
57
73
128kB) this means that there should be no seeks and often only one
78
94
import sys, zlib, struct, mdiff, stat, os, sha
79
95
from binascii import hexlify, unhexlify
85
99
_HEADER = "bzr revfile v1\n"
227
241
return self._add_compressed(text_sha, text, _NO_RECORD, compress)
245
def _choose_base(self, seed, base):
247
if base == _NO_RECORD:
250
if idxrec[I_BASE] == _NO_RECORD:
253
base = idxrec[I_BASE]
256
return base # relative to this full text
230
260
def _add_delta(self, text, text_sha, base, compress):
231
261
"""Add a text stored relative to a previous text."""
232
262
self._check_index(base)
235
base_text = self.get(base, recursion_limit=CHAIN_LIMIT)
265
base_text = self.get(base, CHAIN_LIMIT)
236
266
except LimitHitException:
237
267
return self._add_full_text(text, text_sha, compress)
272
302
# it's the same, in case someone ever breaks SHA-1.
273
303
return idx # already present
305
# base = self._choose_base(ord(text_sha[0]), base)
275
307
if base == _NO_RECORD:
276
308
return self._add_full_text(text, text_sha, compress)
388
420
"""Read back all index records.
390
422
Do not seek the index file while this is underway!"""
391
sys.stderr.write(" ** iter called ** \n")
423
## sys.stderr.write(" ** iter called ** \n")
392
424
self._seek_index(0)
394
426
idxrec = self._read_next_index()
445
478
except IndexError:
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"
479
sys.stderr.write("usage: revfile dump REVFILE\n"
480
" revfile add REVFILE < INPUT\n"
481
" revfile add-delta REVFILE BASE < INPUT\n"
482
" revfile add-series REVFILE BASE FILE...\n"
483
" revfile get REVFILE IDX\n"
484
" revfile find-sha REVFILE HEX\n"
485
" revfile total-text-size REVFILE\n"
486
" revfile last REVFILE\n")
456
return Revfile('testrev', 'w')
490
return Revfile(filename, 'w')
459
return Revfile('testrev', 'r')
493
return Revfile(filename, 'r')
462
496
print rw().add(sys.stdin.read())
463
497
elif cmd == 'add-delta':
464
print rw().add(sys.stdin.read(), int(argv[2]))
498
print rw().add(sys.stdin.read(), int(argv[3]))
499
elif cmd == 'add-series':
504
rev = r.add(file(fn).read(), rev)
465
505
elif cmd == 'dump':
467
507
elif cmd == 'get':
470
510
except IndexError:
471
sys.stderr.write("usage: revfile get IDX\n")
511
sys.stderr.write("usage: revfile get FILE IDX\n")
474
516
if idx < 0 or idx >= len(r):
475
517
sys.stderr.write("invalid index %r\n" % idx)
478
sys.stdout.write(ro().get(idx))
520
sys.stdout.write(r.get(idx))
479
521
elif cmd == 'find-sha':
481
s = unhexlify(argv[2])
523
s = unhexlify(argv[3])
482
524
except IndexError:
483
sys.stderr.write("usage: revfile find-sha HEX\n")
525
sys.stderr.write("usage: revfile find-sha FILE HEX\n")
486
528
idx = ro().find_sha(s)