PageRenderTime 56ms CodeModel.GetById 17ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-hibernate/src/main/java/com/hazelcast/hibernate/local/LocalRegionCache.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 250 lines | 203 code | 29 blank | 18 comment | 66 complexity | a5fc53a3872027e207f0ec56d69f6f53 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.hibernate.local;
 18
 19import com.hazelcast.config.MapConfig;
 20import com.hazelcast.core.HazelcastInstance;
 21import com.hazelcast.core.ITopic;
 22import com.hazelcast.core.Message;
 23import com.hazelcast.core.MessageListener;
 24import com.hazelcast.hibernate.CacheEnvironment;
 25import com.hazelcast.hibernate.RegionCache;
 26import com.hazelcast.util.Clock;
 27import org.hibernate.cache.CacheDataDescription;
 28import org.hibernate.cache.access.SoftLock;
 29
 30import java.util.*;
 31import java.util.Map.Entry;
 32import java.util.concurrent.ConcurrentHashMap;
 33import java.util.concurrent.ConcurrentMap;
 34
 35/**
 36 * @mdogan 11/9/12
 37 */
 38public class LocalRegionCache implements RegionCache {
 39
 40    protected final ITopic<Object> topic;
 41    protected final MessageListener<Object> messageListener;
 42    protected final ConcurrentMap<Object, Value> cache;
 43    protected final Comparator versionComparator;
 44    protected MapConfig config;
 45
 46    public LocalRegionCache(final String name, final HazelcastInstance hazelcastInstance,
 47                            final CacheDataDescription metadata) {
 48        try {
 49            config = hazelcastInstance != null ? hazelcastInstance.getConfig().findMatchingMapConfig(name) : null;
 50        } catch (UnsupportedOperationException ignored) {
 51        }
 52        versionComparator = metadata != null && metadata.isVersioned() ? metadata.getVersionComparator() : null;
 53        cache = new ConcurrentHashMap<Object, Value>();
 54
 55        messageListener = createMessageListener();
 56        if (hazelcastInstance != null) {
 57            topic = hazelcastInstance.getTopic(name);
 58            topic.addMessageListener(messageListener);
 59        } else {
 60            topic = null;
 61        }
 62    }
 63
 64    public Object get(final Object key) {
 65        final Value value = cache.get(key);
 66        return value != null ? value.getValue() : null;
 67    }
 68
 69    public boolean put(final Object key, final Object value, final Object currentVersion) {
 70        final Value newValue = new Value(currentVersion, value, null, Clock.currentTimeMillis());
 71        cache.put(key, newValue);
 72        return true;
 73    }
 74
 75    public boolean update(final Object key, final Object value, final Object currentVersion,
 76                       final Object previousVersion, final SoftLock lock) {
 77        if (lock == LOCK_FAILURE) {
 78            return false;
 79        }
 80
 81        final Value currentValue = cache.get(key);
 82        if (lock == LOCK_SUCCESS) {
 83            if (currentValue != null && currentVersion != null
 84                && versionComparator.compare(currentVersion, currentValue.getVersion()) < 0) {
 85                return false;
 86            }
 87        }
 88        if (topic != null) {
 89            topic.publish(createMessage(key, value, currentVersion));
 90        }
 91        cache.put(key, new Value(currentVersion, value, lock, Clock.currentTimeMillis()));
 92        return true;
 93    }
 94
 95    protected Object createMessage(final Object key, Object value, final Object currentVersion) {
 96        return new Invalidation(key, currentVersion);
 97    }
 98
 99    protected MessageListener<Object> createMessageListener() {
100        return new MessageListener<Object>() {
101            public void onMessage(final Message<Object> message) {
102                final Invalidation invalidation = (Invalidation) message.getMessageObject();
103                if (versionComparator != null) {
104                    final Value value = cache.get(invalidation.getKey());
105                    if (value != null) {
106                        Object currentVersion = value.getVersion();
107                        Object newVersion = invalidation.getVersion();
108                        if (versionComparator.compare(newVersion, currentVersion) > 0) {
109                            cache.remove(invalidation.getKey(), value);
110                        }
111                    }
112                } else {
113                    cache.remove(invalidation.getKey());
114                }
115            }
116        };
117    }
118
119    public boolean remove(final Object key) {
120        return cache.remove(key) != null;
121    }
122
123    public SoftLock tryLock(final Object key, final Object version) {
124        final Value value = cache.get(key);
125        if (value == null) {
126            if (cache.putIfAbsent(key, new Value(version, null, LOCK_SUCCESS, Clock.currentTimeMillis())) == null) {
127                return LOCK_SUCCESS;
128            } else {
129                return LOCK_FAILURE;
130            }
131        } else {
132            if (version == null || versionComparator.compare(version, value.getVersion()) >= 0) {
133                if (cache.replace(key, value, value.createLockedValue(LOCK_SUCCESS))) {
134                    return LOCK_SUCCESS;
135                } else {
136                    return LOCK_FAILURE;
137                }
138            } else {
139                return LOCK_FAILURE;
140            }
141        }
142    }
143
144    public void unlock(final Object key, SoftLock lock) {
145        final Value value = cache.get(key);
146        if (value != null) {
147            final SoftLock currentLock = value.getLock();
148            if (currentLock == lock) {
149                cache.replace(key, value, value.createUnlockedValue());
150            }
151        }
152    }
153
154    public boolean contains(final Object key) {
155        return cache.containsKey(key);
156    }
157
158    public void clear() {
159        cache.clear();
160    }
161
162    public long size() {
163        return cache.size();
164    }
165
166    public long getSizeInMemory() {
167        return 0;
168    }
169
170    public Map asMap() {
171        return cache;
172    }
173
174    void cleanup() {
175        final int maxSize;
176        final long timeToLive;
177        if (config != null) {
178            maxSize = config.getMaxSizeConfig().getSize();
179            timeToLive = config.getTimeToLiveSeconds() * 1000L;
180        } else {
181            maxSize = 100000;
182            timeToLive = CacheEnvironment.getDefaultCacheTimeoutInMillis();
183        }
184
185        if ((maxSize > 0 && maxSize != Integer.MAX_VALUE) || timeToLive > 0) {
186            final Iterator<Entry<Object, Value>> iter = cache.entrySet().iterator();
187            SortedSet<EvictionEntry> entries = null;
188            final long now = Clock.currentTimeMillis();
189            while (iter.hasNext()) {
190                final Entry<Object, Value> e = iter.next();
191                final Object k = e.getKey();
192                final Value v = e.getValue();
193                if (v.getLock() == LOCK_SUCCESS) {
194                    continue;
195                }
196                if (v.getCreationTime() + timeToLive > now) {
197                    iter.remove();
198                } else if (maxSize > 0 && maxSize != Integer.MAX_VALUE) {
199                    if (entries == null) {
200                        entries = new TreeSet<EvictionEntry>();
201                    }
202                    entries.add(new EvictionEntry(k, v));
203                }
204            }
205            final int diff = cache.size() - maxSize;
206            final int k = diff >= 0 ? (diff + maxSize * 20 / 100) : 0;
207            if (k > 0 && entries != null) {
208                int i = 0;
209                for (EvictionEntry entry : entries) {
210                    if (cache.remove(entry.key, entry.value)) {
211                        if (++i == k) {
212                            break;
213                        }
214                    }
215                }
216            }
217        }
218    }
219
220    private class EvictionEntry implements Comparable<EvictionEntry> {
221        final Object key;
222        final Value value;
223
224        private EvictionEntry(final Object key, final Value value) {
225            this.key = key;
226            this.value = value;
227        }
228
229        public int compareTo(final EvictionEntry o) {
230            final long thisVal = this.value.getCreationTime();
231            final long anotherVal = o.value.getCreationTime();
232            return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
233        }
234    }
235
236    private static final SoftLock LOCK_SUCCESS = new SoftLock() {
237        @Override
238        public String toString() {
239            return "Lock::Success";
240        }
241    };
242
243    private static final SoftLock LOCK_FAILURE = new SoftLock() {
244        @Override
245        public String toString() {
246            return "Lock::Failure";
247        }
248    };
249
250}