Repository: calcite Updated Branches: refs/heads/master 9721283bd -> 6e8bb5a16
[CALCITE-2380] Javadoc warnings (Andrei Sereda) Close apache/calcite#741 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/4f835465 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/4f835465 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/4f835465 Branch: refs/heads/master Commit: 4f8354651021dbabf123836d205aee25c658b6d9 Parents: 9721283 Author: Andrei Sereda <[email protected]> Authored: Sun Jun 24 18:59:26 2018 -0400 Committer: Julian Hyde <[email protected]> Committed: Mon Jun 25 20:42:06 2018 -0700 ---------------------------------------------------------------------- .../Elasticsearch2Enumerator.java | 2 + .../elasticsearch2/Elasticsearch2Schema.java | 3 + .../elasticsearch2/Elasticsearch2Table.java | 6 +- .../ElasticSearch2AdapterTest.java | 4 +- .../elasticsearch2/EmbeddedElasticNode.java | 15 +- .../elasticsearch2/EmbeddedElasticRule.java | 35 ++-- .../org/apache/calcite/test/ElasticChecker.java | 6 +- .../Elasticsearch5Enumerator.java | 1 + .../elasticsearch5/Elasticsearch5Schema.java | 2 + .../elasticsearch5/Elasticsearch5Table.java | 5 +- .../ElasticSearch5AdapterTest.java | 4 +- .../elasticsearch5/EmbeddedElasticNode.java | 153 ------------------ .../elasticsearch5/EmbeddedElasticRule.java | 98 ----------- .../EmbeddedElasticsearchNode.java | 161 +++++++++++++++++++ .../org/apache/calcite/test/ElasticChecker.java | 49 ------ .../calcite/adapter/mongodb/MongoSchema.java | 4 +- .../calcite/adapter/mongodb/MongoTable.java | 5 + .../mongodb/MongoToEnumerableConverter.java | 7 +- .../adapter/mongodb/MongoAdapterTest.java | 9 +- .../adapter/mongodb/MongoDatabaseRule.java | 1 + .../apache/calcite/test/MongoAssertions.java | 15 +- 21 files changed, 257 insertions(+), 328 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Enumerator.java ---------------------------------------------------------------------- diff --git a/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Enumerator.java b/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Enumerator.java index c3d2ac0..8247957 100644 --- a/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Enumerator.java +++ b/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Enumerator.java @@ -98,6 +98,8 @@ public class Elasticsearch2Enumerator implements Enumerator<Object> { * objects. * * @param fields List of fields to project + * + * @return function that converts the search result into a generic array */ private static Function1<SearchHit, Object[]> listGetter( final List<Map.Entry<String, Class>> fields) { http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Schema.java ---------------------------------------------------------------------- diff --git a/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Schema.java b/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Schema.java index 46e3fc5..f02a668 100644 --- a/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Schema.java +++ b/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Schema.java @@ -92,6 +92,9 @@ public class Elasticsearch2Schema extends AbstractSchema /** * Allows schema to be instantiated from existing elastic search client. * This constructor is used in tests. + * + * @param client Existing (injectable) client instance + * @param index Index name */ @VisibleForTesting Elasticsearch2Schema(Client client, String index) { http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Table.java ---------------------------------------------------------------------- diff --git a/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Table.java b/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Table.java index 2928835..8bfd8cf 100644 --- a/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Table.java +++ b/elasticsearch2/src/main/java/org/apache/calcite/adapter/elasticsearch2/Elasticsearch2Table.java @@ -39,8 +39,12 @@ public class Elasticsearch2Table extends AbstractElasticsearchTable { /** * Creates an Elasticsearch2Table. + * + * @param client Existing ES instance + * @param indexName Name of the index + * @param typeName Index type */ - public Elasticsearch2Table(Client client, String indexName, String typeName) { + Elasticsearch2Table(Client client, String indexName, String typeName) { super(indexName, typeName); this.client = client; } http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/ElasticSearch2AdapterTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/ElasticSearch2AdapterTest.java b/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/ElasticSearch2AdapterTest.java index 287e094..a7eff8f 100644 --- a/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/ElasticSearch2AdapterTest.java +++ b/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/ElasticSearch2AdapterTest.java @@ -58,6 +58,8 @@ public class ElasticSearch2AdapterTest { /** * Used to create {@code zips} index and insert some data + * + * @throws Exception when couldn't create the instance */ @BeforeClass public static void setupInstance() throws Exception { @@ -144,7 +146,7 @@ public class ElasticSearch2AdapterTest { * Tests using calcite view */ @Test - public void view() throws Exception { + public void view() { calciteAssert() .query("select * from zips where \"city\" = 'BROOKLYN'") .returns("city=BROOKLYN; longitude=-73.956985; latitude=40.646694; " http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticNode.java ---------------------------------------------------------------------- diff --git a/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticNode.java b/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticNode.java index 4474add..55f83f9 100644 --- a/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticNode.java +++ b/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticNode.java @@ -56,13 +56,15 @@ class EmbeddedElasticNode implements AutoCloseable { private static class LocalNode extends Node { private LocalNode(Settings settings, Collection<Class<? extends Plugin>> classpathPlugins) { super(InternalSettingsPreparer.prepareEnvironment(settings, null), - Version.CURRENT, - classpathPlugins); + Version.CURRENT, classpathPlugins); } } /** * Creates an instance with existing settings + * + * @param settings ES settings for the node + * @return un-started node; call {@link #start()} to start the instance */ private static EmbeddedElasticNode create(Settings settings) { // ensure GroovyPlugin is installed or otherwise scripted fields would not work @@ -73,6 +75,8 @@ class EmbeddedElasticNode implements AutoCloseable { /** * Creates elastic node as single member of a cluster. Node will not be started * unless {@link #start()} is explicitly called. + * + * @return node with default configuration */ public static EmbeddedElasticNode create() { File data = Files.createTempDir(); @@ -105,7 +109,9 @@ class EmbeddedElasticNode implements AutoCloseable { } /** - * Returns current address to connect to with HTTP client. + * Returns the current address to connect to with HTTP client. + * + * @return {@code HTTP} address (hostname / port) */ public TransportAddress httpAddress() { Preconditions.checkState(isStarted, "node is not started"); @@ -123,8 +129,9 @@ class EmbeddedElasticNode implements AutoCloseable { /** * Exposes elastic * <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html">transport client</a> - * * (use of HTTP client is preferred). + * + * @return client API to access ES functionality */ public Client client() { Preconditions.checkState(isStarted, "node is not started"); http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticRule.java ---------------------------------------------------------------------- diff --git a/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticRule.java b/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticRule.java index a633078..4bd9c25 100644 --- a/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticRule.java +++ b/elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticRule.java @@ -28,33 +28,31 @@ import org.junit.rules.ExternalResource; * * This rule should be used as follows: * <pre> - * {@code * * public class MyTest { - * @literal @ClassRule + * @ClassRule * public static final ElasticSearchRule RULE = ElasticSearchRule.create(); * - * @literal @BeforeClass - * public void setup() { + * @BeforeClass + * public static void setup() { * // ... populate instance * } * - * @literal @Test + * @Test * public void myTest() { * TransportAddress address = RULE.httpAddress(); - * // .... + * // .... (connect) * } - * } - * } + * } * </pre> * * @see ExternalResource */ class EmbeddedElasticRule extends ExternalResource { - private final EmbeddedElasticNode node; + private final EmbeddedElasticsearchNode node; - private EmbeddedElasticRule(EmbeddedElasticNode resource) { + private EmbeddedElasticsearchPolicy(EmbeddedElasticsearchNode resource) { this.node = Preconditions.checkNotNull(resource, "resource"); } @@ -72,13 +70,22 @@ class EmbeddedElasticRule extends ExternalResource { /** * Factory method to create this rule. + * + * @return new rule instance to be used in unit tests */ +<<<<<<< HEAD:elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticRule.java public static EmbeddedElasticRule create() { return new EmbeddedElasticRule(EmbeddedElasticNode.create()); +======= + public static EmbeddedElasticsearchPolicy create() { + return new EmbeddedElasticsearchPolicy(EmbeddedElasticsearchNode.create()); +>>>>>>> 54c598206... more 2380:elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticsearchPolicy.java } /** * Exposes current ES transport client. + * + * @return initialized instance of ES */ Client client() { return node.client(); @@ -86,12 +93,16 @@ class EmbeddedElasticRule extends ExternalResource { /** * HTTP address for rest clients (can be ES native or any other). + * +<<<<<<< HEAD:elasticsearch2/src/test/java/org/apache/calcite/adapter/elasticsearch2/EmbeddedElasticRule.java + * @return HTTP hostname/port to connect to this ES instance +======= + * @return {@code HTTP} connection parameters +>>>>>>> 54c598206... more 2380:elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticsearchPolicy.java */ TransportAddress httpAddress() { return node.httpAddress(); } - - } // End EmbeddedElasticRule.java http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch2/src/test/java/org/apache/calcite/test/ElasticChecker.java ---------------------------------------------------------------------- diff --git a/elasticsearch2/src/test/java/org/apache/calcite/test/ElasticChecker.java b/elasticsearch2/src/test/java/org/apache/calcite/test/ElasticChecker.java index 21fc491..86e9d9f 100644 --- a/elasticsearch2/src/test/java/org/apache/calcite/test/ElasticChecker.java +++ b/elasticsearch2/src/test/java/org/apache/calcite/test/ElasticChecker.java @@ -31,7 +31,11 @@ public class ElasticChecker { /** Returns a function that checks that a particular Elasticsearch pipeline is - * generated to implement a query. */ + * generated to implement a query. + * + * @param strings list of expected queries + * @return function to perform the check + */ public static Function<List, Void> elasticsearchChecker(final String... strings) { return new Function<List, Void>() { @Nullable http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Enumerator.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Enumerator.java b/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Enumerator.java index 262058a..4ebb626 100644 --- a/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Enumerator.java +++ b/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Enumerator.java @@ -97,6 +97,7 @@ public class Elasticsearch5Enumerator implements Enumerator<Object> { * objects. * * @param fields List of fields to project + * @return conversion function to transform ES search result into a generic array */ private static Function1<SearchHit, Object[]> listGetter( final List<Map.Entry<String, Class>> fields) { http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Schema.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Schema.java b/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Schema.java index ebb96c8..b5e0f34 100644 --- a/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Schema.java +++ b/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Schema.java @@ -92,6 +92,8 @@ public class Elasticsearch5Schema extends AbstractSchema /** * Allows schema to be instantiated from existing elastic search client. * This constructor is used in tests. + * @param client existing client instance + * @param index name of ES index */ @VisibleForTesting Elasticsearch5Schema(Client client, String index) { http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Table.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Table.java b/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Table.java index 66e3edf..96bef62 100644 --- a/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Table.java +++ b/elasticsearch5/src/main/java/org/apache/calcite/adapter/elasticsearch5/Elasticsearch5Table.java @@ -50,8 +50,11 @@ public class Elasticsearch5Table extends AbstractElasticsearchTable { /** * Creates an Elasticsearch5Table. + * @param client existing ES instance + * @param indexName ES index name + * @param typeName ES type */ - public Elasticsearch5Table(Client client, String indexName, String typeName) { + Elasticsearch5Table(Client client, String indexName, String typeName) { super(indexName, typeName); this.client = client; } http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/ElasticSearch5AdapterTest.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/ElasticSearch5AdapterTest.java b/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/ElasticSearch5AdapterTest.java index adec593..438e048 100644 --- a/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/ElasticSearch5AdapterTest.java +++ b/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/ElasticSearch5AdapterTest.java @@ -60,6 +60,8 @@ public class ElasticSearch5AdapterTest { /** * Used to create {@code zips} index and insert some data + * + * @throws Exception when ES instance setup failed */ @BeforeClass public static void setupInstance() throws Exception { @@ -148,7 +150,7 @@ public class ElasticSearch5AdapterTest { * Tests using calcite view */ @Test - public void view() throws Exception { + public void view() { calciteAssert() .query("select * from zips where \"city\" = 'BROOKLYN'") .returns("city=BROOKLYN; longitude=-73.956985; latitude=40.646694; " http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticNode.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticNode.java b/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticNode.java deleted file mode 100644 index c644406..0000000 --- a/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticNode.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * 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.calcite.adapter.elasticsearch5; - -import com.google.common.base.Preconditions; -import com.google.common.io.Files; - -import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; -import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; -import org.elasticsearch.client.Client; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.node.InternalSettingsPreparer; -import org.elasticsearch.node.Node; -import org.elasticsearch.node.NodeValidationException; -import org.elasticsearch.painless.PainlessPlugin; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.transport.Netty3Plugin; - -import java.io.File; -import java.util.Arrays; -import java.util.Collection; - -/** - * Represents a single elastic search node which can run embedded in a java application. - * Intended for unit and integration tests. Settings and plugins are crafted for Calcite. - */ -class EmbeddedElasticNode implements AutoCloseable { - - private final Node node; - private volatile boolean isStarted; - - private EmbeddedElasticNode(Node node) { - this.node = Preconditions.checkNotNull(node, "node"); - } - - - /** - * Creates an instance with existing settings - */ - private static EmbeddedElasticNode create(Settings settings) { - // ensure GroovyPlugin is installed or otherwise scripted fields would not work - Node node = new LocalNode(settings, Arrays.asList(Netty3Plugin.class, PainlessPlugin.class)); - return new EmbeddedElasticNode(node); - } - - /** - * Creates elastic node as single member of a cluster. Node will not be started - * unless {@link #start()} is explicitly called. - */ - public static EmbeddedElasticNode create() { - File data = Files.createTempDir(); - data.deleteOnExit(); - File home = Files.createTempDir(); - home.deleteOnExit(); - - Settings settings = Settings.builder() - .put("node.name", "fake-elastic") - .put("path.home", home.getAbsolutePath()) - .put("path.data", data.getAbsolutePath()) - .put("transport.type", "local") - .put("http.type", "netty3") - .put("script.inline", true) // requires groovy or painless - .put("network.host", "localhost") - .build(); - - return create(settings); - } - - /** - * Starts current node - */ - public void start() { - Preconditions.checkState(!isStarted, "already started"); - try { - node.start(); - this.isStarted = true; - } catch (NodeValidationException e) { - throw new RuntimeException(e); - } - } - - /** - * Returns current address to connect to with HTTP client. - */ - public TransportAddress httpAddress() { - Preconditions.checkState(isStarted, "node is not started"); - - NodesInfoResponse response = client().admin().cluster().prepareNodesInfo() - .execute().actionGet(); - if (response.getNodes().size() != 1) { - throw new IllegalStateException("Expected single node but got " - + response.getNodes().size()); - } - NodeInfo node = response.getNodes().get(0); - return node.getHttp().address().boundAddresses()[0]; - } - - /** - * Exposes elastic - * <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html">transport client</a> - * - * (use of HTTP client is preferred). - */ - public Client client() { - Preconditions.checkState(isStarted, "node is not started"); - return node.client(); - } - - @Override public void close() throws Exception { - node.close(); - // cleanup data dirs - for (String name: Arrays.asList("path.data", "path.home")) { - if (node.settings().get(name) != null) { - File file = new File(node.settings().get(name)); - if (file.exists()) { - file.delete(); - } - } - } - } - - /** - * Having separate class to expose (protected) constructor which allows to install - * different plugins. In our case it is {@code GroovyPlugin} for scripted fields - * like {@code loc[0]} or {@code loc[1]['foo']}. - * - * <p>This class is intended solely for tests - */ - private static class LocalNode extends Node { - - private LocalNode(Settings settings, Collection<Class<? extends Plugin>> classpathPlugins) { - super(InternalSettingsPreparer.prepareEnvironment(settings, null), - classpathPlugins); - } - } -} - -// End EmbeddedElasticNode.java http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticRule.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticRule.java b/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticRule.java deleted file mode 100644 index d25e8b1..0000000 --- a/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticRule.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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.calcite.adapter.elasticsearch5; - -import com.google.common.base.Preconditions; - -import org.elasticsearch.client.Client; -import org.elasticsearch.common.transport.TransportAddress; - -import org.junit.rules.ExternalResource; - -/** - * Used to initialize a single elastic node. For performance reasons (node startup costs), - * same instance is usually shared across multiple tests. - * - * This rule should be used as follows: - * <pre> - * {@code - * - * public class MyTest { - * @literal @ClassRule - * public static final ElasticSearchRule RULE = ElasticSearchRule.create(); - * - * @literal @BeforeClass - * public void setup() { - * // ... populate instance - * } - * - * @literal @Test - * public void myTest() { - * TransportAddress address = RULE.httpAddress(); - * // .... - * } - * } - * } - * </pre> - * - * @see ExternalResource - */ -class EmbeddedElasticRule extends ExternalResource { - - private final EmbeddedElasticNode node; - - private EmbeddedElasticRule(EmbeddedElasticNode resource) { - this.node = Preconditions.checkNotNull(resource, "resource"); - } - - @Override protected void before() throws Throwable { - node.start(); - } - - @Override protected void after() { - try { - node.close(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - /** - * Factory method to create this rule. - */ - public static EmbeddedElasticRule create() { - return new EmbeddedElasticRule(EmbeddedElasticNode.create()); - } - - /** - * Exposes current ES transport client. - */ - Client client() { - return node.client(); - } - - /** - * HTTP address for rest clients (can be ES native or any other). - */ - TransportAddress httpAddress() { - return node.httpAddress(); - } - - -} - -// End EmbeddedElasticRule.java http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticsearchNode.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticsearchNode.java b/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticsearchNode.java new file mode 100644 index 0000000..d603d87 --- /dev/null +++ b/elasticsearch5/src/test/java/org/apache/calcite/adapter/elasticsearch5/EmbeddedElasticsearchNode.java @@ -0,0 +1,161 @@ +/* + * 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.calcite.adapter.elasticsearch5; + +import com.google.common.base.Preconditions; +import com.google.common.io.Files; + +import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; +import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.node.InternalSettingsPreparer; +import org.elasticsearch.node.Node; +import org.elasticsearch.node.NodeValidationException; +import org.elasticsearch.painless.PainlessPlugin; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.transport.Netty3Plugin; + +import java.io.File; +import java.util.Arrays; +import java.util.Collection; + +/** + * Represents a single elastic search node which can run embedded in a java application. + * Intended for unit and integration tests. Settings and plugins are crafted for Calcite. + */ +class EmbeddedElasticsearchNode implements AutoCloseable { + + private final Node node; + private volatile boolean isStarted; + + private EmbeddedElasticsearchNode(Node node) { + this.node = Preconditions.checkNotNull(node, "node"); + } + + + /** + * Creates an instance with existing settings + * + * @param settings ES configuration + * @return un-initialized node. Use {@link #start()} explicitly. + */ + private static EmbeddedElasticsearchNode create(Settings settings) { + // ensure GroovyPlugin is installed or otherwise scripted fields would not work + Node node = new LocalNode(settings, Arrays.asList(Netty3Plugin.class, PainlessPlugin.class)); + return new EmbeddedElasticsearchNode(node); + } + + /** + * Creates elastic node as single member of a cluster. Node will not be started + * unless {@link #start()} is explicitly called. + * + * @return un-initialized node. Use {@link #start()} explicitly. + */ + public static EmbeddedElasticsearchNode create() { + File data = Files.createTempDir(); + data.deleteOnExit(); + File home = Files.createTempDir(); + home.deleteOnExit(); + + Settings settings = Settings.builder() + .put("node.name", "fake-elastic") + .put("path.home", home.getAbsolutePath()) + .put("path.data", data.getAbsolutePath()) + .put("transport.type", "local") + .put("http.type", "netty3") + .put("script.inline", true) // requires groovy or painless + .put("network.host", "localhost") + .build(); + + return create(settings); + } + + /** + * Starts current node + */ + public void start() { + Preconditions.checkState(!isStarted, "already started"); + try { + node.start(); + this.isStarted = true; + } catch (NodeValidationException e) { + throw new RuntimeException(e); + } + } + + /** + * Returns the current address to connect to with HTTP client. + * + * @return {@code HTTP} protocol connection parameters + */ + TransportAddress httpAddress() { + Preconditions.checkState(isStarted, "node is not started"); + + NodesInfoResponse response = client().admin().cluster().prepareNodesInfo() + .execute().actionGet(); + if (response.getNodes().size() != 1) { + throw new IllegalStateException("Expected single node but got " + + response.getNodes().size()); + } + NodeInfo node = response.getNodes().get(0); + return node.getHttp().address().boundAddresses()[0]; + } + + /** + * Exposes elastic + * <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html">transport client</a> + * (use of HTTP client is preferred). + * + * @return ES client API on a running instance + */ + public Client client() { + Preconditions.checkState(isStarted, "node is not started"); + return node.client(); + } + + @Override public void close() throws Exception { + node.close(); + // cleanup data dirs + for (String name: Arrays.asList("path.data", "path.home")) { + if (node.settings().get(name) != null) { + File file = new File(node.settings().get(name)); + if (file.exists()) { + file.delete(); + } + } + } + } + + /** + * Having separate class to expose (protected) constructor which allows to install + * different plugins. In our case it is {@code GroovyPlugin} for scripted fields + * like {@code loc[0]} or {@code loc[1]['foo']}. + * + * <p>This class is intended solely for tests + */ + private static class LocalNode extends Node { + + private LocalNode(Settings settings, Collection<Class<? extends Plugin>> classpathPlugins) { + super(InternalSettingsPreparer.prepareEnvironment(settings, null), + classpathPlugins); + } + } +} + +// End EmbeddedElasticsearchNode.java http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/elasticsearch5/src/test/java/org/apache/calcite/test/ElasticChecker.java ---------------------------------------------------------------------- diff --git a/elasticsearch5/src/test/java/org/apache/calcite/test/ElasticChecker.java b/elasticsearch5/src/test/java/org/apache/calcite/test/ElasticChecker.java deleted file mode 100644 index 21fc491..0000000 --- a/elasticsearch5/src/test/java/org/apache/calcite/test/ElasticChecker.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.calcite.test; - -import com.google.common.base.Function; - -import java.util.List; - -import javax.annotation.Nullable; - -/** - * Internal util methods for ElasticSearch tests - */ -public class ElasticChecker { - - private ElasticChecker() {} - - - /** Returns a function that checks that a particular Elasticsearch pipeline is - * generated to implement a query. */ - public static Function<List, Void> elasticsearchChecker(final String... strings) { - return new Function<List, Void>() { - @Nullable - @Override public Void apply(@Nullable List actual) { - Object[] actualArray = actual == null || actual.isEmpty() ? null - : ((List) actual.get(0)).toArray(); - CalciteAssert.assertArrayEqual("expected Elasticsearch query not found", strings, - actualArray); - return null; - } - }; - } -} - -// End ElasticChecker.java http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoSchema.java ---------------------------------------------------------------------- diff --git a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoSchema.java b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoSchema.java index 85b3e61..b7202ae 100644 --- a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoSchema.java +++ b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoSchema.java @@ -43,9 +43,11 @@ public class MongoSchema extends AbstractSchema { * Creates a MongoDB schema. * * @param host Mongo host, e.g. "localhost" + * @param credentialsList Optional credentials (empty list for none) + * @param options Mongo connection options * @param database Mongo database name, e.g. "foodmart" */ - public MongoSchema(String host, String database, + MongoSchema(String host, String database, List<MongoCredential> credentialsList, MongoClientOptions options) { super(); try { http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoTable.java ---------------------------------------------------------------------- diff --git a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoTable.java b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoTable.java index f00b5b4..740516e 100644 --- a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoTable.java +++ b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoTable.java @@ -197,6 +197,11 @@ public class MongoTable extends AbstractQueryableTable /** Called via code-generation. * + * @param filterJson Filter document + * @param projectJson Projection document + * @param fields List of expected fields (and their types) + * @return result of mongo query + * * @see org.apache.calcite.adapter.mongodb.MongoMethod#MONGO_QUERYABLE_FIND */ @SuppressWarnings("UnusedDeclaration") http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoToEnumerableConverter.java ---------------------------------------------------------------------- diff --git a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoToEnumerableConverter.java b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoToEnumerableConverter.java index cbbeb86..579c484 100644 --- a/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoToEnumerableConverter.java +++ b/mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoToEnumerableConverter.java @@ -140,7 +140,12 @@ public class MongoToEnumerableConverter } /** E.g. {@code constantArrayList("x", "y")} returns - * "Arrays.asList('x', 'y')". */ + * "Arrays.asList('x', 'y')". + * + * @param values List of values + * @param clazz Type of values + * @return expression + */ private static <T> MethodCallExpression constantArrayList(List<T> values, Class clazz) { return Expressions.call( http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java ---------------------------------------------------------------------- diff --git a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java index 00daf53..a4d8900 100644 --- a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java +++ b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java @@ -751,8 +751,13 @@ public class MongoAdapterTest implements SchemaFactory { }); } - /** Returns a function that checks that a particular MongoDB pipeline is - * generated to implement a query. */ + /** + * Returns a function that checks that a particular MongoDB pipeline is + * generated to implement a query. + * + * @param strings Expected expressions + * @return validation function + */ private static Function<List, Void> mongoChecker(final String... strings) { return new Function<List, Void>() { public Void apply(List actual) { http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabaseRule.java ---------------------------------------------------------------------- diff --git a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabaseRule.java b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabaseRule.java index 90bd759..d73f503 100644 --- a/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabaseRule.java +++ b/mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoDatabaseRule.java @@ -53,6 +53,7 @@ class MongoDatabaseRule extends ExternalResource { /** * Create an instance based on current maven profile (as defined by {@code -Pit}). + * @return new instance of the rule to be used by unit tests */ static MongoDatabaseRule create() { final MongoClient client; http://git-wip-us.apache.org/repos/asf/calcite/blob/4f835465/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java ---------------------------------------------------------------------- diff --git a/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java b/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java index 028286a..67626c3 100644 --- a/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java +++ b/mongodb/src/test/java/org/apache/calcite/test/MongoAssertions.java @@ -40,8 +40,13 @@ public class MongoAssertions { private MongoAssertions() {} - /** Similar to {@link CalciteAssert#checkResultUnordered}, but filters strings - * before comparing them. */ + /** + * Similar to {@link CalciteAssert#checkResultUnordered}, but filters strings + * before comparing them. + * + * @param lines Expected expressions + * @return validation function + */ public static Function<ResultSet, Void> checkResultUnordered( final String... lines) { return new Function<ResultSet, Void>() { @@ -73,6 +78,8 @@ public class MongoAssertions { * Whether to run Mongo integration tests. Enabled by default, however test is only * included if "it" profile is activated ({@code -Pit}). To disable, * specify {@code -Dcalcite.test.mongodb=false} on the Java command line. + * + * @return Whether current tests should use an external mongo instance */ public static boolean useMongo() { return Util.getBooleanProperty("calcite.integrationTest") @@ -81,12 +88,14 @@ public class MongoAssertions { /** * Checks wherever tests should use Fongo instead of Mongo. Opposite of {@link #useMongo()}. + * + * @return Whether current tests should use embedded + * <a href="https://github.com/fakemongo/fongo">Fongo</a> instance */ public static boolean useFongo() { return !useMongo(); } - /** * Used to skip tests if current instance is not mongo. Some functionalities * are not available in fongo.
