]> arthur.barton.de Git - bup.git/blob - lib/bup/hashsplit.py
hashsplit.py: convert from 'bits' to 'level' earlier in the sequence.
[bup.git] / lib / bup / hashsplit.py
1 import math
2 from bup import _helpers
3 from bup.helpers import *
4
5 BLOB_MAX = 8192*4   # 8192 is the "typical" blob size for bupsplit
6 BLOB_READ_SIZE = 1024*1024
7 MAX_PER_TREE = 256
8 progress_callback = None
9 max_pack_size = 1000*1000*1000  # larger packs will slow down pruning
10 max_pack_objects = 200*1000  # cache memory usage is about 83 bytes per object
11 fanout = 16
12
13 # The purpose of this type of buffer is to avoid copying on peek(), get(),
14 # and eat().  We do copy the buffer contents on put(), but that should
15 # be ok if we always only put() large amounts of data at a time.
16 class Buf:
17     def __init__(self):
18         self.data = ''
19         self.start = 0
20
21     def put(self, s):
22         if s:
23             self.data = buffer(self.data, self.start) + s
24             self.start = 0
25             
26     def peek(self, count):
27         return buffer(self.data, self.start, count)
28     
29     def eat(self, count):
30         self.start += count
31
32     def get(self, count):
33         v = buffer(self.data, self.start, count)
34         self.start += count
35         return v
36
37     def used(self):
38         return len(self.data) - self.start
39
40
41 def readfile_iter(files, progress=None):
42     for filenum,f in enumerate(files):
43         ofs = 0
44         b = ''
45         while 1:
46             if progress:
47                 progress(filenum, len(b))
48             fadvise_done(f, max(0, ofs - 1024*1024))
49             b = f.read(BLOB_READ_SIZE)
50             ofs += len(b)
51             if not b:
52                 fadvise_done(f, ofs)
53                 break
54             yield b
55
56
57 def _splitbuf(buf, basebits, fanbits):
58     while 1:
59         b = buf.peek(buf.used())
60         (ofs, bits) = _helpers.splitbuf(b)
61         if ofs > BLOB_MAX:
62             ofs = BLOB_MAX
63         if ofs:
64             buf.eat(ofs)
65             level = (bits-basebits)//fanbits  # integer division
66             yield buffer(b, 0, ofs), level
67         else:
68             break
69     while buf.used() >= BLOB_MAX:
70         # limit max blob size
71         yield buf.get(BLOB_MAX), 0
72
73
74 def _hashsplit_iter(files, progress):
75     assert(BLOB_READ_SIZE > BLOB_MAX)
76     basebits = _helpers.blobbits()
77     fanbits = int(math.log(fanout or 128, 2))
78     buf = Buf()
79     for inblock in readfile_iter(files, progress):
80         buf.put(inblock)
81         for buf_and_level in _splitbuf(buf, basebits, fanbits):
82             yield buf_and_level
83     if buf.used():
84         yield buf.get(buf.used()), 0
85
86
87 def _hashsplit_iter_keep_boundaries(files, progress):
88     for real_filenum,f in enumerate(files):
89         if progress:
90             def prog(filenum, nbytes):
91                 # the inner _hashsplit_iter doesn't know the real file count,
92                 # so we'll replace it here.
93                 return progress(real_filenum, nbytes)
94         else:
95             prog = None
96         for buf_and_level in _hashsplit_iter([f], progress=prog):
97             yield buf_and_level
98
99
100 def hashsplit_iter(files, keep_boundaries, progress):
101     if keep_boundaries:
102         return _hashsplit_iter_keep_boundaries(files, progress)
103     else:
104         return _hashsplit_iter(files, progress)
105
106
107 total_split = 0
108 def _split_to_blobs(w, files, keep_boundaries, progress):
109     global total_split
110     for (blob, level) in hashsplit_iter(files, keep_boundaries, progress):
111         sha = w.new_blob(blob)
112         total_split += len(blob)
113         if w.outbytes >= max_pack_size or w.count >= max_pack_objects:
114             w.breakpoint()
115         if progress_callback:
116             progress_callback(len(blob))
117         yield (sha, len(blob), level)
118
119
120 def _make_shalist(l):
121     ofs = 0
122     shalist = []
123     for (mode, sha, size) in l:
124         shalist.append((mode, '%016x' % ofs, sha))
125         ofs += size
126     total = ofs
127     return (shalist, total)
128
129
130 def _squish(w, stacks, n):
131     i = 0
132     while i<n or len(stacks[i]) > MAX_PER_TREE:
133         while len(stacks) <= i+1:
134             stacks.append([])
135         if len(stacks[i]) == 1:
136             stacks[i+1] += stacks[i]
137         elif stacks[i]:
138             (shalist, size) = _make_shalist(stacks[i])
139             tree = w.new_tree(shalist)
140             stacks[i+1].append(('40000', tree, size))
141         stacks[i] = []
142         i += 1
143
144
145 def split_to_shalist(w, files, keep_boundaries, progress=None):
146     sl = _split_to_blobs(w, files, keep_boundaries, progress)
147     if not fanout:
148         shal = []
149         for (sha,size,level) in sl:
150             shal.append(('100644', sha, size))
151         return _make_shalist(shal)[0]
152     else:
153         stacks = [[]]
154         for (sha,size,level) in sl:
155             stacks[0].append(('100644', sha, size))
156             if level:
157                 _squish(w, stacks, level)
158         #log('stacks: %r\n' % [len(i) for i in stacks])
159         _squish(w, stacks, len(stacks)-1)
160         #log('stacks: %r\n' % [len(i) for i in stacks])
161         return _make_shalist(stacks[-1])[0]
162
163
164 def split_to_blob_or_tree(w, files, keep_boundaries):
165     shalist = list(split_to_shalist(w, files, keep_boundaries))
166     if len(shalist) == 1:
167         return (shalist[0][0], shalist[0][2])
168     elif len(shalist) == 0:
169         return ('100644', w.new_blob(''))
170     else:
171         return ('40000', w.new_tree(shalist))
172
173
174 def open_noatime(name):
175     fd = _helpers.open_noatime(name)
176     try:
177         return os.fdopen(fd, 'rb', 1024*1024)
178     except:
179         try:
180             os.close(fd)
181         except:
182             pass
183         raise
184
185
186 def fadvise_done(f, ofs):
187     assert(ofs >= 0)
188     if ofs > 0 and hasattr(f, 'fileno'):
189         _helpers.fadvise_done(f.fileno(), ofs)