RYA-17, RYA-19, RYA-20 issue with mongo deletes, typo in constructor, redundant 
indices


Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/1007611e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/1007611e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/1007611e

Branch: refs/heads/develop
Commit: 1007611ee5955ff1371f50dbe9c646f6452f290e
Parents: 1eae901
Author: pujav65 <[email protected]>
Authored: Wed Dec 16 23:23:25 2015 -0500
Committer: Aaron Mihalik <[email protected]>
Committed: Tue Dec 22 11:48:32 2015 -0500

----------------------------------------------------------------------
 dao/mongodb.rya/pom.xml                         |   5 +
 .../java/mvm/rya/mongodb/MongoDBRyaDAO.java     |  44 ++++++-
 .../dao/SimpleMongoDBStorageStrategy.java       |   9 +-
 .../java/mvm/rya/mongodb/MongoDBRyaDAOTest.java | 121 +++++++++++++++++++
 4 files changed, 167 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/1007611e/dao/mongodb.rya/pom.xml
----------------------------------------------------------------------
diff --git a/dao/mongodb.rya/pom.xml b/dao/mongodb.rya/pom.xml
index 0d87fa5..b7d0c0e 100644
--- a/dao/mongodb.rya/pom.xml
+++ b/dao/mongodb.rya/pom.xml
@@ -43,6 +43,11 @@ under the License.
             <groupId>de.flapdoodle.embed</groupId>
             <artifactId>de.flapdoodle.embed.mongo</artifactId>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/1007611e/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java
