From 37ead6eff0dba148ef40954051df501b43444095 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 14 Jan 2019 13:58:46 +0300 Subject: [PATCH] refactoring --- .../org/redisson/RedissonLocalCachedMap.java | 174 +------------ .../java/org/redisson/RedissonObject.java | 1 - .../org/redisson/cache/LocalCacheView.java | 236 ++++++++++++++++++ 3 files changed, 243 insertions(+), 168 deletions(-) create mode 100644 redisson/src/main/java/org/redisson/cache/LocalCacheView.java diff --git a/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java b/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java index afdc35d7b..cbbc04fba 100644 --- a/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java +++ b/redisson/src/main/java/org/redisson/RedissonLocalCachedMap.java @@ -18,9 +18,7 @@ package org.redisson; import java.io.IOException; import java.io.Serializable; import java.math.BigDecimal; -import java.util.AbstractCollection; import java.util.AbstractMap; -import java.util.AbstractSet; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -45,6 +43,7 @@ import org.redisson.cache.CacheKey; import org.redisson.cache.LFUCacheMap; import org.redisson.cache.LRUCacheMap; import org.redisson.cache.LocalCacheListener; +import org.redisson.cache.LocalCacheView; import org.redisson.cache.LocalCachedMapClear; import org.redisson.cache.LocalCachedMapInvalidate; import org.redisson.cache.LocalCachedMapUpdate; @@ -138,6 +137,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R private SyncStrategy syncStrategy; private LocalCacheListener listener; + private LocalCacheView localCacheView; public RedissonLocalCachedMap(CommandAsyncExecutor commandExecutor, String name, LocalCachedMapOptions options, EvictionScheduler evictionScheduler, RedissonClient redisson) { super(commandExecutor, name, redisson, options); @@ -168,6 +168,7 @@ public class RedissonLocalCachedMap extends RedissonMap implements R }; listener.add(); + localCacheView = new LocalCacheView(cache, this); if (options.getSyncStrategy() != SyncStrategy.NONE) { invalidateEntryOnChange = 1; @@ -1095,183 +1096,22 @@ public class RedissonLocalCachedMap extends RedissonMap implements R @Override public Set cachedKeySet() { - return new LocalKeySet(); + return localCacheView.cachedKeySet(); } - class LocalKeySet extends AbstractSet { - - @Override - public Iterator iterator() { - return new Iterator() { - Iterator iter = cache.values().iterator(); - @Override - public boolean hasNext() { - return iter.hasNext(); - } - - @Override - public K next() { - return (K) iter.next().getKey(); - } - }; - } - - @Override - public boolean contains(Object o) { - CacheKey cacheKey = toCacheKey(o); - return cache.containsKey(cacheKey); - } - - @Override - public boolean remove(Object o) { - CacheKey cacheKey = toCacheKey(o); - return cache.remove(cacheKey) != null; - } - - @Override - public int size() { - return cache.size(); - } - - @Override - public void clear() { - cache.clear(); - } - - } - @Override public Collection cachedValues() { - return new LocalValues(); + return localCacheView.cachedValues(); } - final class LocalValues extends AbstractCollection { - - @Override - public Iterator iterator() { - return new Iterator() { - Iterator iter = cache.values().iterator(); - @Override - public boolean hasNext() { - return iter.hasNext(); - } - - @Override - public V next() { - return (V) iter.next().getValue(); - } - }; - } - - @Override - public boolean contains(Object o) { - CacheValue cacheValue = new CacheValue(null, o); - return cache.containsValue(cacheValue); - } - - @Override - public int size() { - return cache.size(); - } - - @Override - public void clear() { - cache.clear(); - } - - } - @Override public Set> cachedEntrySet() { - return new LocalEntrySet(); - } - - final class LocalEntrySet extends AbstractSet> { - - @Override - public Iterator> iterator() { - return new Iterator>() { - Iterator iter = cache.values().iterator(); - @Override - public boolean hasNext() { - return iter.hasNext(); - } - - @Override - public Map.Entry next() { - CacheValue e = iter.next(); - return new AbstractMap.SimpleEntry((K)e.getKey(), (V)e.getValue()); - } - }; - } - - @Override - public boolean contains(Object o) { - if (!(o instanceof Map.Entry)) - return false; - Map.Entry e = (Map.Entry) o; - CacheKey cacheKey = toCacheKey(e.getKey()); - CacheValue entry = cache.get(cacheKey); - return entry != null && entry.getValue().equals(e.getValue()); - } - - @Override - public boolean remove(Object o) { - if (o instanceof Map.Entry) { - Map.Entry e = (Map.Entry) o; - CacheKey cacheKey = toCacheKey(e.getKey()); - return cache.remove(cacheKey, new CacheValue(e.getKey(), e.getValue())); - } - return false; - } - - @Override - public int size() { - return cache.size(); - } - - @Override - public void clear() { - cache.clear(); - } - + return localCacheView.cachedEntrySet(); } @Override public Map getCachedMap() { - return new LocalMap(); + return localCacheView.getCachedMap(); } - final class LocalMap extends AbstractMap { - - @Override - public V get(Object key) { - CacheKey cacheKey = toCacheKey(key); - CacheValue e = cache.get(cacheKey); - if (e != null) { - return (V) e.getValue(); - } - return null; - } - - @Override - public boolean containsKey(Object key) { - CacheKey cacheKey = toCacheKey(key); - return cache.containsKey(cacheKey); - } - - @Override - public boolean containsValue(Object value) { - CacheValue cacheValue = new CacheValue(null, value); - return cache.containsValue(cacheValue); - } - - @Override - public Set> entrySet() { - return RedissonLocalCachedMap.this.cachedEntrySet(); - } - - } - - } diff --git a/redisson/src/main/java/org/redisson/RedissonObject.java b/redisson/src/main/java/org/redisson/RedissonObject.java index 047ecad56..7f2d24bee 100644 --- a/redisson/src/main/java/org/redisson/RedissonObject.java +++ b/redisson/src/main/java/org/redisson/RedissonObject.java @@ -18,7 +18,6 @@ package org.redisson; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; diff --git a/redisson/src/main/java/org/redisson/cache/LocalCacheView.java b/redisson/src/main/java/org/redisson/cache/LocalCacheView.java new file mode 100644 index 000000000..7f143d992 --- /dev/null +++ b/redisson/src/main/java/org/redisson/cache/LocalCacheView.java @@ -0,0 +1,236 @@ +/** + * Copyright 2018 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.cache; + +import java.util.AbstractCollection; +import java.util.AbstractMap; +import java.util.AbstractSet; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.redisson.RedissonLocalCachedMap.CacheValue; +import org.redisson.RedissonObject; + +import io.netty.buffer.ByteBuf; + +/** + * + * @author Nikita Koksharov + * + * @param key type + * @param value type + */ +public class LocalCacheView { + + private final RedissonObject object; + private final Cache cache; + + public LocalCacheView(Cache cache, RedissonObject object) { + this.cache = cache; + this.object = object; + } + + public Set cachedKeySet() { + return new LocalKeySet(); + } + + class LocalKeySet extends AbstractSet { + + @Override + public Iterator iterator() { + return new Iterator() { + Iterator iter = cache.values().iterator(); + @Override + public boolean hasNext() { + return iter.hasNext(); + } + + @Override + public K next() { + return (K) iter.next().getKey(); + } + }; + } + + @Override + public boolean contains(Object o) { + CacheKey cacheKey = toCacheKey(o); + return cache.containsKey(cacheKey); + } + + @Override + public boolean remove(Object o) { + CacheKey cacheKey = toCacheKey(o); + return cache.remove(cacheKey) != null; + } + + @Override + public int size() { + return cache.size(); + } + + @Override + public void clear() { + cache.clear(); + } + + } + + public Collection cachedValues() { + return new LocalValues(); + } + + final class LocalValues extends AbstractCollection { + + @Override + public Iterator iterator() { + return new Iterator() { + Iterator iter = cache.values().iterator(); + @Override + public boolean hasNext() { + return iter.hasNext(); + } + + @Override + public V next() { + return (V) iter.next().getValue(); + } + }; + } + + @Override + public boolean contains(Object o) { + CacheValue cacheValue = new CacheValue(null, o); + return cache.containsValue(cacheValue); + } + + @Override + public int size() { + return cache.size(); + } + + @Override + public void clear() { + cache.clear(); + } + + } + + public Set> cachedEntrySet() { + return new LocalEntrySet(); + } + + final class LocalEntrySet extends AbstractSet> { + + @Override + public Iterator> iterator() { + return new Iterator>() { + Iterator iter = cache.values().iterator(); + @Override + public boolean hasNext() { + return iter.hasNext(); + } + + @Override + public Map.Entry next() { + CacheValue e = iter.next(); + return new AbstractMap.SimpleEntry((K)e.getKey(), (V)e.getValue()); + } + }; + } + + @Override + public boolean contains(Object o) { + if (!(o instanceof Map.Entry)) + return false; + Map.Entry e = (Map.Entry) o; + CacheKey cacheKey = toCacheKey(e.getKey()); + CacheValue entry = cache.get(cacheKey); + return entry != null && entry.getValue().equals(e.getValue()); + } + + @Override + public boolean remove(Object o) { + if (o instanceof Map.Entry) { + Map.Entry e = (Map.Entry) o; + CacheKey cacheKey = toCacheKey(e.getKey()); + return cache.remove(cacheKey, new CacheValue(e.getKey(), e.getValue())); + } + return false; + } + + @Override + public int size() { + return cache.size(); + } + + @Override + public void clear() { + cache.clear(); + } + + } + + public Map getCachedMap() { + return new LocalMap(); + } + + final class LocalMap extends AbstractMap { + + @Override + public V get(Object key) { + CacheKey cacheKey = toCacheKey(key); + CacheValue e = cache.get(cacheKey); + if (e != null) { + return (V) e.getValue(); + } + return null; + } + + @Override + public boolean containsKey(Object key) { + CacheKey cacheKey = toCacheKey(key); + return cache.containsKey(cacheKey); + } + + @Override + public boolean containsValue(Object value) { + CacheValue cacheValue = new CacheValue(null, value); + return cache.containsValue(cacheValue); + } + + @Override + public Set> entrySet() { + return cachedEntrySet(); + } + + } + + public CacheKey toCacheKey(Object key) { + ByteBuf encoded = object.encodeMapKey(key); + try { + return toCacheKey(encoded); + } finally { + encoded.release(); + } + } + + + +}