145
145
if idxrec[I_SHA] == s:
151
def _add_common(self, text_sha, data, base):
152
def _add_compressed(self, text_sha, data, base, compress):
153
# well, maybe compress
158
# don't do compression if it's too small; it's unlikely to win
159
# enough to be worthwhile
160
compr_data = zlib.compress(data)
161
compr_len = len(compr_data)
162
if compr_len < data_len:
165
##print '- compressed %d -> %d, %.1f%%' \
166
## % (data_len, compr_len, float(compr_len)/float(data_len) * 100.0)
167
return self._add_raw(text_sha, data, base, flags)
171
def _add_raw(self, text_sha, data, base, flags):
152
172
"""Add pre-processed data, can be either full text or delta.
154
174
This does the compression if that makes sense."""
159
# don't do compression if it's too small; it's unlikely to win
160
# enough to be worthwhile
161
compr_data = zlib.compress(data)
162
compr_len = len(compr_data)
163
if compr_len < data_len:
166
print '- compressed %d -> %d, %.1f%%' \
167
% (data_len, compr_len, float(compr_len)/float(data_len) * 100.0)
170
176
self.datafile.seek(0, 2) # to end
171
177
self.idxfile.seek(0, 2)
195
201
This is not compressed against any reference version.
197
203
Returns the index for that text."""
198
return self._add_common(text_sha, text, _NO_RECORD)
201
def _add_delta(self, text, text_sha, base):
204
return self._add_compressed(text_sha, text, _NO_RECORD, compress)
207
def _add_delta(self, text, text_sha, base, compress):
202
208
"""Add a text stored relative to a previous text."""
203
209
self._check_index(base)
204
210
base_text = self.get(base)
209
215
# but the overhead of applying it probably still makes it
210
216
# bad, and I don't want to compress both of them to find out.)
211
217
if len(data) >= len(text):
212
return self._add_full_text(text, text_sha)
218
return self._add_full_text(text, text_sha, compress)
214
return self._add_common(text_sha, data, base)
217
def add(self, text, base=_NO_RECORD):
220
return self._add_compressed(text_sha, data, base, compress)
223
def add(self, text, base=_NO_RECORD, compress=True):
218
224
"""Add a new text to the revfile.
220
226
If the text is already present them its existing id is
221
227
returned and the file is not changed.
229
If compress is true then gzip compression will be used if it
223
232
If a base index is specified, that text *may* be used for
224
233
delta compression of the new text. Delta compression will
225
234
only be used if it would be a size win and if the existing
234
243
return idx # already present
236
245
if base == _NO_RECORD:
237
return self._add_full_text(text, text_sha)
246
return self._add_full_text(text, text_sha, compress)
239
return self._add_delta(text, text_sha, base)
248
return self._add_delta(text, text_sha, base, compress)