summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/persistency/MemoryOnlyClusterStorage.java
blob: 4766d828eeeef9f5c1e38d4933c407b126db11a9 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.c2kernel.persistency;

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

import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.utils.Logger;

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) {
        	while (path.endsWith("/")) 
        		path = path.substring(0,path.length()-1);
        	path = path+"/";
        	for (String thisPath : sysKeyMemCache.keySet()) {
				if (thisPath.startsWith(path)) {
					String end = thisPath.substring(path.length());
					int slash = end.indexOf('/');
					String suffix = slash>-1?end.substring(0, slash):end;
					if (!result.contains(suffix)) result.add(suffix);
				}
			}
        }
        return result.toArray(new String[result.size()]);
	}
	
    public void dumpContents(int sysKey) {
        synchronized(memoryCache) {
			Logger.msg(0, "Cached Objects of Entity "+sysKey);
			Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(sysKey);
			if (sysKeyMemCache == null) {
				Logger.msg(0, "No cache found");
				return;
			}
            try {
                synchronized(sysKeyMemCache) {
             for (Object name : sysKeyMemCache.keySet()) {
               String path = (String) name;
               try {
                 Logger.msg(0, "    Path "+path+": "+sysKeyMemCache.get(path).getClass().getName());
               } catch (NullPointerException e) {
                 Logger.msg(0, "    Path "+path+": reaped");
               }
             }
                }
            } catch (ConcurrentModificationException ex) {
                Logger.msg(0, "Cache modified - aborting");
            }
		}
        Logger.msg(0, "Total number of cached entities: "+memoryCache.size());
    }
}