1
# Copyright (C) 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Serializer object for CHK based inventory storage."""
19
from cStringIO import (
27
revision as _mod_revision,
31
from bzrlib.util.bencode import (
36
class BEncodeRevisionSerializer1(object):
37
"""Simple revision serializer based around bencode.
41
def write_revision_to_string(self, rev):
42
encode_utf8 = cache_utf8.encode
44
"revision-id": rev.revision_id,
45
"timestamp": "%.3f" % rev.timestamp,
46
"parent-ids": rev.parent_ids,
47
"inventory-sha1": rev.inventory_sha1,
48
"committer": encode_utf8(rev.committer),
49
"message": encode_utf8(rev.message),
52
for key, value in rev.properties.iteritems():
53
revprops[key] = encode_utf8(value)
54
ret["properties"] = revprops
55
if rev.timezone is not None:
56
ret["timezone"] = rev.timezone
59
def write_revision(self, rev, f):
60
f.write(self.write_revision_to_string(rev))
62
def read_revision_from_string(self, text):
63
decode_utf8 = cache_utf8.decode
65
rev = _mod_revision.Revision(
66
committer=decode_utf8(ret["committer"]),
67
revision_id=ret["revision-id"],
68
parent_ids=ret["parent-ids"],
69
inventory_sha1=ret["inventory-sha1"],
70
timestamp=float(ret["timestamp"]),
71
message=decode_utf8(ret["message"]),
74
rev.timezone = ret["timezone"]
77
for key, value in ret["properties"].iteritems():
78
rev.properties[key] = decode_utf8(value)
81
def read_revision(self, f):
82
return self.read_revision_from_string(f.read())
85
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml6.Serializer_v6):
86
"""A CHKInventory based serializer that supports tree references"""
88
supported_kinds = set(['file', 'directory', 'symlink', 'tree-reference'])
90
revision_format_num = None
91
support_altered_by_hack = False
93
def _unpack_entry(self, elt):
95
if not kind in self.supported_kinds:
96
raise AssertionError('unsupported entry kind %s' % kind)
97
if kind == 'tree-reference':
98
file_id = elt.attrib['file_id']
99
name = elt.attrib['name']
100
parent_id = elt.attrib['parent_id']
101
revision = elt.get('revision')
102
reference_revision = elt.get('reference_revision')
103
return inventory.TreeReference(file_id, name, parent_id, revision,
106
return xml6.Serializer_v6._unpack_entry(self, elt)
108
def __init__(self, node_size, search_key_name):
109
self.maximum_size = node_size
110
self.search_key_name = search_key_name
113
class CHKSerializer(xml5.Serializer_v5):
114
"""A CHKInventory based serializer with 'plain' behaviour."""
117
revision_format_num = None
118
support_altered_by_hack = False
120
def __init__(self, node_size, search_key_name):
121
self.maximum_size = node_size
122
self.search_key_name = search_key_name
125
chk_serializer_255_bigpage = CHKSerializer(65536, 'hash-255-way')
128
class CHKBEncodeSerializer(BEncodeRevisionSerializer1, CHKSerializer):
129
"""A CHKInventory and BEncode based serializer with 'plain' behaviour."""
134
chk_bencode_serializer = CHKBEncodeSerializer(65536, 'hash-255-way')