~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-11 11:47:36 UTC
  • mfrom: (5200.3.8 lock_return)
  • Revision ID: pqm@pqm.ubuntu.com-20100511114736-mc1sq9zyo3vufec7
(lifeless) Provide a consistent interface to Tree, Branch,
 Repository where lock methods return an object with an unlock method to
 unlock the lock. This breaks the API for Branch,
 Repository on their lock_write methods. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
from bzrlib.testament import Testament
53
53
""")
54
54
 
 
55
from bzrlib import (
 
56
    errors,
 
57
    registry,
 
58
    )
55
59
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
56
60
from bzrlib.inter import InterObject
57
61
from bzrlib.inventory import (
60
64
    ROOT_ID,
61
65
    entry_factory,
62
66
    )
63
 
from bzrlib.lock import _RelockDebugMixin
64
 
from bzrlib import (
65
 
    errors,
66
 
    registry,
67
 
    )
 
67
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
68
68
from bzrlib.trace import (
69
69
    log_exception_quietly, note, mutter, mutter_callsite, warning)
70
70
 
870
870
        # versioned roots do not change unless the tree found a change.
871
871
 
872
872
 
 
873
class RepositoryWriteLockResult(LogicalLockResult):
 
874
    """The result of write locking a repository.
 
875
 
 
876
    :ivar repository_token: The token obtained from the underlying lock, or
 
877
        None.
 
878
    :ivar unlock: A callable which will unlock the lock.
 
879
    """
 
880
 
 
881
    def __init__(self, unlock, repository_token):
 
882
        LogicalLockResult.__init__(self, unlock)
 
883
        self.repository_token = repository_token
 
884
 
 
885
    def __repr__(self):
 
886
        return "RepositoryWriteLockResult(%s, %s)" % (self.repository_token,
 
887
            self.unlock)
 
888
 
 
889
 
873
890
######################################################################
874
891
# Repositories
875
892
 
1386
1403
        data during reads, and allows a 'write_group' to be obtained. Write
1387
1404
        groups must be used for actual data insertion.
1388
1405
 
 
1406
        A token should be passed in if you know that you have locked the object
 
1407
        some other way, and need to synchronise this object's state with that
 
1408
        fact.
 
1409
 
 
1410
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
 
1411
 
1389
1412
        :param token: if this is already locked, then lock_write will fail
1390
1413
            unless the token matches the existing lock.
1391
1414
        :returns: a token if this instance supports tokens, otherwise None.
1394
1417
        :raises MismatchedToken: if the specified token doesn't match the token
1395
1418
            of the existing lock.
1396
1419
        :seealso: start_write_group.
1397
 
 
1398
 
        A token should be passed in if you know that you have locked the object
1399
 
        some other way, and need to synchronise this object's state with that
1400
 
        fact.
1401
 
 
1402
 
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
 
1420
        :return: A RepositoryWriteLockResult.
1403
1421
        """
1404
1422
        locked = self.is_locked()
1405
 
        result = self.control_files.lock_write(token=token)
 
1423
        token = self.control_files.lock_write(token=token)
1406
1424
        if not locked:
1407
1425
            self._warn_if_deprecated()
1408
1426
            self._note_lock('w')
1410
1428
                # Writes don't affect fallback repos
1411
1429
                repo.lock_read()
1412
1430
            self._refresh_data()
1413
 
        return result
 
1431
        return RepositoryWriteLockResult(self.unlock, token)
1414
1432
 
1415
1433
    def lock_read(self):
 
1434
        """Lock the repository for read operations.
 
1435
 
 
1436
        :return: An object with an unlock method which will release the lock
 
1437
            obtained.
 
1438
        """
1416
1439
        locked = self.is_locked()
1417
1440
        self.control_files.lock_read()
1418
1441
        if not locked:
1421
1444
            for repo in self._fallback_repositories:
1422
1445
                repo.lock_read()
1423
1446
            self._refresh_data()
 
1447
        return LogicalLockResult(self.unlock)
1424
1448
 
1425
1449
    def get_physical_lock_status(self):
1426
1450
        return self.control_files.get_physical_lock_status()