versioned_collection.collection.tracking_collections.lock module

class versioned_collection.collection.tracking_collections.lock.LockCollection(*args: Any, **kwargs: Any)[source]

Bases: Collection

Collection holding the locking information.

This collection is necessary when multiple users are concurrently interacting with a VersionedCollection.

The documents in this collection have the following format:

{
    _id: ObjectId(...)
    collection_name: 'name of the tracked collection'
    locked: True/ False
}

This collection is shared per database, holding information about each tracked collection in the database. By using atomic updates, each VersionedCollection can implement a simple locking mechanism.

class SCHEMA(collection_name: str, locked: bool)[source]

Bases: object

collection_name: str
locked: bool
init_lock(collection: str)[source]

Initialise the lock.

is_locked(collection: str) bool | None[source]
lock_acquire(collection: str) bool[source]

Acquire the lock for the given collection.

Parameters:

collection – The name of the collection to lock.

Returns:

Whether the process waited for the lock.

lock_release(collection: str) bool[source]

Release the lock for the given collection.

Parameters:

collection – The name of the collection to unlock.

Returns:

Whether the collection was locked.

remove_collection(collection: str) None[source]

Remove the locking information for the given collection.

Parameters:

collection – The name of the collection for which to remove the lock.

try_lock_acquire(collection: str) bool[source]

Try to acquire the lock for the given collection.

Returns:

True if the lock is successfully acquired, False if the lock is held by another process.