avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject [07/35] avro git commit: AVRO-607: Java: Make SpecificData schema cache thread-safe.
Date Sat, 05 Nov 2016 20:20:24 GMT
AVRO-607: Java: Make SpecificData schema cache thread-safe.

Changed SpecificData.getSchema to use a thread-safe Cache class from
Google Guava.


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/cd760da9
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/cd760da9
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/cd760da9

Branch: refs/heads/branch-1.8
Commit: cd760da972d3eb6f9b083f28bab5bd4921509f16
Parents: 391dc15
Author: Andrius Druzinis-Vitkus <andrius.druzinis@gmail.com>
Authored: Thu Jul 28 10:44:06 2016 +0200
Committer: Ryan Blue <blue@apache.org>
Committed: Sat Nov 5 13:14:53 2016 -0700

----------------------------------------------------------------------
 CHANGES.txt                                     |  3 +++
 .../org/apache/avro/specific/SpecificData.java  | 25 +++++++++++++-------
 .../main/java/org/apache/avro/GuavaClasses.java |  6 +++++
 3 files changed, 26 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/cd760da9/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 261e90b..13f897e 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -24,6 +24,9 @@ Trunk (not yet released)
 
     AVRO-1869: Java: Fix Decimal conversion from ByteBuffer. (blue)
 
+    AVRO-607: Java: Make SpecificData schema cache thread-safe.
+    (Andrius Druzinis-Vitkus via blue)
+
 Avro 1.8.1 (14 May 2016)
 
   INCOMPATIBLE CHANGES

http://git-wip-us.apache.org/repos/asf/avro/blob/cd760da9/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
index ec2e33f..be6bde8 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
@@ -23,7 +23,6 @@ import java.util.Map;
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
-import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.LinkedHashMap;
 import java.nio.ByteBuffer;
@@ -32,6 +31,9 @@ import java.lang.reflect.ParameterizedType;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import org.apache.avro.Schema;
 import org.apache.avro.Protocol;
 import org.apache.avro.AvroRuntimeException;
@@ -207,17 +209,24 @@ public class SpecificData extends GenericData {
     return namespace + dot + name;
   }
 
-  private final WeakHashMap<java.lang.reflect.Type,Schema> schemaCache =
-    new WeakHashMap<java.lang.reflect.Type,Schema>();
+  private final LoadingCache<java.lang.reflect.Type,Schema> schemaCache =
+      CacheBuilder.newBuilder()
+          .weakKeys()
+          .build(new CacheLoader<java.lang.reflect.Type,Schema>() {
+            public Schema load(java.lang.reflect.Type type)
+                throws AvroRuntimeException {
+              return createSchema(type, new LinkedHashMap<String,Schema>());
+            }
+          });
 
   /** Find the schema for a Java type. */
   public Schema getSchema(java.lang.reflect.Type type) {
-    Schema schema = schemaCache.get(type);
-    if (schema == null) {
-      schema = createSchema(type, new LinkedHashMap<String,Schema>());
-      schemaCache.put(type, schema);
+    try {
+      return schemaCache.get(type);
+    } catch (Exception e) {
+      throw (e instanceof AvroRuntimeException) ?
+          (AvroRuntimeException)e.getCause() : new AvroRuntimeException(e);
     }
-    return schema;
   }
 
   /** Create the schema for a Java type. */

http://git-wip-us.apache.org/repos/asf/avro/blob/cd760da9/lang/java/guava/src/main/java/org/apache/avro/GuavaClasses.java
----------------------------------------------------------------------
diff --git a/lang/java/guava/src/main/java/org/apache/avro/GuavaClasses.java b/lang/java/guava/src/main/java/org/apache/avro/GuavaClasses.java
index 25d918f..6052217 100644
--- a/lang/java/guava/src/main/java/org/apache/avro/GuavaClasses.java
+++ b/lang/java/guava/src/main/java/org/apache/avro/GuavaClasses.java
@@ -18,6 +18,9 @@
 
 package org.apache.avro;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import com.google.common.collect.MapMaker;
 import com.google.common.primitives.Bytes;
 
@@ -29,5 +32,8 @@ class GuavaClasses {
   static {
     MapMaker.class.getName();
     Bytes.class.getName();
+    LoadingCache.class.getName();
+    CacheBuilder.class.getName();
+    CacheLoader.class.getName();
   }
 }


Mime
View raw message