*Marshaller: get CacheSerilizer only once
CacheSerializer implementations are stateless (often implemented through
the enum-singleton pattern). There is no need to reach out for them for
each (de)serilize operation. As a bonus 'unchecked' doesn't have to
suppressed in 'read' operations.
Change-Id: I25d4ef94c52de9199029117abb73613bd841b1f5
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/KeyWrapperMarshaller.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/KeyWrapperMarshaller.java
index 53de07c..1998c3d 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/KeyWrapperMarshaller.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/KeyWrapperMarshaller.java
@@ -13,6 +13,7 @@
// limitations under the License.
package com.googlesource.gerrit.modules.cache.chroniclemap;
+import com.google.gerrit.server.cache.serialize.CacheSerializer;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.util.ReadResolvable;
import net.openhft.chronicle.hash.serialization.BytesReader;
@@ -24,9 +25,11 @@
ReadResolvable<KeyWrapperMarshaller<V>> {
private final String name;
+ private final CacheSerializer<V> cacheSerializer;
KeyWrapperMarshaller(String name) {
this.name = name;
+ this.cacheSerializer = CacheSerializers.getKeySerializer(name);
}
@Override
@@ -34,13 +37,13 @@
return new KeyWrapperMarshaller<>(name);
}
- @SuppressWarnings({"unchecked", "rawtypes"})
+ @SuppressWarnings("rawtypes")
@Override
public KeyWrapper<V> read(Bytes in, KeyWrapper<V> using) {
int serializedLength = (int) in.readUnsignedInt();
byte[] serialized = new byte[serializedLength];
in.read(serialized, 0, serializedLength);
- V v = (V) CacheSerializers.getKeySerializer(name).deserialize(serialized);
+ V v = cacheSerializer.deserialize(serialized);
using = new KeyWrapper<>(v);
return using;
@@ -49,7 +52,7 @@
@SuppressWarnings("rawtypes")
@Override
public void write(Bytes out, KeyWrapper<V> toWrite) {
- final byte[] serialized = CacheSerializers.getKeySerializer(name).serialize(toWrite.getValue());
+ final byte[] serialized = cacheSerializer.serialize(toWrite.getValue());
out.writeUnsignedInt(serialized.length);
out.write(serialized);
}
diff --git a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/TimedValueMarshaller.java b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/TimedValueMarshaller.java
index f85f57e..f26366f 100644
--- a/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/TimedValueMarshaller.java
+++ b/src/main/java/com/googlesource/gerrit/modules/cache/chroniclemap/TimedValueMarshaller.java
@@ -13,6 +13,7 @@
// limitations under the License.
package com.googlesource.gerrit.modules.cache.chroniclemap;
+import com.google.gerrit.server.cache.serialize.CacheSerializer;
import java.nio.ByteBuffer;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.util.ReadResolvable;
@@ -25,9 +26,11 @@
ReadResolvable<TimedValueMarshaller<V>> {
private final String name;
+ private final CacheSerializer<V> cacheSerializer;
TimedValueMarshaller(String name) {
this.name = name;
+ this.cacheSerializer = CacheSerializers.getValueSerializer(name);
}
@Override
@@ -35,7 +38,7 @@
return new TimedValueMarshaller<>(name);
}
- @SuppressWarnings({"rawtypes", "unchecked"})
+ @SuppressWarnings("rawtypes")
@Override
public TimedValue<V> read(Bytes in, TimedValue<V> using) {
long initialPosition = in.readPosition();
@@ -57,7 +60,7 @@
// Deserialize object V (remaining bytes)
byte[] serializedV = new byte[vLength];
in.read(serializedV, 0, vLength);
- V v = (V) CacheSerializers.getValueSerializer(name).deserialize(serializedV);
+ V v = cacheSerializer.deserialize(serializedV);
using = new TimedValue<>(v, created);
@@ -67,7 +70,7 @@
@SuppressWarnings("rawtypes")
@Override
public void write(Bytes out, TimedValue<V> toWrite) {
- byte[] serialized = CacheSerializers.getValueSerializer(name).serialize(toWrite.getValue());
+ byte[] serialized = cacheSerializer.serialize(toWrite.getValue());
// Serialize as follows:
// created | length of serialized V | serialized value V