~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/benchmarks/tree_creator/kernel_like.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-17 14:18:30 UTC
  • mfrom: (1934.1.21 xml_writer)
  • Revision ID: pqm@pqm.ubuntu.com-20060817141830-383cad75e9090732
(robertc,jam) Custom xml serializer saves a lot of time when serializing inventories

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
    add,
23
23
    bzrdir,
24
24
    osutils,
 
25
    workingtree,
 
26
    xml5,
25
27
    )
26
28
 
27
29
from bzrlib.benchmarks.tree_creator import TreeCreator
144
146
        if in_cache:
145
147
            self._protect_files(root+'/.bzr')
146
148
        return tree
 
149
 
 
150
 
 
151
class KernelLikeInventoryCreator(TreeCreator):
 
152
    """Return just the memory representation of a committed kernel-like tree"""
 
153
 
 
154
    def __init__(self, test):
 
155
        super(KernelLikeInventoryCreator, self).__init__(test,
 
156
            tree_name='kernel_like_inventory',
 
157
            link_working=True,
 
158
            link_bzr=True,
 
159
            hot_cache=True)
 
160
 
 
161
    def ensure_cached(self):
 
162
        """Make sure we have a cached version of the kernel-like inventory"""
 
163
        cache_dir = self._get_cache_dir()
 
164
        if cache_dir is None:
 
165
            return
 
166
 
 
167
        if self.is_cached():
 
168
            return
 
169
 
 
170
        committed_creator = KernelLikeCommittedTreeCreator(self._test,
 
171
                                                           link_working=True,
 
172
                                                           link_bzr=True,
 
173
                                                           hot_cache=False)
 
174
        committed_creator.ensure_cached()
 
175
        committed_cache_dir = committed_creator._get_cache_dir()
 
176
        committed_tree = workingtree.WorkingTree.open(committed_cache_dir)
 
177
        rev_tree = committed_tree.basis_tree()
 
178
        os.mkdir(cache_dir)
 
179
        f = open(cache_dir+'/inventory', 'wb')
 
180
        try:
 
181
            xml5.serializer_v5.write_inventory(rev_tree.inventory, f)
 
182
        finally:
 
183
            f.close()
 
184
 
 
185
    def create(self):
 
186
        """Create a kernel like inventory
 
187
 
 
188
        :return: An Inventory object.
 
189
        """
 
190
        cache_dir = self._get_cache_dir()
 
191
        if cache_dir is None:
 
192
            return self._create_and_return()
 
193
 
 
194
        self.ensure_cached()
 
195
        return self._open_cached(cache_dir)
 
196
 
 
197
    def _create_and_return(self):
 
198
        """Create a kernel-like tree, and return its inventory"""
 
199
        creator = KernelLikeCommittedTreeCreator(self._test,
 
200
                                                 link_working=True,
 
201
                                                 link_bzr=True,
 
202
                                                 hot_cache=False)
 
203
        tree = creator.create('.')
 
204
        return tree.basis_tree().inventory
 
205
 
 
206
    def _open_cached(self, cache_dir):
 
207
        f = open(cache_dir + '/inventory', 'rb')
 
208
        try:
 
209
            return xml5.serializer_v5.read_inventory(f)
 
210
        finally:
 
211
            f.close()