forked from apache/geode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GEODE-3126: Adding a query command to the experimental driver
Adding a QueryService and query operations to the experimental client.
- Loading branch information
1 parent
8ce5ebf
commit c373f37
Showing
9 changed files
with
277 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...ental-driver/src/main/java/org/apache/geode/experimental/driver/ProtobufQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.experimental.driver; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.protobuf.ProtocolStringList; | ||
|
||
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes; | ||
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes.EncodedValue; | ||
import org.apache.geode.internal.protocol.protobuf.v1.BasicTypes.Table; | ||
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol.Message; | ||
import org.apache.geode.internal.protocol.protobuf.v1.ClientProtocol.Message.MessageTypeCase; | ||
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI.OQLQueryRequest; | ||
import org.apache.geode.internal.protocol.protobuf.v1.RegionAPI.OQLQueryResponse; | ||
|
||
class ProtobufQueryService implements QueryService { | ||
private final ProtobufChannel channel; | ||
|
||
public ProtobufQueryService(ProtobufChannel channel) { | ||
this.channel = channel; | ||
} | ||
|
||
@Override | ||
public <T> Query newQuery(final String queryString) { | ||
return new ProtobufQuery<T>(queryString); | ||
} | ||
|
||
class ProtobufQuery<T> implements Query<T> { | ||
|
||
private final String queryString; | ||
|
||
public ProtobufQuery(final String queryString) { | ||
this.queryString = queryString; | ||
} | ||
|
||
@Override | ||
public List<T> execute(final Object... bindParameters) throws IOException { | ||
List<EncodedValue> encodedParameters = Arrays.asList(bindParameters).stream() | ||
.map(ValueEncoder::encodeValue).collect(Collectors.toList());; | ||
Message request = Message.newBuilder().setOqlQueryRequest( | ||
OQLQueryRequest.newBuilder().addAllBindParameter(encodedParameters).setQuery(queryString)) | ||
.build(); | ||
final OQLQueryResponse response = | ||
channel.sendRequest(request, MessageTypeCase.OQLQUERYRESPONSE).getOqlQueryResponse(); | ||
switch (response.getResultCase()) { | ||
case SINGLERESULT: | ||
return (List<T>) parseSingleResult(response); | ||
case LISTRESULT: | ||
return parseListResult(response); | ||
case TABLERESULT: | ||
return (List<T>) parseTableResult(response); | ||
default: | ||
throw new RuntimeException("Unexpected response: " + response); | ||
} | ||
} | ||
|
||
private List<Map<String, Object>> parseTableResult(final OQLQueryResponse response) { | ||
final Table table = response.getTableResult(); | ||
final ProtocolStringList fieldNames = table.getFieldNameList(); | ||
List<Map<String, Object>> results = new ArrayList<>(); | ||
for (BasicTypes.EncodedValueList row : table.getRowList()) { | ||
final List<Object> decodedRow = row.getElementList().stream().map(ValueEncoder::decodeValue) | ||
.collect(Collectors.toList()); | ||
|
||
Map<String, Object> rowMap = new LinkedHashMap<>(decodedRow.size()); | ||
for (int i = 0; i < decodedRow.size(); i++) { | ||
rowMap.put(fieldNames.get(i), decodedRow.get(i)); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
private List<T> parseListResult(final OQLQueryResponse response) { | ||
return response.getListResult().getElementList().stream() | ||
.map(value -> (T) ValueEncoder.decodeValue(value)).collect(Collectors.toList()); | ||
} | ||
|
||
private List<Object> parseSingleResult(final OQLQueryResponse response) { | ||
return Collections.singletonList(ValueEncoder.decodeValue(response.getSingleResult())); | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
geode-experimental-driver/src/main/java/org/apache/geode/experimental/driver/Query.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.experimental.driver; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public interface Query<T> { | ||
|
||
List<T> execute(Object... bindParameters) throws IOException; | ||
} |
20 changes: 20 additions & 0 deletions
20
...-experimental-driver/src/main/java/org/apache/geode/experimental/driver/QueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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.experimental.driver; | ||
|
||
public interface QueryService { | ||
|
||
<T> Query newQuery(String queryString); | ||
} |
74 changes: 74 additions & 0 deletions
74
...mental-driver/src/test/java/org/apache/geode/experimental/driver/IntegrationTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.experimental.driver; | ||
|
||
import java.util.Properties; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.contrib.java.lang.system.RestoreSystemProperties; | ||
|
||
import org.apache.geode.cache.Cache; | ||
import org.apache.geode.cache.CacheFactory; | ||
import org.apache.geode.cache.RegionShortcut; | ||
import org.apache.geode.cache.server.CacheServer; | ||
import org.apache.geode.distributed.ConfigurationProperties; | ||
import org.apache.geode.distributed.Locator; | ||
|
||
/** | ||
* Created by dan on 2/23/18. | ||
*/ | ||
public class IntegrationTestBase { | ||
private static final String REGION = "region"; | ||
@Rule | ||
public RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); | ||
protected Driver driver; | ||
protected org.apache.geode.cache.Region<Object, Object> serverRegion; | ||
private Locator locator; | ||
private Cache cache; | ||
|
||
@Before | ||
public void createServerAndDriver() throws Exception { | ||
System.setProperty("geode.feature-protobuf-protocol", "true"); | ||
|
||
// Create a cache | ||
CacheFactory cf = new CacheFactory(); | ||
cf.set(ConfigurationProperties.MCAST_PORT, "0"); | ||
cache = cf.create(); | ||
|
||
// Start a locator | ||
locator = Locator.startLocatorAndDS(0, null, new Properties()); | ||
int locatorPort = locator.getPort(); | ||
|
||
// Start a server | ||
CacheServer server = cache.addCacheServer(); | ||
server.setPort(0); | ||
server.start(); | ||
|
||
// Create a region | ||
serverRegion = cache.createRegionFactory(RegionShortcut.REPLICATE).create(REGION); | ||
|
||
// Create a driver connected to the server | ||
driver = new DriverFactory().addLocator("localhost", locatorPort).create(); | ||
|
||
} | ||
|
||
@After | ||
public void cleanup() { | ||
locator.stop(); | ||
cache.close(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...river/src/test/java/org/apache/geode/experimental/driver/QueryServiceIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.experimental.driver; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import org.apache.geode.test.junit.categories.IntegrationTest; | ||
|
||
@Category(IntegrationTest.class) | ||
public class QueryServiceIntegrationTest extends IntegrationTestBase { | ||
|
||
@Test | ||
public void testQuery() throws IOException { | ||
serverRegion.put("key1", "value1"); | ||
serverRegion.put("key2", "value2"); | ||
|
||
QueryService service = driver.getQueryService(); | ||
|
||
Query<String> query = service.newQuery("select value from /region value order by value"); | ||
final List<String> results = query.execute(); | ||
|
||
assertEquals(Arrays.asList("value1", "value2"), results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters