diff --git a/common/src/main/java/com/google/common/collect/MigrateMap.java b/common/src/main/java/com/google/common/collect/MigrateMap.java index 74e444da..402bffaf 100644 --- a/common/src/main/java/com/google/common/collect/MigrateMap.java +++ b/common/src/main/java/com/google/common/collect/MigrateMap.java @@ -1,19 +1,134 @@ package com.google.common.collect; +import java.util.Collection; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutionException; import com.google.common.base.Function; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; public class MigrateMap { - @SuppressWarnings("deprecation") - public static ConcurrentMap makeComputingMap(MapMaker maker, + public static ConcurrentMap makeComputingMap(CacheBuilder builder, Function computingFunction) { - return maker.makeComputingMap(computingFunction); + final Function function = computingFunction; + LoadingCache computingCache = builder.build(new CacheLoader() { + + @Override + public V load(K key) throws Exception { + return function.apply(key); + } + }); + + return new MigrateConcurrentMap(computingCache); } - @SuppressWarnings("deprecation") public static ConcurrentMap makeComputingMap(Function computingFunction) { - return new MapMaker().makeComputingMap(computingFunction); + return makeComputingMap(CacheBuilder.newBuilder(), computingFunction); + } + + final static class MigrateConcurrentMap implements ConcurrentMap { + + private final LoadingCache computingCache; + + private final ConcurrentMap cacheView; + + MigrateConcurrentMap(LoadingCache computingCache){ + this.computingCache = computingCache; + this.cacheView = computingCache.asMap(); + } + + @Override + public int size() { + return cacheView.size(); + } + + @Override + public boolean isEmpty() { + return cacheView.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return cacheView.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return cacheView.containsValue(value); + } + + @Override + public V get(Object key) { + try { + return computingCache.get((K) key); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } + } + + @Override + public V put(K key, V value) { + return cacheView.put(key, value); + } + + @Override + public V remove(Object key) { + return cacheView.remove(key); + } + + @Override + public void putAll(Map m) { + cacheView.putAll(m); + } + + @Override + public void clear() { + cacheView.clear(); + } + + @Override + public Set keySet() { + return cacheView.keySet(); + } + + @Override + public Collection values() { + return cacheView.values(); + } + + @Override + public Set> entrySet() { + return cacheView.entrySet(); + } + + @Override + public V putIfAbsent(K key, V value) { + return cacheView.putIfAbsent(key, value); + } + + @Override + public boolean remove(Object key, Object value) { + return cacheView.remove(key, value); + } + + @Override + public boolean replace(K key, V oldValue, V newValue) { + return cacheView.replace(key, oldValue, newValue); + } + + @Override + public V replace(K key, V value) { + return cacheView.replace(key, value); + } + + @Override + public String toString() { + return cacheView.toString(); + } } } diff --git a/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/filter/PatternUtils.java b/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/filter/PatternUtils.java index 8f6228fe..97536a51 100644 --- a/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/filter/PatternUtils.java +++ b/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/filter/PatternUtils.java @@ -7,13 +7,13 @@ import org.apache.oro.text.regex.Pattern; import org.apache.oro.text.regex.PatternCompiler; import org.apache.oro.text.regex.Perl5Compiler; -import com.google.common.collect.MapMaker; +import com.google.common.cache.CacheBuilder; import com.google.common.collect.MigrateMap; public class PatternUtils { @SuppressWarnings("deprecation") - private static Map patterns = MigrateMap.makeComputingMap(new MapMaker().softValues(), + private static Map patterns = MigrateMap.makeComputingMap(CacheBuilder.newBuilder().softValues(), pattern -> { try { PatternCompiler pc = new Perl5Compiler(); diff --git a/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/producer/MQMessageUtils.java b/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/producer/MQMessageUtils.java index d64791fe..879df213 100644 --- a/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/producer/MQMessageUtils.java +++ b/connector/core/src/main/java/com/alibaba/otter/canal/connector/core/producer/MQMessageUtils.java @@ -20,8 +20,8 @@ import com.alibaba.otter.canal.protocol.CanalEntry.RowChange; import com.alibaba.otter.canal.protocol.FlatMessage; import com.alibaba.otter.canal.protocol.Message; import com.google.common.base.Function; +import com.google.common.cache.CacheBuilder; import com.google.common.collect.Lists; -import com.google.common.collect.MapMaker; import com.google.common.collect.MigrateMap; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; @@ -35,7 +35,8 @@ import com.google.protobuf.InvalidProtocolBufferException; public class MQMessageUtils { @SuppressWarnings("deprecation") - private static Map> partitionDatas = MigrateMap.makeComputingMap(new MapMaker().softValues(), + private static Map> partitionDatas = MigrateMap.makeComputingMap(CacheBuilder.newBuilder() + .softValues(), pkHashConfigs -> { List datas = Lists.newArrayList(); @@ -74,7 +75,8 @@ public class MQMessageUtils { }); @SuppressWarnings("deprecation") - private static Map> dynamicTopicDatas = MigrateMap.makeComputingMap(new MapMaker().softValues(), + private static Map> dynamicTopicDatas = MigrateMap.makeComputingMap(CacheBuilder.newBuilder() + .softValues(), new Function>() { public List apply(String pkHashConfigs) { diff --git a/filter/src/main/java/com/alibaba/otter/canal/filter/PatternUtils.java b/filter/src/main/java/com/alibaba/otter/canal/filter/PatternUtils.java index 52b192d0..647b5ecd 100644 --- a/filter/src/main/java/com/alibaba/otter/canal/filter/PatternUtils.java +++ b/filter/src/main/java/com/alibaba/otter/canal/filter/PatternUtils.java @@ -8,13 +8,13 @@ import org.apache.oro.text.regex.PatternCompiler; import org.apache.oro.text.regex.Perl5Compiler; import com.google.common.base.Function; -import com.google.common.collect.MapMaker; +import com.google.common.cache.CacheBuilder; import com.google.common.collect.MigrateMap; public class PatternUtils { @SuppressWarnings("deprecation") - private static Map patterns = MigrateMap.makeComputingMap(new MapMaker().softValues(), + private static Map patterns = MigrateMap.makeComputingMap(CacheBuilder.newBuilder().softValues(), new Function() { public Pattern apply(String pattern) {