# Versioning

{% hint style="warning" %}
**This is the manual for older MicroStream versions (Version < 5.0).**

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

[https://docs.microstream.one/](https://docs.microstream.one/manual)
{% endhint %}

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.

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

Let's have a look at the debugger:

<div align="left"><img src="https://2778493744-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlDABTSyLJZUmpSp2tl%2F-LvGR4RwZxMy0m8HGIW_%2F-LvGSHQWpIa43QMi8LzQ%2Fimage.png?alt=media&#x26;token=59640e70-fa97-407a-8846-0a5e96139e91" alt=""></div>

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.

```java
PersonUpdater.setLastName(john, "Smith");
PersonUpdater.setLastName(john, "Archer");
PersonUpdater.setLastName(john, "Bennett");
```

<div align="left"><img src="https://2778493744-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlDABTSyLJZUmpSp2tl%2F-LvGR4RwZxMy0m8HGIW_%2F-LvGSNAr6O3pwCeOOXad%2Fimage.png?alt=media&#x26;token=00b7d354-8809-4ab0-98a1-c3ca374a530f" alt=""></div>

```java
john.lastName() // now returns "Bennett"
```

If you want to access older versions use the context:

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

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

```java
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.

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

They can be preserved for a specific time range:

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

{% hint style="info" %}
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.
{% endhint %}

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.

```java
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");
```
