/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/memorytree.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""MemoryTree object.
 
18
 
 
19
See MemoryTree for more details.
 
20
"""
 
21
 
 
22
from __future__ import absolute_import
 
23
 
 
24
import os
 
25
 
 
26
from bzrlib import (
 
27
    errors,
 
28
    mutabletree,
 
29
    revision as _mod_revision,
 
30
    )
 
31
from bzrlib.decorators import needs_read_lock
 
32
from bzrlib.inventory import Inventory
 
33
from bzrlib.osutils import sha_file
 
34
from bzrlib.mutabletree import needs_tree_write_lock
 
35
from bzrlib.transport.memory import MemoryTransport
 
36
 
 
37
 
 
38
class MemoryTree(mutabletree.MutableInventoryTree):
 
39
    """A MemoryTree is a specialisation of MutableTree.
 
40
 
 
41
    It maintains nearly no state outside of read_lock and write_lock
 
42
    transactions. (it keeps a reference to the branch, and its last-revision
 
43
    only).
 
44
    """
 
45
 
 
46
    def __init__(self, branch, revision_id):
 
47
        """Construct a MemoryTree for branch using revision_id."""
 
48
        self.branch = branch
 
49
        self.bzrdir = branch.bzrdir
 
50
        self._branch_revision_id = revision_id
 
51
        self._locks = 0
 
52
        self._lock_mode = None
 
53
 
 
54
    def is_control_filename(self, filename):
 
55
        # Memory tree doesn't have any control filenames
 
56
        return False
 
57
 
 
58
    @needs_tree_write_lock
 
59
    def _add(self, files, ids, kinds):
 
60
        """See MutableTree._add."""
 
61
        for f, file_id, kind in zip(files, ids, kinds):
 
62
            if kind is None:
 
63
                kind = 'file'
 
64
            if file_id is None:
 
65
                self._inventory.add_path(f, kind=kind)
 
66
            else:
 
67
                self._inventory.add_path(f, kind=kind, file_id=file_id)
 
68
 
 
69
    def basis_tree(self):
 
70
        """See Tree.basis_tree()."""
 
71
        return self._basis_tree
 
72
 
 
73
    @staticmethod
 
74
    def create_on_branch(branch):
 
75
        """Create a MemoryTree for branch, using the last-revision of branch."""
 
76
        revision_id = _mod_revision.ensure_null(branch.last_revision())
 
77
        return MemoryTree(branch, revision_id)
 
78
 
 
79
    def _gather_kinds(self, files, kinds):
 
80
        """See MutableTree._gather_kinds.
 
81
 
 
82
        This implementation does not care about the file kind of
 
83
        missing files, so is a no-op.
 
84
        """
 
85
 
 
86
    def get_file(self, file_id, path=None):
 
87
        """See Tree.get_file."""
 
88
        if path is None:
 
89
            path = self.id2path(file_id)
 
90
        return self._file_transport.get(path)
 
91
 
 
92
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
93
        """See Tree.get_file_sha1()."""
 
94
        if path is None:
 
95
            path = self.id2path(file_id)
 
96
        stream = self._file_transport.get(path)
 
97
        return sha_file(stream)
 
98
 
 
99
    def get_root_id(self):
 
100
        return self.path2id('')
 
101
 
 
102
    def _comparison_data(self, entry, path):
 
103
        """See Tree._comparison_data."""
 
104
        if entry is None:
 
105
            return None, False, None
 
106
        return entry.kind, entry.executable, None
 
107
 
 
108
    @needs_tree_write_lock
 
109
    def rename_one(self, from_rel, to_rel):
 
110
        file_id = self.path2id(from_rel)
 
111
        to_dir, to_tail = os.path.split(to_rel)
 
112
        to_parent_id = self.path2id(to_dir)
 
113
        self._file_transport.move(from_rel, to_rel)
 
114
        self._inventory.rename(file_id, to_parent_id, to_tail)
 
115
 
 
116
    def path_content_summary(self, path):
 
117
        """See Tree.path_content_summary."""
 
118
        id = self.path2id(path)
 
119
        if id is None:
 
120
            return 'missing', None, None, None
 
121
        kind = self.kind(id)
 
122
        if kind == 'file':
 
123
            bytes = self._file_transport.get_bytes(path)
 
124
            size = len(bytes)
 
125
            executable = self._inventory[id].executable
 
126
            sha1 = None # no stat cache
 
127
            return (kind, size, executable, sha1)
 
128
        elif kind == 'directory':
 
129
            # memory tree does not support nested trees yet.
 
130
            return kind, None, None, None
 
131
        elif kind == 'symlink':
 
132
            raise NotImplementedError('symlink support')
 
133
        else:
 
134
            raise NotImplementedError('unknown kind')
 
135
 
 
136
    def _file_size(self, entry, stat_value):
 
137
        """See Tree._file_size."""
 
138
        if entry is None:
 
139
            return 0
 
140
        return entry.text_size
 
141
 
 
142
    @needs_read_lock
 
143
    def get_parent_ids(self):
 
144
        """See Tree.get_parent_ids.
 
