Skip to content

Atomic Operations

Jan Wiemer edited this page Dec 29, 2020 · 6 revisions

Atomic Operations

Getting a single object from the store is generally an atomic operation. All modifications done by other concurrent transactions are either committed or not. Technically the access on the value is synchronized with the commit operation on this store (only on the current store not all others). However if we access a whole collection of values this operation is no longer atomic by default. Between fetching two objects of the collection another transaction may commit new values for those. This may lead to the phantom read phenomenon discussed in the Transactions chapter.

Sometimes it is desirable to avoid other commits to interfere with the collection of values matching a certain condition. Sometimes even commits on other stores shall be avoided to guarantee a consistent view of the data. For this purpose the API provides two types of methods to execute some logic synchronized with the commits:

  • The execution of atomic operations (suffix Atomic) is synchronized with the commits and other atomic operations on the store they are called.

  • The execution of global atomic operations (suffix GlobalAtomic) is synchronized with the commits and other atomic operations on the store they are called and the commits and global atomic operations on any other store.

The API of the store provides some methods to execute atomic of global atomic operations:

void executeAtomic(Runnable atomicOperation);

Execute the passed operation as an atomic operation.

<R> R computeAtomic(Supplier<R> atomicOperation);

Execute the passed operation computing the returned result as an atomic operation.

void executeGlobalAtomic(Runnable atomicOperation);

Execute the passed operation as a global atomic operation.

<R> R computeGlobalAtomic(Supplier<R> atomicOperation);

Execute the passed operation computing the returned result as a global atomic operation.

List<TV> getAllAtomic(Predicate<TV> filter);

Collect the elements matching the filter as an atomic operation.

List<TV> getAllReadOnlyAtomic(Predicate<TV> filter);

Collect the read only views of the elements matching the filter as an atomic operation.

Next Chapter: Other Operations