1
"""Implementation of multiparent diffs for versionedfile-like storage
 
 
3
Provides mp-regen and mp-extract commands.
 
 
4
Focus is on comparing size/performance to knits.
 
 
7
from bzrlib.lazy_import import lazy_import
 
 
9
lazy_import(globals(), """
 
 
21
from bzrlib.workingtree import WorkingTree
 
 
22
from bzrlib.tests import TestUtil
 
 
24
from bzrlib.plugins.multiparent.multiparent import (
 
 
26
    MultiMemoryVersionedFile,
 
 
30
class cmd_mp_regen(commands.Command):
 
 
31
    """Generate a multiparent versionedfile"""
 
 
35
    takes_args = ['file?']
 
 
37
    takes_options = [commands.Option('sync-snapshots',
 
 
38
                                     help='Snapshots follow source.'),
 
 
39
                     commands.Option('snapshot-interval', type=int,
 
 
40
                                     help='Take snapshots every x revisions.'),
 
 
41
                     commands.Option('outfile', type=unicode,
 
 
42
                                     help='Write pseudo-knit to this file.'),
 
 
43
                     commands.Option('memory', help='Use memory, not disk.'),
 
 
44
                     commands.Option('extract', help='Test extract time.'),
 
 
45
                     commands.Option('single', help='Use a single parent.'),
 
 
46
                     commands.Option('verify', help='Verify added texts.'),
 
 
47
                     commands.Option('cache', help='Aggresively cache.'),
 
 
48
                     commands.Option('size', help='Aggressive size.'),
 
 
49
                     commands.Option('build', help='Aggressive build.'),
 
 
53
    def run(self, file=None, sync_snapshots=False, snapshot_interval=26,
 
 
54
            lsprof_timed=False, dump=False, extract=False, single=False,
 
 
55
            verify=False, outfile=None, memory=False, cache=False,
 
 
56
            size=False, build=False):
 
 
57
        file_weave = get_file_weave(file)
 
 
58
        url = file_weave.transport.abspath(file_weave.filename)
 
 
59
        sys.stderr.write('Importing: %s\n' % \
 
 
60
            urlutils.local_path_from_url(url))
 
 
62
            sys.stderr.write('Snapshots follow input\n')
 
 
64
            sys.stderr.write('Snapshot interval: %d\n' % snapshot_interval)
 
 
70
            vf = MultiVersionedFile(filename, snapshot_interval)
 
 
72
            vf = MultiMemoryVersionedFile(snapshot_interval)
 
 
74
        old_snapshots = set(r for r in file_weave.versions() if
 
 
75
                        file_weave._index.get_method(r) == 'fulltext')
 
 
77
            to_sync = old_snapshots
 
 
82
            to_sync = vf.select_snapshots(file_weave)
 
 
83
        sys.stderr.write("%d fulltext(s)\n" % len(old_snapshots))
 
 
84
        sys.stderr.write("%d planned snapshots\n" % len(to_sync))
 
 
87
            vf.import_versionedfile(file_weave, to_sync, single_parent=single,
 
 
88
                                    verify=verify, no_cache=not cache)
 
 
90
                snapshots = vf.select_by_size(len(old_snapshots))
 
 
91
                for version_id in snapshots:
 
 
92
                    vf.make_snapshot(version_id)
 
 
94
                ranking = vf.get_build_ranking()
 
 
95
                snapshots = ranking[:len(old_snapshots) -\
 
 
97
                for version_id in snapshots:
 
 
98
                    vf.make_snapshot(version_id)
 
 
103
            sys.stderr.write("%d actual snapshots\n" % len(vf._snapshots))
 
 
107
                if outfile is not None:
 
 
108
                    vf_file = MultiVersionedFile(outfile)
 
 
109
                vf_file.import_diffs(vf)
 
 
118
class cmd_mp_extract(commands.Command):
 
 
119
    """Test extraction time multiparent knits"""
 
 
124
        commands.Option('lsprof-timed', help='Use lsprof.'),
 
 
125
        commands.Option('parallel', help='Extract multiple versions at once.'),
 
 
126
        commands.Option('count', help='Number of cycles to do.', type=int),
 
 
129
    takes_args = ['filename', 'vfile?']
 
 
131
    def run(self, filename, vfile=None, lsprof_timed=False, count=1000,
 
 
133
        vf = MultiVersionedFile(filename)
 
 
135
        snapshots = [r for r in vf.versions() if vf.get_diff(r).is_snapshot()]
 
 
136
        print '%d snapshots' % len(snapshots)
 
 
137
        revisions = list(vf.versions())
 
 
138
        revisions = revisions[-count:]
 
 
139
        print 'Testing extract time of %d revisions' % len(revisions)
 
 
141
            revisions_list = [revisions]
 
 
143
            revisions_list = [[r] for r in revisions]
 
 
145
        for revisions in revisions_list:
 
 
146
            vf = MultiVersionedFile(filename)
 
 
148
            vf.get_line_list(revisions)
 
 
149
        sys.stderr.write(time.clock() - start)
 
 
150
        sys.stderr.write('\n')
 
 
152
            from bzrlib.lsprof import profile
 
 
154
            ret, stats = profile(vf.get_line_list, revisions_list[-1][-1])
 
 
158
        for revisions in revisions_list:
 
 
159
            file_weave = get_file_weave(vfile)
 
 
160
            file_weave.get_line_list(revisions)
 
 
161
        sys.stderr.write(time.clock() - start)
 
 
162
        sys.stderr.write('\n')
 
 
165
def get_file_weave(filename=None, wt=None):
 
 
167
        wt, path = WorkingTree.open_containing('.')
 
 
168
        return wt.branch.repository.get_inventory_weave()
 
 
170
        wt, path = WorkingTree.open_containing(filename)
 
 
171
        file_id = wt.path2id(path)
 
 
172
        bt = wt.branch.repository.revision_tree(wt.last_revision())
 
 
173
        return bt.get_weave(file_id)
 
 
176
commands.register_command(cmd_mp_regen)
 
 
177
commands.register_command(cmd_mp_extract)