~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store/weave.py

  • Committer: Martin Pool
  • Date: 2005-09-30 05:56:05 UTC
  • mto: (1185.14.2)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: mbp@sourcefrog.net-20050930055605-a2c534529b392a7d
- fix upgrade for transport changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
 
1
#! /usr/bin/python
 
2
 
 
3
# Copyright (C) 2005 Canonical Ltd
 
4
 
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
7
 
#
 
9
 
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
13
# GNU General Public License for more details.
12
 
#
 
14
 
13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
18
 
17
 
# XXX: Some consideration of the problems that might occur if there are
18
 
# files whose id differs only in case.  That should probably be forbidden.
19
 
 
20
 
 
 
19
# Author: Martin Pool <mbp@canonical.com>
 
20
 
 
21
 
 
22
import os
21
23
import errno
22
 
import os
23
 
from cStringIO import StringIO
24
 
import urllib
25
 
from warnings import warn
26
24
 
27
 
from bzrlib import (
28
 
    errors,
29
 
    osutils,
30
 
    )
31
25
from bzrlib.weavefile import read_weave, write_weave_v5
32
 
from bzrlib.weave import WeaveFile, Weave
33
 
from bzrlib.store import TransportStore
 
26
from bzrlib.weave import Weave
 
27
from bzrlib.store import Store
34
28
from bzrlib.atomicfile import AtomicFile
35
 
from bzrlib.symbol_versioning import (deprecated_method,
36
 
        zero_eight)
37
 
from bzrlib.trace import mutter
38
 
import bzrlib.ui
39
 
 
40
 
 
41
 
class VersionedFileStore(TransportStore):
42
 
    """Collection of many versioned files in a transport."""
43
 
 
44
 
    # TODO: Rather than passing versionedfile_kwargs, perhaps pass in a
45
 
    # transport factory callable?
46
 
    def __init__(self, transport, prefixed=False, precious=False,
47
 
                 dir_mode=None, file_mode=None,
48
 
                 versionedfile_class=WeaveFile,
49
 
                 versionedfile_kwargs={},
50
 
                 escaped=False):
51
 
        super(VersionedFileStore, self).__init__(transport,
52
 
                dir_mode=dir_mode, file_mode=file_mode,
53
 
                prefixed=prefixed, compressed=False, escaped=escaped)
54
 
        self._precious = precious
55
 
        self._versionedfile_class = versionedfile_class
56
 
        self._versionedfile_kwargs = versionedfile_kwargs
57
 
 
58
 
    def _clear_cache_id(self, file_id, transaction):
59
 
        """WARNING may lead to inconsistent object references for file_id.
60
 
 
61
 
        Remove file_id from the transaction map. 
62
 
 
63
 
        NOT in the transaction api because theres no reliable way to clear
64
 
        callers. So its here for very specialised use rather than having an
65
 
        'api' that isn't.
66
 
        """
67
 
        weave = transaction.map.find_weave(file_id)
68
 
        if weave is not None:
69
 
            mutter("old data in transaction in %s for %s", self, file_id)
70
 
            # FIXME abstraction violation - transaction now has stale data.
71
 
            transaction.map.remove_object(weave)
 
29
from bzrlib.errors import NoSuchFile
 
30
 
 
31
from cStringIO import StringIO
 
32
 
 
33
 
 
34
 
 
35
class WeaveStore(Store):
 
36
    """Collection of several weave files in a directory.
 
37
 
 
38
    This has some shortcuts for reading and writing them.
 
39
    """
 
40
    def __init__(self, transport):
 
41
        self._transport = transport
 
42
        self._cache = {}
 
43
        self.enable_cache = False
 
44
 
72
45
 
73
46
    def filename(self, file_id):
74
47
        """Return the path relative to the transport root."""
75
 
        file_id = osutils.safe_file_id(file_id)
76
 
        return self._relpath(file_id)
77
 
 
78
 
    def __iter__(self):
79
 
        suffixes = self._versionedfile_class.get_suffixes()
80
 
        ids = set()
81
 
        for relpath in self._iter_files_recursive():
82
 
            for suffix in suffixes:
83
 
                if relpath.endswith(suffix):
84
 
                    # TODO: use standard remove_suffix function
85
 
                    escaped_id = os.path.basename(relpath[:-len(suffix)])
86
 
                    file_id = self._unescape(escaped_id)
87
 
                    if file_id not in ids:
88
 
                        ids.add(file_id)
89
 
                        yield file_id
90
 
                    break # only one suffix can match
