/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.9.37 by Aaron Bentley
Add docstring
1
"""Implementation of multiparent diffs for versionedfile-like storage
2
3
Provides mp-regen and mp-extract commands.
4
Focus is on comparing size/performance to knits.
5
"""
6
0.9.20 by Aaron Bentley
Convert to a plugin
7
from bzrlib.lazy_import import lazy_import
0.9.12 by Aaron Bentley
Make benchmarks for mp
8
0.9.20 by Aaron Bentley
Convert to a plugin
9
lazy_import(globals(), """
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
10
import (
11
        errno,
12
        os,
13
        sys,
14
        time,
15
        )
16
17
from bzrlib import (
18
    commands,
19
    urlutils
20
    )
0.9.12 by Aaron Bentley
Make benchmarks for mp
21
from bzrlib.workingtree import WorkingTree
0.9.20 by Aaron Bentley
Convert to a plugin
22
from bzrlib.tests import TestUtil
23
0.9.25 by Aaron Bentley
More messy hacking
24
from bzrlib.plugins.multiparent.multiparent import (
25
    MultiVersionedFile,
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
26
    MultiMemoryVersionedFile,
0.9.25 by Aaron Bentley
More messy hacking
27
    )
0.9.20 by Aaron Bentley
Convert to a plugin
28
""")
29
30
class cmd_mp_regen(commands.Command):
31
    """Generate a multiparent versionedfile"""
32
33
    takes_args = ['file?']
34
    takes_options = [commands.Option('sync-snapshots',
35
                                     help='Snapshots follow source'),
36
                     commands.Option('snapshot-interval', type=int,
37
                                     help='take snapshots every x revisions'),
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
38
                     commands.Option('outfile', type=unicode,
39
                                     help='Write pseudo-knit to this file'),
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
40
                     commands.Option('memory', help='Use memory, not disk'),
0.9.20 by Aaron Bentley
Convert to a plugin
41
                     commands.Option('extract', help='test extract time'),
42
                     commands.Option('single', help='use a single parent'),
0.9.22 by Aaron Bentley
Fix restoration bug
43
                     commands.Option('verify', help='verify added texts'),
0.9.33 by Aaron Bentley
Enable caching commandline param
44
                     commands.Option('cache', help='Aggresively cache'),
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
45
                     commands.Option('size', help='Aggressive size'),
0.9.35 by Aaron Bentley
Add build ranking
46
                     commands.Option('build', help='Aggressive build'),
0.9.20 by Aaron Bentley
Convert to a plugin
47
                    ]
48
    hidden = True
49
50
    def run(self, file=None, sync_snapshots=False, snapshot_interval=26,
0.9.22 by Aaron Bentley
Fix restoration bug
51
            lsprof_timed=False, dump=False, extract=False, single=False,
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
52
            verify=False, outfile=None, memory=False, cache=False,
0.9.35 by Aaron Bentley
Add build ranking
53
            size=False, build=False):
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
54
        file_weave = get_file_weave(file)
0.9.20 by Aaron Bentley
Convert to a plugin
55
        url = file_weave.transport.abspath(file_weave.filename)
56
        print >> sys.stderr, 'Importing: %s' % \
57
            urlutils.local_path_from_url(url)
58
        if sync_snapshots:
59
            print >> sys.stderr, 'Snapshots follow input'
60
        else:
61
            print >> sys.stderr, 'Snapshot interval: %d' % snapshot_interval
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
62
        if not memory:
63
            if outfile is None:
64
                filename = 'pknit'
65
            else:
66
                filename = outfile
67
            vf = MultiVersionedFile(filename, snapshot_interval)
68
        else:
69
            vf = MultiMemoryVersionedFile(snapshot_interval)
70
        vf.destroy()
0.9.35 by Aaron Bentley
Add build ranking
71
        old_snapshots = set(r for r in file_weave.versions() if
0.9.20 by Aaron Bentley
Convert to a plugin
72
                        file_weave._index.get_method(r) == 'fulltext')
73
        if sync_snapshots:
0.9.35 by Aaron Bentley
Add build ranking
74
            to_sync = old_snapshots
75
        elif size or build:
76
            assert memory
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
77
            to_sync = set()
0.9.20 by Aaron Bentley
Convert to a plugin
78
        else:
0.9.23 by Aaron Bentley
handle snapshots all at once
79
            to_sync = vf.select_snapshots(file_weave)
0.9.35 by Aaron Bentley
Add build ranking
80
        print >> sys.stderr, "%d fulltext(s)" % len(old_snapshots)
