17
17
"""Serializer object for CHK based inventory storage."""
19
from cStringIO import (
19
23
from bzrlib import (
28
revision as _mod_revision,
26
class CHKSerializerSubtree(xml6.Serializer_v6):
34
def _validate_properties(props, _decode=cache_utf8._utf8_decode):
35
# TODO: we really want an 'isascii' check for key
36
# Cast the utf8 properties into Unicode 'in place'
37
for key, value in props.iteritems():
38
props[key] = _decode(value)[0]
42
def _is_format_10(value):
44
raise ValueError('Format number was not recognized, expected 10 got %d'
49
class BEncodeRevisionSerializer1(object):
50
"""Simple revision serializer based around bencode.
53
# Maps {key:(Revision attribute, bencode_type, validator)}
54
# This tells us what kind we expect bdecode to create, what variable on
55
# Revision we should be using, and a function to call to validate/transform
57
# TODO: add a 'validate_utf8' for things like revision_id and file_id
58
# and a validator for parent-ids
59
_schema = {'format': (None, int, _is_format_10),
60
'committer': ('committer', str, cache_utf8.decode),
61
'timezone': ('timezone', int, None),
62
'timestamp': ('timestamp', str, float),
63
'revision-id': ('revision_id', str, None),
64
'parent-ids': ('parent_ids', list, None),
65
'inventory-sha1': ('inventory_sha1', str, None),
66
'message': ('message', str, cache_utf8.decode),
67
'properties': ('properties', dict, _validate_properties),
70
def write_revision_to_string(self, rev):
71
encode_utf8 = cache_utf8._utf8_encode
72
# Use a list of tuples rather than a dict
73
# This lets us control the ordering, so that we are able to create
77
("committer", encode_utf8(rev.committer)[0]),
79
if rev.timezone is not None:
80
ret.append(("timezone", rev.timezone))
81
# For bzr revisions, the most common property is just 'branch-nick'
82
# which changes infrequently.
84
for key, value in rev.properties.iteritems():
85
revprops[key] = encode_utf8(value)[0]
86
ret.append(('properties', revprops))
88
("timestamp", "%.3f" % rev.timestamp),
89
("revision-id", rev.revision_id),
90
("parent-ids", rev.parent_ids),
91
("inventory-sha1", rev.inventory_sha1),
92
("message", encode_utf8(rev.message)[0]),
94
return bencode.bencode(ret)
96
def write_revision(self, rev, f):
97
f.write(self.write_revision_to_string(rev))
99
def read_revision_from_string(self, text):
100
# TODO: consider writing a Revision decoder, rather than using the
101
# generic bencode decoder
102
# However, to decode all 25k revisions of bzr takes approx 1.3s
103
# If we remove all extra validation that goes down to about 1.2s.
104
# Of that time, probably 0.6s is spend in bencode.bdecode().
105
# Regardless 'time bzr log' of everything is 7+s, so 1.3s to
106
# extract revision texts isn't a majority of time.
107
ret = bencode.bdecode(text)
108
if not isinstance(ret, list):
109
raise ValueError("invalid revision text")
110
schema = self._schema
111
# timezone is allowed to be missing, but should be set
112
bits = {'timezone': None}
113
for key, value in ret:
114
# Will raise KeyError if not a valid part of the schema, or an
115
# entry is given 2 times.
116
var_name, expected_type, validator = schema[key]
117
if value.__class__ is not expected_type:
118
raise ValueError('key %s did not conform to the expected type'
120
% (key, expected_type, type(value)))
121
if validator is not None:
122
value = validator(value)
123
bits[var_name] = value
124
if len(bits) != len(schema):
125
missing = [key for key, (var_name, _, _) in schema.iteritems()
126
if var_name not in bits]
127
raise ValueError('Revision text was missing expected keys %s.'
128
' text %r' % (missing, text))
129
del bits[None] # Get rid of 'format' since it doesn't get mapped
130
rev = _mod_revision.Revision(**bits)
133
def read_revision(self, f):
134
return self.read_revision_from_string(f.read())
137
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml6.Serializer_v6):
27
138
"""A CHKInventory based serializer that supports tree references"""
29
140
supported_kinds = set(['file', 'directory', 'symlink', 'tree-reference'])