[metcat] r197 committed - orientdb features added

6 views
Skip to first unread message

metcat.apach...@codespot.com

unread,
Aug 29, 2012, 4:53:39 AM8/29/12
to metca...@googlegroups.com
Revision: 197
Author: uisurumad...@gmail.com
Date: Wed Aug 29 01:53:20 2012
Log: orientdb features added
http://code.google.com/a/apache-extras.org/p/metcat/source/detail?r=197

Modified:

/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/GraphDataModel.java

/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/GraphModelProperties.java

/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/OrientGraphDataModel.java

=======================================
---
/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/GraphDataModel.java
Thu Aug 23 17:55:36 2012
+++
/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/GraphDataModel.java
Wed Aug 29 01:53:20 2012
@@ -1,5 +1,32 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
package org.apache.airavata.metcat.db;

+/**
+ * Abstract class for any graph database attached to the MetCat.
+ *
+ * @author Isuru Madushanka
+ *
+ */
public abstract class GraphDataModel {

}
=======================================
---
/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/GraphModelProperties.java
Thu Aug 23 17:55:36 2012
+++
/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/GraphModelProperties.java
Wed Aug 29 01:53:20 2012
@@ -1,3 +1,24 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
package org.apache.airavata.metcat.db;

import java.io.FileInputStream;
@@ -9,6 +30,12 @@
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

+/**
+ * Object resposible for loading configurations regarding graph database.
+ *
+ * @author Isuru Madushanka
+ *
+ */
public class GraphModelProperties {

private final Logger logger = LogManager.getLogger(ModelProperties.class);
@@ -76,18 +103,35 @@
this.orientdbUsername =
properties.getProperty(ORIENTDB_USERNAME_KEY);
}

+ /**
+ * Gets the engine type of orient db. Can only be 'local', 'remote' or
+ * 'memory'.
+ * @return string orient db engine type
+ */
public String getOrientdbEngine() {
return orientdbEngine;
}

+ /**
+ * Gets the link to orient db database
+ * @return string url/link to the orient db database.
+ */
public String getOrientdbLink() {
return orientdbLink;
}

+ /**
+ * Gets the password for orient db server
+ * @return string password of orient db server
+ */
public String getOrientdbPassword() {
return orientdbPassword;
}

