GEODE-2995: Adding EncodingType or OpsProcessor

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

Branch: refs/heads/feature/GEODE-2995
Commit: 336b0e0a5ef96ade587fd78486075b699482c889
Parents: 719c691
Author: Udo Kohlmeyer <[email protected]>
Authored: Tue Jun 13 09:39:16 2017 -0700
Committer: Udo Kohlmeyer <[email protected]>
Committed: Tue Jun 13 09:39:16 2017 -0700

----------------------------------------------------------------------
 .../protocol/handler/ProtocolHandler.java       |  2 +-
 .../src/main/proto/clientProtocol.proto         | 16 ++--
 .../geode/client/protocol/EncodingHandler.java  | 22 +++++
 .../protocol/EncodingHandlerRegistry.java       | 23 +++++
 .../geode/client/protocol/OpsHandler.java       | 24 +++++
 .../client/protocol/OpsHandlerRegistry.java     | 24 +++++
 .../geode/client/protocol/OpsProcessorTest.java | 66 ++++++++++++++
 .../client/protocol/SerializationType.java      | 96 ++++++++++++++++++++
 8 files changed, 264 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java
----------------------------------------------------------------------
diff --git 
a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java
 
b/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java
index 1faacec..c0e6697 100644
--- 
a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java
+++ 
b/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java
@@ -24,5 +24,5 @@ import java.io.OutputStream;
 
 public interface ProtocolHandler <T> {
   T deserialize(InputStream inputStream) throws 
InvalidProtocolMessageException;
-  void serialize(T inputMessage, OutputStream byteArrayOutputStream) throws 
IOException;
+  void serialize(T inputMessage, OutputStream outputStream) throws IOException;
 }

http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/geode-protobuf/src/main/proto/clientProtocol.proto
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/main/proto/clientProtocol.proto 
b/geode-protobuf/src/main/proto/clientProtocol.proto
index 9dc0efb..0c19295 100644
--- a/geode-protobuf/src/main/proto/clientProtocol.proto
+++ b/geode-protobuf/src/main/proto/clientProtocol.proto
@@ -45,12 +45,12 @@ message Request {
         RemoveAllRequest removeAllRequest = 7;
         ListKeysRequest listKeysRequest = 8;
 
-        CreateRegionRequest createRegionRequest = 9;
-        DestroyRegionRequest destroyRegionRequest = 10;
+        CreateRegionRequest createRegionRequest = 21;
+        DestroyRegionRequest destroyRegionRequest = 22;
 
-        PingRequest pingRequest = 11;
-        GetServersRequest getServersRequest = 12;
-        GetRegionsRequest getRegionsRequest = 13;
+        PingRequest pingRequest = 41;
+        GetServersRequest getServersRequest = 42;
+        GetRegionsRequest getRegionsRequest = 43;
     }
 }
 
@@ -68,9 +68,9 @@ message Response {
         CreateRegionResponse createRegionResponse = 20;
         DestroyRegionResponse destroyRegionResponse = 21;
 
-        PingResponse pingResponse = 31;
-        GetServersResponse getServersResponse = 32;
-        GetRegionsResponse getRegionsResponse = 33;
+        PingResponse pingResponse = 41;
+        GetServersResponse getServersResponse = 42;
+        GetRegionsResponse getRegionsResponse = 43;
     }
 }
 

http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.java
----------------------------------------------------------------------
diff --git 
a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.java
 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.java
new file mode 100644
index 0000000..8d8830a
--- /dev/null
+++ 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandler.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.client.protocol;
+
+public interface EncodingHandler<T> {
+  T deserialze(byte[] incoming);
+  byte[] serialize(T incoming);
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.java
----------------------------------------------------------------------
diff --git 
a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.java
 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.java
new file mode 100644
index 0000000..44dee6a
--- /dev/null
+++ 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/EncodingHandlerRegistry.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.client.protocol;
+
+import com.sun.xml.internal.bind.v2.runtime.output.Encoded;
+
+public interface EncodingHandlerRegistry {
+  EncodingHandler getEncodingHandlerForType(Class klass)
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java
----------------------------------------------------------------------
diff --git 
a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java
new file mode 100644
index 0000000..864b024
--- /dev/null
+++ 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandler.java
@@ -0,0 +1,24 @@
+/*
+ * 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.client.protocol;
+
+/**
+ * Created by ukohlmeyer on 6/12/17.
+ */
+public interface OpsHandler<Req,Resp> {
+  public Resp process(Req request);
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandlerRegistry.java
----------------------------------------------------------------------
diff --git 
a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandlerRegistry.java
 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandlerRegistry.java
new file mode 100644
index 0000000..3fdd1bc
--- /dev/null
+++ 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsHandlerRegistry.java
@@ -0,0 +1,24 @@
+/*
+ * 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.client.protocol;
+
+/**
+ * Created by ukohlmeyer on 6/12/17.
+ */
+public interface OpsHandlerRegistry<T> {
+  public OpsHandler getOpsHandler(T operationCode);
+}

http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java
----------------------------------------------------------------------
diff --git 
a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java
 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java
new file mode 100644
index 0000000..ba8490b
--- /dev/null
+++ 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.client.protocol;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.google.protobuf.ByteString;
+
+import org.apache.geode.protocol.protobuf.BasicTypes;
+import org.apache.geode.protocol.protobuf.ClientProtocol;
+import org.apache.geode.protocol.protobuf.RegionAPI;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class OpsProcessorTest {
+  @Test
+  public void testOpsProcessor() {
+    OpsHandlerRegistry registryStub = mock(OpsHandlerRegistry.class);
+    OpsHandler operationHandlerStub = mock(OpsHandler.class);
+    EncodingHandlerRegistry encodingHandlerRegistry = 
mock(EncodingHandlerRegistry.class);
+    EncodingHandler encodingHandler = mock(EncodingHandler.class);
+
+
+    ClientProtocol.Request messageRequest = 
ClientProtocol.Request.newBuilder().build();
+    String expectedResponse = "10";
+    when(registryStub.getOpsHandler(2)).thenReturn(operationHandlerStub);
+    
when(operationHandlerStub.process(messageRequest)).thenReturn(expectedResponse);
+
+    OpsProcessor processor = new OpsProcessor(registryStub);
+    ClientProtocol.Response response = processor.process(messageRequest);
+    Assert.assertEquals(expectedResponse, response.getGetResponse());
+  }
+
+  private class OpsProcessor {
+    private OpsHandlerRegistry opsHandlerRegistry;
+    private EncodingHandler encodingHandler;
+
+    public OpsProcessor(OpsHandlerRegistry opsHandlerRegistry,EncodingHandler 
encodingHandler) {
+      this.opsHandlerRegistry = opsHandlerRegistry;
+    }
+
+    public ClientProtocol.Response process(ClientProtocol.Request request) {
+      OpsHandler<Object, Object> opsHandler = 
opsHandlerRegistry.getOpsHandler(2);
+      ClientProtocol.Response.Builder responseBuilder = 
ClientProtocol.Response.newBuilder();
+      Object rawResponse = opsHandler.process(request);
+      BasicTypes.EncodedValue.Builder encodedValueBuilder = 
BasicTypes.EncodedValue.newBuilder();
+      
encodedValueBuilder.setValue(ByteString.copyFrom(rawResponse.getBytes()));
+      
responseBuilder.setGetResponse(RegionAPI.GetResponse.newBuilder().setResult())
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/geode/blob/336b0e0a/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
new file mode 100644
index 0000000..2ffeaf8
--- /dev/null
+++ 
b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/SerializationType.java
@@ -0,0 +1,96 @@
+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;
+  }
+}

Reply via email to