Skip to content

Persistence Strategy

AnirudhRamanan edited this page Mar 18, 2016 · 2 revisions

Persistence Strategy is responsible for persisting the batches. The persistence is performed in two stages, one during the events are generated, and second when the batches are ready.

All the operations are done in the inMemory array list. So if you are using SQL or Tape as persistence, then whenever the app starts, all the events that are stored in those persistence will get synced in the inMemory array list, so as to provide a faster and performant operations.

All these persistence strategies discussed below will extend the InMemoryPersistenceStrategy, so all the events that are added to the persistence will also get added to the in memory array list.

In-depth Description

Persistence Strategy

An interface with following methods

  • boolean add(Collection<E> dataCollection) : This method tells the persistence strategy about the added collection of data and persist it according to the provided implementation of persistenceStrategy. It will return true if that data has been added to the persistence layer(used to prevent duplicacy)

  • Collection<E> getData() : This method returns the collection of data stored in the persistence layer.

  • void removeData(Collection<E> dataCollection) : This method is used to remove particular collection of data from the persistence layer.

  • void onInitialized() : This method is used to initialized all the instances that are used.

Following are the different types of Persistence Strategies that are provided by the library.

  • InMemoryPersistenceStrategy : As the name suggests, all the events/data are persisted in the inMemory array list. Or you can also call it as no persistence, as whenever the app restarts all the events in the inMemory will be cleared.

  • SQLPersistenceStrategy : The SQLPersistenceStrategy uses the database to store the events.

    • public SQLPersistenceStrategy(SerializationStrategy<E, ? extends Batch> serializationStrategy, String databaseName, Context context) : The constructor takes in the SerializationStrategy for serializing and deserializing the objects for storing and removing from the database. It also takes in the database name, providing more flexibility in creating the database.

    The database has three columns :

    • String KEY_ID : It is used to store the timestamp of the data while it got generated.
    • String KEY_DATA : It is used to store the Data object.
    • String KEY_EXPIRY : Currently not being used.
  • TapePersistenceStrategy : The TapePersistenceStrategy uses the queue file to store the events.

    • public TapePersistenceStrategy(String filePath, SerializationStrategy<E, ? extends Batch> serializationStrategy) : It takes in the filePath to create a file to store events. This file is used by the queue file which in turn will store the events.
  • TagBasedPersistenceStrategy : The TagBasedPersistenceStrategy which implements the PersistenceStrategy interface, will have a specific persistence strategy for specific tags. So each different tags can have different persistence strategy.

    • public TagBasedPersistenceStrategy(Tag tag, PersistenceStrategy<E> persistenceStrategy) : The constructor takes in the tag and its respective persistence strategy, and in turn it will route the data for a specific tag to its persistence strategy.
Clone this wiki locally