You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by up...@apache.org on 2018/05/16 18:16:16 UTC

[geode] branch feature/transcoding_experiments updated (0986af2 -> 8b01deb)

This is an automated email from the ASF dual-hosted git repository.

upthewaterspout pushed a change to branch feature/transcoding_experiments
in repository https://gitbox.apache.org/repos/asf/geode.git.


 discard 0986af2  Fixing the way the caching of strings works for the CachingSerializer
     new 8b01deb  Fixing the way the caching of strings works for the CachingSerializer

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (0986af2)
            \
             N -- N -- N   refs/heads/feature/transcoding_experiments (8b01deb)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../geode/protocol/serialization/CachingProtobufStructSerializer.java    | 1 -
 1 file changed, 1 deletion(-)

-- 
To stop receiving notification emails like this one, please contact
upthewaterspout@apache.org.

[geode] 01/01: Fixing the way the caching of strings works for the CachingSerializer

Posted by up...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

upthewaterspout pushed a commit to branch feature/transcoding_experiments
in repository https://gitbox.apache.org/repos/asf/geode.git

commit 8b01debce41b43609ea397a7b550c145b5f7cc9a
Author: Dan Smith <up...@apache.org>
AuthorDate: Wed May 16 11:13:38 2018 -0700

    Fixing the way the caching of strings works for the CachingSerializer
    
    We were still sending the whole string each time
---
 .../CachingProtobufStructSerializer.java           | 53 ++++++++++++++--------
 .../serialization/CachingStructSerializerTest.java |  2 +
 2 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/serialization/CachingProtobufStructSerializer.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/serialization/CachingProtobufStructSerializer.java
index 0249848..1897c1e 100644
--- a/geode-protobuf/src/main/java/org/apache/geode/protocol/serialization/CachingProtobufStructSerializer.java
+++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/serialization/CachingProtobufStructSerializer.java
@@ -15,9 +15,9 @@
 package org.apache.geode.protocol.serialization;
 
 import java.io.IOException;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import com.google.protobuf.ByteString;
 import com.google.protobuf.NullValue;
@@ -33,10 +33,9 @@ import org.apache.geode.pdx.PdxInstance;
 import org.apache.geode.pdx.PdxInstanceFactory;
 
 public class CachingProtobufStructSerializer implements ValueSerializer {
-  static final String PROTOBUF_STRUCT = "__PROTOBUF_STRUCT_AS_PDX";
   private Cache cache;
-  private final Map<String, CachedString> writeCache = new HashMap<>();
-  private final Map<Integer, String> readCache = new HashMap<>();
+  private final Map<String, CachedString> writeCache = new ConcurrentHashMap<>();
+  private final Map<Integer, String> readCache = new ConcurrentHashMap<>();
 
   @Override
   public ByteString serialize(Object object) throws IOException {
@@ -48,13 +47,14 @@ public class CachingProtobufStructSerializer implements ValueSerializer {
     PdxInstance pdxInstance = (PdxInstance) object;
 
     CachingStruct.Builder structBuilder = CachingStruct.newBuilder();
+    structBuilder.setTypeName(cacheWrite(pdxInstance.getClassName()));
+
     for (String fieldName : pdxInstance.getFieldNames()) {
       Object value = pdxInstance.getField(fieldName);
       Field serialized = serializeField(fieldName, value);
       structBuilder.addFields(serialized);
     }
 
-    structBuilder.setTypeName(cacheWrite(pdxInstance.getClassName()));
 
     return structBuilder.build();
   }
@@ -96,8 +96,36 @@ public class CachingProtobufStructSerializer implements ValueSerializer {
   }
 
   private CachedString cacheWrite(final String string) {
-    return writeCache.computeIfAbsent(string,
-        name -> CachedString.newBuilder().setId(writeCache.size() + 1).setValue(name).build());
+    CachedString result;
+    if (string.isEmpty()) {
+      result = CachedString.getDefaultInstance();
+    } else {
+      CachedString cachedValue = writeCache.get(string);
+      if (cachedValue != null) {
+        result = cachedValue;
+      } else {
+        int id = writeCache.size() + 1;
+        writeCache.put(string, CachedString.newBuilder().setId(id).build());
+        result = CachedString.newBuilder().setId(id).setValue(string).build();
+      }
+    }
+
+    return result;
+  }
+
+  private String cacheRead(final CachedString fieldName) {
+    String value = fieldName.getValue();
+    int id = fieldName.getId();
+    if (id == 0) {
+      return value;
+    }
+
+    if (!value.isEmpty()) {
+      readCache.put(id, value);
+      return value;
+    }
+
+    return readCache.get(id);
   }
 
   @Override
@@ -143,17 +171,6 @@ public class CachingProtobufStructSerializer implements ValueSerializer {
     return pdxInstanceFactory.create();
   }
 
-  private String cacheRead(final CachedString fieldName) {
-    String value = fieldName.getValue();
-    int id = fieldName.getId();
-    if (value == null) {
-      value = readCache.get(id);
-    } else if (id != 0) {
-      readCache.put(id, value);
-    }
-    return value;
-  }
-
   private Object deserializeField(Field value) {
     switch (value.getValueCase()) {
       case ENCODEDVALUE:
diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/serialization/CachingStructSerializerTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/serialization/CachingStructSerializerTest.java
index 05b5342..161e753 100644
--- a/geode-protobuf/src/test/java/org/apache/geode/protocol/serialization/CachingStructSerializerTest.java
+++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/serialization/CachingStructSerializerTest.java
@@ -66,6 +66,8 @@ public class CachingStructSerializerTest {
           int.class, long.class, byte.class, byte[].class, double.class,
           PdxInstance.class}) @From(PdxInstanceGenerator.class) PdxInstance original)
       throws IOException, ClassNotFoundException {
+    CachingProtobufStructSerializer serializer = new CachingProtobufStructSerializer();
+    serializer.init(cache);
     ByteString bytes = serializer.serialize(original);
     PdxInstance actual = (PdxInstance) serializer.deserialize(bytes);
     assertThat(original).isEqualTo(actual);

-- 
To stop receiving notification emails like this one, please contact
upthewaterspout@apache.org.