Skip to content

Commit

Permalink
disable classname prefixing
Browse files Browse the repository at this point in the history
* OrientGraphFactory.setLabelAsClassName() to disable the default prefixing of classnames with V_ or E_
* OrientGraph.labelToClassName() and OrientGraph.classNameToLabel() helpers
* OrientGraph.getEdgeIndexedKeys() and OrientGraph.getVertexIndexedKeys() verify the super class
* OrientGraph.createClass() throws if creating the same class with a different base class
* test suites modified
  • Loading branch information
bicouy0 committed Dec 11, 2015
1 parent 901694e commit 9bdb72b
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ORID id() {
public String label() {
String internalClassName = getRawDocument().getClassName();
// User labels on edges/vertices are prepended with E_ or V_ . The user should not see that.
return internalClassName.length() == 1 ? INTERNAL_CLASSES_TO_TINKERPOP_CLASSES.get(internalClassName) : internalClassName.substring(2);
return internalClassName.length() == 1 ? INTERNAL_CLASSES_TO_TINKERPOP_CLASSES.get(internalClassName) : graph.classNameToLabel(internalClassName);
}

public Graph graph() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public final class OrientGraph implements Graph {
public static String CONFIG_OPEN = "orient-open";
public static String CONFIG_TRANSACTIONAL = "orient-transactional";
public static String CONFIG_POOL_SIZE = "orient-max-poolsize";
public static String CONFIG_LABEL_AS_CLASSNAME = "orient-label-as-classname";

protected final ODatabaseDocumentTx database;
protected final Features features;
Expand Down Expand Up @@ -127,9 +128,7 @@ public Vertex addVertex(Object... keyValues) {
if (ElementHelper.getIdValue(keyValues).isPresent()) throw Vertex.Exceptions.userSuppliedIdsNotSupported();

String label = ElementHelper.getLabelValue(keyValues).orElse(OImmutableClass.VERTEX_CLASS_NAME);
String className = label.equals(OImmutableClass.VERTEX_CLASS_NAME) ?
OImmutableClass.VERTEX_CLASS_NAME :
OImmutableClass.VERTEX_CLASS_NAME + "_" + label;
String className = labelToClassName(label, OImmutableClass.VERTEX_CLASS_NAME);
OrientVertex vertex = new OrientVertex(this, className);
vertex.property(keyValues);

Expand Down Expand Up @@ -166,6 +165,26 @@ public Iterator<Vertex> vertices(Object... vertexIds) {
vertexIds);
}

/**
* Convert a label to orientdb class name
*/
public String labelToClassName(String label, String prefix) {
if (configuration.getBoolean(CONFIG_LABEL_AS_CLASSNAME, false)) {
return label;
}
return label.equals(prefix) ? prefix : prefix + "_" + label;
}

/**
* Convert a orientdb class name to label
*/
public String classNameToLabel(String className) {
if (configuration.getBoolean(CONFIG_LABEL_AS_CLASSNAME, false)) {
return className;
}
return className.substring(2);
}

protected Object convertValue(final OIndex<?> idx, Object iValue) {
if (iValue != null) {
final OType[] types = idx.getKeyTypes();
Expand Down Expand Up @@ -209,6 +228,10 @@ public Stream<OrientVertex> getIndexedVertices(OIndex<Object> index, Optional<Ob
private OIndexManager getIndexManager() {
return database.getMetadata().getIndexManager();
}

private OSchema getSchema() {
return database.getMetadata().getSchema();
}


public Set<String> getIndexedKeys(String className) {
Expand Down Expand Up @@ -241,11 +264,21 @@ public Set<String> getIndexedKeys(final Class<? extends Element> elementClass) {
}

public Set<String> getVertexIndexedKeys(final String label) {
return getIndexedKeys(OImmutableClass.VERTEX_CLASS_NAME + "_" + label);
String className = labelToClassName(label, OImmutableClass.VERTEX_CLASS_NAME);
OClass cls = getSchema().getClass(className);
if (cls != null && cls.isSubClassOf(OImmutableClass.VERTEX_CLASS_NAME)) {
return getIndexedKeys(className);
}
return new HashSet<String>();
}

public Set<String> getEdgeIndexedKeys(final String label) {
return getIndexedKeys(OImmutableClass.EDGE_CLASS_NAME + "_" + label);
String className = labelToClassName(label, OImmutableClass.EDGE_CLASS_NAME);
OClass cls = getSchema().getClass(className);
if (cls != null && cls.isSubClassOf(OImmutableClass.EDGE_CLASS_NAME)) {
return getIndexedKeys(className);
}
return new HashSet<String>();
}

@Override
Expand Down Expand Up @@ -438,7 +471,8 @@ public void createClass(final String className, final String superClassName) {
public void createClass(final String className, final OClass superClass) {
makeActive();
OSchemaProxy schema = database.getMetadata().getSchema();
if (schema.getClass(className) == null) {
OClass cls = schema.getClass(className);
if (cls == null) {
try
{
schema.createClass(className, superClass);
Expand All @@ -449,6 +483,11 @@ public void createClass(final String className, final OClass superClass) {
}
OLogManager.instance().info(this, "created class '" + className + "' as subclass of '" + superClass + "'");
}
else {
if (!cls.isSubClassOf(superClass)) {
throw new IllegalArgumentException("unable to create class '" + className + "' as subclass of '" + superClass + "'. different super class.");
}
}
}

public ODatabaseDocumentTx getRawDatabase() {
Expand Down Expand Up @@ -500,14 +539,14 @@ protected void prepareIndexConfiguration(Configuration config) {


public <E extends Element> void createVertexIndex(final String key, final String label, final Configuration configuration) {
String className = OrientVertexType.CLASS_NAME + "_" + label;
String className = labelToClassName(label, OrientVertexType.CLASS_NAME);
createVertexClass(className);
createIndex(key, className, configuration);
}


public <E extends Element> void createEdgeIndex(final String key, final String label, final Configuration configuration) {
String className = OrientEdgeType.CLASS_NAME + "_" + label;
String className = labelToClassName(label, OrientEdgeType.CLASS_NAME);
createEdgeClass(className);
createIndex(key, className, configuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class OrientGraphFactory {
protected final String password;
protected Configuration configuration;
protected volatile OPartitionedDatabasePool pool;
protected boolean labelAsClassName;

public OrientGraphFactory(String url) {
this(url, ADMIN, ADMIN);
Expand All @@ -26,6 +27,7 @@ public OrientGraphFactory(String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
this.labelAsClassName = false;
}

public OrientGraphFactory(Configuration config) {
Expand Down Expand Up @@ -104,6 +106,7 @@ protected Configuration getConfiguration(boolean create, boolean open, boolean t
setProperty(OrientGraph.CONFIG_CREATE, create);
setProperty(OrientGraph.CONFIG_OPEN, open);
setProperty(OrientGraph.CONFIG_TRANSACTIONAL, transactional);
setProperty(OrientGraph.CONFIG_LABEL_AS_CLASSNAME, labelAsClassName);
}};
}

Expand All @@ -121,6 +124,16 @@ protected ODatabaseDocumentTx getDatabase(boolean create, boolean open) {
return db;
}

/**
* Enable or disable the prefixing of class names with V_<label> for vertices or E_<label> for edges.
*
* @param is if true classname equals label, if false classname is prefixed with V_ or E_ (default)
*/
public OrientGraphFactory setLabelAsClassName(boolean is) {
this.labelAsClassName = is;
return this;
}

/**
* Setting up the factory to use database pool instead of creation a new instance of database connection each time.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ public Edge addEdge(String label, Vertex inVertex, Object... keyValues) {
throw new IllegalStateException("label cannot be null");

// CREATE THE EDGE DOCUMENT TO STORE FIELDS TOO
String className = label.equals(OImmutableClass.EDGE_CLASS_NAME) ?
OImmutableClass.EDGE_CLASS_NAME : OImmutableClass.EDGE_CLASS_NAME + "_" + label;
String className = graph.labelToClassName(label, OImmutableClass.EDGE_CLASS_NAME);
edge = new OrientEdge(graph, className, outDocument, inDocument, label);
edge.property(keyValues);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Optional<OrientIndexQuery> findIndex() {
String key = indexedKeyAndValue.get().getValue0();
Object value = indexedKeyAndValue.get().getValue1();

String className = OImmutableClass.VERTEX_CLASS_NAME + '_' + elementLabel.get();
String className = graph.labelToClassName(elementLabel.get(), OImmutableClass.VERTEX_CLASS_NAME);
Set<OIndex<?>> classIndexes = indexManager.getClassIndexes(className);
Iterator<OIndex<?>> keyIndexes = classIndexes.stream().filter(idx -> idx.getDefinition().getFields().contains(key)).iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.junit.Assert;
import org.junit.Test;

public class OrientGraphTest {
Expand Down Expand Up @@ -148,6 +149,26 @@ public void testPooledTransactionalGraph() throws Exception {
graph.close();
}

@Test
public void testUnprefixedLabelGraph() throws Exception {
Graph graph = graphFactory().setLabelAsClassName(true).getNoTx();
assertEquals(true, graph.configuration().getBoolean(OrientGraph.CONFIG_LABEL_AS_CLASSNAME));

performBasicTests(graph);

Vertex vertex = graph.addVertex("VERTEX_LABEL");
assertEquals("VERTEX_LABEL", vertex.label());

try {
graph.addVertex("EDGE_LABEL");
Assert.fail("must throw unable to create different super class");
} catch (IllegalArgumentException e) {
// ok
}

graph.close();
}

protected void performBasicTests(Graph graph) {
Vertex vertex = graph.addVertex();
assertNotNull(vertex);
Expand Down

0 comments on commit 9bdb72b

Please sign in to comment.