91
 
 
92
 
    def has_id(self, file_id):
93
 
        file_id = osutils.safe_file_id(file_id)
94
 
        suffixes = self._versionedfile_class.get_suffixes()
95
 
        filename = self.filename(file_id)
96
 
        for suffix in suffixes:
97
 
            if not self._transport.has(filename + suffix):
98
 
                return False
99
 
        return True
100
 
 
101
 
    def get_empty(self, file_id, transaction):
102
 
        """Get an empty weave, which implies deleting the existing one first."""
103
 
        file_id = osutils.safe_file_id(file_id)
104
 
        if self.has_id(file_id):
105
 
            self.delete(file_id, transaction)
106
 
        return self.get_weave_or_empty(file_id, transaction)
107
 
 
108
 
    def delete(self, file_id, transaction):
109
 
        """Remove file_id from the store."""
110
 
        file_id = osutils.safe_file_id(file_id)
111
 
        suffixes = self._versionedfile_class.get_suffixes()
112
 
        filename = self.filename(file_id)
113
 
        for suffix in suffixes:
114
 
            self._transport.delete(filename + suffix)
115
 
        self._clear_cache_id(file_id, transaction)
 
48
        return file_id + '.weave'
116
49
 
117
50
    def _get(self, file_id):
118
51
        return self._transport.get(self.filename(file_id))
119
52
 
120
53
    def _put(self, file_id, f):
121
 
        fn = self.filename(file_id)
122
 
        try:
123
 
            return self._transport.put_file(fn, f, mode=self._file_mode)
124
 
        except errors.NoSuchFile:
125
 
            if not self._prefixed:
126
 
                raise
127
 
            self._transport.mkdir(os.path.dirname(fn), mode=self._dir_mode)
128
 
            return self._transport.put_file(fn, f, mode=self._file_mode)
129
 
 
130
 
    def get_weave(self, file_id, transaction, _filename=None):
131
 
        """Return the VersionedFile for file_id.
132
 
 
133
 
        :param _filename: filename that would be returned from self.filename for
134
 
        file_id. This is used to reduce duplicate filename calculations when
135
 
        using 'get_weave_or_empty'. FOR INTERNAL USE ONLY.
136
 
        """
137
 
        file_id = osutils.safe_file_id(file_id)
138
 
        weave = transaction.map.find_weave(file_id)
139
 
        if weave is not None:
140
 
            #mutter("cache hit in %s for %s", self, file_id)
141
 
            return weave
142
 
        if _filename is None:
143
 
            _filename = self.filename(file_id)
144
 
        if transaction.writeable():
145
 
            w = self._versionedfile_class(_filename, self._transport, self._file_mode,
146
 
                                          **self._versionedfile_kwargs)
147
 
            transaction.map.add_weave(file_id, w)
148
 
            transaction.register_dirty(w)
149
 
        else:
150
 
            w = self._versionedfile_class(_filename,
151
 
                                          self._transport,
152
 
                                          self._file_mode,
153
 
                                          create=False,
154
 
                                          access_mode='r',
155
 
                                          **self._versionedfile_kwargs)
156
 
            transaction.map.add_weave(file_id, w)
157
 
            transaction.register_clean(w, precious=self._precious)
 
54
        return self._transport.put(self.filename(file_id), f)
 
55
 
 
56
 
 
57
    def get_weave(self, file_id):
 
58
        if self.enable_cache:
 
59
            if file_id in self._cache:
 
60
                return self._cache[file_id]
 
61
        w = read_weave(self._get(file_id))
 
62
        if self.enable_cache:
 
63
            self._cache[file_id] = w
158
64
        return w
159
65
 
160
 
    @deprecated_method(zero_eight)
161
 
    def get_lines(self, file_id, rev_id, transaction):
 
66
 
 
67
    def get_lines(self, file_id, rev_id):
162
68
        """Return text from a particular version of a weave.
163
69
 
164
 
        Returned as a list of lines.
165
 
        """
166
 
        file_id = osutils.safe_file_id(file_id)
167
 
        w = self.get_weave(file_id, transaction)
168
 
        return w.get_lines(rev_id)
169
 
    
170
 
    def _make_new_versionedfile(self, file_id, transaction,
171
 
        known_missing=False, _filename=None):
172
 
        """Make a new versioned file.
173
 
        
174
 
        :param _filename: filename that would be returned from self.filename for
175
 
        file_id. This is used to reduce duplicate filename calculations when
176
 
        using 'get_weave_or_empty'. FOR INTERNAL USE ONLY.
177
 
        """
