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

  • Committer: Jelmer Vernooij
  • Date: 2009-06-01 20:40:21 UTC
  • mto: (4398.5.3 bencode_serializer)
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: jelmer@samba.org-20090601204021-3ocd7tv6lmr7k2ff
Review feedback from Aaron: 
* Store ints rather than strings for timezone
* Remove incorrect comment about ordering
* Use self.assertEquals rather than alias
* Wrap lines

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Serializer object for CHK based inventory storage."""
 
18
 
 
19
from cStringIO import (
 
20
    StringIO,
 
21
    )
 
22
 
 
23
from bzrlib import (
 
24
    cache_utf8,
 
25
    inventory,
 
26
    osutils,
 
27
    revision as _mod_revision,
 
28
    xml5,
 
29
    xml6,
 
30
    )
 
31
from bzrlib.util.bencode import (
 
32
    bdecode,
 
33
    bencode,
 
34
    )
 
35
 
 
36
class BEncodeRevisionSerializer1(object):
 
37
    """Simple revision serializer based around bencode. 
 
38
    
 
39
    """
 
40
 
 
41
    def write_revision_to_string(self, rev):
 
42
        encode_utf8 = cache_utf8.encode
 
43
        ret = {
 
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),
 
50
            }
 
51
        revprops = {}
 
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
 
57
        return bencode(ret)
 
58
 
 
59
    def write_revision(self, rev, f):
 
60
        f.write(self.write_revision_to_string(rev))
 
61
 
 
62
    def read_revision_from_string(self, text):
 
63
        decode_utf8 = cache_utf8.decode
 
64
        ret = bdecode(text)
 
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"]),
 
72
            properties={})
 
73
        if "timezone" in ret:
 
74
            rev.timezone = ret["timezone"]
 
75
        else:
 
76
            rev.timezone = None
 
77
        for key, value in ret["properties"].iteritems():
 
78
            rev.properties[key] = decode_utf8(value)
 
79
        return rev
 
80
 
 
81
    def read_revision(self, f):
 
82
        return self.read_revision_from_string(f.read())
 
83
 
 
84
 
 
85
class CHKSerializerSubtree(BEncodeRevisionSerializer1, xml6.Serializer_v6):
 
86
    """A CHKInventory based serializer that supports tree references"""
 
87
 
 
88
    supported_kinds = set(['file', 'directory', 'symlink', 'tree-reference'])
 
89
    format_num = '9'
 
90
    revision_format_num = None
 
91
    support_altered_by_hack = False
 
92
 
 
93
    def _unpack_entry(self, elt):
 
94
        kind = elt.tag
 
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,
 
104
                                           reference_revision)
 
105
        else:
 
106
            return xml6.Serializer_v6._unpack_entry(self, elt)
 
107
 
 
108
    def __init__(self, node_size, search_key_name):
 
109
        self.maximum_size = node_size
 
110
        self.search_key_name = search_key_name
 
111
 
 
112
 
 
113
class CHKSerializer(xml5.Serializer_v5):
 
114
    """A CHKInventory based serializer with 'plain' behaviour."""
 
115
 
 
116
    format_num = '9'
 
117
    revision_format_num = None
 
118
    support_altered_by_hack = False
 
119
 
 
120
    def __init__(self, node_size, search_key_name):
 
121
        self.maximum_size = node_size
 
122
        self.search_key_name = search_key_name
 
123
 
 
124
 
 
125
chk_serializer_255_bigpage = CHKSerializer(65536, 'hash-255-way')
 
126
 
 
127
 
 
128
class CHKBEncodeSerializer(BEncodeRevisionSerializer1, CHKSerializer):
 
129
    """A CHKInventory and BEncode based serializer with 'plain' behaviour."""
 
130
 
 
131
    format_num = '10'
 
132
 
 
133
 
 
134
chk_bencode_serializer = CHKBEncodeSerializer(65536, 'hash-255-way')