----------------------------------------------------------------------
diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java 
b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java
index 1f341dc..b9124e3 100644
--- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java
+++ b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/MongoDBRyaDAO.java
@@ -69,18 +69,39 @@ public class MongoDBRyaDAO implements 
RyaDAO<MongoDBRdfConfiguration>{
        
        public MongoDBRyaDAO(MongoDBRdfConfiguration conf) throws 
RyaDAOException{
                this.conf = conf;
+               initConnection();
+               init();
+       }
+
+       
+       public MongoDBRyaDAO(MongoDBRdfConfiguration conf, MongoClient 
mongoClient) throws RyaDAOException{
+               this.conf = conf;
+               this.mongoClient = mongoClient;
                init();
        }
 
        public void setConf(MongoDBRdfConfiguration conf) {
                this.conf = conf;
        }
+       
+       public void setMongoClient(MongoClient mongoClient) {
+               this.mongoClient = mongoClient;
+       }
+
+       public void setDB(DB db) {
+               this.db = db;
+       }
+
+       
+       public void setDBCollection(DBCollection coll) {
+               this.coll = coll;
+       }
 
     public MongoDBRdfConfiguration getConf() {
         return conf;
     }
 
-    public void init() throws RyaDAOException {
+    public void initConnection() throws RyaDAOException {
         try {
             boolean useMongoTest = conf.getUseTestMongo();
             if (useMongoTest) {
@@ -94,13 +115,26 @@ public class MongoDBRyaDAO implements 
RyaDAO<MongoDBRdfConfiguration>{
                 if (conf.get(MongoDBRdfConfiguration.MONGO_USER) != null) {
                     MongoCredential cred = MongoCredential.createCredential(
                             conf.get(MongoDBRdfConfiguration.MONGO_USER),
-                            
conf.get(MongoDBRdfConfiguration.MONGO_USER_PASSWORD),
-                            
conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME).toCharArray());
+                            conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME),
+                            
conf.get(MongoDBRdfConfiguration.MONGO_USER_PASSWORD).toCharArray());
                     mongoClient = new MongoClient(server, Arrays.asList(cred));
                 } else {
                     mongoClient = new MongoClient(server);
                 }
             }
+        } catch (UnknownHostException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+
+    }
+
+    
+    public void init() throws RyaDAOException {
+        try {
             secondaryIndexers = conf.getAdditionalIndexers();
             for(RyaSecondaryIndexer index: secondaryIndexers) {
                 index.setConf(conf);
@@ -179,7 +213,7 @@ public class MongoDBRyaDAO implements 
RyaDAO<MongoDBRdfConfiguration>{
 
        public void delete(RyaStatement statement, MongoDBRdfConfiguration conf)
                        throws RyaDAOException {
-               DBObject obj = storageStrategy.serialize(statement);
+               DBObject obj = storageStrategy.getQuery(statement);
                coll.remove(obj);
        }
 
@@ -192,7 +226,7 @@ public class MongoDBRyaDAO implements 
RyaDAO<MongoDBRdfConfiguration>{
                        MongoDBRdfConfiguration conf) throws RyaDAOException {
                while (statements.hasNext()){
                        RyaStatement ryaStatement = statements.next();
-                       coll.remove(storageStrategy.serialize(ryaStatement));
+                       coll.remove(storageStrategy.getQuery(ryaStatement));
                }
                
        }

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/1007611e/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java
----------------------------------------------------------------------
diff --git 
a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java
 
b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java
index 24d16c1..3ecc0dc 100644
--- 
a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java
+++ 
b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java
@@ -52,17 +52,12 @@ public class SimpleMongoDBStorageStrategy implements 
MongoDBStorageStrategy {
        
        @Override
        public void createIndices(DBCollection coll){
-               coll.createIndex("subject");
-               coll.createIndex("predicate");
                BasicDBObject doc = new BasicDBObject();
            doc.put(SUBJECT, 1);
            doc.put(PREDICATE, 1);
                coll.createIndex(doc);
-               doc = new BasicDBObject(OBJECT, 1);
-               doc.put(OBJECT_TYPE, 1);
-               doc.put(PREDICATE, 1);
-               coll.createIndex(doc);
-               doc = new BasicDBObject(OBJECT, 1);
+               doc = new BasicDBObject(PREDICATE, 1);
+               doc.put(OBJECT, 1);
                doc.put(OBJECT_TYPE, 1);
                coll.createIndex(doc);
                doc = new BasicDBObject(OBJECT, 1);

http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/1007611e/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java
----------------------------------------------------------------------
diff --git 
a/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java 
b/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java
new file mode 100644
index 0000000..3d900b0
--- /dev/null
+++ b/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java
@@ -0,0 +1,121 @@
+package mvm.rya.mongodb;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import mvm.rya.api.RdfCloudTripleStoreConfiguration;
+import mvm.rya.api.domain.RyaStatement;
+import mvm.rya.api.domain.RyaStatement.RyaStatementBuilder;
+import mvm.rya.api.domain.RyaURI;
+import mvm.rya.api.persist.RyaDAOException;
+
+import org.apache.hadoop.conf.Configuration;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.MongoClient;
+import com.mongodb.MongoException;
+
+import de.flapdoodle.embed.mongo.distribution.Version;
+import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
+
+public class MongoDBRyaDAOTest {
+       
+       private MongodForTestsFactory testsFactory;
+       private MongoDBRyaDAO dao;
+       private MongoDBRdfConfiguration configuration;
+       private MongoClient mongoClient;
+       
+       @Before
+       public void setUp() throws IOException, RyaDAOException{
+               testsFactory = 
MongodForTestsFactory.with(Version.Main.PRODUCTION);
+              Configuration conf = new Configuration();
+               conf.set(MongoDBRdfConfiguration.USE_TEST_MONGO, "true");
+               conf.set(MongoDBRdfConfiguration.MONGO_DB_NAME, "test");
+               conf.set(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, 
"rya_");
+               conf.set(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX, 
"rya_");
+               configuration = new MongoDBRdfConfiguration(conf);
+                       mongoClient = testsFactory.newMongo();
+            int port = mongoClient.getServerAddressList().get(0).getPort();
+            configuration.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, 
Integer.toString(port));
+                       dao = new MongoDBRyaDAO(configuration, mongoClient);
+               
+       }
+
+       @Test
+       public void testDeleteWildcard() throws RyaDAOException {
+               RyaStatementBuilder builder = new RyaStatementBuilder();
+               builder.setPredicate(new RyaURI("http://temp.com";));
+               dao.delete(builder.build(), configuration);
+       }
+       
+       
+       @Test
+       public void testAdd() throws RyaDAOException, MongoException, 
IOException {
+               RyaStatementBuilder builder = new RyaStatementBuilder();
+               builder.setPredicate(new RyaURI("http://temp.com";));
+               builder.setSubject(new RyaURI("http://subject.com";));
+               builder.setObject(new RyaURI("http://object.com";));
+               
+               DB db = 
mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
+        DBCollection coll = 
db.getCollection(configuration.getTriplesCollectionName());
+          
+               dao.add(builder.build());
+
+        assertEquals(coll.count(),1);
+               
+       }
+       
+       @Test
+       public void testDelete() throws RyaDAOException, MongoException, 
IOException {
+               RyaStatementBuilder builder = new RyaStatementBuilder();
+               builder.setPredicate(new RyaURI("http://temp.com";));
+               builder.setSubject(new RyaURI("http://subject.com";));
+               builder.setObject(new RyaURI("http://object.com";));
+               RyaStatement statement = builder.build();
+               
+               DB db = 
mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
+        DBCollection coll = 
db.getCollection(configuration.getTriplesCollectionName());
+          
+               dao.add(statement);
+
+        assertEquals(coll.count(),1);
+               
+        dao.delete(statement, configuration);
+        
+        assertEquals(coll.count(),0);
+
+       }
+
+       @Test
+       public void testDeleteWildcardSubjectWithContext() throws 
RyaDAOException, MongoException, IOException {
+               RyaStatementBuilder builder = new RyaStatementBuilder();
+               builder.setPredicate(new RyaURI("http://temp.com";));
+               builder.setSubject(new RyaURI("http://subject.com";));
+               builder.setObject(new RyaURI("http://object.com";));
+               builder.setContext(new RyaURI("http://context.com";));
+               RyaStatement statement = builder.build();
+               
+               DB db = 
mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
+        DBCollection coll = 
db.getCollection(configuration.getTriplesCollectionName());
+          
+               dao.add(statement);
+
+        assertEquals(coll.count(),1);
+        
+               RyaStatementBuilder builder2 = new RyaStatementBuilder();
+               builder2.setPredicate(new RyaURI("http://temp.com";));
+               builder2.setObject(new RyaURI("http://object.com";));
+               builder2.setContext(new RyaURI("http://context3.com";));
+               RyaStatement query = builder2.build();
+               
+        dao.delete(query, configuration);
+        
+        assertEquals(coll.count(),1);
+
+       }
+
+}

Reply via email to