178
 
        if not known_missing and self.has_id(file_id):
179
 
            self.delete(file_id, transaction)
180
 
        if _filename is None:
181
 
            _filename = self.filename(file_id)
182
 
        try:
183
 
            # we try without making the directory first because thats optimising
184
 
            # for the common case.
185
 
            weave = self._versionedfile_class(_filename, self._transport, self._file_mode, create=True,
186
 
                                              **self._versionedfile_kwargs)
187
 
        except errors.NoSuchFile:
188
 
            if not self._prefixed:
189
 
                # unexpected error - NoSuchFile is expected to be raised on a
190
 
                # missing dir only and that only occurs when we are prefixed.
191
 
                raise
192
 
            self._transport.mkdir(self.hash_prefix(file_id), mode=self._dir_mode)
193
 
            weave = self._versionedfile_class(_filename, self._transport, 
194
 
                                              self._file_mode, create=True,
195
 
                                              **self._versionedfile_kwargs)
196
 
        return weave
197
 
 
198
 
    def get_weave_or_empty(self, file_id, transaction):
199
 
        """Return a weave, or an empty one if it doesn't exist."""
200
 
        # This is typically used from 'commit' and 'fetch/push/pull' where 
201
 
        # we scan across many versioned files once. As such the small overhead
202
 
        # of calculating the filename before doing a cache lookup is more than
203
 
        # compensated for by not calculating the filename when making new
204
 
        # versioned files.
205
 
        file_id = osutils.safe_file_id(file_id)
206
 
        _filename = self.filename(file_id)
207
 
        try:
208
 
            return self.get_weave(file_id, transaction, _filename=_filename)
209
 
        except errors.NoSuchFile:
210
 
            weave = self._make_new_versionedfile(file_id, transaction,
211
 
                known_missing=True, _filename=_filename)
212
 
            transaction.map.add_weave(file_id, weave)
213
 
            # has to be dirty - its able to mutate on its own.
214
 
            transaction.register_dirty(weave)
215
 
            return weave
216
 
 
217
 
    @deprecated_method(zero_eight)
218
 
    def put_weave(self, file_id, weave, transaction):
219
 
        """This is a deprecated API: It writes an entire collection of ids out.
220
 
        
221
 
        This became inappropriate when we made a versioned file api which
222
 
        tracks the state of the collection of versions for a single id.
223
 
        
224
 
        Its maintained for backwards compatability but will only work on
225
 
        weave stores - pre 0.8 repositories.
226
 
        """
227
 
        file_id = osutils.safe_file_id(file_id)
228
 
        self._put_weave(file_id, weave, transaction)
229
 
 
230
 
    def _put_weave(self, file_id, weave, transaction):
231
 
        """Preserved here for upgrades-to-weaves to use."""
232
 
        myweave = self._make_new_versionedfile(file_id, transaction)
233
 
        myweave.join(weave)
234
 
 
235
 
    @deprecated_method(zero_eight)
236
 
    def add_text(self, file_id, rev_id, new_lines, parents, transaction):
237
 
        """This method was a shorthand for 
238
 
 
239
 
        vfile = self.get_weave_or_empty(file_id, transaction)
240
 
        vfile.add_lines(rev_id, parents, new_lines)
241
 
        """
242
 
        file_id = osutils.safe_file_id(file_id)
243
 
        vfile = self.get_weave_or_empty(file_id, transaction)
244
 
        vfile.add_lines(rev_id, parents, new_lines)
245
 
        
246
 
    @deprecated_method(zero_eight)
247
 
    def add_identical_text(self, file_id, old_rev_id, new_rev_id, parents,
248
 
                           transaction):
249
 
        """This method was a shorthand for
250
 
 
251
 
        vfile = self.get_weave_or_empty(file_id, transaction)
252
 
        vfile.clone_text(new_rev_id, old_rev_id, parents)
253
 
        """
254
 
        file_id = osutils.safe_file_id(file_id)
255
 
        old_rev_id = osutils.safe_revision_id(old_rev_id)
256
 
        new_rev_id = osutils.safe_revision_id(new_rev_id)
257
 
        vfile = self.get_weave_or_empty(file_id, transaction)
258
 
        vfile.clone_text(new_rev_id, old_rev_id, parents)
259
 
 
260
 
    def copy(self, source, result_id, transaction):
261
 
        """Copy the source versioned file to result_id in this store."""
262
 
        self._clear_cache_id(result_id, transaction)
263
 
        source.copy_to(self.filename(result_id), self._transport)
