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
The following commit(s) were added to
refs/heads/feature/transcoding_experiments by this push:
new 0986af2 Fixing the way the caching of strings works for the
CachingSerializer
0986af2 is described below
commit 0986af2beb316809e06d269f338a3a2a58b3a059
Author: Dan Smith <[email protected]>
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 | 54 ++++++++++++++--------
.../serialization/CachingStructSerializerTest.java | 2 +
2 files changed, 38 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..18b2b4a 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,37 @@ 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();
+ }
+ }
+
+ System.out.println("cacheWrite with '" + string + "' returned " + result);
+ 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 +172,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
[email protected].