~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-10-10 20:13:49 UTC
  • mfrom: (3770.1.5 dump_btree)
  • Revision ID: pqm@pqm.ubuntu.com-20081010201349-ccw3kwu9fe7iaw77
(jam) Add a hidden 'dump-btree' command for getting the raw info out
        of a btree index.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from bzrlib import (
30
30
    bugtracker,
31
31
    bundle,
 
32
    btree_index,
32
33
    bzrdir,
33
34
    delta,
34
35
    config,
255
256
                                                 ' revision.')
256
257
                rev_id = rev.as_revision_id(b)
257
258
                self.outf.write(b.repository.get_revision_xml(rev_id).decode('utf-8'))
258
 
    
 
259
 
 
260
 
 
261
class cmd_dump_btree(Command):
 
262
    """Dump the contents of a btree index file to stdout.
 
263
 
 
264
    PATH is a btree index file, it can be any URL. This includes things like
 
265
    .bzr/repository/pack-names, or .bzr/repository/indices/a34b3a...ca4a4.iix
 
266
 
 
267
    By default, the tuples stored in the index file will be displayed. With
 
268
    --raw, we will uncompress the pages, but otherwise display the raw bytes
 
269
    stored in the index.
 
270
    """
 
271
 
 
272
    # TODO: Do we want to dump the internal nodes as well?
 
273
    # TODO: It would be nice to be able to dump the un-parsed information,
 
274
    #       rather than only going through iter_all_entries. However, this is
 
275
    #       good enough for a start
 
276
    hidden = True
 
277
    encoding_type = 'exact'
 
278
    takes_args = ['path']
 
279
    takes_options = [Option('raw', help='Write the uncompressed bytes out,'
 
280
                                        ' rather than the parsed tuples.'),
 
281
                    ]
 
282
 
 
283
    def run(self, path, raw=False):
 
284
        dirname, basename = osutils.split(path)
 
285
        t = transport.get_transport(dirname)
 
286
        if raw:
 
287
            self._dump_raw_bytes(t, basename)
 
288
        else:
 
289
            self._dump_entries(t, basename)
 
290
 
 
291
    def _get_index_and_bytes(self, trans, basename):
 
292
        """Create a BTreeGraphIndex and raw bytes."""
 
293
        bt = btree_index.BTreeGraphIndex(trans, basename, None)
 
294
        bytes = trans.get_bytes(basename)
 
295
        bt._file = cStringIO.StringIO(bytes)
 
296
        bt._size = len(bytes)
 
297
        return bt, bytes
 
298
 
 
299
    def _dump_raw_bytes(self, trans, basename):
 
300
        import zlib
 
301
 
 
302
        # We need to parse at least the root node.
 
303
        # This is because the first page of every row starts with an
 
304
        # uncompressed header.
 
305
        bt, bytes = self._get_index_and_bytes(trans, basename)
 
306
        for page_idx, page_start in enumerate(xrange(0, len(bytes),
 
307
                                                     btree_index._PAGE_SIZE)):
 
308
            page_end = min(page_start + btree_index._PAGE_SIZE, len(bytes))
 
309
            page_bytes = bytes[page_start:page_end]
 
310
            if page_idx == 0:
 
311
                self.outf.write('Root node:\n')
 
312
                header_end, data = bt._parse_header_from_bytes(page_bytes)
 
313
                self.outf.write(page_bytes[:header_end])
 
314
                page_bytes = data
 
315
            self.outf.write('\nPage %d\n' % (page_idx,))
 
316
            decomp_bytes = zlib.decompress(page_bytes)
 
317
            self.outf.write(decomp_bytes)
 
318
            self.outf.write('\n')
 
319
 
 
320
    def _dump_entries(self, trans, basename):
 
321
        try:
 
322
            st = trans.stat(basename)
 
323
        except errors.TransportNotPossible:
 
324
            # We can't stat, so we'll fake it because we have to do the 'get()'
 
325
            # anyway.
 
326
            bt, _ = self._get_index_and_bytes(trans, basename)
 
327
        else:
 
328
            bt = btree_index.BTreeGraphIndex(trans, basename, st.st_size)
 
329
        for node in bt.iter_all_entries():
 
330
            # Node is made up of:
 
331
            # (index, key, value, [references])
 
332
            self.outf.write('%s\n' % (node[1:],))
 
333
 
259
334
 
260
335
class cmd_remove_tree(Command):
261
336
    """Remove the working tree from a given branch/checkout.