264
 
 
265
 
    def copy_all_ids(self, store_from, pb=None, from_transaction=None,
266
 
                     to_transaction=None):
267
 
        """Copy all the file ids from store_from into self."""
268
 
        if from_transaction is None:
269
 
            warn("Please pass from_transaction into "
270
 
                 "versioned_store.copy_all_ids.", stacklevel=2)
271
 
        if to_transaction is None:
272
 
            warn("Please pass to_transaction into "
273
 
                 "versioned_store.copy_all_ids.", stacklevel=2)
274
 
        if not store_from.listable():
275
 
            raise errors.UnlistableStore(store_from)
276
 
        ids = []
277
 
        for count, file_id in enumerate(store_from):
278
 
            if pb:
279
 
                pb.update('listing files', count, count)
280
 
            ids.append(file_id)
281
 
        if pb:
282
 
            pb.clear()
283
 
        mutter('copy_all ids: %r', ids)
284
 
        self.copy_multi(store_from, ids, pb=pb,
285
 
                        from_transaction=from_transaction,
286
 
                        to_transaction=to_transaction)
287
 
 
288
 
    def copy_multi(self, from_store, file_ids, pb=None, from_transaction=None,
289
 
                   to_transaction=None):
290
 
        """Copy all the versions for multiple file_ids from from_store.
291
 
        
292
 
        :param from_transaction: required current transaction in from_store.
293
 
        """
294
 
        from bzrlib.transactions import PassThroughTransaction
295
 
        assert isinstance(from_store, WeaveStore)
296
 
        if from_transaction is None:
297
 
            warn("WeaveStore.copy_multi without a from_transaction parameter "
298
 
                 "is deprecated. Please provide a from_transaction.",
299
 
                 DeprecationWarning,
300
 
                 stacklevel=2)
301
 
            # we are reading one object - caching is irrelevant.
302
 
            from_transaction = PassThroughTransaction()
303
 
        if to_transaction is None:
304
 
            warn("WeaveStore.copy_multi without a to_transaction parameter "
305
 
                 "is deprecated. Please provide a to_transaction.",
306
 
                 DeprecationWarning,
307
 
                 stacklevel=2)
308
 
            # we are copying single objects, and there may be open tranasactions
309
 
            # so again with the passthrough
310
 
            to_transaction = PassThroughTransaction()
311
 
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
312
 
        try:
313
 
            file_ids = [osutils.safe_file_id(f) for f in file_ids]
314
 
            for count, f in enumerate(file_ids):
315
 
                mutter("copy weave {%s} into %s", f, self)
316
 
                pb.update('copy', count, len(file_ids))
317
 
                # if we have it in cache, its faster.
318
 
                # joining is fast with knits, and bearable for weaves -
319
 
                # indeed the new case can be optimised if needed.
320
 
                target = self._make_new_versionedfile(f, to_transaction)
321
 
                target.join(from_store.get_weave(f, from_transaction))
322
 
        finally:
323
 
            pb.finished()
324
 
 
325
 
    def total_size(self):
326
 
        count, bytes =  super(VersionedFileStore, self).total_size()
327
 
        return (count / len(self._versionedfile_class.get_suffixes())), bytes
328
 
 
329
 
WeaveStore = VersionedFileStore
 
70
        Returned as a list of lines."""
 
71
        w = self.get_weave(file_id)
 
72
        return w.get(w.lookup(rev_id))
 
73
    
 
74
 
 
75
    def get_weave_or_empty(self, file_id):
 
76
        """Return a weave, or an empty one if it doesn't exist.""" 
 
77
        try:
 
78
            inf = self._get(file_id)
 
79
        except NoSuchFile:
 
80
            return Weave(weave_name=file_id)
 
81
        else:
 
82
            return read_weave(inf)
 
83
    
 
84
 
 
85
    def put_weave(self, file_id, weave):
 
86
        """Write back a modified weave"""
 
87
        if self.enable_cache:
 
88
            self._cache[file_id] = weave
 
89
 
 
90
        sio = StringIO()
 
91
        write_weave_v5(weave, sio)
 
92
        sio.seek(0)
 
93
 
 
94
        self._put(file_id, sio)
 
95
 
 
96
 
 
97
    def add_text(self, file_id, rev_id, new_lines, parents):
 
98
        w = self.get_weave_or_empty(file_id)
 
99
        parent_idxs = map(w.lookup, parents)
 
100
        w.add(rev_id, parent_idxs, new_lines)
 
101
        self.put_weave(file_id, w)
 
102
        
 
103