+ /**
+ * Gets the username of the orient db server
+ * @return string username of orient db server.
+ */
public String getOrientdbUsername() {
return orientdbUsername;
}
=======================================
---
/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/OrientGraphDataModel.java
Sat Aug 25 09:36:53 2012
+++
/trunk/metcatserver/src/main/java/org/apache/airavata/metcat/db/OrientGraphDataModel.java
Wed Aug 29 01:53:20 2012
@@ -1,56 +1,145 @@
-//package org.apache.airavata.metcat.db;
-//
-//import com.tinkerpop.blueprints.Index;
-//import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
-//import com.tinkerpop.blueprints.Vertex;
-//import com.tinkerpop.blueprints.impls.orient.OrientGraph;
-//
-//public class OrientGraphDataModel extends GraphDataModel {
-//
-// private GraphModelProperties properties = null;
-//
-// private OrientGraph graph = null;
-// private Index<Vertex> index = null;
-//
-// public OrientGraphDataModel() throws Exception {
-// this.properties = GraphModelProperties.getInstance();
-//
-// this.connect();
-// index = graph.createIndex("dp-idx", Vertex.class);
-// }
-//
-// private void connect() throws Exception {
-// String connectstr = properties.getOrientdbEngine() + ":" +
properties.getOrientdbLink();
-//
-// graph = new OrientGraph(connectstr, properties.getOrientdbUsername(),
properties.getOrientdbPassword());
-// }
-//
-// public void addVertex(String vId) throws Exception {
-// try {
-// Vertex v = graph.addVertex(null);
-// v.setProperty("id", vId);
-// index.put("id", vId, v);
-//
-// graph.stopTransaction(Conclusion.SUCCESS);
-// } catch (Exception ex) {
-// graph.stopTransaction(Conclusion.FAILURE);
-// throw ex;
-// }
-// }
-//
-// public void addEdge(String fromVertex, String toVertex) throws
Exception {
-// Iterable<Vertex> v1s = index.get("id", fromVertex);
-// Iterable<Vertex> v2s = index.get("id", toVertex);
-//
-// Vertex v1 = v1s.iterator().next();
-// Vertex v2 = v2s.iterator().next();
-//
-// graph.addEdge(null, v1, v2, "produces");
-// }
-//
-// public void close() throws Exception {
-// if (graph != null) {
-// graph.shutdown();
-// }
-// }
-//}
+package org.apache.airavata.metcat.db;
+
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+
+import com.orientechnologies.orient.core.db.graph.OGraphDatabase;
+import com.orientechnologies.orient.core.id.ORecordId;
+import com.orientechnologies.orient.core.index.OIndex;
+import com.orientechnologies.orient.core.index.OIndexManagerProxy;
+import com.orientechnologies.orient.core.metadata.schema.OClass;
+import com.orientechnologies.orient.core.metadata.schema.OType;
+import com.orientechnologies.orient.core.record.impl.ODocument;
+import com.orientechnologies.orient.core.sql.OCommandSQL;
+
+/**
+ * Interface class for OrientDB database connection.
+ *
+ * @author Isuru Madushanka
+ *
+ */
+public class OrientGraphDataModel extends GraphDataModel {
+
+ private final Logger logger =
LogManager.getLogger(OrientGraphDataModel.class);
+
+ private GraphModelProperties properties = null;
+
+ private OGraphDatabase graph = null;
+ private OIndex<?> dpidindex = null;
+
+ private static OrientGraphDataModel instance = null;
+
+ /**
+ * Gets the singleton object of OrientGraphDataModel.
+ * @return OrientGraphDataModel instance
+ * @throws Exception OrientDB Exception
+ */
+ public static OrientGraphDataModel getInstance() throws Exception {
+ if (instance == null) {
+ instance = new OrientGraphDataModel();
+ }
+ return instance;
+ }
+
+ /**
+ * Local constructor to load OrientDB properties and connect it.
+ * @throws Exception OrientDB Exception
+ */
+ private OrientGraphDataModel() throws Exception {
+ this.properties = GraphModelProperties.getInstance();
+
+ this.connect();
+ }
+
+ /**
+ * Connects to the OrientDB database instance and loads necessary indexes.
+ * @throws Exception OrientDB Exception
+ */
+ private void connect() throws Exception {
+ String connectstr = properties.getOrientdbLink();
+
+ logger.info(" [OrientDB] Connecting to " + connectstr + " with username
= " + properties.getOrientdbUsername() + ", "
+ + properties.getOrientdbPassword());
+ graph = new OGraphDatabase(connectstr);
+ graph.open(properties.getOrientdbUsername(),
properties.getOrientdbPassword());
+
+ if (!graph.getMetadata().getSchema().existsClass("DataProduct")) {
+ OClass cls = graph.createVertexType("DataProduct");
+ cls.createProperty("id", OType.STRING);
+ logger.info(" [OrientDB] new class type 'DataProduct' created!");
+ }
+
+ OIndexManagerProxy index = graph.getMetadata().getIndexManager();
+
+ if (index.areIndexed("DataProduct", "id")) {
+ dpidindex = index.getIndex("DataProduct.id");
+ logger.info(" [OrientDB] Data Product index is retrieved!");
+ } else {
+ graph.command(new OCommandSQL("create index DataProduct.id
unique")).execute();
+ logger.info(" [OrientDB] new index of DataProduct.id is
created! ");
+ }
+
+ if (dpidindex == null) {
+ throw new Exception(" [OrientDB] Index can't be retrieved!");
+ }
+ }
+
+ /**
+ * Adds a new vertex with unique id into the OrientDB database.
+ * @param vId unique id of the vertex
+ * @throws Exception OrientDB Exception
+ */
+ public void addVertex(String vId) throws Exception {
+ if (dpidindex.get(vId) == null) {
+ ODocument vertex = graph.createVertex("DataProduct").field("id", vId);
+ dpidindex.put(vId, vertex);
+ vertex.save();
+ logger.info(" [OrientDB] Added new data product vertex to the graph
database.");
+ }
+ }
+
+ public String getVertexRID(String vertex) throws Exception {
+ ORecordId odoc1 = (ORecordId) dpidindex.get(vertex);
+ if (odoc1 == null) return null;
+ String str = odoc1.getIdentity().toString();
+ if (str.startsWith("#")) return str.substring(1);
+ return str;
+ }
+
+ /**
+ * Adds a new edge from given vertex to the other.
+ * @param fromVertex starting vertex of the edge
+ * @param toVertex ending vertex of the edge
+ */
+ public void addEdge(String fromVertex, String toVertex) {
+ try {
+ ORecordId odoc1 = (ORecordId) dpidindex.get(fromVertex);
+ ORecordId odoc2 = (ORecordId) dpidindex.get(toVertex);
+
+ ODocument edge = graph.createEdge((ODocument)odoc1.getRecord(),
(ODocument)odoc2.getRecord());
+ edge.save();
+ } catch (Exception ex) {
+ logger.debug(" [OrientDB] Edge is already exist for " + fromVertex + "
to " + toVertex + "!");
+ // do nothing, because it may edge exist
+ }
+ }
+
+ /**
+ * Close the graph database connection.
+ * @throws Exception OrientDB Exception
+ */
+ public void close() throws Exception {
+ if (graph != null) {
+ graph.close();
+ }
+ }
+
+ /**
+ * Gets graph database instance
+ * @return graph database instance
+ */
+ public OGraphDatabase getGraphDatabase() {
+ return this.graph;
+ }
+
+}
Reply all
Reply to author
Forward
0 new messages