aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/att/research/mdbc/mixins/StagingTable.java
blob: 7da348df48aeead06ec6a1cffcdc87156ff83bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.att.research.mdbc.mixins;

import java.io.Serializable;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;
import org.apache.commons.lang3.tuple.Pair;
import org.json.JSONObject;

import com.att.research.logging.EELFLoggerDelegate;

public class StagingTable implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7583182634761771943L;
	private transient static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(StagingTable.class);
	private HashMap<String,Deque<Operation>> operations;
	
	public StagingTable() {
		operations = new HashMap<>();
	}
	
	synchronized public void addOperation(String key, OperationType type, String oldVal, String newVal) {
		if(!operations.containsKey(key)) {
			operations.put(key, new LinkedList<>());
		}
		operations.get(key).add(new Operation(type,newVal,oldVal));
	}
	
	synchronized public Deque<Pair<String,Operation>> getIterableSnapshot() throws NoSuchFieldException{
		Deque<Pair<String,Operation>> response=new LinkedList<Pair<String,Operation>>();
		//\TODO: check if we can just return the last change to a given key 
		Set<String> keys = operations.keySet();
		for(String key : keys) {
			Deque<Operation> ops = operations.get(key);
			if(ops.isEmpty()) {
				logger.error(EELFLoggerDelegate.errorLogger, "Invalid state of the Operation data structure when creating snapshot");
				throw new NoSuchFieldException("Invalid state of the operation data structure");
			}
			response.add(Pair.of(key,ops.getLast()));
		}
		return response;
	}
	
	synchronized public void clean() {
		operations.clear();
	}
}