You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by lu...@apache.org on 2015/08/14 05:21:07 UTC

[02/50] [abbrv] incubator-kylin git commit: KYLIN-928

KYLIN-928


Project: http://git-wip-us.apache.org/repos/asf/incubator-kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kylin/commit/08acfaa8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kylin/tree/08acfaa8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kylin/diff/08acfaa8

Branch: refs/heads/0.7
Commit: 08acfaa8985f77f66010a005cb7313a50d08cff8
Parents: 8278974
Author: qianhao.zhou <qi...@ebay.com>
Authored: Thu Aug 6 10:56:03 2015 +0800
Committer: qianhao.zhou <qi...@ebay.com>
Committed: Thu Aug 6 10:56:03 2015 +0800

----------------------------------------------------------------------
 .../apache/kylin/dict/DictionaryManager.java    | 37 ++++++++++++++------
 1 file changed, 27 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/08acfaa8/dictionary/src/main/java/org/apache/kylin/dict/DictionaryManager.java
----------------------------------------------------------------------
diff --git a/dictionary/src/main/java/org/apache/kylin/dict/DictionaryManager.java b/dictionary/src/main/java/org/apache/kylin/dict/DictionaryManager.java
index c2eb927..949e288 100644
--- a/dictionary/src/main/java/org/apache/kylin/dict/DictionaryManager.java
+++ b/dictionary/src/main/java/org/apache/kylin/dict/DictionaryManager.java
@@ -25,7 +25,12 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import org.apache.commons.compress.utils.IOUtils;
 import org.apache.hadoop.fs.FSDataOutputStream;
 import org.apache.hadoop.fs.FileSystem;
@@ -71,14 +76,24 @@ public class DictionaryManager {
     // ============================================================================
 
     private KylinConfig config;
-    private ConcurrentHashMap<String, DictionaryInfo> dictCache; // resource
+    private LoadingCache<String, DictionaryInfo> dictCache;
 
     // path ==>
     // DictionaryInfo
 
     private DictionaryManager(KylinConfig config) {
         this.config = config;
-        dictCache = new ConcurrentHashMap<String, DictionaryInfo>();
+        this.dictCache = CacheBuilder.newBuilder().weakValues().expireAfterWrite(10, TimeUnit.MINUTES).build(new CacheLoader<String, DictionaryInfo>() {
+            @Override
+            public DictionaryInfo load(String key) throws Exception {
+                DictionaryInfo dictInfo = DictionaryManager.this.load(key, true);
+                if (dictInfo == null) {
+                    return NONE_INDICATOR;
+                } else {
+                    return dictInfo;
+                }
+            }
+        });
     }
 
     public Dictionary<?> getDictionary(String resourcePath) throws IOException {
@@ -87,14 +102,16 @@ public class DictionaryManager {
     }
 
     public DictionaryInfo getDictionaryInfo(String resourcePath) throws IOException {
-        DictionaryInfo dictInfo = dictCache.get(resourcePath);
-        if (dictInfo == null) {
-            dictInfo = load(resourcePath, true);
-            if (dictInfo == null)
-                dictInfo = NONE_INDICATOR;
-            dictCache.put(resourcePath, dictInfo);
+        try {
+            DictionaryInfo result = dictCache.get(resourcePath);
+            if (result == NONE_INDICATOR) {
+                return null;
+            } else {
+                return result;
+            }
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e.getCause());
         }
-        return dictInfo == NONE_INDICATOR ? null : dictInfo;
     }
 
     public DictionaryInfo trySaveNewDict(Dictionary<?> newDict, DictionaryInfo newDictInfo) throws IOException {
@@ -335,7 +352,7 @@ public class DictionaryManager {
     public void removeDictionary(String resourcePath) throws IOException {
         ResourceStore store = MetadataManager.getInstance(config).getStore();
         store.deleteResource(resourcePath);
-        dictCache.remove(resourcePath);
+        dictCache.invalidate(resourcePath);
     }
 
     public void removeDictionaries(String srcTable, String srcCol) throws IOException {