1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.util.store; |
11 | |
|
12 | |
import org.mule.config.i18n.CoreMessages; |
13 | |
import org.mule.util.monitor.ExpiryMonitor; |
14 | |
|
15 | |
import java.util.Map; |
16 | |
|
17 | |
import edu.emory.mathcs.backport.java.util.concurrent.helpers.Utils; |
18 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentSkipListMap; |
19 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class InMemoryObjectStore extends AbstractMonitoredObjectStore |
29 | |
{ |
30 | |
protected ConcurrentSkipListMap store; |
31 | |
|
32 | |
public InMemoryObjectStore() |
33 | 18 | { |
34 | 18 | this.store = new ConcurrentSkipListMap(); |
35 | 18 | } |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public boolean containsObject(String id) throws Exception |
50 | |
{ |
51 | 146 | if (id == null) |
52 | |
{ |
53 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("id").toString()); |
54 | |
} |
55 | |
|
56 | |
|
57 | 146 | return store.values().contains(new StoredObject(id, null)); |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public boolean storeObject(String id, Object item) throws Exception |
72 | |
{ |
73 | 64 | if (id == null) |
74 | |
{ |
75 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("id").toString()); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 64 | StoredObject obj = new StoredObject(id, item); |
83 | 64 | synchronized (store) |
84 | |
{ |
85 | 64 | if (store.values().contains(obj)) |
86 | |
{ |
87 | 0 | return false; |
88 | |
} |
89 | |
|
90 | 64 | boolean written = false; |
91 | 128 | while (!written) |
92 | |
{ |
93 | 64 | written = (store.putIfAbsent(new Long(Utils.nanoTime()), obj) == null); |
94 | |
} |
95 | |
|
96 | 64 | return true; |
97 | 0 | } |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public Object retrieveObject(String id) throws Exception |
111 | |
{ |
112 | 0 | StoredObject obj = (StoredObject)store.get(id); |
113 | 0 | if(obj!=null) |
114 | |
{ |
115 | 0 | return obj.getItem(); |
116 | |
} |
117 | 0 | return null; |
118 | |
} |
119 | |
|
120 | |
public boolean removeObject(String id) throws Exception |
121 | |
{ |
122 | 0 | StoredObject obj = (StoredObject)store.get(id); |
123 | 0 | if(obj!=null) |
124 | |
{ |
125 | 0 | return store.remove(obj) !=null; |
126 | |
} |
127 | 0 | return true; |
128 | |
} |
129 | |
|
130 | |
public final void expire() |
131 | |
{ |
132 | |
|
133 | 101 | int currentSize = store.size(); |
134 | |
|
135 | |
|
136 | 101 | int excess = (currentSize - maxEntries); |
137 | 101 | if (excess > 0) |
138 | |
{ |
139 | 76 | while (currentSize > maxEntries) |
140 | |
{ |
141 | 46 | store.pollFirstEntry(); |
142 | 46 | currentSize--; |
143 | |
} |
144 | |
|
145 | 30 | if (logger.isDebugEnabled()) |
146 | |
{ |
147 | 0 | logger.debug("Expired " + excess + " excess entries"); |
148 | |
} |
149 | |
} |
150 | |
|
151 | |
|
152 | 101 | if (entryTTL > 0 && currentSize != 0) |
153 | |
{ |
154 | 43 | final long now = Utils.nanoTime(); |
155 | 43 | int expiredEntries = 0; |
156 | |
Map.Entry oldestEntry; |
157 | |
|
158 | |
purge: |
159 | 61 | while ((oldestEntry = store.firstEntry()) != null) |
160 | |
{ |
161 | 37 | Long oldestKey = (Long) oldestEntry.getKey(); |
162 | 37 | long oldestKeyValue = oldestKey.longValue(); |
163 | |
|
164 | 37 | if (TimeUnit.NANOSECONDS.toMillis(now - oldestKeyValue) >= entryTTL) |
165 | |
{ |
166 | 18 | store.remove(oldestKey); |
167 | 18 | expiredEntries++; |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | |
break purge; |
172 | |
} |
173 | 18 | } |
174 | |
|
175 | 43 | if (logger.isDebugEnabled()) |
176 | |
{ |
177 | 0 | logger.debug("Expired " + expiredEntries + " old entries"); |
178 | |
} |
179 | |
} |
180 | 101 | } |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
protected static class StoredObject |
186 | |
{ |
187 | |
private String id; |
188 | |
private Object item; |
189 | |
|
190 | |
public StoredObject(String id, Object item) |
191 | 210 | { |
192 | 210 | this.id = id; |
193 | 210 | this.item = item; |
194 | 210 | } |
195 | |
|
196 | |
public String getId() |
197 | |
{ |
198 | 12 | return id; |
199 | |
} |
200 | |
|
201 | |
public Object getItem() |
202 | |
{ |
203 | 12 | return item; |
204 | |
} |
205 | |
|
206 | |
public boolean equals(Object o) |
207 | |
{ |
208 | 388 | if (this == o) |
209 | |
{ |
210 | 0 | return true; |
211 | |
} |
212 | 388 | if (o == null || getClass() != o.getClass()) |
213 | |
{ |
214 | 0 | return false; |
215 | |
} |
216 | |
|
217 | 388 | StoredObject that = (StoredObject) o; |
218 | |
|
219 | 388 | if (!id.equals(that.id)) |
220 | |
{ |
221 | 296 | return false; |
222 | |
} |
223 | |
|
224 | 92 | return true; |
225 | |
} |
226 | |
|
227 | |
public int hashCode() |
228 | |
{ |
229 | 0 | return id.hashCode(); |
230 | |
} |
231 | |
|
232 | |
|
233 | |
public String toString() |
234 | |
{ |
235 | 0 | final StringBuffer sb = new StringBuffer(); |
236 | 0 | sb.append("StoredObject"); |
237 | 0 | sb.append("{id='").append(id).append('\''); |
238 | 0 | sb.append(", item=").append(item); |
239 | 0 | sb.append('}'); |
240 | 0 | return sb.toString(); |
241 | |
} |
242 | |
} |
243 | |
|
244 | |
|
245 | |
|
246 | |
} |