2 from bup import _helpers
3 from bup.helpers import *
9 progress_callback = None
10 max_pack_size = 1000*1000*1000 # larger packs will slow down pruning
11 max_pack_objects = 200*1000 # cache memory usage is about 83 bytes per object
21 self.data = buffer(self.data, self.start) + s
24 def peek(self, count):
25 return buffer(self.data, self.start, count)
31 v = buffer(self.data, self.start, count)
36 return len(self.data) - self.start
40 b = buf.peek(buf.used())
41 (ofs, bits) = _helpers.splitbuf(b)
44 return (buffer(b, 0, ofs), bits)
52 fadvise_done(f, max(0, ofs - 1024*1024))
61 def drainbuf(buf, finalize):
63 (blob, bits) = splitbuf(buf)
68 if buf.used() > BLOB_MAX:
70 yield (buf.get(buf.used()), 0)
71 elif finalize and buf.used():
72 yield (buf.get(buf.used()), 0)
75 def hashsplit_iter(files):
76 assert(BLOB_HWM > BLOB_MAX)
80 for i in drainbuf(buf, finalize=False):
82 while buf.used() < BLOB_HWM:
86 for i in drainbuf(buf, finalize=True):
93 def _split_to_blobs(w, files):
95 for (blob, bits) in hashsplit_iter(files):
96 sha = w.new_blob(blob)
97 total_split += len(blob)
98 if w.outbytes >= max_pack_size or w.count >= max_pack_objects:
100 if progress_callback:
101 progress_callback(len(blob))
102 yield (sha, len(blob), bits)
105 def _make_shalist(l):
108 for (mode, sha, size) in l:
109 shalist.append((mode, '%016x' % ofs, sha))
112 return (shalist, total)
115 def _squish(w, stacks, n):
117 while i<n or len(stacks[i]) > MAX_PER_TREE:
118 while len(stacks) <= i+1:
120 if len(stacks[i]) == 1:
121 stacks[i+1] += stacks[i]
123 (shalist, size) = _make_shalist(stacks[i])
124 tree = w.new_tree(shalist)
125 stacks[i+1].append(('40000', tree, size))
130 def split_to_shalist(w, files):
131 sl = _split_to_blobs(w, files)
134 for (sha,size,bits) in sl:
135 shal.append(('100644', sha, size))
136 return _make_shalist(shal)[0]
138 base_bits = _helpers.blobbits()
139 fanout_bits = int(math.log(fanout, 2))
141 assert(n >= base_bits)
142 return (n - base_bits)/fanout_bits
144 for (sha,size,bits) in sl:
146 stacks[0].append(('100644', sha, size))
148 _squish(w, stacks, bits_to_idx(bits))
149 #log('stacks: %r\n' % [len(i) for i in stacks])
150 _squish(w, stacks, len(stacks)-1)
151 #log('stacks: %r\n' % [len(i) for i in stacks])
152 return _make_shalist(stacks[-1])[0]
155 def split_to_blob_or_tree(w, files):
156 shalist = list(split_to_shalist(w, files))
157 if len(shalist) == 1:
158 return (shalist[0][0], shalist[0][2])
159 elif len(shalist) == 0:
160 return ('100644', w.new_blob(''))
162 return ('40000', w.new_tree(shalist))
165 def open_noatime(name):
166 fd = _helpers.open_noatime(name)
168 return os.fdopen(fd, 'rb', 1024*1024)
177 def fadvise_done(f, ofs):
180 _helpers.fadvise_done(f.fileno(), ofs)