~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/versionedfile.py

  • Committer: Robert Collins
  • Date: 2009-02-15 21:47:42 UTC
  • mto: (4022.1.4 fetch.RemoteSink)
  • mto: This revision was merged to the branch mainline in revision 4026.
  • Revision ID: robertc@robertcollins.net-20090215214742-8n251ozbun3ncjsm
First passing NetworkRecordStream test - a fulltext from any record type which isn't a chunked or fulltext can be serialised and deserialised successfully.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from bzrlib import (
32
32
    errors,
33
33
    index,
 
34
    knit,
34
35
    osutils,
35
36
    multiparent,
36
37
    tsort,
39
40
    )
40
41
from bzrlib.graph import DictParentsProvider, Graph, _StackedParentsProvider
41
42
from bzrlib.transport.memory import MemoryTransport
 
43
from bzrlib.util import bencode
42
44
""")
43
45
from bzrlib.inter import InterObject
44
46
from bzrlib.registry import Registry
1472
1474
                pb.update("iterating texts", i, len(keys))
1473
1475
            for l in self._get_lines(key):
1474
1476
                yield (l, key)
 
1477
 
 
1478
 
 
1479
def network_bytes_to_kind_and_offset(network_bytes):
 
1480
    """Strip of a record kind from the front of network_bytes.
 
1481
 
 
1482
    :param network_bytes: The bytes of a record.
 
1483
    :return: A tuple (storage_kind, offset_of_remaining_bytes)
 
1484
    """
 
1485
    line_end = network_bytes.find('\n')
 
1486
    storage_kind = network_bytes[:line_end]
 
1487
    return storage_kind, line_end + 1
 
1488
 
 
1489
 
 
1490
class NetworkRecordStream(object):
 
1491
    """A record_stream which reconstitures a serialised stream."""
 
1492
 
 
1493
    def __init__(self, bytes_iterator):
 
1494
        """Create a NetworkRecordStream.
 
1495
 
 
1496
        :param bytes_iterator: An iterator of bytes. Each item in this
 
1497
            iterator should have been obtained from a record_streams'
 
1498
            record.get_bytes_as(record.storage_kind) call.
 
1499
        """
 
1500
        self._bytes_iterator = bytes_iterator
 
1501
        self._kind_factory = {'knit-ft-gz':knit.knit_network_to_record,
 
1502
            'knit-annotated-ft-gz':knit.knit_network_to_record,
 
1503
            }
 
1504
 
 
1505
    def read(self):
 
1506
        """Read the stream.
 
1507
 
 
1508
        :return: An iterator as per VersionedFiles.get_record_stream().
 
1509
        """
 
1510
        for bytes in self._bytes_iterator:
 
1511
            storage_kind, line_end = network_bytes_to_kind_and_offset(bytes)
 
1512
            yield self._kind_factory[storage_kind](
 
1513
                storage_kind, bytes, line_end)