145
 
 
146
        This implementation returns the current cached value from
 
147
            self._parent_ids.
 
148
        """
 
149
        return list(self._parent_ids)
 
150
 
 
151
    def has_filename(self, filename):
 
152
        """See Tree.has_filename()."""
 
153
        return self._file_transport.has(filename)
 
154
 
 
155
    def is_executable(self, file_id, path=None):
 
156
        return self._inventory[file_id].executable
 
157
 
 
158
    def kind(self, file_id):
 
159
        return self._inventory[file_id].kind
 
160
 
 
161
    def mkdir(self, path, file_id=None):
 
162
        """See MutableTree.mkdir()."""
 
163
        self.add(path, file_id, 'directory')
 
164
        if file_id is None:
 
165
            file_id = self.path2id(path)
 
166
        self._file_transport.mkdir(path)
 
167
        return file_id
 
168
 
 
169
    @needs_read_lock
 
170
    def last_revision(self):
 
171
        """See MutableTree.last_revision."""
 
172
        return self._branch_revision_id
 
173
 
 
174
    def lock_read(self):
 
175
        """Lock the memory tree for reading.
 
176
 
 
177
        This triggers population of data from the branch for its revision.
 
178
        """
 
179
        self._locks += 1
 
180
        try:
 
181
            if self._locks == 1:
 
182
                self.branch.lock_read()
 
183
                self._lock_mode = "r"
 
184
                self._populate_from_branch()
 
185
        except:
 
186
            self._locks -= 1
 
187
            raise
 
188
 
 
189
    def lock_tree_write(self):
 
190
        """See MutableTree.lock_tree_write()."""
 
191
        self._locks += 1
 
192
        try:
 
193
            if self._locks == 1:
 
194
                self.branch.lock_read()
 
195
                self._lock_mode = "w"
 
196
                self._populate_from_branch()
 
197
            elif self._lock_mode == "r":
 
198
                raise errors.ReadOnlyError(self)
 
199
        except:
 
200
            self._locks -= 1
 
201
            raise
 
202
 
 
203
    def lock_write(self):
 
204
        """See MutableTree.lock_write()."""
 
205
        self._locks += 1
 
206
        try:
 
207
            if self._locks == 1:
 
208
                self.branch.lock_write()
 
209
                self._lock_mode = "w"
 
210
                self._populate_from_branch()
 
211
            elif self._lock_mode == "r":
 
212
                raise errors.ReadOnlyError(self)
 
213
        except:
 
214
            self._locks -= 1
 
215
            raise
 
216
 
 
217
    def _populate_from_branch(self):
 
218
        """Populate the in-tree state from the branch."""
 
219
        self._set_basis()
 
220
        if self._branch_revision_id == _mod_revision.NULL_REVISION:
 
221
            self._parent_ids = []
 
222
        else:
 
223
            self._parent_ids = [self._branch_revision_id]
 
224
        self._inventory = Inventory(None, self._basis_tree.get_revision_id())
 
225
        self._file_transport = MemoryTransport()
 
226
        # TODO copy the revision trees content, or do it lazy, or something.
 
227
        inventory_entries = self._basis_tree.iter_entries_by_dir()
 
228
        for path, entry in inventory_entries:
 
229
            self._inventory.add(entry.copy())
 
230
            if path == '':
 
231
                continue
 
232
            if entry.kind == 'directory':
 
233
                self._file_transport.mkdir(path)
 
234
            elif entry.kind == 'file':
 
235
                self._file_transport.put_file(path,
 
236
                    self._basis_tree.get_file(entry.file_id))
 
237
            else:
 
238
                raise NotImplementedError(self._populate_from_branch)
 
239
 
 
240
    def put_file_bytes_non_atomic(self, file_id, bytes):
 
241
        """See MutableTree.put_file_bytes_non_atomic."""
 
242
        self._file_transport.put_bytes(self.id2path(file_id), bytes)
 
243
 
 
244
    def unlock(self):
 
245
        """Release a lock.
 
