~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

  • Committer: Robert Collins
  • Date: 2007-08-02 02:18:17 UTC
  • mto: (2592.3.65 repository)
  • mto: This revision was merged to the branch mainline in revision 2667.
  • Revision ID: robertc@robertcollins.net-20070802021817-n8a86kevyvk2f9jo
* ``bzrlib.pack.ContainerWriter`` now returns an offset, length tuple to
  callers when inserting data, allowing generation of readv style access
  during pack creation, without needing a separate pass across the output
  pack to gather such details. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
        :param write_func: a callable that will be called when this
67
67
            ContainerWriter needs to write some bytes.
68
68
        """
69
 
        self.write_func = write_func
 
69
        self._write_func = write_func
 
70
        self.current_offset = 0
70
71
 
71
72
    def begin(self):
72
73
        """Begin writing a container."""
73
74
        self.write_func(FORMAT_ONE + "\n")
74
75
 
 
76
    def write_func(self, bytes):
 
77
        self._write_func(bytes)
 
78
        self.current_offset += len(bytes)
 
79
 
75
80
    def end(self):
76
81
        """Finish writing a container."""
77
82
        self.write_func("E")
78
83
 
79
84
    def add_bytes_record(self, bytes, names):
80
 
        """Add a Bytes record with the given names."""
 
85
        """Add a Bytes record with the given names.
 
86
        
 
87
        :param bytes: The bytes to insert.
 
88
        :param names: The names to give the inserted bytes.
 
89
        :return: An offset, length tuple. The offset is the offset
 
90
            of the record within the container, and the length is the
 
91
            length of data that will need to be read to reconstitute the
 
92
            record. These offset and length can only be used with the pack
 
93
            interface - they might be offset by headers or other such details
 
94
            and thus are only suitable for use by a ContainerReader.
 
95
        """
 
96
        current_offset = self.current_offset
81
97
        # Kind marker
82
98
        self.write_func("B")
83
99
        # Length
92
108
        self.write_func("\n")
93
109
        # Finally, the contents.
94
110
        self.write_func(bytes)
 
111
        # return a memo of where we wrote data to allow random access.
 
112
        return current_offset, self.current_offset - current_offset
95
113
 
96
114
 
97
115
class BaseReader(object):