~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
860
860
        # versioned roots do not change unless the tree found a change.
861
861
 
862
862
 
 
863
class RepositoryWriteLockResult(object):
 
864
    """The result of write locking a repository.
 
865
 
 
866
    :ivar repository_token: The token obtained from the underlying lock, or
 
867
        None.
 
868
    :ivar unlock: A callable which will unlock the lock.
 
869
    """
 
870
 
 
871
    def __init__(self, unlock, repository_token):
 
872
        self.repository_token = repository_token
 
873
        self.unlock = unlock
 
874
 
 
875
 
863
876
######################################################################
864
877
# Repositories
865
878
 
1376
1389
        data during reads, and allows a 'write_group' to be obtained. Write
1377
1390
        groups must be used for actual data insertion.
1378
1391
 
 
1392
        A token should be passed in if you know that you have locked the object
 
1393
        some other way, and need to synchronise this object's state with that
 
1394
        fact.
 
1395
 
 
1396
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
 
1397
 
1379
1398
        :param token: if this is already locked, then lock_write will fail
1380
1399
            unless the token matches the existing lock.
1381
1400
        :returns: a token if this instance supports tokens, otherwise None.
1384
1403
        :raises MismatchedToken: if the specified token doesn't match the token
1385
1404
            of the existing lock.
1386
1405
        :seealso: start_write_group.
1387
 
 
1388
 
        A token should be passed in if you know that you have locked the object
1389
 
        some other way, and need to synchronise this object's state with that
1390
 
        fact.
1391
 
 
1392
 
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
 
1406
        :return: A RepositoryWriteLockResult.
1393
1407
        """
1394
1408
        locked = self.is_locked()
1395
 
        result = self.control_files.lock_write(token=token)
 
1409
        token = self.control_files.lock_write(token=token)
1396
1410
        if not locked:
1397
1411
            self._warn_if_deprecated()
1398
1412
            self._note_lock('w')
1400
1414
                # Writes don't affect fallback repos
1401
1415
                repo.lock_read()
1402
1416
            self._refresh_data()
1403
 
        return result
 
1417
        return RepositoryWriteLockResult(self.unlock, token)
1404
1418
 
1405
1419
    def lock_read(self):
 
1420
        """Lock the repository for read operations.
 
1421
 
 
1422
        :return: An object with an unlock method which will release the lock
 
1423
            obtained.
 
1424
        """
1406
1425
        locked = self.is_locked()
1407
1426
        self.control_files.lock_read()
1408
1427
        if not locked:
1411
1430
            for repo in self._fallback_repositories:
1412
1431
                repo.lock_read()
1413
1432
            self._refresh_data()
 
1433
        return self
1414
1434
 
1415
1435
    def get_physical_lock_status(self):
1416
1436
        return self.control_files.get_physical_lock_status()