|
| 1 | +package com.devicehive.application; |
| 2 | + |
| 3 | +/* |
| 4 | + * #%L |
| 5 | + * DeviceHive Dao RDBMS Implementation |
| 6 | + * %% |
| 7 | + * Copyright (C) 2016 DataArt |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import com.hazelcast.core.HazelcastInstance; |
| 24 | +import com.hazelcast.hibernate.local.Timestamp; |
| 25 | +import com.hazelcast.hibernate.local.TimestampsRegionCache; |
| 26 | +import com.hazelcast.hibernate.serialization.Expirable; |
| 27 | +import com.hazelcast.hibernate.serialization.Value; |
| 28 | + |
| 29 | +// Fix for issues described in https://github.com/hazelcast/hazelcast/issues/13271 and https://github.com/hazelcast/hazelcast-hibernate5/issues/33 |
| 30 | +// Monitor when either of those are fixed, after that we should remove this and DhLocalCacheRegionFactory |
| 31 | +public class DhTimestampsRegionCache extends TimestampsRegionCache { |
| 32 | + |
| 33 | + public DhTimestampsRegionCache(String name, HazelcastInstance hazelcastInstance) { |
| 34 | + super(name, hazelcastInstance); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void maybeInvalidate(final Object messageObject) { |
| 39 | + Timestamp ts = (Timestamp) messageObject; |
| 40 | + final Object key = ts.getKey(); |
| 41 | + |
| 42 | + if (ts.getTimestamp() > System.currentTimeMillis()) { |
| 43 | + ts = new Timestamp(ts.getKey(), System.currentTimeMillis()); |
| 44 | + } |
| 45 | + |
| 46 | + while (true) { |
| 47 | + final Expirable value = cache.get(key); |
| 48 | + final Long current = value != null ? (Long) value.getValue() : null; |
| 49 | + if (current != null) { |
| 50 | + if (ts.getTimestamp() > current) { |
| 51 | + if (cache.replace(key, value, new Value(value.getVersion(), nextTimestamp(), ts.getTimestamp()))) { |
| 52 | + return; |
| 53 | + } |
| 54 | + } else { |
| 55 | + return; |
| 56 | + } |
| 57 | + } else { |
| 58 | + if (cache.putIfAbsent(key, new Value(null, nextTimestamp(), ts.getTimestamp())) == null) { |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments