MicroStream Reference Manual
MicroStream HomeAPI Docs
4.0
4.0
  • Preface
  • System Requirements
  • License
  • Changelog
  • Installation
  • Data-Store
    • Overview
    • Getting Started
    • Root Instances
    • Configuration
      • Properties
      • Storage Files and Directories
      • Using Channels
      • Housekeeping
      • Backup
      • Lock File
    • Storage Targets
      • Local File System
      • SQL Databases
        • Oracle
        • MySQL
        • SQLite
      • Blob Stores
        • Oracle Cloud
        • Oracle NoSQL
        • Coherence
    • Storing Data
      • Convenience Methods and Explicit Storing (Transactions)
      • Lazy and Eager Storing
      • Transient Fields
      • Best Practice
    • Loading Data
      • Lazy Loading
        • Touched Timestamp, Null-Safe Variant
        • Clearing Lazy References
    • Deleting Data
    • Queries
    • Application Life-Cycle
    • Legacy Type Mapping
      • User Interaction
    • Backup Strategies
    • Import / Export
    • Housekeeping
    • Customizing
      • Custom Type Handler
      • Custom Legacy Type Handler
      • Custom Class Loader
      • Custom Storing Behavior
      • Optional Storage Manager Reference in Entities
    • REST Interface
      • Setup
      • REST API
      • Client GUI
    • FAQ
      • Data Model
      • Data Management
      • File Storage
      • Java Features
      • Miscellaneous
    • Addendum
      • Supported Java Features
      • Specialized Type Handlers
      • Examples and Demo Projects
  • Cache
    • Overview
    • Getting Started
    • Configuration
      • Properties
      • Storage
    • Use Cases
      • Hibernate Second Level Cache
      • Spring Cache
  • Basic Concepts
    • Layered Entities
      • Configuration
      • Defining Entities
      • Creating Entities
      • Updating Entities
      • Versioning
      • Logging
      • Multiple Layers
    • Wrapping
      • Configuration
      • Usage
Powered by GitBook
On this page
Export as PDF
  1. Basic Concepts
  2. Layered Entities

Versioning

PreviousUpdating EntitiesNextLogging

Last updated 3 years ago

This is the manual for older MicroStream versions (Version < 5.0).

The new documentation (Version >= 5.0) is located at:

https://docs.microstream.one/

An arbitrary amount of logic layers can be added to entities. Let's use the predefined versioning layer. It will keep track of all changes. Technically every new data layer which is added by the updater, will create a new version entry.

EntityVersionContext<Long> versionContext = 
	EntityVersionContext.AutoIncrementingLong();
		
Person john = PersonCreator.New()
	.addLayer(versionContext)
	.firstName("John")
	.lastName("Doe")
	.create();

Let's have a look at the debugger:

Now the versioning layer is chained between the identity layer and the data layer.

If we update the entity a few times, we will see how the versioning layer works. In this case we use an auto-incrementing Long as key.

PersonUpdater.setLastName(john, "Smith");
PersonUpdater.setLastName(john, "Archer");
PersonUpdater.setLastName(john, "Bennett");
john.lastName() // now returns "Bennett"

If you want to access older versions use the context:

versionContext.versions(john).get(1L); // -> "John Smith"

To limit the amount of preserved versions, a cleaner can be utilized:

EntityVersionCleaner<Long> versionCleaner = 
    EntityVersionCleaner.AmountPreserving(10);
EntityVersionContext<Long> versionContext = 
    EntityVersionContext.AutoIncrementingLong(cleaner);

This will keep only the last ten versions of the person.

Additionally to number keys, timestamps can be used as well.

EntityVersionContext<Long> systemTimeContext = 
    EntityVersionContext.AutoIncrementingSystemTimeMillis();
EntityVersionContext<Long> nanoTimeContext = 
    EntityVersionContext.AutoIncrementingSystemNanoTime();
EntityVersionContext<Instant> instantContext = 
    EntityVersionContext.AutoIncrementingInstant();

They can be preserved for a specific time range:

EntityVersionCleaner<Instant> cleaner = 
    EntityVersionCleaner.AgePreservingInstant(Duration.of(1, ChronoUnit.YEARS));
EntityVersionContext<Instant> context = 
    EntityVersionContext.AutoIncrementingInstant(cleaner);

The version context can be used as a shared state object. So you can control versioning for multiple entities at once, or even for the hole entity graph.

The auto-incrementing contexts take care of the key creation. If you need to control it by yourself, use the mutable context. But be aware that you have to set the version before updating any data, otherwise the current one will be overwritten.

EntityVersionContext.Mutable<String> versionContext = 
    EntityVersionContext.Mutable();

versionContext.currentVersion("rev-1");

Person john = PersonCreator.New()
    .addLayer(versionContext)
    .firstName("John")
    .lastName("Doe")
    .create();

versionContext.currentVersion("rev-2");
PersonUpdater.setLastName(john, "Smith");

versionContext.currentVersion("rev-3");
PersonUpdater.setLastName(john, "Archer");

versionContext.currentVersion("rev-4");
PersonUpdater.setLastName(john, "Bennett");