246
 
 
247
        This frees all cached state when the last lock context for the tree is
 
248
        left.
 
249
        """
 
250
        if self._locks == 1:
 
251
            self._basis_tree = None
 
252
            self._parent_ids = []
 
253
            self._inventory = None
 
254
            try:
 
255
                self.branch.unlock()
 
256
            finally:
 
257
                self._locks = 0
 
258
                self._lock_mode = None
 
259
        else:
 
260
            self._locks -= 1
 
261
 
 
262
    @needs_tree_write_lock
 
263
    def unversion(self, file_ids):
 
264
        """Remove the file ids in file_ids from the current versioned set.
 
265
 
 
266
        When a file_id is unversioned, all of its children are automatically
 
267
        unversioned.
 
268
 
 
269
        :param file_ids: The file ids to stop versioning.
 
270
        :raises: NoSuchId if any fileid is not currently versioned.
 
271
        """
 
272
        # XXX: This should be in mutabletree, but the inventory-save action
 
273
        # is not relevant to memory tree. Until that is done in unlock by
 
274
        # working tree, we cannot share the implementation.
 
275
        for file_id in file_ids:
 
276
            if self._inventory.has_id(file_id):
 
277
                self._inventory.remove_recursive_id(file_id)
 
278
            else:
 
279
                raise errors.NoSuchId(self, file_id)
 
280
 
 
281
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
 
282
        """See MutableTree.set_parent_trees()."""
 
283
        for revision_id in revision_ids:
 
284
            _mod_revision.check_not_reserved_id(revision_id)
 
285
        if len(revision_ids) == 0:
 
286
            self._parent_ids = []
 
287
            self._branch_revision_id = _mod_revision.NULL_REVISION
 
288
        else:
 
289
            self._parent_ids = revision_ids
 
290
            self._branch_revision_id = revision_ids[0]
 
291
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
 
292
        self._set_basis()
 
293
    
 
294
    def _set_basis(self):
 
295
        try:
 
296
            self._basis_tree = self.branch.repository.revision_tree(
 
297
                self._branch_revision_id)
 
298
        except errors.NoSuchRevision:
 
299
            if self._allow_leftmost_as_ghost:
 
300
                self._basis_tree = self.branch.repository.revision_tree(
 
301
                    _mod_revision.NULL_REVISION)
 
302
            else:
 
303
                raise
 
304
 
 
305
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
306
        """See MutableTree.set_parent_trees()."""
 
307
        if len(parents_list) == 0:
 
308
            self._parent_ids = []
 
309
            self._basis_tree = self.branch.repository.revision_tree(
 
310
                                   _mod_revision.NULL_REVISION)
 
311
        else:
 
312
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
 
313
                # a ghost in the left most parent
 
314
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
315
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
 
316
            if parents_list[0][1] is None or parents_list[0][1] == 'null:':
 
317
                self._basis_tree = self.branch.repository.revision_tree(
 
318
                                       _mod_revision.NULL_REVISION)
 
319
            else:
 
320
                self._basis_tree = parents_list[0][1]
 
321
            self._branch_revision_id = parents_list[0][0]