You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ud...@apache.org on 2017/06/13 22:06:34 UTC
geode git commit: GEODE-2995: Adding encoding registry
Repository: geode
Updated Branches:
refs/heads/feature/GEODE-2995 51fccbb0d -> c7c1717d1
GEODE-2995: Adding encoding registry
Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/c7c1717d
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/c7c1717d
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/c7c1717d
Branch: refs/heads/feature/GEODE-2995
Commit: c7c1717d1ff2f4428065f2ebac43c7d308b21b0d
Parents: 51fccbb
Author: Udo Kohlmeyer <uk...@pivotal.io>
Authored: Tue Jun 13 15:06:31 2017 -0700
Committer: Udo Kohlmeyer <uk...@pivotal.io>
Committed: Tue Jun 13 15:06:31 2017 -0700
----------------------------------------------------------------------
.../geode/serialization/SerializationType.java | 22 +++++
.../apache/geode/serialization/TypeCodec.java | 22 +++++
.../registry/SerializationCodecRegistry.java | 47 ++++++++++
.../CodecAlreadyRegisteredForType.java | 23 +++++
.../CodecNotRegisteredForTypeException.java | 26 ++++++
.../client/protocol/SerializationType.java | 96 --------------------
.../registry/CodecRegistryJUnitTest.java | 86 ++++++++++++++++++
7 files changed, 226 insertions(+), 96 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/geode/blob/c7c1717d/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java
new file mode 100644
index 0000000..ada0674
--- /dev/null
+++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/SerializationType.java
@@ -0,0 +1,22 @@
+package org.apache.geode.serialization;
+
+import org.apache.geode.pdx.PdxInstance;
+
+import java.nio.charset.Charset;
+
+public enum SerializationType {
+ STRING(String.class),
+ BYTE_BLOB(byte[].class),
+ INT(int.class),
+ BYTE(byte.class),
+ SHORT(short.class),
+ LONG(long.class),
+ JSON(PdxInstance.class);
+
+ private static final Charset UTF8 = Charset.forName("UTF-8");
+ public final Class klass;
+
+ SerializationType(Class klass) {
+ this.klass = klass;
+ }
+}
http://git-wip-us.apache.org/repos/asf/geode/blob/c7c1717d/geode-protobuf/src/main/java/org/apache/geode/serialization/TypeCodec.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/TypeCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/TypeCodec.java
new file mode 100644
index 0000000..e756d0a
--- /dev/null
+++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/TypeCodec.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geode.serialization;
+
+public interface TypeCodec<T> {
+ T decode(byte[] incoming);
+ byte[] encode(T incoming);
+}
http://git-wip-us.apache.org/repos/asf/geode/blob/c7c1717d/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/SerializationCodecRegistry.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/SerializationCodecRegistry.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/SerializationCodecRegistry.java
new file mode 100644
index 0000000..b88b186
--- /dev/null
+++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/SerializationCodecRegistry.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geode.serialization.registry;
+
+import org.apache.geode.serialization.SerializationType;
+import org.apache.geode.serialization.TypeCodec;
+import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForType;
+import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException;
+
+import java.util.HashMap;
+
+public class SerializationCodecRegistry {
+ private HashMap<SerializationType, TypeCodec> codecRegistry = new HashMap<>();
+
+ public synchronized void register(SerializationType serializationType, TypeCodec<?> typeCodec) {
+ if (codecRegistry.containsKey(serializationType)) {
+ throw new CodecAlreadyRegisteredForType("There is already a codec registered for type: " + serializationType);
+ }
+ codecRegistry.put(serializationType, typeCodec);
+ }
+
+ public int getRegisteredCodecCount() {
+ return codecRegistry.size();
+ }
+
+ public TypeCodec getCodecForType(SerializationType serializationType) {
+ TypeCodec typeCodec = codecRegistry.get(serializationType);
+ if (typeCodec == null) {
+ throw new CodecNotRegisteredForTypeException("There is no codec registered for type: " + serializationType);
+ }
+ return typeCodec;
+ }
+}
http://git-wip-us.apache.org/repos/asf/geode/blob/c7c1717d/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForType.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForType.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForType.java
new file mode 100644
index 0000000..34c5202
--- /dev/null
+++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForType.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geode.serialization.registry.exception;
+
+public class CodecAlreadyRegisteredForType extends RuntimeException {
+ public CodecAlreadyRegisteredForType(String message) {
+ super(message);
+ }
+}
http://git-wip-us.apache.org/repos/asf/geode/blob/c7c1717d/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java
new file mode 100644
index 0000000..8d895a1
--- /dev/null
+++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geode.serialization.registry.exception;
+
+/**
+ * Created by ukohlmeyer on 6/13/17.
+ */
+public class CodecNotRegisteredForTypeException extends RuntimeException {
+ public CodecNotRegisteredForTypeException(String message) {
+ super(message);
+ }
+}
http://git-wip-us.apache.org/repos/asf/geode/blob/c7c1717d/geode-protobuf/src/test/java/org/apache/geode/client/protocol/SerializationType.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/SerializationType.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/SerializationType.java
deleted file mode 100644
index 2ffeaf8..0000000
--- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/SerializationType.java
+++ /dev/null
@@ -1,96 +0,0 @@
-package org.apache.geode.client.protocol;
-
-import org.apache.geode.pdx.JSONFormatter;
-import org.apache.geode.pdx.PdxInstance;
-
-import java.nio.ByteBuffer;
-import java.nio.charset.Charset;
-
-public enum SerializationType {
- STRING(String.class, new EncodingHandler<String>() {
- @Override
- public String deserialze(byte[] incoming) {
- return new String(incoming, UTF8);
- }
-
- @Override
- public byte[] serialize(String incoming) {
- return incoming != null ? incoming.getBytes(UTF8) : new byte[0];
- }
- }),
- BYTE_BLOB(byte[].class, new EncodingHandler<byte[]>() {
- @Override
- public byte[] deserialze(byte[] incoming) {
- return incoming;
- }
-
- @Override
- public byte[] serialize(byte[] incoming) {
- return incoming;
- }
- }),
- INT(int.class, new EncodingHandler<Integer>() {
- @Override
- public Integer deserialze(byte[] incoming) {
- return ByteBuffer.wrap(incoming).getInt();
- }
-
- @Override
- public byte[] serialize(Integer incoming) {
- return ByteBuffer.allocate(Integer.BYTES).putInt(incoming).array();
- }
- }),
- BYTE(byte.class, new EncodingHandler<Byte>() {
- @Override
- public Byte deserialze(byte[] incoming) {
- return ByteBuffer.wrap(incoming).get();
- }
-
- @Override
- public byte[] serialize(Byte incoming) {
- return ByteBuffer.allocate(Byte.BYTES).put(incoming).array();
- }
- }),
- SHORT(short.class, new EncodingHandler<Short>() {
- @Override
- public Short deserialze(byte[] incoming) {
- return ByteBuffer.wrap(incoming).getShort();
- }
-
- @Override
- public byte[] serialize(Short incoming) {
- return ByteBuffer.allocate(Short.BYTES).putShort(incoming).array();
- }
- }),
- LONG(long.class, new EncodingHandler<Long>() {
- @Override
- public Long deserialze(byte[] incoming) {
- return ByteBuffer.wrap(incoming).getLong();
- }
-
- @Override
- public byte[] serialize(Long incoming) {
- return ByteBuffer.allocate(Long.BYTES).putLong(incoming).array();
- }
- }),
- JSON(PdxInstance.class, new EncodingHandler<PdxInstance>() {
- @Override
- public PdxInstance deserialze(byte[] incoming) {
- return JSONFormatter.fromJSON(incoming);
- }
-
- @Override
- public byte[] serialize(PdxInstance incoming) {
- return JSONFormatter.toJSONByteArray(incoming);
- }
- });
-
- private static final Charset UTF8 = Charset.forName("UTF-8");
- public final Class klass;
- public final EncodingHandler encodingHandler;
-
- <T> SerializationType(Class<T> klass, EncodingHandler<T> encodingHandler) {
- this.klass = klass;
- this.encodingHandler = encodingHandler;
- }
-}
http://git-wip-us.apache.org/repos/asf/geode/blob/c7c1717d/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java
new file mode 100644
index 0000000..19d8edb
--- /dev/null
+++ b/geode-protobuf/src/test/java/org/apache/geode/serialization/registry/CodecRegistryJUnitTest.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geode.serialization.registry;
+
+import org.apache.geode.serialization.SerializationType;
+import org.apache.geode.serialization.TypeCodec;
+import org.apache.geode.serialization.registry.SerializationCodecRegistry;
+import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForType;
+import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Created by ukohlmeyer on 6/13/17.
+ */
+public class CodecRegistryJUnitTest {
+ @Test
+ public void testRegisterCodec() {
+ SerializationCodecRegistry codecRegistry = new SerializationCodecRegistry();
+ Assert.assertEquals(0, codecRegistry.getRegisteredCodecCount());
+ codecRegistry.register(SerializationType.INT, new DummyTypeCodec());
+ Assert.assertEquals(1, codecRegistry.getRegisteredCodecCount());
+ }
+
+ @Test
+ public void testRegisteringCodecForRegisteredType_throwsException() {
+ SerializationCodecRegistry codecRegistry = new SerializationCodecRegistry();
+ codecRegistry.register(SerializationType.INT, new DummyTypeCodec());
+
+ boolean caughtException = false;
+ try {
+ codecRegistry.register(SerializationType.INT, new DummyTypeCodec());
+ } catch (CodecAlreadyRegisteredForType e) {
+ caughtException = true;
+ }
+ Assert.assertTrue("This was supposed to have thrown a CodecAlreadyRegisteredException",caughtException);
+ }
+
+ @Test
+ public void testGetRegisteredCodec() {
+ SerializationCodecRegistry codecRegistry = new SerializationCodecRegistry();
+ TypeCodec expectedCodec = new DummyTypeCodec();
+ codecRegistry.register(SerializationType.INT, expectedCodec);
+ Assert.assertEquals(1, codecRegistry.getRegisteredCodecCount());
+ TypeCodec codec = codecRegistry.getCodecForType(SerializationType.INT);
+ Assert.assertSame(expectedCodec, codec);
+ }
+
+ @Test
+ public void testGetCodecForUnregisteredType_throwsException() {
+ SerializationCodecRegistry codecRegistry = new SerializationCodecRegistry();
+ boolean caughtException = false;
+ try {
+ codecRegistry.getCodecForType(SerializationType.INT);
+ } catch (CodecNotRegisteredForTypeException e) {
+ caughtException = true;
+ }
+ Assert.assertTrue("This should have thrown a CodecNotRegisteredForTypeException", caughtException);
+ }
+
+ class DummyTypeCodec implements TypeCodec {
+ @Override
+ public Object decode(byte[] incoming) {
+ return null;
+ }
+
+ @Override
+ public byte[] encode(Object incoming) {
+ return new byte[0];
+ }
+ }
+}