Skip to main content

Versioned Key-Value operations

In this section we will go through the versioned key-value operations on the basis of MappedContainer. The same operations can be executed with GeneralContainer, but the calls will look more complicated.

Create a Versioned Key​

The following operation will create a versioned key that will be automatically removed from the storage after one hour.

final MappedContainer<String> container = //...
final long unixEndOfLife = System.currentTimeMillis() + 1000*60*60;
final VersionedObject<String,String> vo = VersionedObject.create("versioned-key", "value", 1L, unixEndOfLife);

This operation returns VersionedObject that contains the key, value and information about version.

Get a Versioned Key​

To get the versioned key the following command is used:

final MappedContainer<String> container = //...
final VersionedObject<String,String> vo = VersionedObject.get("versioned-key", String.class);

The VersionedObject contains all the information about the version, that will be provided in the next update operation, what will be compared to the rostore service current version, and in case of any concurrent updates, it will be detected and used.

Update a Versioned Key​

The operation works when one existing VersionedObject already exist:

final MappedContainer<String> container = // ... mapped container
final VersionedObject<String,String> vo = // ... original version object
final VersionedObject<String,String> updatedVO = container.update(vo, (v) -> "new-value");

Update command will replace the previous value by the one provided in the update function and execute the respecive update operation on the storage. In case the original version of the key has been concurrently updated on the rostore service, the operation would load the new version from the storage and execute the update function again.

Of cause, this operation can fail again, in case another processes changes the version during the update operation is executed.

In this case the get operation will be executed again and again, until it will be successful or some predifined number of attempts will fail. The operation will introduce a jitter in the operation to avoid a grid-lock operation on massive updates.