/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/revfile.py

  • Committer: Martin Pool
  • Date: 2005-08-12 15:45:35 UTC
  • Revision ID: mbp@sourcefrog.net-20050812154535-8add210e3e4a2464
- add new Branch.get_inventory_xml() method

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
 
55
 
 
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
 
58
sequentially.
 
59
 
 
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.
 
64
 
 
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?
 
70
 
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
73
89
# if there are gaps and that can happen if we're interrupted while
74
90
# writing to the datafile.  Overlapping would be very bad though.
75
91
 
76
 
 
 
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.
77
95
 
78
96
import sys, zlib, struct, mdiff, stat, os, sha
79
97
from binascii import hexlify, unhexlify
80
98
 
81
 
factor = 10
82
 
 
83
99
_RECORDSIZE = 48
84
100
 
85
101
_HEADER = "bzr revfile v1\n"
96
112
FL_GZIP = 1
97
113
 
98
114
# maximum number of patches in a row before recording a whole text.
99
 
CHAIN_LIMIT = 50
 
115
CHAIN_LIMIT = 25
100
116
 
101
117
 
102
118
class RevfileError(Exception):
227
243
        return self._add_compressed(text_sha, text, _NO_RECORD, compress)
228
244
 
229
245
 
 
246
    # NOT USED
 
247
    def _choose_base(self, seed, base):
 
248
        while seed & 3 == 3:
 
249
            if base == _NO_RECORD:
 
250
                return _NO_RECORD
 
251
            idxrec = self[base]
 
252
            if idxrec[I_BASE] == _NO_RECORD:
 
253
                return base
 
254
 
 
255
            base = idxrec[I_BASE]
 
256
            seed >>= 2
 
257
                
 
258
        return base        # relative to this full text
 
259
        
 
260
 
 
261
 
230
262
    def _add_delta(self, text, text_sha, base, compress):
231
263
        """Add a text stored relative to a previous text."""
232
264
        self._check_index(base)
233
 
        
 
265
 
234
266
        try:
235
 
            base_text = self.get(base, recursion_limit=CHAIN_LIMIT)
 
267
            base_text = self.get(base, CHAIN_LIMIT)
236
268
        except LimitHitException:
237
269
            return self._add_full_text(text, text_sha, compress)
238
270
        
272
304
            # it's the same, in case someone ever breaks SHA-1.
273
305
            return idx                  # already present
274
306
        
 
307
        # base = self._choose_base(ord(text_sha[0]), base)
 
308
 
275
309
        if base == _NO_RECORD:
276
310
            return self._add_full_text(text, text_sha, compress)
277
311
        else:
372
406
        self._seek_index(idx)
373
407
        idxrec = self._read_next_index()
374
408
        if idxrec == None:
375
 
            raise IndexError()
 
409
            raise IndexError("no index %d" % idx)
376
410
        else:
377
411
            return idxrec
378
412
 
388
422
        """Read back all index records.
389
423
 
390
424
        Do not seek the index file while this is underway!"""
391
 
        sys.stderr.write(" ** iter called ** \n")
 
425
        ## sys.stderr.write(" ** iter called ** \n")
392
426
        self._seek_index(0)
393
427
        while True:
394
428
            idxrec = self._read_next_index()
442
476
def main(argv):
443
477
    try:
444
478
        cmd = argv[1]
 
479
        filename = argv[2]
445
480
    except IndexError:
446
 
        sys.stderr.write("usage: revfile dump\n"
447
 
                         "       revfile add\n"
448
 
                         "       revfile add-delta BASE\n"
449
 
                         "       revfile get IDX\n"
450
 
                         "       revfile find-sha HEX\n"
451
 
                         "       revfile total-text-size\n"
452
 
                         "       revfile last\n")
 
481
        sys.stderr.write("usage: revfile dump REVFILE\n"
 
482
                         "       revfile add REVFILE < INPUT\n"
 
483
                         "       revfile add-delta REVFILE BASE < INPUT\n"
 
484
                         "       revfile add-series REVFILE BASE FILE...\n"
 
485
                         "       revfile get REVFILE IDX\n"
 
486
                         "       revfile find-sha REVFILE HEX\n"
 
487
                         "       revfile total-text-size REVFILE\n"
 
488
                         "       revfile last REVFILE\n")
453
489
        return 1
454
490
 
455
491
    def rw():
456
 
        return Revfile('testrev', 'w')
 
492
        return Revfile(filename, 'w')
457
493
 
458
494
    def ro():
459
 
        return Revfile('testrev', 'r')
 
495
        return Revfile(filename, 'r')
460
496
 
461
497
    if cmd == 'add':
462
498
        print rw().add(sys.stdin.read())
463
499
    elif cmd == 'add-delta':
464
 
        print rw().add(sys.stdin.read(), int(argv[2]))
 
500
        print rw().add(sys.stdin.read(), int(argv[3]))
 
501
    elif cmd == 'add-series':
 
502
        r = rw()
 
503
        rev = int(argv[3])
 
504
        for fn in argv[4:]:
 
505
            print rev
 
506
            rev = r.add(file(fn).read(), rev)
465
507
    elif cmd == 'dump':
466
508
        ro().dump()
467
509
    elif cmd == 'get':
468
510
        try:
469
 
            idx = int(argv[2])
 
511
            idx = int(argv[3])
470
512
        except IndexError:
471
 
            sys.stderr.write("usage: revfile get IDX\n")
 
513
            sys.stderr.write("usage: revfile get FILE IDX\n")
472
514
            return 1
473
515
 
 
516
        r = ro()
 
517
 
474
518
        if idx < 0 or idx >= len(r):
475
519
            sys.stderr.write("invalid index %r\n" % idx)
476
520
            return 1
477
521
 
478
 
        sys.stdout.write(ro().get(idx))
 
522
        sys.stdout.write(r.get(idx))
479
523
    elif cmd == 'find-sha':
480
524
        try:
481
 
            s = unhexlify(argv[2])
 
525
            s = unhexlify(argv[3])
482
526
        except IndexError:
483
 
            sys.stderr.write("usage: revfile find-sha HEX\n")
 
527
            sys.stderr.write("usage: revfile find-sha FILE HEX\n")
484
528
            return 1
485
529
 
486
530
        idx = ro().find_sha(s)