powered by ASAP

1. What you will learn

In this short summary you will get an idea on how data in PAK is persisted.

2. Basics

The IPersistenceService-interface is the main component involved in storing and persisting your data. While an Engine is running, the Persistence Service functions as the datastore, making it the direct entry point for all data created or consumed by any Command. The Persistence Service should essentially function as a key-value database.

3. Signature

Before giving a short example on how the interface should be used, this section describes the signature of the IPersistenceService interface, as well as an overview on which methods have to be implemented.

3.1. Transaction Model

The interface offers the ability to implement the commonly known ACID-principle by declaring different methods based on transactions.

void begin() throws PersistenceException;

Begins a new transaction in the current Persistence; Should always be thread dependent

void commit() throws PersistenceException;

Finishes the current transaction by committing all changes of the current thread

void rollback() throws PersistenceException;

Undoes all changes made in the current transaction and finishes it.

While you can choose not to follow the transaction model, be advised that your implementation should ensure thread-safety

3.2. Data Processing

The interface offers two operations relating to data processing. find for looking up values stored in the datastore and store in order to populate it.

Object find(final String key);

Tries retrieving the object for the specified key from the Persistence, returns null if no value was found.

Map<String, Object> find(final Collection<String> key);

Tries retrieving all objects for the specified keys from the Persistence, if no value was found for a key, there will be no entry in the resulting map.

void store(final String key, final Object value);

Stores the given object for the specified key in the Persistence.

void store(final Map<String, ?> keyValues);

Stores all given entries in the Persistence.

You must implement the find and store method for single values. By default, the respective operations which take a map as an input only call your single element operations for every element of the map.

If the need for data deletion arises, implement this functionality for the store operation using IPersistenceService.InitialValue.class

4. Usage

This section briefly outlines a few examples on how an implementing Persistence should be used.

4.1. Accessing Data

private final IPersistenceService persistence = ...;

public void storeExample() {
	this.persistence.begin(); (1)
	this.persistence.store("testKey", "Hello World"); (2)
	this.persistence.commit(); (3)

public Object retrieveExample() {
    this.persistence.begin(); (1)
    try {
    	return this.persistence().find("testKey"); (2)
    } finally {
    	this.persistence.commit(); (3)
1 Before we perform actions on our Persistence we should start a new transaction, so possible mistakes may be rolled back
2 After we initialized our transaction we can either store or retrieve a value to/from the Persistence
3 Finally, when we are finished editing, we commit the transaction, thus closing it and saving the state

4.2. Data Deletion

The deletion of data is not a built-in specification as the deleting of data from the datastore should hardly be necessary. However, if you need the functionality the IPersistenceService offers a marker class for that reason:

/** marker class */
final class InitialValue {
    private InitialValue() {
        // private constructor this is only used via InitialValue.class

Let’s suppose we want to delete the object behind the key superSecretVar. We simply call store using our marker class like

persistence.store("superSecretVar", IPersistenceService.InitialValue.class);

4.3. Data Transformation

All data that is written to the Persistence Service will be in the JSON format by default. However, should your implementation be dependent on another data format, do not solve this in your Persistence, but rather use the designated interface IDataTransformer.

5. Default Implementations

There are two default implementations of the IPersistenceService offered by PAK.

  • The SimplePersistenceService: Functions as an in-memory Persistence which can be used analogous to a map. It is mostly used for prototyping and testing (Dependency: de.asap.pak.core:simple)

  • The RocksDBPersistence: Functions as a bridge between PAK and the RocksDB implementation. (Dependency: de.asap.pak.extra:pak-rocksdb)