| 1 | package org.jtoolkit.essence.data.impl; |
| 2 | |
| 3 | import org.apache.commons.logging.Log; |
| 4 | import org.apache.commons.logging.LogFactory; |
| 5 | import org.jetbrains.annotations.NotNull; |
| 6 | import org.jetbrains.annotations.Nullable; |
| 7 | import org.jtoolkit.essence.data.*; |
| 8 | import org.jtoolkit.essence.data.EvictionStrategy; |
| 9 | |
| 10 | import javax.cache.*; |
| 11 | import java.util.*; |
| 12 | import java.util.concurrent.ConcurrentMap; |
| 13 | |
| 14 | public class CacheStore<K, V> implements Store<K, V> { |
| 15 | private static final Log LOG = LogFactory.getLog(CacheStore.class); |
| 16 | private final String name; |
| 17 | private final Map<K, V> map; |
| 18 | private final StoreView view; |
| 19 | private boolean closed = false; |
| 20 | |
| 21 | public CacheStore(int defaultCacheSize, CacheComponentBuilder.CacheData cd) { |
| 22 | name = cd.name; |
| 23 | int cacheSize = defaultCacheSize; |
| 24 | if (cd.cacheSize == null) { |
| 25 | if (cd.evictionStrategy != EvictionStrategy.NONE) |
| 26 | LOG.info(cd.name + ": The default cache size of " + defaultCacheSize + " is used."); |
| 27 | } else { |
| 28 | cacheSize = cd.cacheSize; |
| 29 | } |
| 30 | boolean reordered = false; |
| 31 | switch (cd.evictionStrategy) { |
| 32 | case LFU: |
| 33 | LOG.warn(cd.name + ": Eviction policy LFU not supported, LRU used instead."); |
| 34 | reordered = true; |
| 35 | break; |
| 36 | case LRU: |
| 37 | reordered = true; |
| 38 | break; |
| 39 | case FIFO: |
| 40 | reordered = false; |
| 41 | break; |
| 42 | case NONE: |
| 43 | reordered = false; |
| 44 | cacheSize = Integer.MAX_VALUE; |
| 45 | break; |
| 46 | } |
| 47 | final int cacheSize2 = cacheSize; |
| 48 | //noinspection ClassExtendsConcreteCollection |
| 49 | map = Collections.synchronizedMap(new LinkedHashMap<K, V>(64, 0.7f, reordered) { |
| 50 | protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { |
| 51 | return size() > cacheSize2; |
| 52 | } |
| 53 | }); |
| 54 | view = new StoreView(); |
| 55 | } |
| 56 | |
| 57 | public Cluster getCluster() { |
| 58 | throw new UnsupportedOperationException(name + ": Not a member of a cluster"); |
| 59 | } |
| 60 | |
| 61 | @NotNull public Event.ListenerSet getListenerSet() { |
| 62 | return throwUseACluster(); |
| 63 | } |
| 64 | |
| 65 | private <T> T throwUseACluster() { |
| 66 | throw new UnsupportedOperationException(name + ": Not supported for simple cache, use a Cluster."); |
| 67 | } |
| 68 | |
| 69 | @NotNull public Cache<K, V> getCacheView() { |
| 70 | return view; |
| 71 | } |
| 72 | |
| 73 | @NotNull public ConcurrentMap<K, V> getMapView() { |
| 74 | return view; |
| 75 | } |
| 76 | |
| 77 | @NotNull public Queue<V> getQueueView() { |
| 78 | return throwUseACluster(); |
| 79 | } |
| 80 | |
| 81 | @NotNull public Map<K, V> asMap() { |
| 82 | return view; |
| 83 | } |
| 84 | |
| 85 | public Map<K, V> selectMatching(Predicate<Map.Entry<K, V>> predicate) { |
| 86 | return view.selectMatching(predicate); |
| 87 | } |
| 88 | |
| 89 | public int removeMatching(Predicate<Map.Entry<K, V>> predicate) { |
| 90 | return view.removeMatching(predicate); |
| 91 | } |
| 92 | |
| 93 | public void close() { |
| 94 | closed = true; |
| 95 | } |
| 96 | |
| 97 | public boolean isClosed() { |
| 98 | return closed; |
| 99 | } |
| 100 | |
| 101 | @NotNull public String getName() { |
| 102 | return name; |
| 103 | } |
| 104 | |
| 105 | public <R> R visit(@NotNull Visitor<Store<K, V>, R> visitor) throws VisitorException { |
| 106 | return visitor.visit(this); |
| 107 | } |
| 108 | |
| 109 | class StoreView implements Cache<K, V>, ConcurrentMap<K, V> { |
| 110 | |
| 111 | public V get(Object key) { |
| 112 | return map.get(key); |
| 113 | } |
| 114 | |
| 115 | public Map<K, V> getAll(@NotNull Collection<K> keys) throws CacheException { |
| 116 | Map<K, V> ret = new LinkedHashMap<K, V>(); |
| 117 | synchronized (map) { |
| 118 | for (K key : keys) |
| 119 | ret.put(key, map.get(key)); |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | public void load(@NotNull K key) throws CacheException { |
| 125 | } |
| 126 | |
| 127 | public void loadAll(@NotNull Collection<K> keys) throws CacheException { |
| 128 | } |
| 129 | |
| 130 | public V peek(@NotNull K key) { |
| 131 | return map.get(key); |
| 132 | } |
| 133 | |
| 134 | public CacheEntry<K, V> getCacheEntry(@NotNull final K key) { |
| 135 | return new CSCacheEntry(key); |
| 136 | } |
| 137 | |
| 138 | public CacheStatistics getCacheStatistics() { |
| 139 | throw new UnsupportedOperationException(name + ": CacheStats not supported, try Cluster"); |
| 140 | } |
| 141 | |
| 142 | public void evict() { |
| 143 | } |
| 144 | |
| 145 | public void addListener(@NotNull CacheListener<K> cacheListener) { |
| 146 | throw new UnsupportedOperationException(name + ": CacheListeners not supported, try Cluster"); |
| 147 | } |
| 148 | |
| 149 | public void removeListener(@NotNull CacheListener<K> cacheListener) { |
| 150 | } |
| 151 | |
| 152 | public int size() { |
| 153 | return map.size(); |
| 154 | } |
| 155 | |
| 156 | public boolean isEmpty() { |
| 157 | return map.isEmpty(); |
| 158 | } |
| 159 | |
| 160 | public boolean containsKey(Object key) { |
| 161 | return map.containsKey(key); |
| 162 | } |
| 163 | |
| 164 | public boolean containsValue(Object value) { |
| 165 | return map.containsValue(value); |
| 166 | } |
| 167 | |
| 168 | public V put(K key, V value) { |
| 169 | return map.put(key, value); |
| 170 | } |
| 171 | |
| 172 | public V remove(Object key) { |
| 173 | return map.remove(key); |
| 174 | } |
| 175 | |
| 176 | public void putAll(Map<? extends K, ? extends V> t) { |
| 177 | map.putAll(t); |
| 178 | } |
| 179 | |
| 180 | public void clear() { |
| 181 | map.clear(); |
| 182 | } |
| 183 | |
| 184 | public Set<K> keySet() { |
| 185 | synchronized (map) { |
| 186 | return Collections.unmodifiableSet(new LinkedHashSet<K>(map.keySet())); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public Collection<V> values() { |
| 191 | synchronized (map) { |
| 192 | return Collections.unmodifiableCollection(new ArrayList<V>(map.values())); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | public Set<Entry<K, V>> entrySet() { |
| 197 | synchronized (map) { |
| 198 | return Collections.unmodifiableMap(new LinkedHashMap<K, V>(map)).entrySet(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public V putIfAbsent(K key, V value) { |
| 203 | synchronized (map) { |
| 204 | if (map.containsKey(key)) return map.get(key); |
| 205 | else return map.put(key, value); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @SuppressWarnings({"SuspiciousMethodCalls"}) |
| 210 | public boolean remove(Object key, Object value) { |
| 211 | synchronized (map) { |
| 212 | if (map.containsKey(key) && map.get(key).equals(value)) { |
| 213 | map.remove(key); |
| 214 | return true; |
| 215 | } else return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | public boolean replace(K key, V oldValue, V newValue) { |
| 220 | synchronized (map) { |
| 221 | if (map.containsKey(key) && map.get(key).equals(oldValue)) { |
| 222 | map.put(key, newValue); |
| 223 | return true; |
| 224 | } else return false; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @Nullable public V replace(K key, V value) { |
| 229 | synchronized (map) { |
| 230 | if (map.containsKey(key)) { |
| 231 | return map.put(key, value); |
| 232 | } else return null; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | public Map<K, V> selectMatching(Predicate<Entry<K, V>> predicate) { |
| 237 | Map<K, V> ret = new LinkedHashMap<K, V>(); |
| 238 | synchronized (map) { |
| 239 | for (Map.Entry<K, V> entry : map.entrySet()) |
| 240 | if (predicate.isSatisfiedBy(entry)) |
| 241 | ret.put(entry.getKey(), entry.getValue()); |
| 242 | } |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | public int removeMatching(Predicate<Entry<K, V>> predicate) { |
| 247 | int count = 0; |
| 248 | synchronized (map) { |
| 249 | for (Iterator<Map.Entry<K, V>> i = map.entrySet().iterator(); i.hasNext();) |
| 250 | if (predicate.isSatisfiedBy(i.next())) { |
| 251 | i.remove(); |
| 252 | count++; |
| 253 | } |
| 254 | } |
| 255 | return count; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private class CSCacheEntry implements CacheEntry<K, V> { |
| 260 | private final K key; |
| 261 | |
| 262 | CSCacheEntry(K key) { |
| 263 | this.key = key; |
| 264 | } |
| 265 | |
| 266 | public long getCost() { |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | public long getCreationTime() { |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | public long getExpirationTime() { |
| 275 | return Long.MAX_VALUE; |
| 276 | } |
| 277 | |
| 278 | public int getHits() { |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | public long getLastAccessTime() { |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | public long getLastUpdateTime() { |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | public long getVersion() { |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | public boolean isValid() { |
| 295 | return map.containsKey(key); |
| 296 | } |
| 297 | |
| 298 | public K getKey() { |
| 299 | return key; |
| 300 | } |
| 301 | |
| 302 | public V getValue() { |
| 303 | return map.get(key); |
| 304 | } |
| 305 | |
| 306 | public V setValue(V value) { |
| 307 | return map.put(key, value); |
| 308 | } |
| 309 | } |
| 310 | } |