0.9.25 by Aaron Bentley
More messy hacking
81
        print >> sys.stderr, "%d planned snapshots" % len(to_sync)
0.9.23 by Aaron Bentley
handle snapshots all at once
82
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
83
        try:
84
            vf.import_versionedfile(file_weave, to_sync, single_parent=single,
0.9.33 by Aaron Bentley
Enable caching commandline param
85
                                    verify=verify, no_cache=not cache)
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
86
            if size:
0.9.36 by Aaron Bentley
merge changes
87
                snapshots = vf.select_by_size(len(old_snapshots))
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
88
                for version_id in snapshots:
89
                    vf.make_snapshot(version_id)
0.9.35 by Aaron Bentley
Add build ranking
90
            if build:
91
                ranking = vf.get_build_ranking()
0.9.36 by Aaron Bentley
merge changes
92
                snapshots = ranking[:len(old_snapshots) -\
0.9.35 by Aaron Bentley
Add build ranking
93
                    len(vf._snapshots)]
94
                for version_id in snapshots:
0.9.36 by Aaron Bentley
merge changes
95
                    vf.make_snapshot(version_id)
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
96
        except:
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
97
            vf.destroy()
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
98
            raise
99
        try:
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
100
            print >> sys.stderr, "%d actual snapshots" % len(vf._snapshots)
0.9.33 by Aaron Bentley
Enable caching commandline param
101
            if not cache:
102
                vf.clear_cache()
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
103
            if memory:
104
                if outfile is not None:
105
                    vf_file = MultiVersionedFile(outfile)
0.9.36 by Aaron Bentley
merge changes
106
                vf_file.import_diffs(vf)
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
107
            else:
108
                vf_file = vf
0.9.30 by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile
109
        finally:
110
            if outfile is None:
0.9.31 by Aaron Bentley
Allow selecting MemoryVersionedFile from commandline
111
                vf.destroy()
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
112
            else:
113
                vf_file.save()
114
115
class cmd_mp_extract(commands.Command):
0.9.36 by Aaron Bentley
merge changes
116
    """Test extraction time multiparent knits"""
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
117
118
    takes_options = [
119
        commands.Option('lsprof-timed', help='Use lsprof'),
120
        commands.Option('parallel', help='extract multiple versions at once'),
121
        commands.Option('count', help='Number of cycles to do', type=int),
122
        ]
123
124
    takes_args = ['filename', 'vfile?']
125
126
    def run(self, filename, vfile=None, lsprof_timed=False, count=1000,
127
            parallel=False):
128
        vf = MultiVersionedFile(filename)
129
        vf.load()
0.9.36 by Aaron Bentley
merge changes
130
        snapshots = [r for r in vf.versions() if vf.get_diff(r).is_snapshot()]
131
        print '%d snapshots' % len(snapshots)
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
132
        revisions = list(vf.versions())
133
        revisions = revisions[-count:]
134
        print 'Testing extract time of %d revisions' % len(revisions)
135
        if parallel:
136
            revisions_list = [revisions]
137
        else:
138
            revisions_list = [[r] for r in revisions]
139
        start = time.clock()
140
        for revisions in revisions_list:
141
            vf = MultiVersionedFile(filename)
142
            vf.load()
143
            vf.get_line_list(revisions)
144
        print >> sys.stderr, time.clock() - start
145
        if lsprof_timed:
146
            from bzrlib.lsprof import profile
147
            vf.clear_cache()
148
            ret, stats = profile(vf.get_line_list, revisions_list[-1][-1])
149
            stats.sort()
150
            stats.pprint()
151
        start = time.clock()
152
        for revisions in revisions_list:
153
            file_weave = get_file_weave(vfile)
154
            file_weave.get_line_list(revisions)
155
        print >> sys.stderr, time.clock() - start
156
157
158
def get_file_weave(filename=None, wt=None):
159
    if filename is None:
160
        wt, path = WorkingTree.open_containing('.')
161
        return wt.branch.repository.get_inventory_weave()
162
    else:
163
        wt, path = WorkingTree.open_containing(filename)
164
        file_id = wt.path2id(path)
165
        bt = wt.branch.repository.revision_tree(wt.last_revision())
166
        return bt.get_weave(file_id)
167
0.9.20 by Aaron Bentley
Convert to a plugin
168
169
commands.register_command(cmd_mp_regen)
0.9.34 by Aaron Bentley
Implement save, load, snapshot-by-size
170
commands.register_command(cmd_mp_extract)