~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

  • Committer: Robert Collins
  • Date: 2006-03-13 11:03:44 UTC
  • mto: (1615.1.2 bzr.mbp.integration)
  • mto: This revision was merged to the branch mainline in revision 1616.
  • Revision ID: robertc@robertcollins.net-20060313110344-f82a10b55cba1f99
Switch to delta based content copying in the generic versioned file copier.

Show diffs side-by-side

added added

removed removed

Lines of Context:
115
115
        """Return a list of (origin, text) tuples."""
116
116
        return list(self.annotate_iter())
117
117
 
118
 
    def apply_delta(self, delta):
119
 
        """Apply delta to this content."""
120
 
        offset = 0
121
 
        for start, end, count, lines in delta:
122
 
            self._lines[offset+start:offset+end] = lines
123
 
            offset = offset + (start - end) + count
124
 
 
125
118
    def line_delta_iter(self, new_lines):
126
119
        """Generate line-based delta from this content to new_lines."""
127
120
        new_texts = [text for origin, text in new_lines._lines]
289
282
        self._data = _KnitData(transport, relpath + DATA_SUFFIX,
290
283
            access_mode, create=not len(self.versions()))
291
284
 
 
285
    def _add_delta(self, version_id, parents, delta_parent, sha1, noeol, delta):
 
286
        """See VersionedFile._add_delta()."""
 
287
        self._check_add(version_id, []) # should we check the lines ?
 
288
        self._check_versions_present(parents)
 
289
        present_parents = []
 
290
        ghosts = []
 
291
        parent_texts = {}
 
292
        for parent in parents:
 
293
            if not self.has_version(parent):
 
294
                ghosts.append(parent)
 
295
            else:
 
296
                present_parents.append(parent)
 
297
 
 
298
        if delta_parent is None:
 
299
            # reconstitute as full text.
 
300
            assert len(delta) == 1 or len(delta) == 0
 
301
            if len(delta):
 
302
                assert delta[0][0] == 0
 
303
                assert delta[0][1] == 0
 
304
            return super(KnitVersionedFile, self)._add_delta(version_id,
 
305
                                                             parents,
 
306
                                                             delta_parent,
 
307
                                                             sha1,
 
308
                                                             noeol,
 
309
                                                             delta)
 
310
 
 
311
        digest = sha1
 
312
 
 
313
        options = []
 
314
        if noeol:
 
315
            options.append('no-eol')
 
316
 
 
317
        if delta_parent is not None:
 
318
            # determine the current delta chain length.
 
319
            # To speed the extract of texts the delta chain is limited
 
320
            # to a fixed number of deltas.  This should minimize both
 
321
            # I/O and the time spend applying deltas.
 
322
            count = 0
 
323
            delta_parents = [delta_parent]
 
324
            while count < 25:
 
325
                parent = delta_parents[0]
 
326
                method = self._index.get_method(parent)
 
327
                if method == 'fulltext':
 
328
                    break
 
329
                delta_parents = self._index.get_parents(parent)
 
330
                count = count + 1
 
331
            if method == 'line-delta':
 
332
                # did not find a fulltext in the delta limit.
 
333
                # just do a normal insertion.
 
334
                return super(KnitVersionedFile, self)._add_delta(version_id,
 
335
                                                                 parents,
 
336
                                                                 delta_parent,
 
337
                                                                 sha1,
 
338
                                                                 noeol,
 
339
                                                                 delta)
 
340
 
 
341
        options.append('line-delta')
 
342
        store_lines = self.factory.lower_line_delta(delta)
 
343
 
 
344
        where, size = self._data.add_record(version_id, digest, store_lines)
 
345
        self._index.add_version(version_id, options, where, size, parents)
 
346
 
292
347
    def clear_cache(self):
293
348
        """Clear the data cache only."""
294
349
        self._data.clear_cache()
335
390
        data_pos, data_size = self._index.get_position(version_id)
336
391
        data, sha1 = self._data.read_records(((version_id, data_pos, data_size),))[version_id]
337
392
        version_idx = self._index.lookup(version_id)
 
393
        noeol = 'no-eol' in self._index.get_options(version_id)
338
394
        if 'fulltext' == self._index.get_method(version_id):
339
395
            new_content = self.factory.parse_fulltext(data, version_idx)
340
396
            if parent is not None:
344
400
                old_texts = []
345
401
            new_texts = new_content.text()
346
402
            delta_seq = SequenceMatcher(None, old_texts, new_texts)
347
 
            return parent, sha1, self._make_line_delta(delta_seq, new_content)
 
403
            return parent, sha1, noeol, self._make_line_delta(delta_seq, new_content)
348
404
        else:
349
405
            delta = self.factory.parse_line_delta(data, version_idx)
350
 
            return parent, sha1, delta
 
406
            return parent, sha1, noeol, delta
351
407
        
352
408
    def get_graph_with_ghosts(self):
353
409
        """See VersionedFile.get_graph_with_ghosts()."""
500
556
                content = self.factory.parse_fulltext(data, version_idx)
501
557
            elif method == 'line-delta':
502
558
                delta = self.factory.parse_line_delta(data, version_idx)
503
 
                content.apply_delta(delta)
 
559
                content._lines = self._apply_delta(content._lines, delta)
504
560
 
505
561
        if 'no-eol' in self._index.get_options(version_id):
506
562
            line = content._lines[-1][1].rstrip('\n')