Snapshot Version Manager.

Navin Kumar
1 min readJul 9, 2022

The below code is for the SnapshotVersionManager in java

package com.navin.learn.snapshot;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class SnapShotVersionManager<K, V> {
private static volatile SnapShotVersionManager snapshotVersionManager;
private AtomicInteger snapshotVersion;
private final Map<Integer, Map<K, V>> valueMapped;

private SnapShotVersionManager(){
System.out.println("Instance is created");
valueMapped = new HashMap<>();
}

public static SnapShotVersionManager getInstance(){
if(snapshotVersionManager == null){
synchronized (SnapShotVersionManager.class){
if(snapshotVersionManager==null){
snapshotVersionManager = new SnapShotVersionManager();
}
}
}
return snapshotVersionManager;
}

public synchronized void snapshot(){
if(snapshotVersion == null){
throw new IllegalArgumentException();
}
int oldSnapshotVersion = snapshotVersion.get();
snapshotVersion.compareAndSet(oldSnapshotVersion, oldSnapshotVersion+1);
Map<K, V> oldData = valueMapped.get(oldSnapshotVersion);
Map<K, V> newData = new HashMap<>(oldData);
valueMapped.put(snapshotVersion.get(), newData);
}

public synchronized void put(K key, V value){
if(snapshotVersion == null){
snapshotVersion = new AtomicInteger(0);
Map<K, V> newEntry = new HashMap<>();
newEntry.put(key, value);
valueMapped.put(snapshotVersion.get(), newEntry);
}
valueMapped.get(snapshotVersion.get()).put(key, value);
}

public synchronized void put(K key, V value, final int version){
if(isVersionGreater(version)){
throw new IllegalArgumentException();
}
valueMapped.get(version).put(key, value);
}

public synchronized V getValue(K key){
if(snapshotVersion == null){
throw new IllegalArgumentException("not present any entry");
}
if(valueMapped.get(snapshotVersion.get()).containsKey(key)){
return valueMapped.get(snapshotVersion.get()).get(key);
}
return null;
}

private synchronized boolean isVersionGreater(final int version){
return version > snapshotVersion.get();
}

public synchronized V getValue(K key, final int version){
if(isVersionGreater(version)){
throw new IllegalArgumentException("version is not present");
}
if(valueMapped.get(version).containsKey(key)){
return valueMapped.get(version).get(key);
}
return null;
}

public synchronized void update(K key, V value, final int version){
if(isVersionGreater(version)){
throw new IllegalArgumentException("Version is not present");
}
if(!valueMapped.get(version).containsKey(key)){
throw new IllegalArgumentException("Key is not found");
}
this.put(key, value, version);
}

public static void main(String[] args) {
SnapShotVersionManager<String, String> snapShotVersionManager = SnapShotVersionManager.getInstance();
snapShotVersionManager.put("navin", "10");
System.out.println(snapShotVersionManager.getValue("navin", 0));
snapShotVersionManager.put("pranav", "11");
// System.out.println(snapShotVersionManager.getValue("pranav", 2));
snapShotVersionManager.snapshot();
snapShotVersionManager.put("viday", "234");
System.out.println(snapShotVersionManager.getValue("viday"));
System.out.println(snapShotVersionManager.getValue("viday", 0));

}

}

--

--