/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/versionedfile.py

  • Committer: Andrew Bennetts
  • Date: 2009-02-20 23:20:06 UTC
  • mfrom: (4028 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4036.
  • Revision ID: andrew.bennetts@canonical.com-20090220232006-07u7152q6fi5ul9d
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from copy import copy
23
23
from cStringIO import StringIO
24
24
import os
 
25
import struct
25
26
from zlib import adler32
26
27
 
27
28
from bzrlib.lazy_import import lazy_import
31
32
from bzrlib import (
32
33
    errors,
33
34
    index,
 
35
    knit,
34
36
    osutils,
35
37
    multiparent,
36
38
    tsort,
44
46
from bzrlib.registry import Registry
45
47
from bzrlib.symbol_versioning import *
46
48
from bzrlib.textmerge import TextMerge
 
49
from bzrlib.util import bencode
47
50
 
48
51
 
49
52
adapter_registry = Registry()
1484
1487
                pb.update("iterating texts", i, len(keys))
1485
1488
            for l in self._get_lines(key):
1486
1489
                yield (l, key)
 
1490
 
 
1491
 
 
1492
def network_bytes_to_kind_and_offset(network_bytes):
 
1493
    """Strip of a record kind from the front of network_bytes.
 
1494
 
 
1495
    :param network_bytes: The bytes of a record.
 
1496
    :return: A tuple (storage_kind, offset_of_remaining_bytes)
 
1497
    """
 
1498
    line_end = network_bytes.find('\n')
 
1499
    storage_kind = network_bytes[:line_end]
 
1500
    return storage_kind, line_end + 1
 
1501
 
 
1502
 
 
1503
class NetworkRecordStream(object):
 
1504
    """A record_stream which reconstitures a serialised stream."""
 
1505
 
 
1506
    def __init__(self, bytes_iterator):
 
1507
        """Create a NetworkRecordStream.
 
1508
 
 
1509
        :param bytes_iterator: An iterator of bytes. Each item in this
 
1510
            iterator should have been obtained from a record_streams'
 
1511
            record.get_bytes_as(record.storage_kind) call.
 
1512
        """
 
1513
        self._bytes_iterator = bytes_iterator
 
1514
        self._kind_factory = {'knit-ft-gz':knit.knit_network_to_record,
 
1515
            'knit-delta-gz':knit.knit_network_to_record,
 
1516
            'knit-annotated-ft-gz':knit.knit_network_to_record,
 
1517
            'knit-annotated-delta-gz':knit.knit_network_to_record,
 
1518
            'knit-delta-closure':knit.knit_delta_closure_to_records,
 
1519
            'fulltext':fulltext_network_to_record,
 
1520
            }
 
1521
 
 
1522
    def read(self):
 
1523
        """Read the stream.
 
1524
 
 
1525
        :return: An iterator as per VersionedFiles.get_record_stream().
 
1526
        """
 
1527
        for bytes in self._bytes_iterator:
 
1528
            storage_kind, line_end = network_bytes_to_kind_and_offset(bytes)
 
1529
            for record in self._kind_factory[storage_kind](
 
1530
                storage_kind, bytes, line_end):
 
1531
                yield record
 
1532
 
 
1533
 
 
1534
def fulltext_network_to_record(kind, bytes, line_end):
 
1535
    """Convert a network fulltext record to record."""
 
1536
    meta_len, = struct.unpack('!L', bytes[line_end:line_end+4])
 
1537
    record_meta = record_bytes[line_end+4:line_end+4+meta_len]
 
1538
    key, parents = bencode.bdecode_as_tuple(record_meta)
 
1539
    if parents == 'nil':
 
1540
        parents = None
 
1541
    fulltext = record_bytes[line_end+4+meta_len:]
 
1542
    return FulltextContentFactory(key, parents, None, fulltext)
 
1543
 
 
1544
 
 
1545
def _length_prefix(bytes):
 
1546
    return struct.pack('!L', len(bytes))
 
1547
 
 
1548
 
 
1549
def record_to_fulltext_bytes(self, record):
 
1550
    if record.parents is None:
 
1551
        parents = 'nil'
 
1552
    else:
 
1553
        parents = record.parents
 
1554
    record_meta = bencode.bencode((record.key, parents))
 
1555
    record_content = record.get_bytes_as('fulltext')
 
1556
    return "fulltext\n%s%s%s" % (
 
1557
        _length_prefix(record_meta), record_meta, record_content)