├── NOTICE ├── doc ├── src │ └── site │ │ └── sphinx │ │ ├── index.rst │ │ └── about.rst └── pom.xml ├── connector-mongodb └── src │ ├── main │ ├── template │ │ ├── .MongoDBConnector.swp │ │ └── MongoDBConnector │ ├── include │ │ └── NOTICE │ ├── unix │ │ ├── files_and_dirs │ │ │ ├── etc │ │ │ │ ├── default │ │ │ │ │ └── mongodb_connector │ │ │ │ └── init.d │ │ │ │ │ └── connector_mongodb │ │ │ └── DEBIAN │ │ │ │ ├── copyright │ │ │ │ └── conffiles │ │ └── scripts │ │ │ ├── pre │ │ │ ├── preinst │ │ │ ├── post │ │ │ └── postinst │ ├── config │ │ ├── connector-application.conf │ │ ├── log4j.properties │ │ ├── MongoDataStore.xml │ │ └── MongoConnector.xml │ └── java │ │ └── com │ │ └── stratio │ │ └── connector │ │ └── mongodb │ │ └── core │ │ ├── configuration │ │ ├── ShardKeyType.java │ │ ├── CustomMongoIndexType.java │ │ ├── NativeTypes.java │ │ ├── IndexOptions.java │ │ ├── ConfigurationManager.java │ │ ├── TableOptions.java │ │ └── ConfigurationOptions.java │ │ ├── exceptions │ │ ├── MongoDeleteException.java │ │ ├── MongoInsertException.java │ │ ├── MongoExecutionException.java │ │ └── MongoValidationException.java │ │ ├── engine │ │ ├── query │ │ │ ├── utils │ │ │ │ ├── LimitDBObjectBuilder.java │ │ │ │ ├── DBObjectBuilder.java │ │ │ │ ├── OrderByDBObjectBuilder.java │ │ │ │ ├── GroupByDBObjectBuilder.java │ │ │ │ ├── ProjectDBObjectBuilder.java │ │ │ │ └── MetaResultUtils.java │ │ │ ├── MongoLogicalWorkflowValidator.java │ │ │ ├── LogicalWorkflowExecutorFactory.java │ │ │ ├── BasicLogicalWorkflowExecutor.java │ │ │ ├── AggregationLogicalWorkflowExecutor.java │ │ │ └── LogicalWorkflowExecutor.java │ │ ├── metadata │ │ │ ├── AlterOptionsUtils.java │ │ │ ├── SelectorOptionsUtils.java │ │ │ └── StorageUtils.java │ │ └── MongoQueryEngine.java │ │ ├── connection │ │ ├── MongoConnectionHandler.java │ │ └── DriverConnection.java │ │ └── MongoConnector.java │ └── test │ ├── resources │ ├── log4j.xml │ ├── MongoDataStore.xml │ └── MongoConnector.xml │ └── java │ └── com │ └── stratio │ └── connector │ └── mongodb │ └── core │ ├── engine │ ├── metadata │ │ ├── AlterOptionsUtilsTest.java │ │ ├── SelectorOptionUtilsTest.java │ │ ├── StorageUtilsTest.java │ │ ├── UpdateDBObjectBuilderTest.java │ │ ├── IndexUtilsTest.java │ │ └── ShardUtilsTest.java │ ├── query │ │ ├── LogicalWorkflowExecutorFactoryTest.java │ │ └── utils │ │ │ ├── GroupByDBObjectBuilderTest.java │ │ │ ├── OrderByDBObjectBuilderTest.java │ │ │ ├── ProjectBuilderTest.java │ │ │ └── MetaResultUtilTest.java │ └── MongoQueryEngineTest.java │ ├── configuration │ └── ConfigurationManagerTest.java │ ├── MongoConnectorTest.java │ └── connection │ └── ConnectionHandlerTest.java ├── .gitmodules ├── .gitignore ├── CHANGELOG.md ├── README.md └── pom.xml /NOTICE: -------------------------------------------------------------------------------- 1 | Stratio Connector-MongoDB 2 | Copyright 2014 Stratio 3 | 4 | This product includes software developed at 5 | Stratio (http://www.openstratio.org/). 6 | -------------------------------------------------------------------------------- /doc/src/site/sphinx/index.rst: -------------------------------------------------------------------------------- 1 | Contents: 2 | ******** 3 | .. toctree:: 4 | :maxdepth: 4 5 | :numbered: 6 | 7 | about.rst 8 | First_Steps.rst 9 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/template/.MongoDBConnector.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Stratio/stratio-connector-mongodb/HEAD/connector-mongodb/src/main/template/.MongoDBConnector.swp -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "doc/src/site/sphinx/_themes/stratio"] 2 | path = doc/src/site/sphinx/_themes/stratio 3 | url = https://github.com/Stratio/sphinx-theme-stratio.git 4 | branch = master 5 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/include/NOTICE: -------------------------------------------------------------------------------- 1 | Stratio Connector-MongoDB 2 | Copyright 2014 Stratio 3 | 4 | This product includes software developed at 5 | Stratio (http://www.openstratio.org/). 6 | 7 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/files_and_dirs/etc/default/mongodb_connector: -------------------------------------------------------------------------------- 1 | export MONGODB_CONNECTOR_CONF="/etc/sds/connectors/mongodb" 2 | export MONGODB_CONNECTOR_HOME="/opt/sds/connectors/mongodb" 3 | export MONGODB_CONNECTOR_LOGS="/var/logs/sds/connectors/mongodb" 4 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/files_and_dirs/DEBIAN/copyright: -------------------------------------------------------------------------------- 1 | Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Copyright: 2014, Stratio, Inc. All rights reserved 3 | License: Stratio License available at http://www.stratio.com/licenses 4 | Files: * 5 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/files_and_dirs/DEBIAN/conffiles: -------------------------------------------------------------------------------- 1 | /etc/default/stratio-connector-mongodb 2 | /etc/sds/stratio-connector-mongodb/connector-application.conf 3 | /etc/sds/stratio-connector-mongodb/log.properties 4 | /etc/sds/stratio-connector-mongodb/connector-reference.conf 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Package Files # 2 | *.jar 3 | *.war 4 | *.ear 5 | *.swp 6 | 7 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 8 | hs_err_pid* 9 | /bin 10 | /target 11 | /.settings 12 | 13 | # Eclipse 14 | .classpath 15 | .project 16 | .settings/ 17 | 18 | # Intellij 19 | .idea/ 20 | *.iml 21 | *.iws 22 | 23 | # Mac 24 | .DS_Store 25 | 26 | # Maven 27 | log/ 28 | target/ 29 | 30 | 31 | .clover 32 | *.out 33 | *.log 34 | *.class 35 | .orig 36 | *~ 37 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/config/connector-application.conf: -------------------------------------------------------------------------------- 1 | #Seed nodes in the akka cluster including servers 2 | crossdata-connector.akka.cluster.seed-nodes = ["akka.tcp://CrossdataServerCluster@127.0.0.1:13420"] 3 | #Local IP where the connector binds to 4 | crossdata-connector.akka.remote.netty.tcp.hostname = "127.0.0.1" 5 | #Local port, use 0 for autoselection 6 | crossdata-connector.akka.remote.netty.tcp.port = 0 7 | #Number of threads receiving incoming queries 8 | crossdata-connector.config.akka.number.connector-actor = 5 9 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/scripts/pre: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #RPM 4 | 5 | set -e 6 | 7 | GROUP='stratio' 8 | USER='stratio' 9 | 10 | # Add stratio group if it doesn't already exists 11 | if ! getent group $GROUP >/dev/null 2>&1; then 12 | groupadd ${GROUP} 13 | fi 14 | 15 | # Add stratio user if it doesn't already exists 16 | if ! getent passwd $USER >/dev/null 2>&1; then 17 | [ -d /opt ] || mkdir /opt 18 | useradd -M -d /opt/sds -g ${GROUP} -s /bin/bash ${USER} 19 | else 20 | usermod -G $GROUP $USER 21 | fi 22 | 23 | exit 0 -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 0.5.4 (November 2015) 4 | 5 | * Adapted to Stratio Crossdata 0.5.1 6 | 7 | ## 0.5.3 (October 2015) 8 | 9 | * Adapted to Stratio Crossdata 0.5.0 10 | * Fixed bug with import catalogs 11 | 12 | ## 0.5.2 (September 2015) 13 | 14 | * Adapted to Stratio Crossdata 0.4.3 15 | 16 | ## 0.5.1 (September 2015) 17 | 18 | * DEB and RPM generation. 19 | * Adapted to Stratio Crossdata 0.4.1 20 | 21 | 22 | ## 0.5.0 (August 2015) 23 | 24 | * Bugs fixed 25 | * Adapted to Stratio Crossdata 0.4 26 | 27 | 28 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/scripts/preinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | #DEB 6 | 7 | GROUP='stratio' 8 | USER='stratio' 9 | 10 | case "$1" in 11 | configure) 12 | 13 | # Add stratio group if it doesn't already exists 14 | if ! getent group $GROUP >/dev/null 2>&1; then 15 | groupadd ${GROUP} 16 | fi 17 | 18 | # Add stratio user if it doesn't already exists 19 | if ! getent passwd $USER >/dev/null 2>&1; then 20 | [ -d /opt ] || mkdir /opt 21 | useradd -M -d /opt/sds -g ${GROUP} -s /bin/bash ${USER} 22 | else 23 | usermod -G $GROUP $USER 24 | fi 25 | 26 | esac 27 | 28 | 29 | exit 0 -------------------------------------------------------------------------------- /connector-mongodb/src/main/config/log4j.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to STRATIO (C) under one or more contributor license agreements. 3 | # See the NOTICE file distributed with this work for additional information 4 | # regarding copyright ownership. The STRATIO (C) licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | # 18 | 19 | log4j.rootLogger=INFO, STDOUT 20 | log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender 21 | log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout 22 | log4j.appender.STDOUT.layout.ConversionPattern=[Driver] %d{dd-MM-yyyy HH:mm:ss.SSS} [%p|%c{1}] %m%n 23 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/scripts/post: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #RPM 4 | 5 | set -e 6 | 7 | GROUP='stratio' 8 | USER='stratio' 9 | DIR_INSTALL='/opt/sds/connectors/mongodb' 10 | DIR_CONF='/etc/sds/connectors/mongodb' 11 | DIR_LOG='/var/log/sds/connectors/mongodb' 12 | 13 | 14 | # Add stratio group if it doesn't already exists 15 | if ! getent group $GROUP >/dev/null 2>&1; then 16 | groupadd ${GROUP} 17 | fi 18 | 19 | # Add stratio user if it doesn't already exists 20 | if ! getent passwd $USER >/dev/null 2>&1; then 21 | [ -d /opt ] || mkdir /opt 22 | useradd -M -d /opt/sds -g ${GROUP} -s /bin/bash ${USER} 23 | else 24 | usermod -G $GROUP $USER 25 | fi 26 | 27 | if [ ! -d "$DIR_LOG" ]; then 28 | mkdir $DIR_LOG 29 | fi 30 | 31 | chown root:root /etc/init.d/connector_mongodb 32 | 33 | chown -R ${USER}:${GROUP} $DIR_INSTALL 34 | 35 | chown -R ${USER}:${GROUP} $DIR_CONF 36 | chown -R ${USER}:${GROUP} $DIR_LOG 37 | chmod -R 775 $DIR_INSTALL 38 | chmod 775 $DIR_LOG 39 | 40 | chmod 755 /etc/init.d/connector_mongodb 41 | chmod 755 /opt/sds/connectors/mongodb/bin/stratio-connector-mongodb 42 | chmod +x /etc/default/mongodb_connector 43 | 44 | sed -i 's!"$BASEDIR"/conf!/etc/sds/connectors/mongodb!g' /opt/sds/connectors/mongodb/bin/stratio-connector-mongodb 45 | 46 | export MONGODB_CONNECTOR_LIB=/opt/sds/connectors/mongodb/ 47 | export MONGODB_CONNECTOR_CONF="/etc/sds/connectors/mongodb" 48 | 49 | /sbin/chkconfig --add connector_mongodb 50 | 51 | exit 0 52 | 53 | 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | *This project has been discontinued in favour of [Stratio Crossdata](https://github.com/Stratio/Crossdata), which is a data federation system leveraging Apache Spark capabilities.* 3 | 4 | # About # 5 | 6 | 7 | The Stratio Connector-MongoDB allows [Stratio Crossdata] () to interact with MongoDB. 8 | 9 | ## How to use Stratio Connector-MongoDB ## 10 | 11 | Requirements, installation and use of the connector can be found [here] () 12 | 13 | There also exists a [First Steps] () document where through an easy example the main features of this connector are illustrated. 14 | 15 | # License # 16 | 17 | Licensed to STRATIO (C) under one or more contributor license 18 | agreements. See the NOTICE file distributed with this work for 19 | additional information regarding copyright ownership. The STRATIO (C) 20 | licenses this file to you under the Apache License, Version 2.0 (the 21 | "License"); you may not use this file except in compliance with the 22 | License. You may obtain a copy of the License at 23 | 24 | http://www.apache.org/licenses/LICENSE-2.0 25 | 26 | Unless required by applicable law or agreed to in writing, software 27 | distributed under the License is distributed on an "AS IS" BASIS, 28 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 29 | See the License for the specific language governing permissions and 30 | limitations under the License. 31 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/scripts/postinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | #DEB 6 | 7 | GROUP='stratio' 8 | USER='stratio' 9 | DIR_INSTALL='/opt/sds/connectors/mongodb' 10 | DIR_CONF='/etc/sds/connectors/mongodb' 11 | DIR_CONF='/etc/sds/connectors/mongodb' 12 | DIR_LOG='/var/log/sds/connectors/mongodb' 13 | 14 | 15 | chmod 755 /etc/init.d/connector_mongodb 16 | chmod 755 /opt/sds/connectors/mongodb/bin 17 | chmod +x /opt/sds/connectors/mongodb/bin/stratio-connector-mongodb 18 | chmod +x /etc/default/mongodb_connector 19 | export MONGODB_CONNECTOR_LIB=/opt/sds/connectors/mongodb 20 | export MONGODB_CONNECTOR_CONF="/etc/sds/connectors/mongodb" 21 | 22 | 23 | case "$1" in 24 | configure) 25 | 26 | # Add stratio group if it doesn't already exists 27 | if ! getent group $GROUP >/dev/null 2>&1; then 28 | groupadd ${GROUP} 29 | fi 30 | 31 | # Add stratio user if it doesn't already exists 32 | if ! getent passwd $USER >/dev/null 2>&1; then 33 | [ -d /opt ] || mkdir /opt 34 | useradd -M -d /opt/sds -g ${GROUP} -s /bin/bash ${USER} 35 | else 36 | usermod -G $GROUP $USER 37 | fi 38 | 39 | if [ ! -d "$DIR_LOG" ]; then 40 | mkdir $DIR_LOG 41 | fi 42 | 43 | chown root:root /etc/init.d/connector_mongodb 44 | 45 | chown -R ${USER}:${GROUP} $DIR_INSTALL 46 | chown -R ${USER}:${GROUP} $DIR_CONF 47 | chown -R ${USER}:${GROUP} $DIR_LOG 48 | chmod -R 775 $DIR_INSTALL 49 | chmod 775 $DIR_LOG 50 | 51 | sed -i 's!"$BASEDIR"/conf!/etc/sds/connectors/mongodb!g' /opt/sds/connectors/mongodb/bin/stratio-connector-mongodb 52 | 53 | update-rc.d connector_mongodb defaults 54 | ;; 55 | esac 56 | 57 | 58 | exit 0 59 | 60 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/configuration/ShardKeyType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.configuration; 20 | 21 | /** 22 | * Types for the shard key. 23 | * 24 | */ 25 | 26 | public enum ShardKeyType { 27 | 28 | /** The hashed. */ 29 | HASHED("hashed"), 30 | /** The asc. */ 31 | ASC("asc"); 32 | 33 | /** The shard key type. */ 34 | private final String keyType; 35 | 36 | /** 37 | * Gets the shard key type. 38 | * 39 | * @return the key type 40 | */ 41 | public String getKeyType() { 42 | return keyType; 43 | } 44 | 45 | /** 46 | * Instantiates a new shard key type. 47 | * 48 | * @param keyType 49 | * the key type 50 | */ 51 | ShardKeyType(String keyType) { 52 | this.keyType = keyType; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/metadata/AlterOptionsUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.metadata; 19 | 20 | import org.junit.Assert; 21 | import org.junit.Test; 22 | 23 | import com.mongodb.BasicDBObject; 24 | 25 | public class AlterOptionsUtilsTest { 26 | 27 | @Test 28 | public void renameColumnTest() { 29 | BasicDBObject expected = new BasicDBObject("$rename", new BasicDBObject("old", "new")); 30 | Assert.assertEquals("The command is not the expected", expected, 31 | AlterOptionsUtils.buildRenameColumnDBObject("old", "new")); 32 | } 33 | 34 | @Test 35 | public void dropColumnTest() { 36 | BasicDBObject expected = new BasicDBObject("$unset", new BasicDBObject("column", "")); 37 | Assert.assertEquals("The command is not the expected", expected, 38 | AlterOptionsUtils.buildDropColumnDBObject("column")); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/exceptions/MongoDeleteException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.exceptions; 20 | 21 | import com.stratio.crossdata.common.exceptions.ExecutionException; 22 | 23 | /** 24 | * The Class MongoDeleteException. 25 | */ 26 | public class MongoDeleteException extends ExecutionException { 27 | 28 | /** The Constant serialVersionUID. */ 29 | static final long serialVersionUID = 4437784374390458324L; 30 | 31 | /** 32 | * Instantiates a new mongo delete exception. 33 | * 34 | * @param msg 35 | * the msg 36 | * @param cause 37 | * the cause 38 | */ 39 | public MongoDeleteException(String msg, Throwable cause) { 40 | super(msg, cause); 41 | } 42 | 43 | /** 44 | * Instantiates a new mongo delete exception. 45 | * 46 | * @param msg 47 | * the msg 48 | */ 49 | public MongoDeleteException(String msg) { 50 | super(msg); 51 | } 52 | } -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/exceptions/MongoInsertException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.exceptions; 20 | 21 | import com.stratio.crossdata.common.exceptions.ExecutionException; 22 | 23 | /** 24 | * The Class MongoInsertException. 25 | */ 26 | public class MongoInsertException extends ExecutionException { 27 | 28 | /** The Constant serialVersionUID. */ 29 | static final long serialVersionUID = 3007123571810028475L; 30 | 31 | /** 32 | * Instantiates a new mongo insert exception. 33 | * 34 | * @param msg 35 | * the msg 36 | * @param cause 37 | * the cause 38 | */ 39 | public MongoInsertException(String msg, Throwable cause) { 40 | super(msg, cause); 41 | } 42 | 43 | /** 44 | * Instantiates a new mongo insert exception. 45 | * 46 | * @param msg 47 | * the msg 48 | */ 49 | public MongoInsertException(String msg) { 50 | super(msg); 51 | } 52 | } -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/exceptions/MongoExecutionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.exceptions; 20 | 21 | import com.stratio.crossdata.common.exceptions.ExecutionException; 22 | 23 | /** 24 | * The Class MongoQueryException. 25 | */ 26 | public class MongoExecutionException extends ExecutionException { 27 | 28 | /** The Constant serialVersionUID. */ 29 | static final long serialVersionUID = 2782686873531397631L; 30 | 31 | /** 32 | * Instantiates a new mongo query exception. 33 | * 34 | * @param msg 35 | * the msg 36 | * @param cause 37 | * the cause 38 | */ 39 | public MongoExecutionException(String msg, Throwable cause) { 40 | super(msg, cause); 41 | } 42 | 43 | /** 44 | * Instantiates a new mongo query exception. 45 | * 46 | * @param msg 47 | * the msg 48 | */ 49 | public MongoExecutionException(String msg) { 50 | super(msg); 51 | } 52 | } -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/utils/LimitDBObjectBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.query.utils; 20 | 21 | import com.mongodb.BasicDBObject; 22 | import com.mongodb.DBObject; 23 | import com.stratio.crossdata.common.logicalplan.Limit; 24 | 25 | /** 26 | * The Class LimitDBObjectBuilder. 27 | */ 28 | public class LimitDBObjectBuilder extends DBObjectBuilder { 29 | 30 | /** The limit. */ 31 | private int limit; 32 | 33 | /** 34 | * Instantiates a new limit builder. 35 | * 36 | * @param limit 37 | * the limit 38 | */ 39 | public LimitDBObjectBuilder(Limit limit) { 40 | super(true); 41 | this.limit = limit.getLimit(); 42 | } 43 | 44 | /** 45 | * Builds the object. Insert a $limit if the aggregation framework is used. 46 | * 47 | * @return the DB object 48 | */ 49 | @Override 50 | public DBObject build() { 51 | return new BasicDBObject("$limit", limit); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/exceptions/MongoValidationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.exceptions; 20 | 21 | import com.stratio.crossdata.common.exceptions.ExecutionException; 22 | 23 | /** 24 | * The Class MongoValidationException. 25 | */ 26 | public class MongoValidationException extends ExecutionException { 27 | 28 | /** The Constant serialVersionUID. */ 29 | static final long serialVersionUID = -6751513453299437587L; 30 | 31 | /** 32 | * Instantiates a new mongo validation exception. 33 | * 34 | * @param msg 35 | * the msg 36 | * @param cause 37 | * the cause 38 | */ 39 | public MongoValidationException(String msg, Throwable cause) { 40 | super(msg, cause); 41 | } 42 | 43 | /** 44 | * Instantiates a new mongo validation exception. 45 | * 46 | * @param msg 47 | * the msg 48 | */ 49 | public MongoValidationException(String msg) { 50 | super(msg); 51 | } 52 | } -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/configuration/CustomMongoIndexType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.configuration; 20 | 21 | /** 22 | * The ConfigurationOptions. Set of custom index types allowed in Mongo. 23 | * 24 | */ 25 | 26 | public enum CustomMongoIndexType { 27 | 28 | /** The hashed. */ 29 | HASHED("hashed"), 30 | /** The compound. */ 31 | COMPOUND("compound"), 32 | /** The geospatial sphere. */ 33 | GEOSPATIAL_SPHERE("geo_sphere"), 34 | /** The geospatial flat. */ 35 | GEOSPATIAL_FLAT("geo_flat"); 36 | 37 | /** The index type. */ 38 | private final String indexType; 39 | 40 | /** 41 | * Gets the index type. 42 | * 43 | * @return the index type 44 | */ 45 | public String getIndexType() { 46 | return indexType; 47 | } 48 | 49 | /** 50 | * Instantiates a new custom Mongo index type. 51 | * 52 | * @param indexType 53 | * the index type 54 | */ 55 | CustomMongoIndexType(String indexType) { 56 | this.indexType = indexType; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/utils/DBObjectBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.query.utils; 20 | 21 | import com.mongodb.DBObject; 22 | 23 | /** 24 | * A generic DBObject builder to use in Mongo queries. 25 | */ 26 | public abstract class DBObjectBuilder { 27 | 28 | /** Whether the object is built for the aggregation framework or not. */ 29 | private final boolean useAggregation; 30 | 31 | /** 32 | * Instantiates a new DB object builder. 33 | * 34 | * @param useAggregation 35 | * whether the query use the aggregation framework or not 36 | */ 37 | public DBObjectBuilder(boolean useAggregation) { 38 | this.useAggregation = useAggregation; 39 | } 40 | 41 | /** 42 | * Checks if the object use the pipeline. 43 | * 44 | * @return true if the object use the aggregation framework 45 | */ 46 | public final boolean useAggregationPipeline() { 47 | return useAggregation; 48 | } 49 | 50 | /** 51 | * Builds the object. 52 | * 53 | * @return the DB object 54 | */ 55 | public abstract DBObject build(); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/configuration/NativeTypes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.configuration; 20 | 21 | /** 22 | * Native types supported in Mongo. 23 | * 24 | */ 25 | 26 | public enum NativeTypes { 27 | 28 | /** The date. */ 29 | DATE("date", java.util.Date.class); 30 | 31 | /** The db type. */ 32 | private final String dbType; 33 | 34 | /** The class type. */ 35 | private final Class classType; 36 | 37 | /** 38 | * Gets the db type. 39 | * 40 | * @return the db type 41 | */ 42 | public String getDbType() { 43 | return dbType; 44 | } 45 | 46 | /** 47 | * Gets the class type. 48 | * 49 | * @return the class type 50 | */ 51 | public Class getClassType() { 52 | return classType; 53 | } 54 | 55 | /** 56 | * Instantiates a new native types. 57 | * 58 | * @param dbType 59 | * the db type 60 | * @param classType 61 | * the class type 62 | */ 63 | NativeTypes(String dbType, Class classType) { 64 | this.dbType = dbType; 65 | this.classType = classType; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/metadata/AlterOptionsUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.metadata; 19 | 20 | import com.mongodb.BasicDBObject; 21 | import com.mongodb.DBObject; 22 | 23 | /** 24 | * Contains a set of utilities to run Alter Table commands. 25 | */ 26 | public final class AlterOptionsUtils { 27 | 28 | /** The Constant RENAME_COMMAND. */ 29 | public static final String RENAME_COMMAND = "$rename"; 30 | 31 | /** The Constant DROP_COMMAND. */ 32 | public static final String DROP_COMMAND = "$unset"; 33 | 34 | private AlterOptionsUtils() { 35 | } 36 | 37 | /** 38 | * Builds a rename column DBOject. 39 | * 40 | * @param oldName 41 | * the old name 42 | * @param newName 43 | * the new name 44 | * @return the DBObject 45 | */ 46 | public static DBObject buildRenameColumnDBObject(String oldName, String newName) { 47 | return new BasicDBObject(RENAME_COMMAND, new BasicDBObject(oldName, newName)); 48 | } 49 | 50 | /** 51 | * Builds a DBObject to drop a column. 52 | * 53 | * @param name 54 | * the column name 55 | * @return the DBObject 56 | */ 57 | public static DBObject buildDropColumnDBObject(String name) { 58 | return new BasicDBObject(DROP_COMMAND, new BasicDBObject(name, "")); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/template/MongoDBConnector: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: stratio-connector-mongodb 4 | # Required-Start: $local_fs $network $named $time $syslog 5 | # Required-Stop: $local_fs $network $named $time $syslog 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Description: mongo connector script 9 | ### END INIT INFO 10 | #export JAVA_HOME=/usr/java/default 11 | #export JAVA=$JAVA_HOME/bin/java 12 | RUNAS=root 13 | 14 | PIDPATH=/var/run/sds/ 15 | PIDNAME=stratio-connector-mongodb.pid 16 | PIDFILE=$PIDPATH$PIDNAME 17 | LOGPATH=/var/log/sds/connectors/mongodb/ 18 | LOGFILE=/var/log/sds/connectors/mongodb/stratio-connector-mongodb.log 19 | start() { 20 | 21 | if [ ! -d "$LOGPATH" ]; then 22 | mkdir $LOGPATH 23 | fi 24 | 25 | if [ ! -d "$PIDPATH" ]; then 26 | mkdir $PIDPATH 27 | fi 28 | 29 | if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then 30 | echo 'Service already running' >&2 31 | return 1 32 | fi 33 | echo 'Starting service…' >&2 34 | cd /opt/sds/connectors/mongodb/ 35 | 36 | nohup bin/stratio-connector-mongodb start > $LOGFILE & echo $! $RUNAS > $PIDFILE 37 | echo 'Service started' 38 | } 39 | 40 | stop() { 41 | # if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then 42 | # echo 'Service not running' >&2 43 | # return 1 44 | # fi 45 | # echo 'Stopping service…' >&2 46 | # kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE" 47 | # echo 'Service stopped' >&2 48 | cd /opt/sds/connectors/mongodb/ 49 | nohup bin/stratio-connector-mongodb stop > $LOGFILE 50 | echo 'Service stopped' 51 | } 52 | 53 | uninstall() { 54 | echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " 55 | local SURE 56 | read SURE 57 | if [ "$SURE" = "yes" ]; then 58 | stop 59 | rm -f "$PIDFILE" 60 | echo "Notice: log file is not be removed: '$LOGFILE'" >&2 61 | update-rc.d -f remove 62 | rm -fv "$0" 63 | fi 64 | } 65 | 66 | case "$1" in 67 | start) 68 | start 69 | ;; 70 | stop) 71 | stop 72 | ;; 73 | uninstall) 74 | uninstall 75 | ;; 76 | retart) 77 | stop 78 | start 79 | ;; 80 | *) 81 | echo "Usage: $0 start|stop|restart|uninstall" 82 | exit 1 83 | 84 | esac 85 | 86 | exit 0 87 | 88 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/configuration/IndexOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.configuration; 20 | 21 | import com.stratio.crossdata.common.statements.structures.SelectorType; 22 | 23 | /** 24 | * Set of index options. 25 | * 26 | */ 27 | 28 | public enum IndexOptions { 29 | 30 | /** The custom index type. */ 31 | INDEX_TYPE("index_type", SelectorType.STRING), 32 | /** The compound fields. */ 33 | COMPOUND_FIELDS("compound_fields", SelectorType.STRING), 34 | /** Wheter the index is sparse or not. */ 35 | SPARSE("sparse", SelectorType.BOOLEAN), 36 | /** Wheter the index is unique or not. */ 37 | UNIQUE("unique", SelectorType.BOOLEAN); 38 | 39 | /** The option name. */ 40 | private final String optionName; 41 | 42 | /** The selector type. */ 43 | private final SelectorType selectorType; 44 | 45 | /** 46 | * Gets the option name. 47 | * 48 | * @return the option name 49 | */ 50 | public String getOptionName() { 51 | return optionName; 52 | } 53 | 54 | /** 55 | * Gets the selector type. 56 | * 57 | * @return the selector type 58 | */ 59 | public SelectorType getSelectorType() { 60 | return selectorType; 61 | } 62 | 63 | /** 64 | * Instantiates a new index options. 65 | * 66 | * @param optionName 67 | * the option name 68 | * @param selectorType 69 | * the selector type 70 | */ 71 | IndexOptions(String optionName, SelectorType selectorType) { 72 | this.optionName = optionName; 73 | this.selectorType = selectorType; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/configuration/ConfigurationManager.java: -------------------------------------------------------------------------------- 1 | package com.stratio.connector.mongodb.core.configuration; 2 | 3 | import com.stratio.connector.commons.util.PropertyValueRecovered; 4 | import com.stratio.crossdata.common.connector.ConnectorClusterConfig; 5 | import com.stratio.crossdata.common.exceptions.ConnectionException; 6 | import com.stratio.crossdata.common.exceptions.ExecutionException; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | import java.util.Map; 11 | 12 | /** 13 | * This class must managed the configuration. 14 | * Created by jmgomez on 2/07/15. 15 | */ 16 | public class ConfigurationManager { 17 | 18 | 19 | /** 20 | * The cluster configuration. 21 | * 22 | */ 23 | private ConnectorClusterConfig configuration; 24 | 25 | /** 26 | * The Log. 27 | */ 28 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 29 | 30 | /** 31 | * Constructor. 32 | * * @param configuration the cluster configuration. 33 | */ 34 | public ConfigurationManager(ConnectorClusterConfig configuration) { 35 | this.configuration = configuration; 36 | } 37 | 38 | /** 39 | * Recovered the option value or the default value if the property is not set. 40 | * @param configurationOption the configuration option. 41 | * @return the configuration option value. 42 | * @throws ConnectionException if the property is not set. 43 | */ 44 | public String[] recoverConfigurationValue( ConfigurationOptions configurationOption) throws ConnectionException { 45 | String[] optionValue = {}; 46 | try { 47 | Map config = configuration.getClusterOptions(); 48 | if (config != null) { 49 | String strPorts = config.get(configurationOption.getOptionName()); 50 | if (strPorts != null) { 51 | optionValue = PropertyValueRecovered.recoveredValueASArray(String.class, strPorts); 52 | } else { 53 | optionValue = configurationOption.getDefaultValue(); 54 | } 55 | } else { 56 | optionValue = configurationOption.getDefaultValue(); 57 | } 58 | }catch(ExecutionException e){ 59 | String msg = String.format("Error recovering [%s] option ",configurationOption.getOptionName()); 60 | logger.error(msg); 61 | throw new ConnectionException(msg,e); 62 | } 63 | return optionValue; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/MongoLogicalWorkflowValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query; 19 | 20 | import com.stratio.connector.commons.engine.query.ProjectParsed; 21 | import com.stratio.connector.commons.engine.query.ProjectValidator; 22 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 23 | import com.stratio.crossdata.common.exceptions.ExecutionException; 24 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 25 | 26 | /** 27 | * Validates the logical workflow and stores the needed steps. 28 | */ 29 | public class MongoLogicalWorkflowValidator implements ProjectValidator { 30 | 31 | /** 32 | * This method validate the projectParsed. 33 | * 34 | * @param projectParsed 35 | * the projectParsed. 36 | * @throws UnsupportedException 37 | * if the specified operation is not supported 38 | * @throws ExecutionException 39 | * if the project is not validated. 40 | */ 41 | @Override 42 | public void validate(ProjectParsed projectParsed) throws MongoValidationException, UnsupportedException { 43 | 44 | if (projectParsed.getProject() == null) { 45 | throw new MongoValidationException("Projection has not been found in the logical workflow"); 46 | } 47 | if (projectParsed.getSelect() == null) { 48 | throw new MongoValidationException("Select has not been found in the logical workflow"); 49 | 50 | } 51 | if (!projectParsed.getMatchList().isEmpty()) { 52 | throw new UnsupportedException("Full-text queries not yet supported"); 53 | } 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/query/LogicalWorkflowExecutorFactoryTest.java: -------------------------------------------------------------------------------- 1 | package com.stratio.connector.mongodb.core.engine.query; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.powermock.core.classloader.annotations.PowerMockIgnore; 8 | import org.powermock.modules.junit4.PowerMockRunner; 9 | 10 | import com.stratio.connector.commons.engine.query.ProjectParsed; 11 | import com.stratio.connector.commons.test.util.LogicalWorkFlowCreator; 12 | import com.stratio.crossdata.common.data.ClusterName; 13 | import com.stratio.crossdata.common.logicalplan.LogicalWorkflow; 14 | import com.stratio.crossdata.common.logicalplan.Project; 15 | @PowerMockIgnore( {"javax.management.*"}) 16 | @RunWith(PowerMockRunner.class) 17 | public class LogicalWorkflowExecutorFactoryTest { 18 | 19 | public static final String COLUMN_1 = "column1"; 20 | private static final ClusterName CLUSTER_NAME = new ClusterName("clustername"); 21 | public static final String TABLE = "table_unit_test"; 22 | public static final String CATALOG = "catalog_unit_test"; 23 | 24 | /** 25 | * aggregationRequired() test 26 | * 27 | * @throws Exception 28 | */ 29 | @Test 30 | public void logicalWorkflowExecutorFactoryTest() throws Exception { 31 | 32 | LogicalWorkFlowCreator logWorkFlowCreator = new LogicalWorkFlowCreator(CATALOG, TABLE, CLUSTER_NAME); 33 | LogicalWorkflow logicalWorkflow = logWorkFlowCreator.addColumnName(COLUMN_1).build(); 34 | ProjectParsed logWorkflowData = new ProjectParsed((Project) logicalWorkflow.getInitialSteps().get(0), 35 | new MongoLogicalWorkflowValidator()); 36 | LogicalWorkflowExecutor lwExecutor = LogicalWorkflowExecutorFactory.getLogicalWorkflowExecutor(logWorkflowData); 37 | assertTrue("The aggregation should not be required without groupBy statements", 38 | lwExecutor instanceof BasicLogicalWorkflowExecutor); 39 | 40 | logWorkFlowCreator.addGroupBy(COLUMN_1); 41 | logicalWorkflow = logWorkFlowCreator.build(); 42 | logWorkflowData = new ProjectParsed((Project) logicalWorkflow.getInitialSteps().get(0), 43 | new MongoLogicalWorkflowValidator()); 44 | lwExecutor = LogicalWorkflowExecutorFactory.getLogicalWorkflowExecutor(logWorkflowData); 45 | assertTrue("The aggregation should be required with groupBy statements", 46 | lwExecutor instanceof AggregationLogicalWorkflowExecutor); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/connection/MongoConnectionHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.connection; 20 | 21 | import com.mongodb.MongoClient; 22 | import com.stratio.connector.commons.connection.Connection; 23 | import com.stratio.connector.commons.connection.ConnectionHandler; 24 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 25 | import com.stratio.connector.commons.TimerJ; 26 | import com.stratio.crossdata.common.connector.ConnectorClusterConfig; 27 | import com.stratio.crossdata.common.connector.IConfiguration; 28 | import com.stratio.crossdata.common.exceptions.ConnectionException; 29 | import com.stratio.crossdata.common.security.ICredentials; 30 | 31 | /** 32 | * MongoConnectionHandler. See {@link ConnectionHandler} 33 | */ 34 | public class MongoConnectionHandler extends ConnectionHandler { 35 | 36 | /** 37 | * @param configuration 38 | */ 39 | public MongoConnectionHandler(IConfiguration configuration) { 40 | super(configuration); 41 | } 42 | 43 | /* 44 | * (non-Javadoc) 45 | * 46 | * @see 47 | * com.stratio.connector.commons.connection.ConnectionHandler#createNativeConnection(com.stratio.meta.common.security 48 | * .ICredentials, com.stratio.meta.common.connector.ConnectorClusterConfig) 49 | */ 50 | @Override 51 | @TimerJ 52 | protected Connection createNativeConnection(ICredentials credentials, 53 | ConnectorClusterConfig connectorClusterConfig) throws ConnectionException { 54 | try { 55 | return new DriverConnection(credentials, connectorClusterConfig); 56 | } catch (MongoValidationException e) { 57 | throw new ConnectionException("MongoConnector validation error", e); 58 | } 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/metadata/SelectorOptionsUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.metadata; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | import java.util.Map.Entry; 24 | 25 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 26 | import com.stratio.crossdata.common.statements.structures.Selector; 27 | import com.stratio.crossdata.common.statements.structures.StringSelector; 28 | 29 | /** 30 | * The Class SelectorOptionsUtils. 31 | * 32 | */ 33 | public final class SelectorOptionsUtils { 34 | 35 | private SelectorOptionsUtils() { 36 | } 37 | 38 | /** 39 | * Process the options to return a map with string key. 40 | * 41 | * @param options 42 | * the options. the key has to be a StringSelector 43 | * @return the processed options 44 | * @throws MongoValidationException 45 | * if the options cannot be processed 46 | */ 47 | public static Map processOptions(Map options) throws MongoValidationException { 48 | Map stringOptions = null; 49 | if (options != null) { 50 | stringOptions = new HashMap(); 51 | Selector leftSelector; 52 | for (Entry e : options.entrySet()) { 53 | leftSelector = e.getKey(); 54 | if (leftSelector instanceof StringSelector) { 55 | stringOptions.put(((StringSelector) e.getKey()).getValue(), e.getValue()); 56 | } else { 57 | throw new MongoValidationException("The left term must be a string selector"); 58 | } 59 | } 60 | } 61 | return stringOptions; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/LogicalWorkflowExecutorFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query; 19 | 20 | import com.stratio.connector.commons.engine.query.ProjectParsed; 21 | import com.stratio.crossdata.common.exceptions.ExecutionException; 22 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 23 | 24 | /** 25 | * A factory for creating LogicalWorkflowExecutor objects. 26 | */ 27 | public final class LogicalWorkflowExecutorFactory { 28 | 29 | private LogicalWorkflowExecutorFactory() { 30 | 31 | } 32 | 33 | /** 34 | * Checks Whether the aggregation framework is required for logicalworkflow or not. 35 | * 36 | * @param logicalWorkfloParsed 37 | * the logical workflow parsed 38 | * @throws ExecutionException 39 | * if the execution fails or the query specified in the logical workflow is not supported. 40 | * @throws UnsupportedException 41 | * if the specified operation is not supported. 42 | * @return a BasicLogicalWorkflowExecutor if the aggregation is not required. Otherwise, a 43 | * AggregationLogicalWorkflowExecutor is returned. 44 | * 45 | */ 46 | public static LogicalWorkflowExecutor getLogicalWorkflowExecutor(ProjectParsed logicalWorkfloParsed) 47 | throws ExecutionException, UnsupportedException { 48 | LogicalWorkflowExecutor logicalWorkflowExecutor; 49 | 50 | if (logicalWorkfloParsed.getGroupBy() != null) { 51 | logicalWorkflowExecutor = new AggregationLogicalWorkflowExecutor(logicalWorkfloParsed); 52 | } else { 53 | logicalWorkflowExecutor = new BasicLogicalWorkflowExecutor(logicalWorkfloParsed); 54 | } 55 | return logicalWorkflowExecutor; 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/config/MongoDataStore.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | Mongo 22 | 0.3.0 23 | 24 | 25 | Hosts 26 | The list of hosts ips (csv). Example: host1,host2,host3 27 | 28 | 29 | Port 30 | The list of ports (csv). 31 | 32 | 33 | 34 | 35 | mongo.readPreference 36 | primary, primarypreferred(default), secondary, secondarypreferred or nearest 37 | 38 | 39 | mongo.writeConcern 40 | acknowledged(default), unacknowledged, replica_acknowledged or journaled 41 | 42 | 43 | mongo.threshold 44 | the acceptable threshold in milliseconds 45 | 46 | 47 | mongo.maxConnectionsPerHost 48 | the maximum number of connections allowed per host 49 | 50 | 51 | mongo.maxConnectionIdleTime 52 | The maximum idle time of a pooled connection(ms). A zero value indicates no limit 53 | 54 | 55 | mongo.connectTimeout 56 | the connection timeout(ms). A zero value indicates no timeout 57 | 58 | 59 | 60 | UPSERT_ON_INSERT 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/resources/MongoDataStore.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | Mongo 22 | 0.3.0 23 | 24 | 25 | Hosts 26 | The list of hosts ips (csv). Example: host1,host2,host3 27 | 28 | 29 | Port 30 | The list of ports (csv). 31 | 32 | 33 | 34 | 35 | mongo.readPreference 36 | primary, primarypreferred(default), secondary, secondarypreferred or nearest 37 | 38 | 39 | mongo.writeConcern 40 | acknowledged(default), unacknowledged, replica_acknowledged or journaled 41 | 42 | 43 | mongo.acceptableLatencyDifference 44 | the acceptable latency difference(ms) 45 | 46 | 47 | mongo.maxConnectionsPerHost 48 | the maximum number of connections allowed per host 49 | 50 | 51 | mongo.maxConnectionIdleTime 52 | The maximum idle time of a pooled connection(ms). A zero value indicates no limit 53 | 54 | 55 | mongo.connectTimeout 56 | the connection timeout(ms). A zero value indicates no timeout 57 | 58 | 59 | 60 | UPSERT_ON_INSERT 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/configuration/TableOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.configuration; 20 | 21 | import com.stratio.crossdata.common.statements.structures.SelectorType; 22 | 23 | /** 24 | * Set of table options for the mongo connector. A default value is provided. 25 | * 26 | */ 27 | 28 | public enum TableOptions { 29 | 30 | /** The sharding enabled. */ 31 | SHARDING_ENABLED("enable_sharding", SelectorType.BOOLEAN, false), 32 | /** The shard key type. */ 33 | SHARD_KEY_TYPE("shard_key_type", SelectorType.STRING, ShardKeyType.ASC), 34 | /** The shard key fields. */ 35 | SHARD_KEY_FIELDS("shard_key_fields", SelectorType.STRING, new String[] { "_id" }); 36 | 37 | /** The option name. */ 38 | private final String optionName; 39 | 40 | /** The selector type. */ 41 | private final SelectorType selectorType; 42 | 43 | /** The default value. */ 44 | private final Object defaultValue; 45 | 46 | /** 47 | * Gets the default value. 48 | * 49 | * @return the default value 50 | */ 51 | public Object getDefaultValue() { 52 | return defaultValue; 53 | } 54 | 55 | /** 56 | * Gets the option name. 57 | * 58 | * @return the option name 59 | */ 60 | public String getOptionName() { 61 | return optionName; 62 | } 63 | 64 | /** 65 | * Gets the selector type. 66 | * 67 | * @return the selector type 68 | */ 69 | public SelectorType getSelectorType() { 70 | return selectorType; 71 | } 72 | 73 | /** 74 | * Instantiates a new table options. 75 | * 76 | * @param optionName 77 | * the option name 78 | * @param selectorType 79 | * the selector type 80 | * @param defaultValue 81 | * the default value 82 | */ 83 | TableOptions(String optionName, SelectorType selectorType, Object defaultValue) { 84 | this.optionName = optionName; 85 | this.selectorType = selectorType; 86 | this.defaultValue = defaultValue; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/configuration/ConfigurationManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.stratio.connector.mongodb.core.configuration; 2 | 3 | import com.stratio.crossdata.common.connector.ConnectorClusterConfig; 4 | import com.stratio.crossdata.common.data.ClusterName; 5 | import com.stratio.crossdata.common.exceptions.ConnectionException; 6 | import org.junit.Test; 7 | 8 | import java.util.Collections; 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | import static org.junit.Assert.*; 13 | 14 | /** 15 | * Created by jmgomez on 2/07/15. 16 | */ 17 | public class ConfigurationManagerTest { 18 | 19 | 20 | public static final String HOST_VALUE = "A_HOST"; 21 | 22 | @Test 23 | public void testRecoverConfigurationValueDefault() throws ConnectionException { 24 | ConnectorClusterConfig connectorClusterConfig = new ConnectorClusterConfig(new ClusterName("CLUSTER_NAME"), Collections.EMPTY_MAP,Collections.EMPTY_MAP); 25 | 26 | ConfigurationManager configurationManager = new ConfigurationManager(connectorClusterConfig); 27 | String[] value = configurationManager.recoverConfigurationValue(ConfigurationOptions.HOST); 28 | 29 | assertEquals("The value must be one",1,value.length); 30 | assertEquals("The value must be the default value",ConfigurationOptions.HOST.getDefaultValue()[0],value[0]); 31 | } 32 | 33 | 34 | 35 | @Test 36 | public void testRecoverConfigurationValueSet() throws ConnectionException { 37 | Map clusterOption = new HashMap<>(); 38 | clusterOption.put(ConfigurationOptions.HOST.getOptionName(), HOST_VALUE); 39 | ConnectorClusterConfig connectorClusterConfig = new ConnectorClusterConfig(new ClusterName("CLUSTER_NAME"), Collections.EMPTY_MAP,clusterOption); 40 | 41 | ConfigurationManager configurationManager = new ConfigurationManager(connectorClusterConfig); 42 | String[] value = configurationManager.recoverConfigurationValue(ConfigurationOptions.HOST); 43 | 44 | assertEquals("The value must be one",1,value.length); 45 | assertEquals("The value must be the set value",HOST_VALUE,value[0]); 46 | } 47 | 48 | 49 | @Test 50 | public void testRecoverConfigurationValueDefaultConfigSet() throws ConnectionException { 51 | Map clusterOption = new HashMap<>(); 52 | clusterOption.put(ConfigurationOptions.HOST.getOptionName(), HOST_VALUE); 53 | ConnectorClusterConfig connectorClusterConfig = new ConnectorClusterConfig(new ClusterName("CLUSTER_NAME"), Collections.EMPTY_MAP,clusterOption); 54 | 55 | ConfigurationManager configurationManager = new ConfigurationManager(connectorClusterConfig); 56 | String[] value = configurationManager.recoverConfigurationValue(ConfigurationOptions.PORT); 57 | 58 | assertEquals("The value must be one",1,value.length); 59 | assertEquals("The value must be the default value",ConfigurationOptions.PORT.getDefaultValue()[0],value[0]); 60 | } 61 | 62 | } 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/utils/OrderByDBObjectBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query.utils; 19 | 20 | import com.mongodb.BasicDBObject; 21 | import com.mongodb.DBObject; 22 | import com.stratio.connector.commons.util.SelectorHelper; 23 | import com.stratio.crossdata.common.exceptions.ExecutionException; 24 | import com.stratio.crossdata.common.logicalplan.OrderBy; 25 | import com.stratio.crossdata.common.statements.structures.OrderByClause; 26 | import com.stratio.crossdata.common.statements.structures.OrderDirection; 27 | import com.stratio.crossdata.common.statements.structures.SelectorType; 28 | 29 | /** 30 | * The Class OrderByDBObjectBuilder. Allows build a "$sort" MongoDB query. 31 | */ 32 | public class OrderByDBObjectBuilder extends DBObjectBuilder { 33 | 34 | /** The orderBy query. */ 35 | private BasicDBObject orderByQuery; 36 | 37 | /** 38 | * Instantiates a new order by DBObject builder. 39 | * 40 | * @param useAggregation 41 | * whether the query use the aggregation framework or not 42 | * @param orderBy 43 | * the orderBy conditions 44 | * @throws ExecutionException 45 | * if the conditions specified in the logical workflow are not supported 46 | */ 47 | public OrderByDBObjectBuilder(boolean useAggregation, OrderBy orderBy) throws ExecutionException { 48 | super(useAggregation); 49 | orderByQuery = new BasicDBObject(); 50 | 51 | for (OrderByClause orderClause : orderBy.getIds()) { 52 | add(orderClause); 53 | } 54 | 55 | } 56 | 57 | private void add(OrderByClause orderClause) throws ExecutionException { 58 | String column = (String) SelectorHelper.getRestrictedValue(orderClause.getSelector(), SelectorType.COLUMN); 59 | int sortType = (orderClause.getDirection() == OrderDirection.ASC) ? 1 : -1; 60 | orderByQuery.put(column, sortType); 61 | } 62 | 63 | /** 64 | * Builds the DBObject. Insert a $sort if the aggregation framework is used. 65 | * 66 | * @return the DB object 67 | */ 68 | public DBObject build() { 69 | 70 | DBObject container; 71 | if (useAggregationPipeline()) { 72 | container = new BasicDBObject(); 73 | container.put("$sort", orderByQuery); 74 | } else { 75 | container = orderByQuery; 76 | } 77 | 78 | return container; 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/metadata/SelectorOptionUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.metadata; 20 | 21 | import static org.junit.Assert.assertEquals; 22 | import static org.junit.Assert.assertTrue; 23 | 24 | import java.util.Map; 25 | 26 | import org.junit.Test; 27 | 28 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 29 | import com.stratio.crossdata.common.statements.structures.BooleanSelector; 30 | import com.stratio.crossdata.common.statements.structures.IntegerSelector; 31 | import com.stratio.crossdata.common.statements.structures.Selector; 32 | import com.stratio.crossdata.common.statements.structures.StringSelector; 33 | import org.powermock.core.classloader.annotations.PowerMockIgnore; 34 | 35 | /** 36 | * @author david 37 | */ 38 | @PowerMockIgnore( {"javax.management.*"}) 39 | public class SelectorOptionUtilsTest { 40 | 41 | private static final String OPTION_NAME = "option"; 42 | private static final String OPTION_NAME2 = "option2"; 43 | private static final Integer INT_VALUE = 5; 44 | private static final Boolean BOOLEAN_VALUE = true; 45 | 46 | @Test 47 | public void processOptionsTest() throws MongoValidationException { 48 | 49 | Map options = new java.util.HashMap(); 50 | options.put(new StringSelector(OPTION_NAME), new IntegerSelector(INT_VALUE)); 51 | options.put(new StringSelector(OPTION_NAME2), new BooleanSelector(BOOLEAN_VALUE)); 52 | 53 | Map processedOptions = SelectorOptionsUtils.processOptions(options); 54 | 55 | assertTrue("The options must contain " + OPTION_NAME, processedOptions.containsKey(OPTION_NAME)); 56 | assertTrue("The options must contain " + OPTION_NAME2, processedOptions.containsKey(OPTION_NAME2)); 57 | assertEquals("The retrieved integer option is not the expected", INT_VALUE.longValue(), 58 | ((IntegerSelector) processedOptions.get(OPTION_NAME)).getValue()); 59 | assertEquals("The retrieved boolean option is not the expected", BOOLEAN_VALUE, 60 | ((BooleanSelector) processedOptions.get(OPTION_NAME2)).getValue()); 61 | 62 | } 63 | 64 | @Test(expected = MongoValidationException.class) 65 | public void processMalformedOptionsTest() throws MongoValidationException { 66 | 67 | Map options = new java.util.HashMap(); 68 | options.put(new IntegerSelector(INT_VALUE), new IntegerSelector(INT_VALUE)); 69 | 70 | SelectorOptionsUtils.processOptions(options); 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 16 | 4.0.0 17 | com.stratio.connector 18 | stratio-connector-mongodb 19 | 0.5.5-RC1-SNAPSHOT 20 | Stratio MongoDB-Crossdata connector parent 21 | http://www.stratio.com 22 | Stratio MongoDB Connector 23 | pom 24 | 25 | com.stratio 26 | parent 27 | 0.5.0 28 | 29 | 30 | 0.6.4 31 | 2.13.2 32 | UTF-8 33 | UTF-8 34 | 7191 35 | 36 | 37 | 38 | Apache License, Version 2.0 39 | http://www.apache.org/licenses/LICENSE-2.0t 40 | repo 41 | 42 | 43 | 44 | scm:git:git@github.com:Stratio/stratio-connector-mongodb.git 45 | scm:git:git@github.com:Stratio/stratio-connector-mongodb.git 46 | https://github.com/Stratio/stratio-connector-mongodb 47 | 48 | 49 | doc 50 | connector-mongodb 51 | 52 | 53 | 54 | darroyo 55 | David Arroyo Cazorla 56 | darroyo@stratio.com 57 | 58 | architect 59 | developer 60 | maintainer 61 | 62 | 63 | 64 | ccaballe 65 | Cristian Caballero Montiel 66 | ccaballero@stratio.com 67 | 68 | architect 69 | developer 70 | maintainer 71 | 72 | 73 | 74 | 75 | 76 | 77 | org.apache.maven.plugins 78 | maven-compiler-plugin 79 | 3.1 80 | 81 | 1.7 82 | 1.7 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/utils/GroupByDBObjectBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.query.utils; 20 | 21 | import java.util.List; 22 | import java.util.Set; 23 | 24 | import com.mongodb.BasicDBObject; 25 | import com.mongodb.DBObject; 26 | import com.stratio.connector.commons.util.SelectorHelper; 27 | import com.stratio.crossdata.common.exceptions.ExecutionException; 28 | import com.stratio.crossdata.common.logicalplan.GroupBy; 29 | import com.stratio.crossdata.common.statements.structures.Selector; 30 | import com.stratio.crossdata.common.statements.structures.SelectorType; 31 | 32 | /** 33 | * The Class GroupByDBObjectBuilder. Allows build a "group by" MongoDB query used in the aggregation framework. 34 | */ 35 | public class GroupByDBObjectBuilder extends DBObjectBuilder { 36 | 37 | /** The groupBy query. */ 38 | private BasicDBObject groupQuery; 39 | 40 | /** 41 | * Instantiates a new group by from the logical plan groupBy and the fields selected. 42 | * 43 | * @param groupBy 44 | * the groupBy conditions 45 | * @param selectColumns 46 | * the columns expected in the result 47 | * @throws ExecutionException 48 | * if the conditions specified in the logical workflow are not supported 49 | */ 50 | public GroupByDBObjectBuilder(GroupBy groupBy, Set selectColumns) throws ExecutionException { 51 | super(true); 52 | groupQuery = new BasicDBObject(); 53 | 54 | String identifier; 55 | List ids = groupBy.getIds(); 56 | 57 | if (ids.size() == 1) { 58 | identifier = (String) SelectorHelper.getRestrictedValue(ids.get(0), SelectorType.COLUMN); 59 | groupQuery.put("_id", "$" + identifier); 60 | } else if (ids.size() > 1) { 61 | BasicDBObject groupFields = new BasicDBObject(); 62 | for (Selector selector : ids) { 63 | identifier = (String) SelectorHelper.getRestrictedValue(selector, SelectorType.COLUMN); 64 | groupFields.put(identifier, "$" + identifier); 65 | } 66 | groupQuery.put("_id", groupFields); 67 | } 68 | for (Selector colSelector : selectColumns) { 69 | String columnName = (String) SelectorHelper.getRestrictedValue(colSelector, SelectorType.COLUMN); 70 | groupQuery.put(columnName, new BasicDBObject("$first", "$" + columnName)); 71 | } 72 | 73 | } 74 | 75 | /** 76 | * Builds the DBObject. Insert a $group if the aggregation framework is used. 77 | * 78 | * @return the DB object 79 | */ 80 | public DBObject build() { 81 | return new BasicDBObject("$group", groupQuery); 82 | } 83 | } -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/configuration/ConfigurationOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.configuration; 20 | 21 | import com.stratio.connector.commons.util.PropertyValueRecovered; 22 | import com.stratio.crossdata.common.connector.ConnectorClusterConfig; 23 | import com.stratio.crossdata.common.exceptions.ConnectionException; 24 | import com.stratio.crossdata.common.exceptions.ExecutionException; 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | 28 | import java.util.Map; 29 | 30 | /** 31 | * The ConfigurationOptions. They could be set in the ConnectorClusterConfiguration Set of options for the Mongo 32 | * Connector. A default value is provided. 33 | * 34 | */ 35 | 36 | public enum ConfigurationOptions { 37 | 38 | /** The acceptable latency. */ 39 | LOCAL_THREADSHOLD("mongo.threshold", "15"), 40 | /** The max connections per host. */ 41 | MAX_CONNECTIONS_PER_HOST("mongo.maxConnectionsPerHost", "10000"), 42 | /** The max idle time. */ 43 | MAX_IDLE_TIME("mongo.maxConnectionIdleTime", "0"), 44 | /** The connection timeout. */ 45 | CONNECTION_TIMEOUT("mongo.connectTimeout", "10000"), 46 | /** The read preference. */ 47 | READ_PREFERENCE("mongo.readPreference", "primaryPreferred"), 48 | /** The write concern. */ 49 | WRITE_CONCERN("mongo.writeConcern", "acknowledged"), 50 | /** The host. */ 51 | HOST("Hosts", new String[] { "localhost" }), 52 | /** The port. */ 53 | PORT("Port", new String[] { "27017" }), 54 | /** Number of samples to deduce fields. */ 55 | SAMPLE_PROBABILITY("sample_probability", "1"), 56 | /** A default limit for queries */ 57 | DEFAULT_LIMIT("default_limit","100"); 58 | 59 | 60 | /** The option name. */ 61 | private final String optionName; 62 | 63 | /** The default value. */ 64 | private final String[] defaultValue; 65 | 66 | /** 67 | * Gets the default value. 68 | * 69 | * @return the default value 70 | */ 71 | public String[] getDefaultValue() { 72 | return defaultValue.clone(); 73 | } 74 | 75 | /** 76 | * Gets the option name. 77 | * 78 | * @return the option name 79 | */ 80 | public String getOptionName() { 81 | return optionName; 82 | } 83 | 84 | /** 85 | * Instantiates a new configuration options. 86 | * 87 | * @param optionName 88 | * the option name 89 | * @param defaultValue 90 | * the default value 91 | */ 92 | ConfigurationOptions(String optionName, String... defaultValue) { 93 | this.optionName = optionName; 94 | this.defaultValue = defaultValue; 95 | } 96 | 97 | 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /doc/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 18 | 4.0.0 19 | com.stratio.connector 20 | stratio-connector-mongodb-doc 21 | Stratio MongoDB-Crossdata connector documentation 22 | http://www.stratio.com 23 | Stratio MongoDB Connector Documentation 24 | pom 25 | 26 | com.stratio.connector 27 | stratio-connector-mongodb 28 | 0.5.5-RC1-SNAPSHOT 29 | 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-site-plugin 35 | 3.4 36 | 37 | 38 | 39 | org.apache.maven.plugins 40 | maven-project-info-reports-plugin 41 | 2.8 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | com.stratio.maven 50 | sphinx-maven-plugin 51 | 2.0.0 52 | 53 | 54 | html 55 | 56 | 57 | 58 | src/site/sphinx 59 | true 60 | 61 | conf.py 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /doc/src/site/sphinx/about.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | The Stratio Connector-MongoDB allows `Stratio Crossdata `_ to interact with MongoDB. 5 | 6 | Requirements 7 | ------------ 8 | 9 | `MongoDB `_ 3.0 version or later. 10 | `Crossdata `__ is needed to interact with this connector. 11 | rpm is needed to generate executable connector. You can install it as follow: 12 | :: 13 | 14 | > sudo apt-get install rpm 15 | 16 | Compiling an building an executable Stratio Connector-MongoDB 17 | ------------------------------------------------------------- 18 | To automatically build execute the following command: 19 | 20 | :: 21 | 22 | > mvn clean install 23 | 24 | 25 | Running the Stratio Connector-MongoDB 26 | ------------------------------------- 27 | 28 | Run the executable: 29 | 30 | :: 31 | 32 | > ./connector-mongodb/target/stratio-connector-mongodb/bin/stratio-connector-mongodb 33 | :: 34 | 35 | Build a redistributable package 36 | ------------------------------- 37 | 38 | It is possible too, to create a RPM or DEB package, as : 39 | 40 | :: 41 | 42 | > mvn package -Ppackage 43 | 44 | 45 | Once the package is created, execute this commands to install: 46 | 47 | RPM Package: 48 | 49 | :: 50 | 51 | > rpm -i target/stratio-connector-mongodb-.rpm 52 | 53 | 54 | 55 | DEB Package: 56 | 57 | :: 58 | 59 | > dpkg -i target/stratio-connector-mongodb-.deb 60 | 61 | 62 | 63 | Now, to start/stop the connector: 64 | 65 | :: 66 | 67 | > service connector-mongodb start 68 | > service connector-mongodb stop 69 | 70 | 71 | How to use Stratio Connector-MongoDB 72 | ------------------------------------ 73 | 74 | A complete tutorial is available `here `__. The basic commands are described below. 76 | 77 | 1. Start `Stratio Crossdata Server and then Stratio Crossdata Shell `__. 78 | 79 | 2. Start Stratio Connector-MongoDB as it is explained before. 80 | 81 | 3. In the Stratio Crossdata Shell: 82 | 83 | Attach a cluster on the datastore. The datastore name must be the same 84 | as the defined in the Datastore manifest. 85 | 86 | :: 87 | 88 | xdsh:user> ATTACH CLUSTER ON DATASTORE WITH OPTIONS {'Hosts': '[]', 'Port': '[]'}; 89 | 90 | 91 | Attach the connector to the previously defined cluster. The connector name must match the one defined in the 92 | Connector Manifest. 93 | 94 | :: 95 | 96 | xdsh:user> ATTACH CONNECTOR TO WITH OPTIONS {}; 97 | 98 | At this point, we can start to send queries in the Stratio Crossdata Shell. 99 | 100 | License 101 | ======= 102 | 103 | Licensed to STRATIO (C) under one or more contributor license agreements. 104 | See the NOTICE file distributed with this work for additional information 105 | regarding copyright ownership. The STRATIO (C) licenses this file 106 | to you under the Apache License, Version 2.0 (the 107 | "License"); you may not use this file except in compliance 108 | with the License. You may obtain a copy of the License at 109 | 110 | http://www.apache.org/licenses/LICENSE-2.0 111 | 112 | Unless required by applicable law or agreed to in writing, 113 | software distributed under the License is distributed on an 114 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 115 | KIND, either express or implied. See the License for the 116 | specific language governing permissions and limitations 117 | under the License. 118 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/MongoConnectorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core; 20 | 21 | import static org.mockito.Mockito.mock; 22 | import static org.mockito.Mockito.times; 23 | import static org.mockito.Mockito.verify; 24 | 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | 28 | import org.junit.Assert; 29 | import org.junit.Test; 30 | import org.junit.runner.RunWith; 31 | import org.mockito.internal.util.reflection.Whitebox; 32 | import org.powermock.core.classloader.annotations.PowerMockIgnore; 33 | import org.powermock.core.classloader.annotations.PrepareForTest; 34 | import org.powermock.modules.junit4.PowerMockRunner; 35 | 36 | import com.stratio.connector.mongodb.core.connection.DriverConnection; 37 | import com.stratio.connector.mongodb.core.connection.MongoConnectionHandler; 38 | import com.stratio.crossdata.common.connector.ConnectorClusterConfig; 39 | import com.stratio.crossdata.common.data.ClusterName; 40 | import com.stratio.crossdata.common.exceptions.InitializationException; 41 | import com.stratio.crossdata.common.security.ICredentials; 42 | 43 | @PowerMockIgnore( {"javax.management.*"}) 44 | @RunWith(PowerMockRunner.class) 45 | @PrepareForTest(value = { DriverConnection.class, MongoConnector.class }) 46 | public class MongoConnectorTest { 47 | private static final String CLUSTER_NAME = "CLUSTER_NAME"; 48 | private static final String CLUSTER_NAME_OTHER = "OTHER_CLUSTER_NAME"; 49 | 50 | @Test 51 | public void connectTest() throws Exception { 52 | MongoConnector mongoConnector = new MongoConnector(); 53 | ICredentials iCredentials = mock(ICredentials.class); 54 | ClusterName clusterName = new ClusterName(CLUSTER_NAME); 55 | Map options = new HashMap<>(); 56 | ConnectorClusterConfig config = new ConnectorClusterConfig(clusterName, null, options); 57 | MongoConnectionHandler connectionHandler = mock(MongoConnectionHandler.class); 58 | Whitebox.setInternalState(mongoConnector, "connectionHandler", connectionHandler); 59 | mongoConnector.connect(iCredentials, config); 60 | verify(connectionHandler, times(1)).createConnection(iCredentials, config); 61 | } 62 | 63 | @Test 64 | public void multipleConnectTest() throws Exception { 65 | MongoConnector mongoConnector = new MongoConnector(); 66 | ICredentials iCredentials = mock(ICredentials.class); 67 | ClusterName clusterName = new ClusterName(CLUSTER_NAME); 68 | ClusterName clusterNameOther = new ClusterName(CLUSTER_NAME_OTHER); 69 | Map options = new HashMap<>(); 70 | ConnectorClusterConfig configCluster1 = new ConnectorClusterConfig(clusterName, null, options); 71 | ConnectorClusterConfig configCluster2 = new ConnectorClusterConfig(clusterNameOther, null, options); 72 | MongoConnectionHandler connectionHandler = mock(MongoConnectionHandler.class); 73 | Whitebox.setInternalState(mongoConnector, "connectionHandler", connectionHandler); 74 | mongoConnector.connect(iCredentials, configCluster1); 75 | mongoConnector.connect(iCredentials, configCluster2); 76 | 77 | verify(connectionHandler, times(1)).createConnection(iCredentials, configCluster1); 78 | verify(connectionHandler, times(1)).createConnection(iCredentials, configCluster2); 79 | } 80 | 81 | 82 | 83 | 84 | } -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/utils/ProjectDBObjectBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.query.utils; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | import java.util.Set; 24 | 25 | import com.mongodb.BasicDBObject; 26 | import com.mongodb.DBObject; 27 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 28 | import com.stratio.crossdata.common.data.ColumnName; 29 | import com.stratio.crossdata.common.exceptions.ExecutionException; 30 | import com.stratio.crossdata.common.logicalplan.Project; 31 | import com.stratio.crossdata.common.logicalplan.Select; 32 | import com.stratio.crossdata.common.statements.structures.ColumnSelector; 33 | import com.stratio.crossdata.common.statements.structures.Selector; 34 | 35 | /** 36 | * The Class ProjectDBObjectBuilder. 37 | */ 38 | public class ProjectDBObjectBuilder extends DBObjectBuilder { 39 | 40 | /** The fields projected in Mongo. */ 41 | private DBObject projectQuery; 42 | 43 | /** 44 | * Instantiates a new project builder. 45 | * 46 | * @param useAggregation 47 | * whether the project use the aggregation framework or not 48 | * @param project 49 | * the project 50 | * @param select 51 | * the select 52 | * @throws ExecutionException 53 | * if the select specified in the logical workflow is not supported 54 | */ 55 | public ProjectDBObjectBuilder(boolean useAggregation, Project project, Select select) throws ExecutionException { 56 | super(useAggregation); 57 | 58 | projectQuery = new BasicDBObject(); 59 | List columnNameList; 60 | 61 | if (!useAggregation) { 62 | Set columnSelectorList = select.getColumnMap().keySet(); 63 | columnNameList = new ArrayList(); 64 | for (Selector selector : columnSelectorList) { 65 | columnNameList.add(((ColumnSelector) selector).getColumnName()); 66 | } 67 | 68 | } else { 69 | columnNameList = project.getColumnList(); 70 | } 71 | 72 | if (columnNameList == null || columnNameList.isEmpty()) { 73 | throw new MongoValidationException("The query has to request at least one field"); 74 | } else { 75 | boolean includeId = false; 76 | for (ColumnName columnName : columnNameList) { 77 | projectQuery.put(columnName.getName(), 1); 78 | if (columnName.getName().equals("_id")){ 79 | includeId = true; 80 | } 81 | } 82 | if (!includeId){ 83 | projectQuery.put("_id", 0); 84 | } 85 | } 86 | 87 | } 88 | 89 | /** 90 | * Builds the object. Insert a $project if the aggregation framework is used. 91 | * 92 | * @return the DB object 93 | */ 94 | @Override 95 | public DBObject build() { 96 | DBObject projectDBObject; 97 | 98 | if (useAggregationPipeline()) { 99 | projectDBObject = new BasicDBObject("$project", projectQuery); 100 | } else { 101 | projectDBObject = projectQuery; 102 | } 103 | 104 | return projectDBObject; 105 | } 106 | } -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/metadata/StorageUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.metadata; 20 | 21 | import java.util.List; 22 | 23 | import com.mongodb.BasicDBObject; 24 | import com.mongodb.DBObject; 25 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 26 | import com.stratio.crossdata.common.data.ColumnName; 27 | import com.stratio.crossdata.common.data.Row; 28 | import com.stratio.crossdata.common.metadata.ColumnType; 29 | import com.stratio.crossdata.common.metadata.TableMetadata; 30 | 31 | /** 32 | * The Class StorageUtils. 33 | * 34 | */ 35 | public final class StorageUtils { 36 | 37 | /** 38 | * Instantiates a new storage utils. 39 | */ 40 | private StorageUtils() { 41 | } 42 | 43 | /** 44 | * Builds the primaryKey. Only the value will be stored when receiving a single primaryKey. Otherwise, either the 45 | * name or the value will be stored. 46 | * 47 | * @param tableMetadata 48 | * the table metadata 49 | * @param row 50 | * the row 51 | * @return the primary key value 52 | * @throws MongoValidationException 53 | * if the specified primary key is not valid 54 | */ 55 | public static Object buildPK(TableMetadata tableMetadata, Row row) throws MongoValidationException { 56 | 57 | Object pk = null; 58 | DBObject bsonPK = null; 59 | Object cellValue; 60 | 61 | List primaryKeyList = tableMetadata.getPrimaryKey(); 62 | 63 | // Building the pk to insert in Mongo 64 | if (!primaryKeyList.isEmpty()) { 65 | if (primaryKeyList.size() == 1) { 66 | cellValue = row.getCell(primaryKeyList.get(0).getName()).getValue(); 67 | validatePKDataType(tableMetadata.getColumns().get(primaryKeyList.get(0)).getColumnType()); 68 | pk = cellValue; 69 | } else { 70 | 71 | bsonPK = new BasicDBObject(); 72 | for (ColumnName columnName : primaryKeyList) { 73 | cellValue = row.getCell(columnName.getName()).getValue(); 74 | validatePKDataType(tableMetadata.getColumns().get(columnName).getColumnType()); 75 | bsonPK.put(columnName.getName(), cellValue); 76 | } 77 | pk = bsonPK; 78 | } 79 | } 80 | 81 | return pk; 82 | } 83 | 84 | /** 85 | * Checks if the column type is supported in primary keys. 86 | * 87 | * @param columnType 88 | * the columnType type 89 | * @throws MongoValidationException 90 | * if the type is not supported 91 | */ 92 | private static void validatePKDataType(ColumnType columnType) throws MongoValidationException { 93 | 94 | switch (columnType.getDataType()) { 95 | case BIGINT: 96 | case INT: 97 | case TEXT: 98 | case VARCHAR: 99 | case DOUBLE: 100 | case FLOAT: 101 | break; 102 | case BOOLEAN: 103 | case SET: 104 | case LIST: 105 | case MAP: 106 | case NATIVE: 107 | default: 108 | throw new MongoValidationException("Type not supported as PK: " + columnType.toString()); 109 | 110 | } 111 | 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/resources/MongoConnector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | MongoConnector 22 | 23 | Mongo 24 | 25 | 0.3.0 26 | 27 | DROP_CATALOG 28 | CREATE_TABLE 29 | ALTER_TABLE 30 | DROP_TABLE 31 | UPDATE_NO_FILTERS 32 | UPDATE_PK_EQ 33 | UPDATE_PK_GT 34 | UPDATE_PK_LT 35 | UPDATE_PK_GET 36 | UPDATE_PK_LET 37 | UPDATE_NON_INDEXED_EQ 38 | UPDATE_NON_INDEXED_GT 39 | UPDATE_NON_INDEXED_LT 40 | UPDATE_NON_INDEXED_GET 41 | UPDATE_NON_INDEXED_LET 42 | UPDATE_INDEXED_EQ 43 | UPDATE_INDEXED_GT 44 | UPDATE_INDEXED_LT 45 | UPDATE_INDEXED_GET 46 | UPDATE_INDEXED_LET 47 | TRUNCATE_TABLE 48 | INSERT 49 | INSERT_IF_NOT_EXISTS 50 | DELETE_NO_FILTERS 51 | DELETE_PK_EQ 52 | DELETE_PK_GT 53 | DELETE_PK_LT 54 | DELETE_PK_GET 55 | DELETE_PK_LET 56 | DELETE_NON_INDEXED_EQ 57 | DELETE_NON_INDEXED_GT 58 | DELETE_NON_INDEXED_LT 59 | DELETE_NON_INDEXED_GET 60 | DELETE_NON_INDEXED_LET 61 | DELETE_INDEXED_EQ 62 | DELETE_INDEXED_GT 63 | DELETE_INDEXED_LT 64 | DELETE_INDEXED_GET 65 | DELETE_INDEXED_LET 66 | CREATE_INDEX 67 | DROP_INDEX 68 | PROJECT 69 | SELECT_OPERATOR 70 | SELECT_LIMIT 71 | SELECT_GROUP_BY 72 | SELECT_ORDER_BY 73 | FILTER_PK_EQ 74 | FILTER_PK_GT 75 | FILTER_PK_LT 76 | FILTER_PK_GET 77 | FILTER_PK_LET 78 | FILTER_PK_NOT_EQ 79 | FILTER_NON_INDEXED_EQ 80 | FILTER_NON_INDEXED_GT 81 | FILTER_NON_INDEXED_LT 82 | FILTER_NON_INDEXED_GET 83 | FILTER_NON_INDEXED_LET 84 | FILTER_NON_INDEXED_NOT_EQ 85 | FILTER_INDEXED_EQ 86 | FILTER_INDEXED_GT 87 | FILTER_INDEXED_LT 88 | FILTER_INDEXED_GET 89 | FILTER_INDEXED_LET 90 | FILTER_INDEXED_NOT_EQ 91 | 92 | 93 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/connection/DriverConnection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.connection; 20 | 21 | import com.stratio.connector.mongodb.core.configuration.ConfigurationOptions; 22 | import com.stratio.connector.mongodb.core.engine.metadata.DiscoverMetadataUtils; 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | 26 | import com.mongodb.MongoClient; 27 | import com.stratio.connector.commons.connection.Connection; 28 | import com.stratio.connector.mongodb.core.configuration.MongoClientConfiguration; 29 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 30 | import com.stratio.crossdata.common.connector.ConnectorClusterConfig; 31 | import com.stratio.crossdata.common.exceptions.ConnectionException; 32 | import com.stratio.crossdata.common.security.ICredentials; 33 | import static com.stratio.connector.mongodb.core.configuration.ConfigurationOptions.*; 34 | 35 | 36 | /** 37 | * Implements a Mongo Connection. See {@link Connection} 38 | */ 39 | public class DriverConnection extends Connection { 40 | 41 | /** 42 | * The Log. 43 | */ 44 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 45 | 46 | private MongoClient mongoClient = null; 47 | 48 | /** 49 | * Instantiates a new driver connection. 50 | * 51 | * @param credentials 52 | * the credentials 53 | * @param connectorClusterConfig 54 | * the connector cluster configuration 55 | * @throws ConnectionException 56 | * @throws MongoValidationException 57 | * the mongo validation exception 58 | */ 59 | public DriverConnection(ICredentials credentials, ConnectorClusterConfig connectorClusterConfig) 60 | throws ConnectionException, MongoValidationException { 61 | MongoClientConfiguration mongoClientConfiguration = new MongoClientConfiguration(connectorClusterConfig); 62 | String sampleProperty = DiscoverMetadataUtils.recoveredSampleProperty(connectorClusterConfig); 63 | 64 | String defaultLimit = DiscoverMetadataUtils.recoveredDefaultLimit(connectorClusterConfig); 65 | 66 | addObjectToSession(SAMPLE_PROBABILITY.getOptionName(), sampleProperty); 67 | addObjectToSession(DEFAULT_LIMIT.getOptionName(), defaultLimit); 68 | 69 | if (credentials == null) { 70 | mongoClient = new MongoClient(mongoClientConfiguration.getSeeds(), 71 | mongoClientConfiguration.getMongoClientOptions()); 72 | if (isConnected()) { 73 | logger.info("New MongoDB connection established"); 74 | } else { 75 | throw new ConnectionException("Cannot connect with MongoDB"); 76 | } 77 | 78 | } else { 79 | throw new ConnectionException("Credentials are not supported yet"); 80 | } 81 | } 82 | 83 | 84 | 85 | /* 86 | * (non-Javadoc) 87 | * 88 | * @see com.stratio.connector.commons.connection.Connection#close() 89 | */ 90 | @Override 91 | public void close() { 92 | if (mongoClient != null) { 93 | mongoClient.close(); 94 | mongoClient = null; 95 | } 96 | } 97 | 98 | /* 99 | * (non-Javadoc) 100 | * 101 | * @see com.stratio.connector.commons.connection.Connection#getNativeConnection() 102 | */ 103 | @Override 104 | public MongoClient getNativeConnection() { 105 | return mongoClient; 106 | } 107 | 108 | /* 109 | * (non-Javadoc) 110 | * 111 | * @see com.stratio.connector.commons.connection.Connection#isConnect() 112 | */ 113 | @Override 114 | public final boolean isConnected() { 115 | 116 | boolean isConnected = false; 117 | 118 | if (mongoClient != null) { 119 | try { 120 | mongoClient.getConnectPoint(); 121 | isConnected = true; 122 | } catch (Exception error) { 123 | logger.error("Error connecting with Mongo :" + error.getMessage(), error); 124 | } 125 | } 126 | return isConnected; 127 | } 128 | 129 | 130 | 131 | } 132 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/metadata/StorageUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.metadata; 20 | 21 | import static org.junit.Assert.assertEquals; 22 | import static org.junit.Assert.assertTrue; 23 | 24 | import java.util.Collections; 25 | 26 | import org.junit.Test; 27 | 28 | import com.mongodb.DBObject; 29 | import com.stratio.connector.commons.metadata.TableMetadataBuilder; 30 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 31 | import com.stratio.crossdata.common.data.Cell; 32 | import com.stratio.crossdata.common.data.Row; 33 | import com.stratio.crossdata.common.metadata.ColumnType; 34 | import com.stratio.crossdata.common.metadata.DataType; 35 | import com.stratio.crossdata.common.metadata.TableMetadata; 36 | 37 | public class StorageUtilsTest { 38 | 39 | private static final String COLLECTION_NAME = "coll_name"; 40 | private static final String DB_NAME = "db_name"; 41 | private static final String CLUSTER_NAME = "cluster_name"; 42 | private static final String ROW_NAME = "row_name"; 43 | private static final String OTHER_ROW_NAME = "other_row_name"; 44 | private static final String CELL_VALUE = "cell_value"; 45 | private static final Object OTHER_CELL_VALUE = "othercellvalue"; 46 | private static final Integer INTEGER_CELL_VALUE = new Integer(5); 47 | private static final ColumnType VARCHAR_COLUMN_TYPE = new ColumnType(DataType.VARCHAR); 48 | private static final ColumnType INT_COLUMN_TYPE = new ColumnType(DataType.INT); 49 | 50 | @Test 51 | public void buildPKTest() throws MongoValidationException { 52 | 53 | TableMetadataBuilder tableMetaBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); 54 | tableMetaBuilder.addColumn(ROW_NAME, VARCHAR_COLUMN_TYPE).addColumn(OTHER_ROW_NAME, INT_COLUMN_TYPE); 55 | tableMetaBuilder.withPartitionKey(ROW_NAME); 56 | TableMetadata tableMetadata = tableMetaBuilder.build(); 57 | Row row = new Row(); 58 | row.addCell(ROW_NAME, new Cell(CELL_VALUE)); 59 | row.addCell(OTHER_ROW_NAME, new Cell(OTHER_CELL_VALUE)); 60 | 61 | Object pk = StorageUtils.buildPK(tableMetadata, row); 62 | 63 | assertEquals("The value is not the expected", CELL_VALUE, (String) pk); 64 | 65 | } 66 | 67 | @Test 68 | public void buildCompoundPKTest() throws MongoValidationException { 69 | 70 | TableMetadataBuilder tableMetaBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); 71 | tableMetaBuilder.addColumn(ROW_NAME, VARCHAR_COLUMN_TYPE).addColumn(OTHER_ROW_NAME, INT_COLUMN_TYPE); 72 | tableMetaBuilder.withPartitionKey(ROW_NAME); 73 | tableMetaBuilder.withClusterKey(OTHER_ROW_NAME); 74 | TableMetadata tableMetadata = tableMetaBuilder.build(); 75 | Row row = new Row(); 76 | row.addCell(ROW_NAME, new Cell(CELL_VALUE)); 77 | row.addCell(OTHER_ROW_NAME, new Cell(INTEGER_CELL_VALUE)); 78 | 79 | DBObject pk = (DBObject) StorageUtils.buildPK(tableMetadata, row); 80 | 81 | assertTrue("The primary key must contain " + ROW_NAME, pk.containsField(ROW_NAME)); 82 | assertTrue("The primary key must contain " + OTHER_ROW_NAME, pk.containsField(OTHER_ROW_NAME)); 83 | assertEquals("The string value is not the expected", CELL_VALUE, pk.get(ROW_NAME)); 84 | assertEquals("The integer value is not the expected", INTEGER_CELL_VALUE, pk.get(OTHER_ROW_NAME)); 85 | 86 | } 87 | 88 | @Test(expected = MongoValidationException.class) 89 | public void buildMalformedPKTest() throws MongoValidationException { 90 | 91 | TableMetadataBuilder tableMetaBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); 92 | tableMetaBuilder.addColumn(ROW_NAME, VARCHAR_COLUMN_TYPE).addColumn(OTHER_ROW_NAME, new ColumnType(DataType.SET)); 93 | tableMetaBuilder.withPartitionKey(OTHER_ROW_NAME); 94 | TableMetadata tableMetadata = tableMetaBuilder.build(); 95 | 96 | Row row = new Row(); 97 | row.addCell(OTHER_ROW_NAME, new Cell(Collections.EMPTY_SET)); 98 | 99 | StorageUtils.buildPK(tableMetadata, row); 100 | 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/config/MongoConnector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | MongoConnector 22 | 23 | Mongo 24 | 25 | 0.3.0 26 | 27 | 28 | sample_probability 29 | Number between 0 and 1 with the probability of take part of the import process . 30 | 31 | 32 | 33 | 34 | default_limit 35 | A positive integer used to limit the query results. 36 | 37 | 38 | 39 | DROP_CATALOG 40 | CREATE_TABLE 41 | ALTER_TABLE 42 | DROP_TABLE 43 | IMPORT_METADATA 44 | UPDATE_NO_FILTERS 45 | UPDATE_PK_EQ 46 | UPDATE_PK_GT 47 | UPDATE_PK_LT 48 | UPDATE_PK_GET 49 | UPDATE_PK_LET 50 | UPDATE_NON_INDEXED_EQ 51 | UPDATE_NON_INDEXED_GT 52 | UPDATE_NON_INDEXED_LT 53 | UPDATE_NON_INDEXED_GET 54 | UPDATE_NON_INDEXED_LET 55 | UPDATE_INDEXED_EQ 56 | UPDATE_INDEXED_GT 57 | UPDATE_INDEXED_LT 58 | UPDATE_INDEXED_GET 59 | UPDATE_INDEXED_LET 60 | TRUNCATE_TABLE 61 | INSERT 62 | INSERT_IF_NOT_EXISTS 63 | DELETE_NO_FILTERS 64 | DELETE_PK_EQ 65 | DELETE_PK_GT 66 | DELETE_PK_LT 67 | DELETE_PK_GET 68 | DELETE_PK_LET 69 | DELETE_NON_INDEXED_EQ 70 | DELETE_NON_INDEXED_GT 71 | DELETE_NON_INDEXED_LT 72 | DELETE_NON_INDEXED_GET 73 | DELETE_NON_INDEXED_LET 74 | DELETE_INDEXED_EQ 75 | DELETE_INDEXED_GT 76 | DELETE_INDEXED_LT 77 | DELETE_INDEXED_GET 78 | DELETE_INDEXED_LET 79 | CREATE_INDEX 80 | DROP_INDEX 81 | PROJECT 82 | SELECT_OPERATOR 83 | SELECT_LIMIT 84 | SELECT_GROUP_BY 85 | SELECT_ORDER_BY 86 | FILTER_PK_EQ 87 | FILTER_PK_GT 88 | FILTER_PK_LT 89 | FILTER_PK_GET 90 | FILTER_PK_LET 91 | FILTER_PK_NOT_EQ 92 | FILTER_NON_INDEXED_EQ 93 | FILTER_NON_INDEXED_GT 94 | FILTER_NON_INDEXED_LT 95 | FILTER_NON_INDEXED_GET 96 | FILTER_NON_INDEXED_LET 97 | FILTER_NON_INDEXED_NOT_EQ 98 | FILTER_INDEXED_EQ 99 | FILTER_INDEXED_GT 100 | FILTER_INDEXED_LT 101 | FILTER_INDEXED_GET 102 | FILTER_INDEXED_LET 103 | FILTER_INDEXED_NOT_EQ 104 | 105 | 106 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/MongoConnector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import com.stratio.connector.commons.CommonsConnector; 25 | import com.stratio.connector.commons.util.ManifestUtil; 26 | import com.stratio.connector.mongodb.core.connection.MongoConnectionHandler; 27 | import com.stratio.connector.mongodb.core.engine.MongoMetadataEngine; 28 | import com.stratio.connector.mongodb.core.engine.MongoQueryEngine; 29 | import com.stratio.connector.mongodb.core.engine.MongoStorageEngine; 30 | import com.stratio.crossdata.common.connector.IConfiguration; 31 | import com.stratio.crossdata.common.connector.IMetadataEngine; 32 | import com.stratio.crossdata.common.connector.IQueryEngine; 33 | import com.stratio.crossdata.common.connector.IStorageEngine; 34 | import com.stratio.crossdata.common.exceptions.ExecutionException; 35 | import com.stratio.crossdata.common.exceptions.InitializationException; 36 | import com.stratio.crossdata.connectors.ConnectorApp; 37 | 38 | /** 39 | * This class implements the connector for Mongo. 40 | */ 41 | public class MongoConnector extends CommonsConnector { 42 | 43 | /** 44 | * The Log. 45 | */ 46 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 47 | 48 | 49 | /** 50 | * Instantiates a new mongo connector. 51 | * 52 | * @throws InitializationException 53 | * if any error exists during the initialization 54 | */ 55 | public MongoConnector() throws InitializationException { 56 | super("/MongoConnector.xml", "/MongoDataStore.xml"); 57 | 58 | } 59 | 60 | /** 61 | * Create a connection to Mongo. 62 | * 63 | * @param configuration 64 | * the connection configuration. It must be not null 65 | */ 66 | @Override 67 | public void init(IConfiguration configuration) { 68 | connectionHandler = new MongoConnectionHandler(configuration); 69 | } 70 | 71 | /** 72 | * REstar the connector. 73 | * @throws ExecutionException if an exception happens. 74 | */ 75 | @Override 76 | public void restart() throws ExecutionException { 77 | 78 | } 79 | 80 | /** 81 | * Return the StorageEngine. 82 | * 83 | * @return the StorageEngine 84 | */ 85 | @Override 86 | public IStorageEngine getStorageEngine() { 87 | return MongoStorageEngine.getInstance((MongoConnectionHandler) connectionHandler); 88 | } 89 | 90 | /** 91 | * Return the QueryEngine. 92 | * 93 | * @return the QueryEngine 94 | */ 95 | @Override 96 | public IQueryEngine getQueryEngine() { 97 | return MongoQueryEngine.getInstance((MongoConnectionHandler) connectionHandler); 98 | 99 | } 100 | 101 | /** 102 | * Return the MetadataEngine. 103 | * 104 | * @return the MetadataEngine 105 | * 106 | */ 107 | @Override 108 | public IMetadataEngine getMetadataEngine() { 109 | return MongoMetadataEngine.getInstance((MongoConnectionHandler) connectionHandler); 110 | 111 | } 112 | 113 | 114 | /** 115 | * The main method. 116 | * 117 | * @param args 118 | * the arguments 119 | * @throws InitializationException 120 | * if any error exists during the initialization 121 | */ 122 | public static void main(String[] args) throws InitializationException { 123 | 124 | MongoConnector mongoConnector = new MongoConnector(); 125 | ConnectorApp connectorApp = new ConnectorApp(); 126 | connectorApp.startup(mongoConnector); 127 | mongoConnector.attachShutDownHook(); 128 | } 129 | 130 | /** 131 | * Attach shut down hook. 132 | */ 133 | public void attachShutDownHook() { 134 | Runtime.getRuntime().addShutdownHook(new Thread() { 135 | @Override 136 | public void run() { 137 | try { 138 | shutdown(); 139 | } catch (ExecutionException e) { 140 | logger.error("Shutdown connector: " + e.getMessage(), e); 141 | } 142 | } 143 | }); 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/unix/files_and_dirs/etc/init.d/connector_mongodb: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: stratio-connector-mongodb 5 | # Required-Start: $remote_fs 6 | # Required-Stop: $remote_fs 7 | # Should-Stop: $all 8 | # Default-Start: 2 3 4 5 9 | # Default-Stop: 0 1 6 10 | # Short-Description: Crossdata connector 11 | # Description: Description for stratio-connector-mongodb 12 | ### END INIT INFO 13 | # Developed by pmadrid@stratio.com 14 | # Broken by alvaro@stratio.com 15 | # Version: 0.1 2014 16 | # When I learn scripting a bit better, I'll try to improve this one... 17 | 18 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 19 | INIT_HOME=`cd "${DIR}/.." >/dev/null; pwd` 20 | 21 | NAME="stratio-connector-mongodb" 22 | DESC="Description for stratio-connector-mongodb" 23 | BASEDIR=/opt/sds/connectors/mongodb 24 | CONFDIR=/etc/sds/connectors/mongodb 25 | LOGFILE=/var/log/sds/connectors/mongodb/connector_mongodb.log 26 | RUNFILE=$BASEDIR/bin/stratio-connector-mongodb 27 | pidDir=/var/run/sds/connectors 28 | serviceUser=stratio 29 | serviceGroup=stratio 30 | PIDFileName=connector_mongodb 31 | 32 | # If JAVA_HOME has not been set, try to determine it.ner 33 | JVM_SEARCH_DIRS="/usr/java/default /usr/java/latest /opt/java" 34 | 35 | if [ ! -d "$pidDir" ]; then 36 | mkdir -p "$pidDir" 37 | if [ $? -ne 0 ]; then exit 1; fi 38 | if [ -n "$serviceGroup" ]; then chown ${serviceUser}:${serviceGroup} "$pidDir"; fi 39 | if [ $? -ne 0 ]; then exit 1; fi 40 | fi 41 | pidFile="${pidDir}/${NAME}.pid" 42 | 43 | if [ ! -z "${PIDFileName}" ]; then 44 | pidFile="${pidDir}/${PIDFileName}.pid" 45 | fi 46 | 47 | # Returns 0 if the process with PID $1 is running. 48 | function checkProcessIsRunning { 49 | local pid="$1" 50 | if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi 51 | ps -Af | grep ${pid} | grep java > /dev/null 2>&1 52 | if [ $? -ne 0 ]; then return 1; fi 53 | return 0; 54 | } 55 | 56 | # Returns 0 if the process with PID $1 is our Java service process. 57 | function checkProcessIsOurService { 58 | local pid="$1" 59 | ps -Af | grep ${pid} | grep java > /dev/null 2>&1 60 | if [ $? -ne 0 ]; then return 1; fi 61 | return 0; 62 | } 63 | 64 | # Returns 0 when the service is running and sets the variable $servicePid to the PID. 65 | function getServicePid { 66 | if [ ! -f $pidFile ]; then return 1; fi 67 | local servicePid="$(<$pidFile)" 68 | checkProcessIsRunning $servicePid || return 1 69 | checkProcessIsOurService $servicePid || return 1 70 | return 0; 71 | } 72 | 73 | function startServiceProcess { 74 | cd $BASEDIR || return 1 75 | rm -f $pidFile 76 | local cmd="$RUNFILE >>$LOGFILE 2>&1 & echo \$! >$pidFile" 77 | echo "Launching service process: ${NAME}" 78 | echo "" 79 | su $serviceUser -c "$cmd" || return 1 80 | sleep 0.1 81 | servicePid="$(<$pidFile)" 82 | echo "PID: ${pidFile} servicePid: ${servicePid}" 83 | if checkProcessIsRunning $servicePid; then :; else 84 | echo -e "\n${NAME} start failed, see logfile." 85 | rm -f $pidfile 86 | return 1 87 | fi 88 | return 0; 89 | } 90 | 91 | function stopServiceProcess { 92 | servicePid="$(<$pidFile)" 93 | kill $servicePid || return 1 94 | local killWaitTime=10 95 | for ((i=0; i<$killWaitTime*10; i++)); do 96 | checkProcessIsRunning $servicePid 97 | if [ $? -ne 0 ]; then 98 | rm -f $pidFile 99 | return 0 100 | fi 101 | sleep 0.1 102 | done 103 | echo -e "\n${NAME} did not terminate within 10 seconds, sending SIGKILL..." 104 | kill -s KILL $servicePid || return 1 105 | for ((i=0; i<$killWaitTime*10; i++)); do 106 | checkProcessIsRunning $servicePid 107 | if [ $? -ne 0 ]; then 108 | rm -f $pidFile 109 | return 0 110 | fi 111 | sleep 0.1 112 | done 113 | echo "Error: ${NAME} could not be stopped within 20 seconds!" 114 | return 1; 115 | } 116 | 117 | function startService { 118 | # Add any service start conditions here 119 | getServicePid 120 | if [ $? -eq 0 ]; then echo "${NAME} is already running"; return 0; fi 121 | echo "Starting ${NAME}" 122 | startServiceProcess 123 | if [ $? -ne 0 ]; then echo "Error starting ${NAME}" ; return 1; fi 124 | return 0; 125 | } 126 | 127 | function stopService { 128 | getServicePid 129 | if [ $? -ne 0 ]; then echo "${NAME} is not running"; return 0; fi 130 | echo "Stopping ${NAME} " 131 | stopServiceProcess 132 | if [ $? -ne 0 ]; then echo "Error stopping ${NAME}"; return 1; fi 133 | return 0; 134 | } 135 | 136 | function checkServiceStatus { 137 | echo "Checking for ${NAME}: " 138 | if [ ! -f "${CONFDIR}/connector-application.conf" ]; then 139 | echo "Error: Configuration file not found!" 140 | return 1 141 | fi 142 | if getServicePid; then 143 | local servicePid="$(<$pidFile)" 144 | echo "$DESC seems to be running (pid $servicePid)" 145 | return 0 146 | else 147 | echo "${NAME} seems to be stopped" 148 | return 1 149 | fi 150 | return 0; 151 | } 152 | 153 | case "$1" in 154 | start) 155 | startService 156 | retval=$? 157 | exit $retval 158 | ;; 159 | stop) 160 | stopService 161 | retval=$? 162 | exit $retval 163 | ;; 164 | restart) 165 | stopService && startService 166 | retval=$? 167 | exit $retval 168 | ;; 169 | status) 170 | checkServiceStatus 171 | retval=$? 172 | exit $retval 173 | ;; 174 | *) 175 | echo "Usage: $0 {start|stop|restart|status}" 176 | exit 1 177 | ;; 178 | esac 179 | 180 | exit 0 -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/MongoQueryEngineTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine; 20 | 21 | import static org.junit.Assert.assertEquals; 22 | import static org.mockito.Mockito.mock; 23 | import static org.mockito.Mockito.times; 24 | import static org.mockito.Mockito.verify; 25 | import static org.mockito.Mockito.when; 26 | 27 | import com.stratio.crossdata.common.exceptions.ConnectorException; 28 | import org.junit.Before; 29 | import org.junit.Test; 30 | import org.junit.runner.RunWith; 31 | import org.mockito.Matchers; 32 | import org.mockito.Mock; 33 | import org.mockito.Mockito; 34 | import org.powermock.api.mockito.PowerMockito; 35 | import org.powermock.core.classloader.annotations.PowerMockIgnore; 36 | import org.powermock.core.classloader.annotations.PrepareForTest; 37 | import org.powermock.modules.junit4.PowerMockRunner; 38 | 39 | import com.mongodb.MongoClient; 40 | import com.stratio.connector.commons.connection.Connection; 41 | import com.stratio.connector.commons.engine.query.ProjectParsed; 42 | import com.stratio.connector.mongodb.core.connection.MongoConnectionHandler; 43 | import com.stratio.connector.mongodb.core.engine.query.LogicalWorkflowExecutor; 44 | import com.stratio.connector.mongodb.core.engine.query.LogicalWorkflowExecutorFactory; 45 | import com.stratio.crossdata.common.connector.IResultHandler; 46 | import com.stratio.crossdata.common.data.ResultSet; 47 | import com.stratio.crossdata.common.exceptions.ExecutionException; 48 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 49 | import com.stratio.crossdata.common.logicalplan.LogicalWorkflow; 50 | import com.stratio.crossdata.common.logicalplan.Project; 51 | import com.stratio.crossdata.common.result.QueryResult; 52 | 53 | @PowerMockIgnore( {"javax.management.*"}) 54 | @RunWith(PowerMockRunner.class) 55 | @PrepareForTest({ MongoQueryEngine.class, LogicalWorkflowExecutor.class, LogicalWorkflowExecutorFactory.class, 56 | QueryResult.class, ResultSet.class }) 57 | public class MongoQueryEngineTest { 58 | 59 | private MongoQueryEngine mongoQueryEngine; 60 | @Mock 61 | MongoConnectionHandler connectionHandler; 62 | 63 | @Before 64 | public void before() throws Exception { 65 | 66 | mongoQueryEngine = MongoQueryEngine.getInstance(connectionHandler); 67 | 68 | } 69 | 70 | /** 71 | * Method: execute(Project logicalWorkflow, Connection connection) 72 | */ 73 | @SuppressWarnings("unchecked") 74 | @Test 75 | public void executeTest() throws Exception { 76 | Project project = mock(Project.class); 77 | LogicalWorkflowExecutor logicalWorkflowExecutor = mock(LogicalWorkflowExecutor.class); 78 | Connection connection = mock(Connection.class); 79 | 80 | ProjectParsed projectParsed = mock(ProjectParsed.class); 81 | PowerMockito.whenNew(ProjectParsed.class).withAnyArguments().thenReturn(projectParsed); 82 | PowerMockito.mockStatic(LogicalWorkflowExecutorFactory.class); 83 | PowerMockito.when(LogicalWorkflowExecutorFactory.getLogicalWorkflowExecutor(Matchers.any(ProjectParsed.class))) 84 | .thenReturn(logicalWorkflowExecutor); 85 | ResultSet resultSet = mock(ResultSet.class); 86 | when(logicalWorkflowExecutor.executeQuery(connection)).thenReturn(resultSet); 87 | QueryResult queryResult = mock(QueryResult.class); 88 | PowerMockito.mockStatic(QueryResult.class); 89 | PowerMockito.when(QueryResult.createQueryResult(resultSet, 0, true)).thenReturn(queryResult); 90 | 91 | QueryResult returnQueryResult = mongoQueryEngine.execute(project, connection); 92 | 93 | verify(logicalWorkflowExecutor, times(1)).executeQuery(connection); 94 | 95 | PowerMockito.verifyStatic(times(1)); 96 | QueryResult.createQueryResult(resultSet, 0, true); 97 | 98 | assertEquals("The query result is wrong", queryResult, returnQueryResult); 99 | } 100 | 101 | /** 102 | * Method: asyncExecute(String queryId, LogicalWorkflow workflow, IResultHandler resultHandler) 103 | */ 104 | @Test(expected = UnsupportedException.class) 105 | public void asyncExecuteTest() throws ConnectorException { 106 | 107 | mongoQueryEngine.asyncExecute("", Mockito.mock(Project.class), null,Mockito.mock(IResultHandler.class)); 108 | 109 | } 110 | 111 | /** 112 | * Method: stop(String queryId) 113 | */ 114 | @Test(expected = UnsupportedException.class) 115 | public void stopTest() throws UnsupportedException, ExecutionException { 116 | mongoQueryEngine.stop(""); 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/MongoQueryEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine; 20 | 21 | import com.mongodb.MongoClient; 22 | import com.stratio.connector.commons.TimerJ; 23 | import com.stratio.connector.commons.connection.Connection; 24 | import com.stratio.connector.commons.engine.SingleProjectQueryEngine; 25 | import com.stratio.connector.commons.engine.query.ProjectParsed; 26 | import com.stratio.connector.mongodb.core.connection.MongoConnectionHandler; 27 | import com.stratio.connector.mongodb.core.engine.query.LogicalWorkflowExecutor; 28 | import com.stratio.connector.mongodb.core.engine.query.LogicalWorkflowExecutorFactory; 29 | import com.stratio.connector.mongodb.core.engine.query.MongoLogicalWorkflowValidator; 30 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 31 | import com.stratio.crossdata.common.connector.IResultHandler; 32 | import com.stratio.crossdata.common.data.ResultSet; 33 | import com.stratio.crossdata.common.exceptions.ConnectorException; 34 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 35 | import com.stratio.crossdata.common.logicalplan.LogicalWorkflow; 36 | import com.stratio.crossdata.common.logicalplan.Project; 37 | import com.stratio.crossdata.common.result.QueryResult; 38 | 39 | /** 40 | * The MongoQueryEngine. 41 | */ 42 | public class MongoQueryEngine extends SingleProjectQueryEngine { 43 | 44 | 45 | private static MongoQueryEngine instance = null; 46 | /** 47 | * Instantiates a new Mongo query engine. 48 | * 49 | * @param connectionHandler 50 | * the connection handler 51 | */ 52 | private MongoQueryEngine(MongoConnectionHandler connectionHandler) { 53 | super(connectionHandler); 54 | } 55 | 56 | public static MongoQueryEngine getInstance(MongoConnectionHandler connectionHandler){ 57 | if(instance == null){ 58 | instance = new MongoQueryEngine(connectionHandler); 59 | } 60 | return instance; 61 | } 62 | 63 | /** 64 | * Instantiates a new Mongo query engine. 65 | * 66 | * @param project 67 | * the initial step of the logical workflow 68 | * @param connection 69 | * the MongoDB connection 70 | * @return the query result 71 | * @throws MongoValidationException 72 | * if the specified operation is not supported 73 | * @throws ConnectorException 74 | * if the execution fails 75 | */ 76 | @Override 77 | @TimerJ 78 | public QueryResult execute(Project project, Connection connection) throws MongoValidationException, 79 | ConnectorException { 80 | 81 | ProjectParsed logicalWorkfloParsed = new ProjectParsed(project, new MongoLogicalWorkflowValidator()); 82 | LogicalWorkflowExecutor executor = LogicalWorkflowExecutorFactory 83 | .getLogicalWorkflowExecutor(logicalWorkfloParsed); 84 | 85 | 86 | ResultSet resultSet = executor.executeQuery(connection); 87 | return QueryResult.createQueryResult(resultSet, 0, true); 88 | 89 | } 90 | 91 | /** 92 | * Execute a async query. 93 | * @param queryId the query ID. 94 | * @param project the query to be executed. 95 | * @param connection the conection. 96 | * @param iResultHandler the resylt handler. 97 | * @throws ConnectorException if any error happens. 98 | */ 99 | @Override 100 | protected void asyncExecute(String queryId, Project project, Connection connection, IResultHandler iResultHandler) throws ConnectorException { 101 | throw new UnsupportedException("The method asyncExecute is not supported"); 102 | } 103 | 104 | /** 105 | * Execute a paged query. 106 | * @param queryId the query ID. 107 | * @param project the query to be executed. 108 | * @param connection the conection. 109 | * @param iResultHandler the resylt handler. 110 | * @throws ConnectorException if any error happens. 111 | */ 112 | @Override 113 | protected void pagedExecute(String queryId, Project project, Connection connection, IResultHandler iResultHandler) throws ConnectorException { 114 | throw new UnsupportedException("The method pagedExecute is ot supported"); 115 | 116 | } 117 | 118 | 119 | /** 120 | * Stiop a query. 121 | * @param queryId the queryID. 122 | * @throws UnsupportedException if any error happens. 123 | */ 124 | @Override 125 | public void stop(String queryId) throws UnsupportedException { 126 | throw new UnsupportedException("Not supported"); 127 | 128 | } 129 | 130 | } 131 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/BasicLogicalWorkflowExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query; 19 | 20 | import java.util.ArrayList; 21 | 22 | import com.mongodb.DB; 23 | import com.mongodb.DBCollection; 24 | import com.mongodb.DBCursor; 25 | import com.mongodb.DBObject; 26 | import com.mongodb.MongoClient; 27 | import com.mongodb.MongoException; 28 | import com.stratio.connector.commons.connection.Connection; 29 | import com.stratio.connector.commons.engine.query.ProjectParsed; 30 | import com.stratio.connector.mongodb.core.engine.query.utils.MetaResultUtils; 31 | import com.stratio.connector.mongodb.core.exceptions.MongoExecutionException; 32 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 33 | import com.stratio.crossdata.common.data.ResultSet; 34 | import com.stratio.crossdata.common.exceptions.ExecutionException; 35 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 36 | 37 | import static com.stratio.connector.mongodb.core.configuration.ConfigurationOptions.DEFAULT_LIMIT; 38 | 39 | /** 40 | * The LogicalWorkflowExecutor for non-aggregation queries. 41 | */ 42 | public class BasicLogicalWorkflowExecutor extends LogicalWorkflowExecutor { 43 | 44 | /** 45 | * Instantiates a new basic logical workflow executor. 46 | * 47 | * @param logicalWorkflowParsed 48 | * the logical workflow parsed 49 | * @throws ExecutionException 50 | * if the execution fails or the query specified in the logical workflow is not supported 51 | * @throws UnsupportedException 52 | * if the specified operation is not supported. 53 | */ 54 | public BasicLogicalWorkflowExecutor(ProjectParsed logicalWorkflowParsed) throws ExecutionException, 55 | UnsupportedException { 56 | super(logicalWorkflowParsed); 57 | } 58 | 59 | /* 60 | * (non-Javadoc) 61 | * 62 | * @see com.stratio.connector.mongodb.core.engine.query.LogicalWorkflowExecutor#buildQuery() 63 | */ 64 | @Override 65 | protected void buildQuery() throws MongoValidationException, UnsupportedException { 66 | query = new ArrayList(); 67 | query.add(buildFilter(false)); 68 | 69 | } 70 | 71 | /** 72 | * Execute an usual query. 73 | * 74 | * @param connection 75 | * the Connection that holds the mongodb client. 76 | * @return the Crossdata ResultSet. 77 | * @throws MongoValidationException . 78 | * @throws ExecutionException 79 | * if the execution fails or the query specified in the logical workflow is not supported. 80 | */ 81 | public ResultSet executeQuery(Connection connection) throws ExecutionException { 82 | 83 | MongoClient mongoClient = connection.getNativeConnection(); 84 | DB db = mongoClient.getDB(logicalWorkflowData.getProject().getCatalogName()); 85 | DBCollection collection = db.getCollection(logicalWorkflowData.getProject().getTableName().getName()); 86 | ResultSet resultSet = new ResultSet(); 87 | resultSet.setColumnMetadata(MetaResultUtils.createMetadata(logicalWorkflowData.getProject(), 88 | logicalWorkflowData.getSelect())); 89 | 90 | if (logger.isDebugEnabled()) { 91 | logger.debug("Executing MongoQuery: " + query.get(0) + ", with fields: " + buildProject(false)); 92 | } 93 | 94 | DBCursor cursor = collection.find(query.get(0), buildProject(false)); 95 | 96 | if (logicalWorkflowData.getOrderBy() != null) { 97 | cursor = cursor.sort(buildOrderBy(false)); 98 | } 99 | 100 | if (logicalWorkflowData.getLimit() != null) { 101 | cursor = cursor.limit(logicalWorkflowData.getLimit().getLimit()); 102 | }else{ 103 | String limit = connection.getSessionObject(String.class, DEFAULT_LIMIT.getOptionName()); 104 | cursor = cursor.limit(Integer.parseInt(limit)); 105 | } 106 | 107 | DBObject rowDBObject; 108 | try { 109 | while (cursor.hasNext()) { 110 | rowDBObject = cursor.next(); 111 | if (logger.isDebugEnabled()) { 112 | logger.debug("BResult: " + rowDBObject); 113 | } 114 | resultSet.add(MetaResultUtils.createRowWithAlias(rowDBObject, logicalWorkflowData.getSelect())); 115 | } 116 | } catch (MongoException e) { 117 | logger.error("Error executing a basic query :" + query.get(0) + ", with fields: " + buildProject(false) 118 | + "\n Error:" + e.getMessage()); 119 | throw new MongoExecutionException(e.getMessage(), e); 120 | } finally { 121 | cursor.close(); 122 | } 123 | 124 | return resultSet; 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/query/utils/GroupByDBObjectBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query.utils; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import java.util.ArrayList; 24 | import java.util.Arrays; 25 | import java.util.HashSet; 26 | import java.util.List; 27 | import java.util.Set; 28 | 29 | import org.junit.Assert; 30 | import org.junit.Test; 31 | import org.mockito.internal.util.reflection.Whitebox; 32 | 33 | import com.mongodb.BasicDBObject; 34 | import com.mongodb.DBObject; 35 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 36 | import com.stratio.crossdata.common.data.ColumnName; 37 | import com.stratio.crossdata.common.exceptions.ExecutionException; 38 | import com.stratio.crossdata.common.logicalplan.GroupBy; 39 | import com.stratio.crossdata.common.metadata.Operations; 40 | import com.stratio.crossdata.common.statements.structures.ColumnSelector; 41 | import com.stratio.crossdata.common.statements.structures.Selector; 42 | 43 | public class GroupByDBObjectBuilderTest { 44 | 45 | private static String CATALOG = "catalog"; 46 | private static String TABLE = "table"; 47 | private static String GROUP_COLUMN = "column"; 48 | private static String GROUP_COLUMN_SECONDARY = "column_sec"; 49 | private static String SELECTED_FIELD = "sel_column"; 50 | 51 | @Test 52 | public void groupByDBObjectBuilderTest() throws MongoValidationException, ExecutionException { 53 | 54 | // GroupBy with one column 55 | Set colSelected = new HashSet(Arrays.asList(new ColumnSelector(new ColumnName(CATALOG, 56 | TABLE, SELECTED_FIELD)))); 57 | GroupBy groupBy = getGroupBy(GROUP_COLUMN); 58 | GroupByDBObjectBuilder groupByDBObjectBuilder = new GroupByDBObjectBuilder(groupBy, colSelected); 59 | assertTrue("useAggregations should be true", 60 | (Boolean) Whitebox.getInternalState(groupByDBObjectBuilder, "useAggregation")); 61 | 62 | DBObject aggregationOperation = new BasicDBObject("$first", "$" + SELECTED_FIELD); 63 | 64 | DBObject internalProjectDBObject = (DBObject) Whitebox.getInternalState(groupByDBObjectBuilder, "groupQuery"); 65 | 66 | Assert.assertEquals("The groupby command is not the expected", "$" + GROUP_COLUMN, 67 | internalProjectDBObject.get("_id")); 68 | Assert.assertEquals("The groupby operand is not the expected", aggregationOperation, 69 | internalProjectDBObject.get(SELECTED_FIELD)); 70 | 71 | // GroupBy with several columns 72 | groupBy = getGroupBy(GROUP_COLUMN, GROUP_COLUMN_SECONDARY); 73 | groupByDBObjectBuilder = new GroupByDBObjectBuilder(groupBy, colSelected); 74 | 75 | groupByDBObjectBuilder = new GroupByDBObjectBuilder(groupBy, colSelected); 76 | assertTrue("useAggregations should be true", 77 | (Boolean) Whitebox.getInternalState(groupByDBObjectBuilder, "useAggregation")); 78 | 79 | aggregationOperation = new BasicDBObject("$first", "$" + SELECTED_FIELD); 80 | DBObject groupColumnsFields = new BasicDBObject(GROUP_COLUMN, "$" + GROUP_COLUMN); 81 | groupColumnsFields.put(GROUP_COLUMN_SECONDARY, "$" + GROUP_COLUMN_SECONDARY); 82 | internalProjectDBObject = (DBObject) Whitebox.getInternalState(groupByDBObjectBuilder, "groupQuery"); 83 | 84 | Assert.assertEquals("The groupby command is not the expected", groupColumnsFields, 85 | internalProjectDBObject.get("_id")); 86 | Assert.assertEquals("The groupby operand is not the expected", aggregationOperation, 87 | internalProjectDBObject.get(SELECTED_FIELD)); 88 | 89 | } 90 | 91 | @Test 92 | public void buildTest() throws Exception { 93 | 94 | Set colSelected = new HashSet(Arrays.asList(new ColumnSelector(new ColumnName(CATALOG, 95 | TABLE, SELECTED_FIELD)))); 96 | GroupBy groupBy = getGroupBy(GROUP_COLUMN); 97 | GroupByDBObjectBuilder groupByDBObjectBuilder = new GroupByDBObjectBuilder(groupBy, colSelected); 98 | 99 | DBObject fakeGroupBy = new BasicDBObject("fake", 5); 100 | 101 | // Default build 102 | Whitebox.setInternalState(groupByDBObjectBuilder, "groupQuery", fakeGroupBy); 103 | Whitebox.setInternalState(groupByDBObjectBuilder, "useAggregation", true); 104 | 105 | DBObject groupByCommand = groupByDBObjectBuilder.build(); 106 | 107 | assertTrue("The groupBy command should have a root $group", groupByCommand.keySet().contains("$group")); 108 | assertEquals("The value is not the expected", fakeGroupBy, groupByCommand.get("$group")); 109 | 110 | } 111 | 112 | public GroupBy getGroupBy(String... fields) { 113 | List ids = new ArrayList(); 114 | for (String field : fields) { 115 | ids.add(new ColumnSelector(new ColumnName(CATALOG, TABLE, field))); 116 | } 117 | Set operations = new HashSet<>(); 118 | operations.add(Operations.SELECT_GROUP_BY); 119 | 120 | return new GroupBy(operations, ids); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/query/utils/OrderByDBObjectBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query.utils; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertFalse; 22 | import static org.junit.Assert.assertTrue; 23 | 24 | import java.util.ArrayList; 25 | import java.util.HashSet; 26 | import java.util.List; 27 | import java.util.Set; 28 | 29 | import org.junit.Test; 30 | import org.mockito.internal.util.reflection.Whitebox; 31 | 32 | import com.mongodb.BasicDBObject; 33 | import com.mongodb.DBObject; 34 | import com.stratio.crossdata.common.data.ColumnName; 35 | import com.stratio.crossdata.common.exceptions.ExecutionException; 36 | import com.stratio.crossdata.common.logicalplan.OrderBy; 37 | import com.stratio.crossdata.common.metadata.Operations; 38 | import com.stratio.crossdata.common.statements.structures.ColumnSelector; 39 | import com.stratio.crossdata.common.statements.structures.OrderByClause; 40 | import com.stratio.crossdata.common.statements.structures.OrderDirection; 41 | 42 | public class OrderByDBObjectBuilderTest { 43 | 44 | private static String CATALOG = "catalog"; 45 | private static String TABLE = "table"; 46 | private static String COLUMN_PRIMARY = "column"; 47 | private static String COLUMN_SECONDARY = "column_sec"; 48 | 49 | @Test 50 | public void basicOrderByDBObjectBuilderTest() throws ExecutionException { 51 | 52 | OrderByDBObjectBuilder orderByDBObjectBuilder = new OrderByDBObjectBuilder(false, getOrderBy(COLUMN_PRIMARY)); 53 | 54 | DBObject internalDBObject = (DBObject) Whitebox.getInternalState(orderByDBObjectBuilder, "orderByQuery"); 55 | 56 | assertTrue("The orderBy query must have the column" + COLUMN_PRIMARY, 57 | internalDBObject.containsField(COLUMN_PRIMARY)); 58 | assertEquals("The orderBy value is not the expected", 1, internalDBObject.get(COLUMN_PRIMARY)); 59 | assertEquals("The orderBy command is not the expected", new BasicDBObject(COLUMN_PRIMARY, 1), internalDBObject); 60 | 61 | } 62 | 63 | @Test 64 | public void combinedOrderByDBObjectBuilderTest() throws ExecutionException { 65 | 66 | OrderBy orderBy = getOrderBy(new String[] { COLUMN_PRIMARY, COLUMN_SECONDARY }, new OrderDirection[] { 67 | OrderDirection.DESC, OrderDirection.ASC }); 68 | OrderByDBObjectBuilder orderByDBObjectBuilder = new OrderByDBObjectBuilder(false, orderBy); 69 | 70 | DBObject internalDBObject = (DBObject) Whitebox.getInternalState(orderByDBObjectBuilder, "orderByQuery"); 71 | 72 | assertTrue("The orderBy query must have the column" + COLUMN_PRIMARY, 73 | internalDBObject.containsField(COLUMN_PRIMARY)); 74 | assertTrue("The orderBy query must have the column" + COLUMN_SECONDARY, 75 | internalDBObject.containsField(COLUMN_SECONDARY)); 76 | 77 | BasicDBObject expectedDBObject = new BasicDBObject(COLUMN_PRIMARY, -1); 78 | expectedDBObject.append(COLUMN_SECONDARY, 1); 79 | 80 | assertEquals("The orderBy command is not the expected", expectedDBObject, internalDBObject); 81 | 82 | } 83 | 84 | @Test 85 | public void buildTest() throws Exception { 86 | 87 | OrderByDBObjectBuilder orderByDBObjectBuilder = new OrderByDBObjectBuilder(false, getOrderBy("mock")); 88 | 89 | DBObject fakeOrderBy = new BasicDBObject("fake", 5); 90 | 91 | // Aggregation build 92 | Whitebox.setInternalState(orderByDBObjectBuilder, "orderByQuery", fakeOrderBy); 93 | Whitebox.setInternalState(orderByDBObjectBuilder, "useAggregation", true); 94 | 95 | DBObject orderByCommand = orderByDBObjectBuilder.build(); 96 | 97 | assertTrue("The orderBy command should have a root $sort", orderByCommand.keySet().contains("$sort")); 98 | assertEquals("The value is not the expected", fakeOrderBy, orderByCommand.get("$sort")); 99 | 100 | // Basic build 101 | Whitebox.setInternalState(orderByDBObjectBuilder, "orderByQuery", fakeOrderBy); 102 | Whitebox.setInternalState(orderByDBObjectBuilder, "useAggregation", false); 103 | 104 | orderByCommand = orderByDBObjectBuilder.build(); 105 | 106 | assertFalse("The orderBy command should not have a root $sort", orderByCommand.keySet().contains("$sort")); 107 | assertEquals("The value is not the expected", fakeOrderBy, orderByCommand); 108 | 109 | } 110 | 111 | private OrderBy getOrderBy(String[] fields, OrderDirection[] direction) { 112 | List ids = new ArrayList<>(); 113 | for (int i = 0; i < fields.length; i++) { 114 | ids.add(new OrderByClause(direction[i], new ColumnSelector(new ColumnName(CATALOG, TABLE, fields[i])))); 115 | } 116 | 117 | Set operations = new HashSet<>(); 118 | operations.add(Operations.SELECT_GROUP_BY); 119 | 120 | return new OrderBy(operations, ids); 121 | } 122 | 123 | private OrderBy getOrderBy(String... fields) { 124 | OrderDirection[] orderDirection = new OrderDirection[fields.length]; 125 | for (int i = 0; i < fields.length; i++) { 126 | orderDirection[i] = OrderDirection.ASC; 127 | } 128 | return getOrderBy(fields, orderDirection); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/AggregationLogicalWorkflowExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query; 19 | 20 | import java.util.ArrayList; 21 | import java.util.Collections; 22 | 23 | import com.mongodb.AggregationOutput; 24 | import com.mongodb.DB; 25 | import com.mongodb.DBCollection; 26 | import com.mongodb.DBObject; 27 | import com.mongodb.MongoClient; 28 | import com.mongodb.MongoException; 29 | import com.stratio.connector.commons.connection.Connection; 30 | import com.stratio.connector.commons.engine.query.ProjectParsed; 31 | import com.stratio.connector.mongodb.core.engine.query.utils.LimitDBObjectBuilder; 32 | import com.stratio.connector.mongodb.core.engine.query.utils.MetaResultUtils; 33 | import com.stratio.connector.mongodb.core.exceptions.MongoExecutionException; 34 | import com.stratio.crossdata.common.data.ResultSet; 35 | import com.stratio.crossdata.common.exceptions.ExecutionException; 36 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 37 | import com.stratio.crossdata.common.logicalplan.Limit; 38 | import com.stratio.crossdata.common.metadata.Operations; 39 | 40 | import static com.stratio.connector.mongodb.core.configuration.ConfigurationOptions.DEFAULT_LIMIT; 41 | 42 | /** 43 | * The LogicalWorkflowExecutor for the MongoDB aggregation framework. 44 | */ 45 | public class AggregationLogicalWorkflowExecutor extends LogicalWorkflowExecutor { 46 | 47 | /** 48 | * Instantiates a new aggregation logical workflow executor. 49 | * 50 | * @param logicalWorkflowParsed 51 | * the logical workflow parsed 52 | * @throws ExecutionException 53 | * if the execution fails or if the query specified in the logical workflow is not supported 54 | * @throws UnsupportedException 55 | * if the specified operation is not supported. 56 | */ 57 | public AggregationLogicalWorkflowExecutor(ProjectParsed logicalWorkflowParsed) throws ExecutionException, 58 | UnsupportedException { 59 | super(logicalWorkflowParsed); 60 | } 61 | 62 | /* 63 | * (non-Javadoc) 64 | * 65 | * @see com.stratio.connector.mongodb.core.engine.query.LogicalWorkflowExecutor#buildQuery() 66 | */ 67 | @Override 68 | protected void buildQuery() throws ExecutionException, UnsupportedException { 69 | query = new ArrayList(); 70 | 71 | if (logicalWorkflowData.getProject() != null) { 72 | query.add(buildProject(true)); 73 | } 74 | if (!logicalWorkflowData.getFilter().isEmpty()) { 75 | query.add(buildFilter(true)); 76 | } 77 | if (logicalWorkflowData.getGroupBy() != null) { 78 | query.add(buildGroupBy()); 79 | } 80 | if (logicalWorkflowData.getOrderBy() != null) { 81 | query.add(buildOrderBy(true)); 82 | } 83 | if (logicalWorkflowData.getLimit() != null) { 84 | query.add(buildLimit()); 85 | } 86 | 87 | } 88 | 89 | /** 90 | * Execute an aggregation query. 91 | * 92 | * @param connection 93 | * the Connection that holds the MongoDB client. 94 | * @return the Crossdata ResultSet. 95 | * @throws ExecutionException 96 | * if the execution fails. 97 | */ 98 | @Override 99 | public ResultSet executeQuery(Connection connection) throws ExecutionException { 100 | MongoClient mongoClient = connection.getNativeConnection(); 101 | 102 | DB db = mongoClient.getDB(logicalWorkflowData.getProject().getCatalogName()); 103 | DBCollection collection = db.getCollection(logicalWorkflowData.getProject().getTableName().getName()); 104 | ResultSet resultSet = new ResultSet(); 105 | resultSet.setColumnMetadata(MetaResultUtils.createMetadata(logicalWorkflowData.getProject(), 106 | logicalWorkflowData.getSelect())); 107 | 108 | if (logicalWorkflowData.getLimit() == null){ 109 | int limit = Integer.parseInt(connection.getSessionObject(String.class, DEFAULT_LIMIT.getOptionName())); 110 | Limit limitObj = new Limit(Collections.singleton(Operations.SELECT_LIMIT),limit); 111 | query.add(new LimitDBObjectBuilder(limitObj).build()); 112 | } 113 | // AggregationOptions aggOptions = AggregationOptions.builder() 114 | // .allowDiskUse(true) 115 | // .batchSize(size) 116 | // pipeline,aggOptions => dbcursor 117 | try { 118 | int stage = 1; 119 | for (DBObject aggregationStage : query) { 120 | logger.debug("Aggregate framework stage (" + (stage++) + ") : " + aggregationStage); 121 | } 122 | AggregationOutput aggOutput = collection.aggregate(query); 123 | for (DBObject result : aggOutput.results()) { 124 | if (logger.isDebugEnabled()) { 125 | logger.debug("AggResult: " + result); 126 | } 127 | resultSet.add(MetaResultUtils.createRowWithAlias(result, logicalWorkflowData.getSelect())); 128 | } 129 | } catch (MongoException mongoException) { 130 | logger.error("Error executing an aggregation query:" + mongoException.getMessage()); 131 | throw new MongoExecutionException(mongoException.getMessage(), mongoException); 132 | } 133 | 134 | return resultSet; 135 | } 136 | 137 | } 138 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/metadata/UpdateDBObjectBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.metadata; 19 | 20 | import org.junit.Assert; 21 | import org.junit.Test; 22 | 23 | import com.mongodb.BasicDBObject; 24 | import com.stratio.crossdata.common.data.ColumnName; 25 | import com.stratio.crossdata.common.exceptions.ExecutionException; 26 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 27 | import com.stratio.crossdata.common.statements.structures.BooleanSelector; 28 | import com.stratio.crossdata.common.statements.structures.ColumnSelector; 29 | import com.stratio.crossdata.common.statements.structures.IntegerSelector; 30 | import com.stratio.crossdata.common.statements.structures.Operator; 31 | import com.stratio.crossdata.common.statements.structures.Relation; 32 | import com.stratio.crossdata.common.statements.structures.RelationSelector; 33 | import com.stratio.crossdata.common.statements.structures.Selector; 34 | import com.stratio.crossdata.common.statements.structures.StringSelector; 35 | 36 | public class UpdateDBObjectBuilderTest { 37 | 38 | private static final String COLLECTION_NAME = "coll_name"; 39 | private static final String DB_NAME = "db_name"; 40 | private static final String COLUMN_NAME = "row_name"; 41 | private static final String OTHER_COLUMN_NAME = "other_row_name"; 42 | private static final String STRING_VALUE = "cell_value"; 43 | private static final String STRING_VALUE_OTHER = "other_cell_value"; 44 | private static final Long LONG_VALUE = 25l; 45 | 46 | @Test 47 | public void addUpdateRelationtest() throws UnsupportedException, ExecutionException { 48 | 49 | UpdateDBObjectBuilder updateDBObjectBuilder = new UpdateDBObjectBuilder(); 50 | Assert.assertEquals("The update query should be empty", new BasicDBObject(), updateDBObjectBuilder.build()); 51 | 52 | Relation rel = getBasicRelation(COLUMN_NAME, Operator.ASSIGN, STRING_VALUE); 53 | updateDBObjectBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); 54 | 55 | BasicDBObject updatedExpected = new BasicDBObject(); 56 | updatedExpected.append("$set", new BasicDBObject(COLUMN_NAME, STRING_VALUE)); 57 | Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); 58 | 59 | // The following operation should not modify the query 60 | updateDBObjectBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); 61 | Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); 62 | 63 | // The following operation should modify the set 64 | updateDBObjectBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), new StringSelector( 65 | STRING_VALUE_OTHER)); 66 | updatedExpected.append("$set", new BasicDBObject(COLUMN_NAME, STRING_VALUE_OTHER)); 67 | Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); 68 | 69 | Relation rel2 = getBasicRelation(OTHER_COLUMN_NAME, Operator.SUBTRACT, 20l); 70 | updateDBObjectBuilder.addUpdateRelation(rel2.getLeftTerm(), rel2.getOperator(), rel2.getRightTerm()); 71 | updatedExpected.append("$inc", new BasicDBObject(OTHER_COLUMN_NAME, -20l)); 72 | Assert.assertEquals("The update query is not the expected", updatedExpected, updateDBObjectBuilder.build()); 73 | 74 | } 75 | 76 | /** 77 | * Method under test addUpdateRelation(). 78 | * 79 | * @throws UnsupportedException 80 | * the unsupported exception 81 | * @throws ExecutionException 82 | * the execution exception 83 | */ 84 | @Test 85 | public void addUpdateInnerRelationTest() throws UnsupportedException, ExecutionException { 86 | 87 | UpdateDBObjectBuilder updateDBObjectBuilder = new UpdateDBObjectBuilder(); 88 | 89 | Relation rel = getBasicRelation(OTHER_COLUMN_NAME, Operator.ASSIGN, 90 | getBasicRelation(OTHER_COLUMN_NAME, Operator.SUBTRACT, 20l)); 91 | 92 | updateDBObjectBuilder.addUpdateRelation(rel.getLeftTerm(), rel.getOperator(), rel.getRightTerm()); 93 | BasicDBObject updatedExpected = new BasicDBObject("$inc", new BasicDBObject(OTHER_COLUMN_NAME, -20l)); 94 | Assert.assertEquals("The inner relation processing is not the expected", updatedExpected, 95 | updateDBObjectBuilder.build()); 96 | 97 | } 98 | 99 | private Relation getBasicRelation(String column1, Operator assign, Object valueUpdated) { 100 | Selector leftSelector = new ColumnSelector(new ColumnName(DB_NAME, COLLECTION_NAME, column1)); 101 | leftSelector.setAlias(column1); 102 | Selector rightSelector = null; 103 | if (valueUpdated instanceof Integer) { 104 | rightSelector = new IntegerSelector((int) valueUpdated); 105 | } else if (valueUpdated instanceof String) { 106 | rightSelector = new StringSelector((String) valueUpdated); 107 | } else if (valueUpdated instanceof Boolean) { 108 | rightSelector = new BooleanSelector((Boolean) valueUpdated); 109 | } else if (valueUpdated instanceof Relation) { 110 | rightSelector = new RelationSelector((Relation) valueUpdated); 111 | } else if (valueUpdated instanceof Long) { 112 | rightSelector = new IntegerSelector((int) (long) valueUpdated); 113 | } 114 | return new Relation(leftSelector, assign, rightSelector); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/connection/ConnectionHandlerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.connection; 20 | 21 | import static org.junit.Assert.assertEquals; 22 | import static org.junit.Assert.assertFalse; 23 | import static org.junit.Assert.assertNotNull; 24 | import static org.junit.Assert.assertSame; 25 | import static org.mockito.Mockito.mock; 26 | import static org.mockito.Mockito.times; 27 | import static org.mockito.Mockito.verify; 28 | import static org.powermock.api.mockito.PowerMockito.whenNew; 29 | 30 | import java.util.HashMap; 31 | import java.util.Map; 32 | 33 | import com.stratio.connector.mongodb.core.configuration.ConfigurationOptions; 34 | import com.stratio.connector.mongodb.core.engine.metadata.DiscoverMetadataUtils; 35 | import org.junit.Before; 36 | import org.junit.Test; 37 | import org.junit.runner.RunWith; 38 | import org.mockito.Mock; 39 | import org.mockito.internal.util.reflection.Whitebox; 40 | import org.powermock.core.classloader.annotations.PowerMockIgnore; 41 | import org.powermock.core.classloader.annotations.PrepareForTest; 42 | import org.powermock.modules.junit4.PowerMockRunner; 43 | 44 | import com.mongodb.MongoClient; 45 | import com.stratio.connector.commons.connection.Connection; 46 | import com.stratio.crossdata.common.connector.ConnectorClusterConfig; 47 | import com.stratio.crossdata.common.connector.IConfiguration; 48 | import com.stratio.crossdata.common.data.ClusterName; 49 | import com.stratio.crossdata.common.exceptions.ExecutionException; 50 | import com.stratio.crossdata.common.security.ICredentials; 51 | 52 | import javax.security.auth.login.Configuration; 53 | 54 | @PowerMockIgnore( {"javax.management.*"}) 55 | @RunWith(PowerMockRunner.class) 56 | @PrepareForTest(value = { MongoConnectionHandler.class }) 57 | public class ConnectionHandlerTest { 58 | 59 | private static final String CLUSTER_NAME = "cluster_name"; 60 | private MongoConnectionHandler connectionHandler = null; 61 | @Mock 62 | private IConfiguration iConfiguration; 63 | 64 | @Before 65 | public void before() throws Exception { 66 | connectionHandler = new MongoConnectionHandler(iConfiguration); 67 | 68 | } 69 | 70 | @SuppressWarnings({ "rawtypes", "unchecked" }) 71 | @Test 72 | public void createDriverConnectionTest() throws Exception { 73 | 74 | ICredentials credentials = mock(ICredentials.class); 75 | Map options = new HashMap<>(); 76 | ConnectorClusterConfig config = new ConnectorClusterConfig(new ClusterName(CLUSTER_NAME), null, options); 77 | 78 | DriverConnection connection = mock(DriverConnection.class); 79 | whenNew(DriverConnection.class).withArguments(credentials, config).thenReturn(connection); 80 | 81 | connectionHandler.createConnection(credentials, config); 82 | 83 | Map mapConnection = (Map) Whitebox.getInternalState(connectionHandler, 84 | "connections"); 85 | 86 | DriverConnection recoveredConnection = (DriverConnection) mapConnection.get(CLUSTER_NAME); 87 | 88 | assertNotNull("The connection does not exist", recoveredConnection); 89 | assertEquals("The recovered connection is not the expected", connection, recoveredConnection); 90 | 91 | } 92 | 93 | public ConnectorClusterConfig initConnectorOptionsWithSampleProbability(String probability){ 94 | Map options = new HashMap<>(); 95 | Map connectorOptions = new HashMap<>(); 96 | connectorOptions.put(ConfigurationOptions.SAMPLE_PROBABILITY.getOptionName(), probability); 97 | ConnectorClusterConfig config = new ConnectorClusterConfig(new ClusterName(CLUSTER_NAME), connectorOptions, null); 98 | return config; 99 | } 100 | 101 | @Test 102 | public void recoveredSamplePropertyTest() throws Exception { 103 | String result = ""; 104 | ConnectorClusterConfig config = initConnectorOptionsWithSampleProbability("0.5"); 105 | result = DiscoverMetadataUtils.recoveredSampleProperty(config); 106 | assertEquals( "0.5",result); 107 | config = initConnectorOptionsWithSampleProbability(null); 108 | result = DiscoverMetadataUtils.recoveredSampleProperty(config); 109 | assertEquals("1", result); 110 | config = new ConnectorClusterConfig(new ClusterName(CLUSTER_NAME), null, null); 111 | result = DiscoverMetadataUtils.recoveredSampleProperty(config); 112 | assertEquals("1", result); 113 | 114 | } 115 | 116 | @SuppressWarnings("unchecked") 117 | @Test 118 | public void closeConnectionTest() throws Exception { 119 | 120 | Map mapConnection = (Map) Whitebox.getInternalState( 121 | connectionHandler, "connections"); 122 | DriverConnection connection = mock(DriverConnection.class); 123 | mapConnection.put(CLUSTER_NAME, connection); 124 | 125 | connectionHandler.closeConnection(CLUSTER_NAME); 126 | 127 | assertFalse("The connection " + CLUSTER_NAME + " should have been closed", 128 | mapConnection.containsKey(CLUSTER_NAME)); 129 | verify(connection, times(1)).close(); 130 | 131 | } 132 | 133 | @SuppressWarnings("unchecked") 134 | @Test 135 | public void getConnectionTest() throws ExecutionException { 136 | Map mapConnection = (Map) Whitebox.getInternalState( 137 | connectionHandler, "connections"); 138 | DriverConnection connection = mock(DriverConnection.class); 139 | mapConnection.put(CLUSTER_NAME, connection); 140 | 141 | Connection recoveredConnection = connectionHandler.getConnection(CLUSTER_NAME); 142 | 143 | assertNotNull("The connection does not exist", recoveredConnection); 144 | assertSame("The connection recovered is not the expected", connection, recoveredConnection); 145 | 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/query/utils/ProjectBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query.utils; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import java.util.*; 24 | 25 | import org.junit.Assert; 26 | import org.junit.Test; 27 | import org.mockito.internal.util.reflection.Whitebox; 28 | 29 | import com.mongodb.BasicDBObject; 30 | import com.mongodb.DBObject; 31 | import com.stratio.crossdata.common.data.ClusterName; 32 | import com.stratio.crossdata.common.data.ColumnName; 33 | import com.stratio.crossdata.common.data.TableName; 34 | import com.stratio.crossdata.common.exceptions.ExecutionException; 35 | import com.stratio.crossdata.common.logicalplan.Project; 36 | import com.stratio.crossdata.common.logicalplan.Select; 37 | import com.stratio.crossdata.common.metadata.ColumnType; 38 | import com.stratio.crossdata.common.metadata.DataType; 39 | import com.stratio.crossdata.common.metadata.Operations; 40 | import com.stratio.crossdata.common.statements.structures.ColumnSelector; 41 | import com.stratio.crossdata.common.statements.structures.Selector; 42 | 43 | public class ProjectBuilderTest { 44 | 45 | public static final String COLUMN_1 = "column1"; 46 | public static final String ALIAS_COLUMN_1 = "alias1"; 47 | public static final String COLUMN_2 = "column2"; 48 | public static final String ALIAS_COLUMN_2 = "alias2"; 49 | public static final String TABLE = "table_unit_test"; 50 | public static final String CATALOG = "catalog_unit_test"; 51 | public static final String CLUSTER_NAME = "cluster"; 52 | 53 | @Test 54 | public void projectDBObjectBuilderTest() throws ExecutionException { 55 | 56 | // Project without aggregation => expected the select columns 57 | List columns; 58 | columns = Arrays.asList(new ConnectorField(COLUMN_1, ALIAS_COLUMN_1, new ColumnType(DataType.VARCHAR))); 59 | Select select = getSelect(columns); 60 | Project project = getProject(COLUMN_1, COLUMN_2); 61 | ProjectDBObjectBuilder projectBuilder = new ProjectDBObjectBuilder(false, project, select); 62 | DBObject expectedProject = new BasicDBObject(COLUMN_1, 1); 63 | expectedProject.put("_id", 0); 64 | DBObject internalProjectDBObject = (DBObject) Whitebox.getInternalState(projectBuilder, "projectQuery"); 65 | 66 | Assert.assertEquals("The project is not the expected", expectedProject, internalProjectDBObject); 67 | 68 | // Project with aggregation => expected the project columns 69 | projectBuilder = new ProjectDBObjectBuilder(true, project, select); 70 | expectedProject = new BasicDBObject(COLUMN_1, 1); 71 | expectedProject.put(COLUMN_2, 1); 72 | expectedProject.put("_id", 0); 73 | internalProjectDBObject = (DBObject) Whitebox.getInternalState(projectBuilder, "projectQuery"); 74 | 75 | Assert.assertEquals("The project is not the expected", expectedProject, internalProjectDBObject); 76 | 77 | } 78 | 79 | private Project getProject(String... columnName) { 80 | Set operations = new HashSet<>(); 81 | operations.add(Operations.PROJECT); 82 | Project project = new Project(operations, new TableName(CATALOG, TABLE), new ClusterName(CLUSTER_NAME)); 83 | for (String col : columnName) { 84 | project.addColumn(new ColumnName(CATALOG, TABLE, col)); 85 | } 86 | 87 | return project; 88 | } 89 | 90 | @Test 91 | public void buildTest() throws Exception { 92 | List columns; 93 | columns = Arrays.asList(new ConnectorField(COLUMN_1, ALIAS_COLUMN_1, new ColumnType(DataType.VARCHAR))); 94 | Select select = getSelect(columns); 95 | Project project = getProject(COLUMN_1); 96 | ProjectDBObjectBuilder projectBuilder = new ProjectDBObjectBuilder(false, project, select); 97 | DBObject fakeProject = new BasicDBObject(COLUMN_1, 1); 98 | 99 | // Default build 100 | Whitebox.setInternalState(projectBuilder, "projectQuery", fakeProject); 101 | Whitebox.setInternalState(projectBuilder, "useAggregation", false); 102 | 103 | DBObject projectCommand = projectBuilder.build(); 104 | 105 | assertEquals("The value is not the expected", fakeProject, projectCommand); 106 | 107 | // Aggregation build 108 | Whitebox.setInternalState(projectBuilder, "useAggregation", true); 109 | 110 | projectCommand = projectBuilder.build(); 111 | 112 | assertTrue("The project command should have a root $project", projectCommand.keySet().contains("$project")); 113 | assertEquals("The value is not the expected", fakeProject, projectCommand.get("$project")); 114 | 115 | } 116 | 117 | private Select getSelect(List fields) { 118 | Select select; 119 | Map mapping = new LinkedHashMap<>(); 120 | Map types = new LinkedHashMap<>(); 121 | Map typeMapFormColumnName = new LinkedHashMap<>(); 122 | 123 | for (ConnectorField connectorField : fields) { 124 | ColumnSelector columnSelector = new ColumnSelector(new ColumnName(CATALOG, TABLE, connectorField.name)); 125 | mapping.put(columnSelector, connectorField.alias); 126 | types.put(connectorField.alias, connectorField.columnType); 127 | typeMapFormColumnName.put(columnSelector, connectorField.columnType); 128 | } 129 | 130 | Set operations = new HashSet<>(); 131 | operations.add(Operations.SELECT_OPERATOR); 132 | 133 | select = new Select(operations, mapping, types, typeMapFormColumnName); 134 | 135 | return select; 136 | 137 | } 138 | 139 | class ConnectorField { 140 | public String name; 141 | public String alias; 142 | public ColumnType columnType; 143 | 144 | public ConnectorField(String name, String alias, ColumnType columnType) { 145 | this.name = name; 146 | this.alias = alias; 147 | this.columnType = columnType; 148 | } 149 | 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/LogicalWorkflowExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.query; 20 | 21 | import java.util.List; 22 | 23 | import com.stratio.connector.commons.TimerJ; 24 | import com.stratio.connector.commons.connection.Connection; 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | 28 | import com.mongodb.DBObject; 29 | import com.mongodb.MongoClient; 30 | import com.stratio.connector.commons.engine.query.ProjectParsed; 31 | import com.stratio.connector.mongodb.core.engine.query.utils.FilterDBObjectBuilder; 32 | import com.stratio.connector.mongodb.core.engine.query.utils.GroupByDBObjectBuilder; 33 | import com.stratio.connector.mongodb.core.engine.query.utils.LimitDBObjectBuilder; 34 | import com.stratio.connector.mongodb.core.engine.query.utils.OrderByDBObjectBuilder; 35 | import com.stratio.connector.mongodb.core.engine.query.utils.ProjectDBObjectBuilder; 36 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 37 | import com.stratio.crossdata.common.data.ResultSet; 38 | import com.stratio.crossdata.common.exceptions.ExecutionException; 39 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 40 | 41 | /** 42 | * Prepares and performs MongoDB queries from a logical workflow. 43 | */ 44 | public abstract class LogicalWorkflowExecutor { 45 | 46 | /** The logger. */ 47 | protected final Logger logger = LoggerFactory.getLogger(this.getClass()); 48 | 49 | /** The logical workflow data. */ 50 | protected ProjectParsed logicalWorkflowData; 51 | 52 | /** The MongoDB query. */ 53 | protected List query = null; 54 | 55 | /** 56 | * Instantiates a new logical workflow executor ready for the execution. 57 | * 58 | * @param logicalWorkflowParsed 59 | * the logical workflow parsed. 60 | * @throws ExecutionException 61 | * if the execution fails or the query specified in the logical workflow is not supported. 62 | * @throws UnsupportedException 63 | * if the specified operation is not supported. 64 | */ 65 | public LogicalWorkflowExecutor(ProjectParsed logicalWorkflowParsed) throws ExecutionException, UnsupportedException { 66 | logicalWorkflowData = logicalWorkflowParsed; 67 | this.buildQuery(); 68 | } 69 | 70 | /** 71 | * Builds the query. 72 | * 73 | * @throws ExecutionException 74 | * if the execution fails or the query specified in the logical workflow is not supported. 75 | * @throws UnsupportedException 76 | * if the specified operation is not supported. 77 | */ 78 | @TimerJ 79 | protected abstract void buildQuery() throws ExecutionException, UnsupportedException; 80 | 81 | /** 82 | * Execute the query. 83 | * 84 | * @param connection 85 | * the Connection that holds the client. 86 | * @throws ExecutionException 87 | * if the execution fails or the query specified in the logical workflow is not supported. 88 | * @return the Crossdata ResultSet. 89 | */ 90 | public abstract ResultSet executeQuery(Connection connection) throws ExecutionException; 91 | 92 | /** 93 | * Builds the group by. 94 | * 95 | * @return the DB object. 96 | * @throws ExecutionException 97 | * if the execution fails. 98 | */ 99 | protected DBObject buildGroupBy() throws ExecutionException { 100 | 101 | GroupByDBObjectBuilder groupDBObject = new GroupByDBObjectBuilder(logicalWorkflowData.getGroupBy(), 102 | logicalWorkflowData.getSelect().getColumnMap().keySet()); 103 | return groupDBObject.build(); 104 | } 105 | 106 | /** 107 | * Builds the limit. 108 | * 109 | * @return the DB object. 110 | */ 111 | protected DBObject buildLimit() { 112 | LimitDBObjectBuilder limitDBObject = new LimitDBObjectBuilder(logicalWorkflowData.getLimit()); 113 | return limitDBObject.build(); 114 | } 115 | 116 | /** 117 | * Builds the project. 118 | * 119 | * @param aggregationRequired 120 | * whether the query use the aggregation framework or not. 121 | * @return the DB object. 122 | * @throws ExecutionException 123 | * if the project specified in the logical workflow is not supported 124 | */ 125 | protected DBObject buildProject(boolean aggregationRequired) throws ExecutionException { 126 | ProjectDBObjectBuilder projectDBObject = new ProjectDBObjectBuilder(aggregationRequired, 127 | logicalWorkflowData.getProject(), logicalWorkflowData.getSelect()); 128 | return projectDBObject.build(); 129 | } 130 | 131 | /** 132 | * Builds the filter. 133 | * 134 | * @param aggregationRequired 135 | * whether the query use the aggregation framework or not. 136 | * @return the DB object. 137 | * @throws MongoValidationException 138 | * if any filters specified in the logical workflow is not supported. 139 | * @throws UnsupportedException 140 | * if the specified filter operation is not supported 141 | */ 142 | protected DBObject buildFilter(boolean aggregationRequired) throws MongoValidationException, UnsupportedException { 143 | 144 | FilterDBObjectBuilder filterDBObjectBuilder = new FilterDBObjectBuilder(aggregationRequired, 145 | logicalWorkflowData.getFilter()); 146 | return filterDBObjectBuilder.build(); 147 | } 148 | 149 | /** 150 | * Builds the orderBy. 151 | * 152 | * @param aggregationRequired 153 | * whether the query use the aggregation framework or not. 154 | * @return the DB object. 155 | * @throws ExecutionException 156 | * if the execution fails. 157 | */ 158 | protected DBObject buildOrderBy(boolean aggregationRequired) throws ExecutionException { 159 | 160 | OrderByDBObjectBuilder orderByDBObjectBuilder = new OrderByDBObjectBuilder(aggregationRequired, 161 | logicalWorkflowData.getOrderBy()); 162 | return orderByDBObjectBuilder.build(); 163 | } 164 | 165 | } -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/query/utils/MetaResultUtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.query.utils; 19 | 20 | import java.util.*; 21 | 22 | import com.mongodb.BasicDBList; 23 | import org.junit.Assert; 24 | import org.junit.Test; 25 | import org.mockito.Mockito; 26 | 27 | import com.mongodb.BasicDBObject; 28 | import com.mongodb.DBObject; 29 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 30 | import com.stratio.crossdata.common.data.ColumnName; 31 | import com.stratio.crossdata.common.data.Row; 32 | import com.stratio.crossdata.common.data.TableName; 33 | import com.stratio.crossdata.common.exceptions.ExecutionException; 34 | import com.stratio.crossdata.common.logicalplan.Project; 35 | import com.stratio.crossdata.common.logicalplan.Select; 36 | import com.stratio.crossdata.common.metadata.ColumnMetadata; 37 | import com.stratio.crossdata.common.metadata.ColumnType; 38 | import com.stratio.crossdata.common.metadata.DataType; 39 | import com.stratio.crossdata.common.metadata.Operations; 40 | import com.stratio.crossdata.common.statements.structures.ColumnSelector; 41 | import com.stratio.crossdata.common.statements.structures.Selector; 42 | 43 | public class MetaResultUtilTest { 44 | 45 | public static final String COLUMN_1 = "column1"; 46 | public static final String COLUMN_2 = "column2"; 47 | public static final String COLUMN_3 = "column3"; 48 | public static final String ALIAS_COLUMN_1 = COLUMN_1 + "alias"; 49 | public static final String ALIAS_COLUMN_2 = COLUMN_2 + "alias"; 50 | public static final String ALIAS_COLUMN_3 = COLUMN_3 + "alias"; 51 | public static final String COLUMNS_VALUE = "value"; 52 | public static final String OTHER_COLUMN_VALUE = "other_value"; 53 | public static final String TABLE = "table_unit_test"; 54 | public static final String CATALOG = "catalog_unit_test"; 55 | public static final String VALUE_1 = "value1"; 56 | public static final String VALUE_2 = "value2"; 57 | 58 | 59 | @Test 60 | public void createRowWithAliasTest() throws ExecutionException { 61 | List columns; 62 | columns = Arrays.asList(new ConnectorField(COLUMN_1, ALIAS_COLUMN_1, new ColumnType(DataType.VARCHAR)), new ConnectorField( 63 | COLUMN_2, ALIAS_COLUMN_2, new ColumnType(DataType.VARCHAR)), new ConnectorField(COLUMN_3, ALIAS_COLUMN_3, 64 | new ColumnType(DataType.VARCHAR))); 65 | Select select = getSelect(columns); 66 | 67 | DBObject rowDBObject = new BasicDBObject(COLUMN_1, COLUMNS_VALUE); 68 | rowDBObject.put(COLUMN_2, OTHER_COLUMN_VALUE); 69 | 70 | Row rowWithAlias = MetaResultUtils.createRowWithAlias(rowDBObject, select); 71 | 72 | // Checking the number of columns returned 73 | Assert.assertEquals("There should be 3 columns", 3, rowWithAlias.size()); 74 | 75 | // Checking the alias and the value 76 | Assert.assertEquals(ALIAS_COLUMN_1 + " should be " + COLUMNS_VALUE, COLUMNS_VALUE, 77 | rowWithAlias.getCell(ALIAS_COLUMN_1).getValue()); 78 | Assert.assertEquals(ALIAS_COLUMN_2 + " should be " + OTHER_COLUMN_VALUE, OTHER_COLUMN_VALUE, rowWithAlias 79 | .getCell(ALIAS_COLUMN_2).getValue()); 80 | Assert.assertNull(ALIAS_COLUMN_3 + " should be null", rowWithAlias.getCell(ALIAS_COLUMN_3).getValue()); 81 | 82 | } 83 | 84 | @Test 85 | public void createMetadataTest() throws MongoValidationException { 86 | List columns; 87 | columns = Arrays.asList(new ConnectorField(COLUMN_1, ALIAS_COLUMN_1, new ColumnType(DataType.VARCHAR)), new ConnectorField( 88 | COLUMN_2, ALIAS_COLUMN_2, new ColumnType(DataType.VARCHAR)), new ConnectorField(COLUMN_3, ALIAS_COLUMN_3, 89 | new ColumnType(DataType.VARCHAR))); 90 | Select select = getSelect(columns); 91 | 92 | Project project = Mockito.mock(Project.class); 93 | Mockito.when(project.getCatalogName()).thenReturn(CATALOG); 94 | Mockito.when(project.getTableName()).thenReturn(new TableName(CATALOG, TABLE)); 95 | 96 | List columnsMetadata = MetaResultUtils.createMetadata(project, select); 97 | 98 | Assert.assertEquals("The number of metadata columns must be equal than the selected column", select 99 | .getColumnMap().size(), columnsMetadata.size()); 100 | 101 | Assert.assertEquals("The name is not the expected", COLUMN_1, columnsMetadata.get(0).getName().getName()); 102 | Assert.assertEquals("The type is not the expected", new ColumnType(DataType.VARCHAR), columnsMetadata.get(0).getColumnType()); 103 | Assert.assertEquals("The alias is not the expected", ALIAS_COLUMN_1, columnsMetadata.get(0).getName() 104 | .getAlias()); 105 | 106 | } 107 | 108 | 109 | private Select getSelect(List fields) { 110 | Select select; 111 | Map mapping = new LinkedHashMap<>(); 112 | Map types = new LinkedHashMap<>(); 113 | Map typeMapFormColumnName = new LinkedHashMap<>(); 114 | 115 | for (ConnectorField connectorField : fields) { 116 | ColumnSelector columnSelector = new ColumnSelector(new ColumnName(CATALOG, TABLE, connectorField.name)); 117 | mapping.put(columnSelector, connectorField.alias); 118 | types.put(connectorField.alias, connectorField.columnType); 119 | typeMapFormColumnName.put(columnSelector, connectorField.columnType); 120 | } 121 | 122 | Set operations = new HashSet<>(); 123 | operations.add(Operations.PROJECT); 124 | 125 | select = new Select(operations, mapping, types, typeMapFormColumnName); 126 | 127 | return select; 128 | 129 | } 130 | 131 | 132 | 133 | class ConnectorField { 134 | public String name; 135 | public String alias; 136 | public ColumnType columnType; 137 | 138 | public ConnectorField(String name, String alias, ColumnType columnType) { 139 | this.name = name; 140 | this.alias = alias; 141 | this.columnType = columnType; 142 | } 143 | 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /connector-mongodb/src/main/java/com/stratio/connector/mongodb/core/engine/query/utils/MetaResultUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.query.utils; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | import java.util.Map; 24 | import java.util.Map.Entry; 25 | 26 | import com.mongodb.BasicDBList; 27 | import com.mongodb.DBObject; 28 | import com.stratio.connector.commons.util.SelectorHelper; 29 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 30 | import com.stratio.crossdata.common.data.Cell; 31 | import com.stratio.crossdata.common.data.ColumnName; 32 | import com.stratio.crossdata.common.data.Row; 33 | import com.stratio.crossdata.common.exceptions.ExecutionException; 34 | import com.stratio.crossdata.common.logicalplan.Project; 35 | import com.stratio.crossdata.common.logicalplan.Select; 36 | import com.stratio.crossdata.common.metadata.ColumnMetadata; 37 | import com.stratio.crossdata.common.metadata.ColumnType; 38 | import com.stratio.crossdata.common.statements.structures.ColumnSelector; 39 | import com.stratio.crossdata.common.statements.structures.Selector; 40 | import com.stratio.crossdata.common.statements.structures.SelectorType; 41 | import org.bson.types.ObjectId; 42 | 43 | /** 44 | * The utility class MetaResultUtils. 45 | * 46 | */ 47 | public final class MetaResultUtils { 48 | 49 | private MetaResultUtils() { 50 | } 51 | 52 | /** 53 | * Creates a row with alias from a Mongo result. A null value is inserted if any field of the row is missing. 54 | * 55 | * @param rowDBObject 56 | * a bson containing the result. 57 | * @param select 58 | * the select 59 | * @return the row. 60 | * @throws ExecutionException 61 | * if the conditions specified in the logical workflow are not supported 62 | */ 63 | public static Row createRowWithAlias(DBObject rowDBObject, Select select) throws ExecutionException { 64 | 65 | Row row = new Row(); 66 | Map aliasMapping = select.getColumnMap(); 67 | 68 | String field; 69 | for (Entry colInfo : aliasMapping.entrySet()) { 70 | field = (String) SelectorHelper.getRestrictedValue(colInfo.getKey(), SelectorType.COLUMN); 71 | Object value = rowDBObject.get(field); 72 | 73 | value = castValue(value, select.getTypeMapFromColumnName().get(colInfo.getKey())); 74 | if (colInfo.getValue() != null) { 75 | field = colInfo.getValue(); 76 | } 77 | 78 | value = convertIfRequieredObjectId(value); 79 | 80 | row.addCell(field, new Cell(value)); 81 | } 82 | 83 | return row; 84 | } 85 | 86 | private static Object convertIfRequieredObjectId(Object value) { 87 | if (value instanceof ObjectId){ 88 | value = value.toString(); 89 | } 90 | return value; 91 | } 92 | 93 | /** 94 | * Cast the value according to column type. 95 | * 96 | * @param value 97 | * the value casted 98 | * @param columnType 99 | * the column type 100 | */ 101 | private static Object castValue(Object value, ColumnType columnType) { 102 | if(value == null) 103 | return value; 104 | Object castedValue = value; 105 | switch (columnType.getDataType()) { 106 | case FLOAT: 107 | castedValue = ((Double) value).floatValue(); 108 | break; 109 | case NATIVE: 110 | castedValue = value.toString(); 111 | break; 112 | case SET: 113 | case LIST: 114 | case MAP: 115 | break; 116 | default: 117 | break; 118 | } 119 | return castedValue; 120 | 121 | } 122 | 123 | /** 124 | * Creates the column metadata. 125 | * 126 | * @param projection 127 | * the projection 128 | * @param select 129 | * the select 130 | * @return the columns metadata 131 | * @throws MongoValidationException 132 | * if the conditions specified in the logical workflow are not supported 133 | */ 134 | public static List createMetadata(Project projection, Select select) 135 | throws MongoValidationException { 136 | List columnsMetadata = new ArrayList<>(); 137 | 138 | for (Entry columnMap : select.getColumnMap().entrySet()) { 139 | // TODO check if it is necessary 140 | ColumnName colName; 141 | if (columnMap.getKey().getType() == SelectorType.COLUMN) { 142 | colName = ((ColumnSelector) columnMap.getKey()).getColumnName(); 143 | } else { 144 | throw new MongoValidationException("The selector must be a column name"); 145 | } 146 | colName.setAlias(columnMap.getValue()); 147 | ColumnType colType = select.getTypeMapFromColumnName().get(columnMap.getKey()); 148 | colType = updateColumnType(colType); 149 | columnsMetadata.add(new ColumnMetadata(colName, null, colType)); 150 | } 151 | 152 | return columnsMetadata; 153 | } 154 | 155 | /** 156 | * Update column type. 157 | * 158 | * @param colType 159 | * the col type 160 | * @return the column type 161 | */ 162 | private static ColumnType updateColumnType(ColumnType colType) { 163 | String dbType; 164 | switch (colType.getDataType()) { 165 | case FLOAT: 166 | break; 167 | case SET: 168 | case LIST: 169 | dbType = colType.getODBCType(); 170 | colType.setDBMapping(dbType, List.class); 171 | colType.setDBCollectionType(updateColumnType(colType.getDBInnerType())); 172 | break; 173 | case MAP: 174 | dbType = colType.getODBCType(); 175 | colType.setDBMapping(dbType, Map.class); 176 | colType.setDBMapType((updateColumnType(colType.getDBInnerType())), 177 | updateColumnType(colType.getDBInnerValueType())); 178 | break; 179 | case BIGINT: 180 | case BOOLEAN: 181 | case DOUBLE: 182 | case INT: 183 | case TEXT: 184 | case VARCHAR: 185 | default: 186 | break; 187 | } 188 | return colType; 189 | 190 | } 191 | 192 | } 193 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/metadata/IndexUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package com.stratio.connector.mongodb.core.engine.metadata; 20 | 21 | import static org.junit.Assert.assertEquals; 22 | 23 | import org.junit.Test; 24 | 25 | import com.mongodb.DBObject; 26 | import com.stratio.connector.commons.metadata.IndexMetadataBuilder; 27 | import com.stratio.connector.mongodb.core.configuration.CustomMongoIndexType; 28 | import com.stratio.connector.mongodb.core.configuration.IndexOptions; 29 | import com.stratio.connector.mongodb.core.exceptions.MongoValidationException; 30 | import com.stratio.crossdata.common.metadata.ColumnType; 31 | import com.stratio.crossdata.common.metadata.DataType; 32 | import com.stratio.crossdata.common.metadata.IndexMetadata; 33 | import com.stratio.crossdata.common.metadata.IndexType; 34 | 35 | /** 36 | * @author david 37 | */ 38 | public class IndexUtilsTest { 39 | 40 | private static final String DB_NAME = "catalog_name"; 41 | private static final String TABLE_NAME = "tablename"; 42 | private final String INDEX_NAME = "indexname"; 43 | private final IndexType INDEX_DEFAULT = IndexType.DEFAULT; 44 | private final ColumnType COLUMN_VARCHAR = new ColumnType(DataType.VARCHAR); 45 | private final String COLUMN_NAME = "colname"; 46 | private final String COLUMN_NAME2 = "colname2"; 47 | 48 | @Test 49 | public void getBasicCustomOptionsTest() throws MongoValidationException { 50 | IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, INDEX_DEFAULT); 51 | indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); 52 | IndexMetadata indexMetadata = indexMetaBuilder.build(); 53 | 54 | DBObject object = IndexUtils.getCustomOptions(indexMetadata); 55 | 56 | assertEquals("The index name is not the expected", object.get("name"), INDEX_NAME); 57 | 58 | } 59 | 60 | @Test 61 | public void getCustomOptionsTest() throws MongoValidationException { 62 | IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, INDEX_DEFAULT); 63 | indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); 64 | indexMetaBuilder.addOption(IndexOptions.SPARSE.getOptionName(), true).addOption( 65 | IndexOptions.UNIQUE.getOptionName(), false); 66 | IndexMetadata indexMetadata = indexMetaBuilder.build(); 67 | 68 | DBObject object = IndexUtils.getCustomOptions(indexMetadata); 69 | 70 | assertEquals("The index name is not the expected", object.get("name"), INDEX_NAME); 71 | assertEquals("The 'sparse' property should be true", object.get("sparse"), true); 72 | assertEquals("The 'unique' property should be false", object.get("unique"), false); 73 | 74 | } 75 | 76 | @Test 77 | public void getIndexDBObjectDefaultTest() throws MongoValidationException { 78 | IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, INDEX_DEFAULT); 79 | indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); 80 | indexMetaBuilder.addColumn(COLUMN_NAME2, COLUMN_VARCHAR); 81 | IndexMetadata indexMetadata = indexMetaBuilder.build(); 82 | 83 | DBObject object = IndexUtils.getIndexDBObject(indexMetadata); 84 | 85 | assertEquals("The value of the default index column should be 1", 1, object.get(COLUMN_NAME)); 86 | assertEquals("The value of the default index column should be 1", 1, object.get(COLUMN_NAME2)); 87 | 88 | } 89 | 90 | @Test 91 | public void getIndexDBObjectFullTextTest() throws MongoValidationException { 92 | IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, 93 | IndexType.FULL_TEXT); 94 | indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); 95 | IndexMetadata indexMetadata = indexMetaBuilder.build(); 96 | 97 | DBObject object = IndexUtils.getIndexDBObject(indexMetadata); 98 | 99 | assertEquals("The value of the full-text index column should be 'text'", "text", object.get(COLUMN_NAME)); 100 | 101 | } 102 | 103 | @Test 104 | public void getHashedIndexDBObjectTest() throws MongoValidationException { 105 | IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, 106 | IndexType.CUSTOM); 107 | indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); 108 | indexMetaBuilder.addOption(IndexOptions.INDEX_TYPE.getOptionName(), CustomMongoIndexType.HASHED.getIndexType()); 109 | IndexMetadata indexMetadata = indexMetaBuilder.build(); 110 | 111 | DBObject object = IndexUtils.getIndexDBObject(indexMetadata); 112 | assertEquals("The value of the hashed index column should be 'hashed'", "hashed", object.get(COLUMN_NAME)); 113 | } 114 | 115 | @Test 116 | public void getCustomDescendIndexDBObjectTest() throws MongoValidationException { 117 | IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, 118 | IndexType.CUSTOM); 119 | indexMetaBuilder.addColumn(COLUMN_NAME, COLUMN_VARCHAR); 120 | indexMetaBuilder.addOption(IndexOptions.INDEX_TYPE.getOptionName(), 121 | CustomMongoIndexType.COMPOUND.getIndexType()); 122 | indexMetaBuilder.addOption(IndexOptions.COMPOUND_FIELDS.getOptionName(), COLUMN_NAME + ":desc"); 123 | IndexMetadata indexMetadata = indexMetaBuilder.build(); 124 | 125 | DBObject object = IndexUtils.getIndexDBObject(indexMetadata); 126 | 127 | assertEquals("The value of the descending index column should be '-1'", -1, object.get(COLUMN_NAME)); 128 | 129 | } 130 | 131 | @Test 132 | public void getCustomCompoundIndexDBObjectTest() throws MongoValidationException { 133 | IndexMetadataBuilder indexMetaBuilder = new IndexMetadataBuilder(DB_NAME, TABLE_NAME, INDEX_NAME, 134 | IndexType.CUSTOM); 135 | indexMetaBuilder.addOption(IndexOptions.INDEX_TYPE.getOptionName(), 136 | CustomMongoIndexType.COMPOUND.getIndexType()); 137 | indexMetaBuilder.addOption(IndexOptions.COMPOUND_FIELDS.getOptionName(), COLUMN_NAME + ":desc," + COLUMN_NAME2 138 | + ":asc"); 139 | IndexMetadata indexMetadata = indexMetaBuilder.build(); 140 | 141 | DBObject object = IndexUtils.getIndexDBObject(indexMetadata); 142 | 143 | assertEquals("The value of the descending index " + COLUMN_NAME + " should be '-1'", -1, 144 | object.get(COLUMN_NAME)); 145 | assertEquals("The value of the ascending index " + COLUMN_NAME2 + " should be '1'", 1, object.get(COLUMN_NAME2)); 146 | 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /connector-mongodb/src/test/java/com/stratio/connector/mongodb/core/engine/metadata/ShardUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to STRATIO (C) under one or more contributor license agreements. 3 | * See the NOTICE file distributed with this work for additional information 4 | * regarding copyright ownership. The STRATIO (C) licenses this file 5 | * to you under the Apache License, Version 2.0 (the 6 | * "License"); you may not use this file except in compliance 7 | * with the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package com.stratio.connector.mongodb.core.engine.metadata; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import org.junit.Assert; 24 | import org.junit.Test; 25 | import org.junit.runner.RunWith; 26 | import org.mockito.Matchers; 27 | import org.mockito.Mock; 28 | import org.mockito.Mockito; 29 | import org.powermock.api.mockito.PowerMockito; 30 | import org.powermock.core.classloader.annotations.PowerMockIgnore; 31 | import org.powermock.core.classloader.annotations.PrepareForTest; 32 | import org.powermock.modules.junit4.PowerMockRunner; 33 | 34 | import com.mongodb.BasicDBObject; 35 | import com.mongodb.CommandResult; 36 | import com.mongodb.DB; 37 | import com.mongodb.MongoClient; 38 | import com.stratio.connector.commons.metadata.TableMetadataBuilder; 39 | import com.stratio.connector.mongodb.core.configuration.ShardKeyType; 40 | import com.stratio.connector.mongodb.core.configuration.TableOptions; 41 | import com.stratio.crossdata.common.exceptions.ExecutionException; 42 | import com.stratio.crossdata.common.exceptions.UnsupportedException; 43 | import com.stratio.crossdata.common.statements.structures.BooleanSelector; 44 | import com.stratio.crossdata.common.statements.structures.Selector; 45 | import com.stratio.crossdata.common.statements.structures.StringSelector; 46 | @PowerMockIgnore( {"javax.management.*"}) 47 | @RunWith(PowerMockRunner.class) 48 | @PrepareForTest(value = { MongoClient.class, DB.class, SelectorOptionsUtils.class }) 49 | public class ShardUtilsTest { 50 | 51 | private static final String DB_NAME = "catalog_name"; 52 | private static final String COLLECTION_NAME = "tablename"; 53 | private static final String CLUSTER_NAME = "clustername"; 54 | 55 | private final String COLUMN_NAME = "colname"; 56 | private final String COLUMN_NAME2 = "colname"; 57 | 58 | @Mock 59 | MongoClient client; 60 | @Mock 61 | DB database; 62 | 63 | @Test 64 | public void isCollectionShardedTest() throws ExecutionException { 65 | Map options; 66 | options = new HashMap<>(); 67 | 68 | Assert.assertFalse("The result should be false by default", ShardUtils.isCollectionSharded(options)); 69 | 70 | options.put(TableOptions.SHARDING_ENABLED.getOptionName(), new BooleanSelector(true)); 71 | Assert.assertTrue("The result should be true", ShardUtils.isCollectionSharded(options)); 72 | 73 | options.put(TableOptions.SHARDING_ENABLED.getOptionName(), new StringSelector("true")); 74 | Assert.assertTrue("The result should be true", ShardUtils.isCollectionSharded(options)); 75 | 76 | } 77 | 78 | @Test 79 | public void shardCollectionDefaultTest() throws ExecutionException, UnsupportedException { 80 | // 81 | TableMetadataBuilder tableMetadataBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); 82 | Mockito.when(client.getDB("admin")).thenReturn(database); 83 | CommandResult commandRes = Mockito.mock(CommandResult.class); 84 | Mockito.when(commandRes.ok()).thenReturn(true); 85 | Mockito.when(database.command(Matchers.any(BasicDBObject.class))).thenReturn(commandRes); 86 | PowerMockito.mockStatic(SelectorOptionsUtils.class); 87 | Map options = new HashMap<>(); 88 | Mockito.when(SelectorOptionsUtils.processOptions(Matchers.anyMap())).thenReturn(options); 89 | ShardUtils.shardCollection(client, tableMetadataBuilder.build()); 90 | 91 | Mockito.verify(database, Mockito.times(1)).command(new BasicDBObject("enableSharding", DB_NAME)); 92 | BasicDBObject commandExpected = new BasicDBObject("shardCollection", DB_NAME + "." + COLLECTION_NAME); 93 | commandExpected.put("key", new BasicDBObject("_id", 1)); 94 | Mockito.verify(database, Mockito.times(1)).command(commandExpected); 95 | 96 | } 97 | 98 | @Test 99 | public void shardCollectionHashedTest() throws ExecutionException, UnsupportedException { 100 | // 101 | TableMetadataBuilder tableMetadataBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); 102 | Mockito.when(client.getDB("admin")).thenReturn(database); 103 | CommandResult commandRes = Mockito.mock(CommandResult.class); 104 | Mockito.when(commandRes.ok()).thenReturn(true); 105 | Mockito.when(database.command(Matchers.any(BasicDBObject.class))).thenReturn(commandRes); 106 | PowerMockito.mockStatic(SelectorOptionsUtils.class); 107 | Map options = new HashMap<>(); 108 | options.put(TableOptions.SHARD_KEY_TYPE.getOptionName(), new StringSelector(ShardKeyType.HASHED.getKeyType())); 109 | options.put(TableOptions.SHARD_KEY_FIELDS.getOptionName(), new StringSelector(COLUMN_NAME)); 110 | Mockito.when(SelectorOptionsUtils.processOptions(Matchers.anyMap())).thenReturn(options); 111 | 112 | ShardUtils.shardCollection(client, tableMetadataBuilder.build()); 113 | 114 | Mockito.verify(database, Mockito.times(1)).command(new BasicDBObject("enableSharding", DB_NAME)); 115 | BasicDBObject commandExpected = new BasicDBObject("shardCollection", DB_NAME + "." + COLLECTION_NAME); 116 | commandExpected.put("key", new BasicDBObject(COLUMN_NAME, "hashed")); 117 | Mockito.verify(database, Mockito.times(1)).command(commandExpected); 118 | 119 | } 120 | 121 | @Test 122 | public void shardCollectionMultiFieldsTest() throws ExecutionException, UnsupportedException { 123 | // 124 | TableMetadataBuilder tableMetadataBuilder = new TableMetadataBuilder(DB_NAME, COLLECTION_NAME, CLUSTER_NAME); 125 | Mockito.when(client.getDB("admin")).thenReturn(database); 126 | CommandResult commandRes = Mockito.mock(CommandResult.class); 127 | Mockito.when(commandRes.ok()).thenReturn(true); 128 | Mockito.when(database.command(Matchers.any(BasicDBObject.class))).thenReturn(commandRes); 129 | PowerMockito.mockStatic(SelectorOptionsUtils.class); 130 | Map options = new HashMap<>(); 131 | options.put(TableOptions.SHARD_KEY_TYPE.getOptionName(), new StringSelector(ShardKeyType.ASC.getKeyType())); 132 | options.put(TableOptions.SHARD_KEY_FIELDS.getOptionName(), new StringSelector(COLUMN_NAME + "," + COLUMN_NAME2)); 133 | Mockito.when(SelectorOptionsUtils.processOptions(Matchers.anyMap())).thenReturn(options); 134 | 135 | ShardUtils.shardCollection(client, tableMetadataBuilder.build()); 136 | 137 | Mockito.verify(database, Mockito.times(1)).command(new BasicDBObject("enableSharding", DB_NAME)); 138 | BasicDBObject keys = new BasicDBObject(COLUMN_NAME, 1); 139 | keys.append(COLUMN_NAME2, 1); 140 | BasicDBObject commandExpected = new BasicDBObject("shardCollection", DB_NAME + "." + COLLECTION_NAME); 141 | commandExpected.put("key", keys); 142 | Mockito.verify(database, Mockito.times(1)).command(commandExpected); 143 | 144 | } 145 | } 146 | --------------------------------------------------------------------------------