summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/persistency/MemoryOnlyClusterStorage.java
blob: a895c329893235290426d6b7fdaa8094d0ca88aa (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.c2kernel.persistency;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.c2kernel.entity.C2KLocalObject;

public class MemoryOnlyClusterStorage extends ClusterStorage {

    HashMap<Integer, Map<String, C2KLocalObject>> memoryCache = new HashMap<Integer, Map<String, C2KLocalObject>>();
	/**
	 * 
	 */
	public MemoryOnlyClusterStorage() {
		memoryCache = new HashMap<Integer, Map<String,C2KLocalObject>>();
	}

	@Override
	public void open() throws ClusterStorageException {

	}

	@Override
	public void close() throws ClusterStorageException {
	}

	@Override
	public short queryClusterSupport(String clusterType) {
		return ClusterStorage.READWRITE;
	}

	@Override
	public String getName() {
		return "Memory Cache";
	}

	@Override
	public String getId() {
		return "Memory Cache";
	}

	@Override
	public C2KLocalObject get(Integer sysKey, String path)
			throws ClusterStorageException {
        Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKey);
        if (sysKeyMemCache != null)
            return sysKeyMemCache.get(path);
        return null;
	}

	@Override
	public void put(Integer sysKey, C2KLocalObject obj)
			throws ClusterStorageException {

		// create item cache if not present
        Map<String, C2KLocalObject> sysKeyMemCache;
        synchronized (memoryCache) {
        	if (memoryCache.containsKey(sysKey))
        		sysKeyMemCache = memoryCache.get(sysKey);
        	else {
        		sysKeyMemCache = new HashMap<String, C2KLocalObject>();
        		memoryCache.put(sysKey, sysKeyMemCache);
            }
        }
        
        // store object in the cache
        String path = ClusterStorage.getPath(obj);
        synchronized(sysKeyMemCache) {
	        sysKeyMemCache.put(path, obj);
        }

	}

	@Override
	public void delete(Integer sysKey, String path)
			throws ClusterStorageException {
        Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKey);
        if (sysKeyMemCache != null) {
        	synchronized (sysKeyMemCache) {
        		if (sysKeyMemCache.containsKey(path)) {
        			sysKeyMemCache.remove(path);
        			if (sysKeyMemCache.isEmpty()) {
        				synchronized (memoryCache) {
        					memoryCache.remove(sysKey);
        				}
        			}
        		}
        	}
        }
	}

	@Override
	public String[] getClusterContents(Integer sysKey, String path)
			throws ClusterStorageException {
		Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKey);
		ArrayList<String> result = new ArrayList<String>();
        if (sysKeyMemCache != null) {
        	if (path.endsWith("/")) 
        		path = path.substring(0,path.length()-1);
        	path = path+"/";
        	for (String thisPath : sysKeyMemCache.keySet()) {
				if (thisPath.startsWith(path)) {
					int slash = path.indexOf('/');
					String suffix = slash>-1?path.substring(slash+1):path;
					if (!result.contains(suffix)) result.add(suffix);
				}
			}
        }
        return result.toArray(new String[result.size()]);
	}

}