[
https://issues.apache.org/jira/browse/GEODE-4054?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16284110#comment-16284110
]
ASF GitHub Bot commented on GEODE-4054:
---------------------------------------
WireBaron commented on a change in pull request #1141: GEODE-4054: Create
module for Protobuf message-based client
URL: https://github.com/apache/geode/pull/1141#discussion_r155855572
##########
File path:
geode-protobuf-client/src/main/java/org/apache/geode/internal/cache/client/protobuf/Region.java
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.internal.cache.client.protobuf;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.google.protobuf.ByteString;
+
+import org.apache.geode.annotations.Experimental;
+import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes;
+import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol;
+import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI;
+
+@Experimental
+public class Region<K, V> {
+ final String name;
+ final Socket socket;
+
+ BasicTypes.EncodedValue encodeValue(Object unencodedValue) {
+ BasicTypes.EncodedValue.Builder builder =
BasicTypes.EncodedValue.newBuilder();
+ if (Integer.class.equals(unencodedValue.getClass())) {
+ builder.setIntResult((Integer) unencodedValue);
+ } else if (Long.class.equals(unencodedValue.getClass())) {
+ builder.setLongResult((Long) unencodedValue);
+ } else if (Short.class.equals(unencodedValue.getClass())) {
+ builder.setShortResult((Short) unencodedValue);
+ } else if (Byte.class.equals(unencodedValue.getClass())) {
+ builder.setByteResult((Byte) unencodedValue);
+ } else if (Double.class.equals(unencodedValue.getClass())) {
+ builder.setDoubleResult((Double) unencodedValue);
+ } else if (Float.class.equals(unencodedValue.getClass())) {
+ builder.setFloatResult((Float) unencodedValue);
+ } else if (byte[].class.equals(unencodedValue.getClass())) {
+ builder.setBinaryResult(ByteString.copyFrom((byte[]) unencodedValue));
+ } else if (Boolean.class.equals(unencodedValue.getClass())) {
+ builder.setBooleanResult((Boolean) unencodedValue);
+ } else if (String.class.equals(unencodedValue.getClass())) {
+ builder.setStringResult((String) unencodedValue);
+ }
+ return builder.build();
+ }
+
+ Object decodeValue(BasicTypes.EncodedValue encodedValue) {
+ switch (encodedValue.getValueCase()) {
+ case BINARYRESULT:
+ return encodedValue.getBinaryResult().toByteArray();
+ case BOOLEANRESULT:
+ return encodedValue.getBooleanResult();
+ case BYTERESULT:
+ return (byte) encodedValue.getByteResult();
+ case DOUBLERESULT:
+ return encodedValue.getDoubleResult();
+ case FLOATRESULT:
+ return encodedValue.getFloatResult();
+ case INTRESULT:
+ return encodedValue.getIntResult();
+ case LONGRESULT:
+ return encodedValue.getLongResult();
+ case SHORTRESULT:
+ return (short) encodedValue.getShortResult();
+ case STRINGRESULT:
+ return encodedValue.getStringResult();
+ default:
+ return null;
+ }
+ }
+
+ BasicTypes.Entry encodeEntry(Object unencodedKey, Object unencodedValue) {
+ if (unencodedValue == null) {
+ return
BasicTypes.Entry.newBuilder().setKey(encodeValue(unencodedKey)).build();
+ }
+ return BasicTypes.Entry.newBuilder().setKey(encodeValue(unencodedKey))
+ .setValue(encodeValue(unencodedValue)).build();
+ }
+
+ Region(String name, Socket socket) {
+ this.name = name;
+ this.socket = socket;
+ }
+
+ public V get(K key) throws Exception {
+ final OutputStream outputStream = socket.getOutputStream();
+ ClientProtocol.Message.newBuilder()
+
.setRequest(ClientProtocol.Request.newBuilder().setGetRequest(RegionAPI.GetRequest
+
.newBuilder().setRegionName(name).setKey(encodeValue(key.toString()))))
+ .build().writeDelimitedTo(outputStream);
+
+ // TODO: How does one get a java.lang.Object out of
+ // org.apache.geode.internal.protocol.protobuf.v1.EncodedValue?
+ final InputStream inputStream = socket.getInputStream();
+ return (V)
ClientProtocol.Message.parseDelimitedFrom(inputStream).getResponse().getGetResponse()
+ .getResult().getStringResult();
+ }
+
+ public Map<K, V> getAll(Collection<K> keys) throws Exception {
+ Map<K, V> values = new HashMap<>();
+
+ final OutputStream outputStream = socket.getOutputStream();
+ RegionAPI.GetAllRequest.Builder getAllRequest =
RegionAPI.GetAllRequest.newBuilder();
+ getAllRequest.setRegionName(name);
+ for (K key : keys) {
+ getAllRequest.addKey(encodeValue(key.toString()));
+ }
+ ClientProtocol.Message.newBuilder()
+
.setRequest(ClientProtocol.Request.newBuilder().setGetAllRequest(getAllRequest)).build()
+ .writeDelimitedTo(outputStream);
+
+ final InputStream inputStream = socket.getInputStream();
+ final RegionAPI.GetAllResponse getAllResponse =
+
ClientProtocol.Message.parseDelimitedFrom(inputStream).getResponse().getGetAllResponse();
+ for (BasicTypes.Entry entry : getAllResponse.getEntriesList()) {
+ // TODO: How does one get a java.lang.Object out of
+ // org.apache.geode.internal.protocol.protobuf.v1.EncodedValue?
+ // TODO values.put((K) entry.getKey().getStringResult(), (V)
+ // entry.getValue().getStringResult());
Review comment:
values[(K)decodeValue(entry.getKey())] = (V)decodeValue(entry.getValue());
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Create module for Protobuf message-based client
> -----------------------------------------------
>
> Key: GEODE-4054
> URL: https://issues.apache.org/jira/browse/GEODE-4054
> Project: Geode
> Issue Type: Improvement
> Components: client/server
> Reporter: Michael Dodge
> Fix For: 1.4.0
>
>
> Create a module, geode-protobuf-client, that contains a simple Java client
> that exercises the Protobuf messages and the new protocol. This client should
> allow the interaction with a locator and cache server based on command-line
> arguments, a file of commands, or an interactive shell.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)