Skip to content

Commit

Permalink
Merge pull request #15 from mpollmeier/update-vertexes
Browse files Browse the repository at this point in the history
Update vertexes
  • Loading branch information
mpollmeier committed Nov 1, 2015
2 parents 9b7b74b + d06c7bb commit 95be912
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.apache.tinkerpop.gremlin.orientdb;

import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.record.impl.ODocument;

import org.apache.tinkerpop.gremlin.structure.Direction;
Expand All @@ -12,6 +14,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -87,6 +90,34 @@ else if (direction.equals(Direction.IN))
throw new IllegalArgumentException("direction " + direction + " is not supported!");
}

public void remove() {
ODocument doc = getRawDocument();
if (doc.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED) {
doc.load();
}

removeLink(Direction.IN);
removeLink(Direction.OUT);
doc.delete();
}

private void removeLink( Direction direction) {
final String fieldName = OrientVertex.getConnectionFieldName(direction, this.label());
ODocument doc = this.getVertex(direction).getRawDocument();
Object found = doc.field(fieldName);
if(found instanceof ORidBag) {
ORidBag bag = (ORidBag)found;
bag.remove(this.getRawElement());
if(bag.size() == 0) doc.removeField(fieldName);
}
else if (found instanceof Collection<?>) {
((Collection<Object>) found).remove(this.getRawElement());
if(((Collection<Object>) found).size() == 0) doc.removeField(fieldName);
} else
throw new IllegalStateException("Relationship content is invalid on field " + fieldName + ". Found: " + found);
doc.save();
}

public OIdentifiable getOutVertex() {
if (vOut != null)
// LIGHTWEIGHT EDGE
Expand All @@ -112,9 +143,6 @@ public OIdentifiable getInVertex() {
return vIn;

final ODocument doc = getRawDocument();
System.out.println(doc);
System.out.println(Arrays.asList(doc.fieldNames()));

if (doc == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.stream.Stream;


public class OrientElement implements Element {
public abstract class OrientElement implements Element {

private static final Map<String, String> INTERNAL_CLASSES_TO_TINKERPOP_CLASSES;
static {
Expand Down Expand Up @@ -61,14 +61,6 @@ public <V> Property<V> property(final String key, final V value) {
return new OrientProperty<>(key, value, this);
}

public void remove() {
ODocument doc = getRawDocument();
if (doc.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED) {
doc.load();
}
doc.delete();
}

public <V> Iterator<? extends Property<V>> properties(final String... propertyKeys) {
ODocument record = rawElement.getRecord();
if (record == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
package org.apache.tinkerpop.gremlin.orientdb;

import static org.apache.tinkerpop.gremlin.orientdb.StreamUtils.asStream;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.NotImplementedException;
import org.apache.tinkerpop.gremlin.orientdb.traversal.strategy.optimization.OrientGraphStepStrategy;
import org.apache.tinkerpop.gremlin.process.computer.GraphComputer;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;

import com.orientechnologies.common.exception.OException;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.util.OCallable;
Expand All @@ -39,18 +15,27 @@
import com.orientechnologies.orient.core.index.OIndexManager;
import com.orientechnologies.orient.core.index.OPropertyIndexDefinition;
import com.orientechnologies.orient.core.iterator.ORecordIteratorClass;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OImmutableClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OSchemaProxy;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.metadata.schema.*;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentInternal;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.NotImplementedException;
import org.apache.tinkerpop.gremlin.orientdb.traversal.strategy.optimization.OrientGraphStepStrategy;
import org.apache.tinkerpop.gremlin.process.computer.GraphComputer;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies;
import org.apache.tinkerpop.gremlin.structure.*;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.apache.tinkerpop.gremlin.orientdb.StreamUtils.asStream;

@Graph.OptIn(Graph.OptIn.SUITE_STRUCTURE_STANDARD)
@Graph.OptIn(Graph.OptIn.SUITE_STRUCTURE_INTEGRATE)
Expand Down Expand Up @@ -427,46 +412,21 @@ protected void prepareIndexConfiguration(Configuration config) {
config.setProperty("metadata", defaultMetadata);
}

/**
* Creates an automatic indexing structure for indexing provided key for element class.
*
* @param key the key to create the index for
* @param label the element label
* @param configuration a collection of parameters for the underlying index implementation:
* <ul>
* <li>"type" is the index type between the supported types (UNIQUE, NOTUNIQUE, FULLTEXT). The default type is NOT_UNIQUE
* <li>"class" is the class to index when it's a custom type derived by Vertex (V) or Edge (E)
* <li>"keytype" to use a key type different by OType.STRING,</li>
* </li>
* </ul>
* @param <T> the element class specification
*/

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

/**
* Creates an automatic indexing structure for indexing provided key for element class.
*
* @param key the key to create the index for
* @param label the element label
* @param configuration a collection of parameters for the underlying index implementation:
* <ul>
* <li>"type" is the index type between the supported types (UNIQUE, NOTUNIQUE, FULLTEXT). The default type is NOT_UNIQUE
* <li>"class" is the class to index when it's a custom type derived by Vertex (V) or Edge (E)
* <li>"keytype" to use a key type different by OType.STRING,</li>
* </li>
* </ul>
* @param <T> the element class specification
*/

public <T extends Element> void createEdgeIndex(final String key, final String label, final Configuration configuration) {
String className = OrientEdgeType.CLASS_NAME + "_" + label;
createEdgeClass(className);
createIndex(key, className, configuration);
}

@SuppressWarnings({"rawtypes"})
private <T extends Element> void createIndex(final String key, String className, final Configuration configuration) {
makeActive();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.util.OPair;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
Expand Down Expand Up @@ -130,7 +131,6 @@ public String toString() {
public Edge addEdge(String label, Vertex inVertex, Object... keyValues) {
if (inVertex == null)
throw new IllegalArgumentException("destination vertex is null");

checkArgument(!isNullOrEmpty(label), "label is invalid");

if (ElementHelper.getIdValue(keyValues).isPresent()) throw Vertex.Exceptions.userSuppliedIdsNotSupported();
Expand Down Expand Up @@ -188,7 +188,6 @@ public Edge addEdge(String label, Vertex inVertex, Object... keyValues) {
// from = from.getIdentity();
// to = to.getIdentity();
// }

createLink(outDocument, edge.getRawElement(), outFieldName);
createLink(inDocument, edge.getRawElement(), inFieldName);

Expand All @@ -200,7 +199,22 @@ public Edge addEdge(String label, Vertex inVertex, Object... keyValues) {
return edge;
}

public String getConnectionFieldName(final Direction iDirection, final String iClassName) {
public void remove() {
ODocument doc = getRawDocument();
if (doc.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED) {
doc.load();
}

Iterator<Edge> allEdges = edges(Direction.BOTH, "E");
while(allEdges.hasNext()) {
allEdges.next().remove();
}

doc.delete();
}


public static String getConnectionFieldName(final Direction iDirection, final String iClassName) {
if (iDirection == null || iDirection == Direction.BOTH)
throw new IllegalArgumentException("Direction not valid");

Expand Down Expand Up @@ -284,9 +298,9 @@ public Object createLink(final ODocument iFromVertex, final OIdentifiable iTo, f
return out;
}


public Iterator<Edge> edges(final Direction direction, String... edgeLabels) {
final ODocument doc = getRawDocument();

// edgeLabels = OrientGraphUtils.getEdgeClassNames(graph, edgeLabels);
// edgeLabels = (String[]) Stream.of(edgeLabels).map(OrientGraphUtils::encodeClassName).toArray();

Expand Down Expand Up @@ -324,7 +338,8 @@ else if (coll instanceof List<?>)
iterable.add(new OrientEdgeIterator(this, iDestination, coll, coll.iterator(), connection, edgeLabels, -1));
}
} else if (fieldValue instanceof ORidBag) {
iterable.add(new OrientEdgeIterator(this, iDestination, fieldValue, ((ORidBag) fieldValue).rawIterator(), connection, edgeLabels, ((ORidBag) fieldValue).size()));
ORidBag bag = (ORidBag) fieldValue;
iterable.add(new OrientEdgeIterator(this, iDestination, fieldValue, bag.rawIterator(), connection, edgeLabels, ((ORidBag) fieldValue).size()));
}
}
}
Expand Down
35 changes: 32 additions & 3 deletions tests-scala/src/test/scala/OrientSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,38 @@ class OrientSpec extends WordSpec with ShouldMatchers with BeforeAndAfterEach {
val e1 = v1.addEdge("label1", v2)
val e2 = v2.addEdge("label2", v1)

graph.E(e2.id).toList should have length 1
graph.E(e2.id).head().remove()
graph.E(e2.id).toList should have length 0
graph.E(e1.id).toList should have length 1
graph.E(e1.id).head().remove()
graph.E(e1.id).toList should have length 0

v1.outE("label1").toList() should have length 0
}

"do not delete entry if there are multiple edges" in {
val v1 = graph.addVertex()
val v2 = graph.addVertex()
val e1 = v1.addEdge("label1", v2)
val e2 = v1.addEdge("label1", v1)

graph.E(e1.id).toList should have length 1
graph.E(e1.id).head().remove()
graph.E(e1.id).toList should have length 0

v1.outE("label1").toList() should have length 1
}

"be removed if vertex is deleted" in {
val v1 = graph.addVertex()
val v2 = graph.addVertex()
val e1 = v1.addEdge("label1", v2)
val e2 = v2.addEdge("label2", v1)

v2.inE("label1").toList() should have length 1
v2.outE("label2").toList() should have length 1

v1.remove();
v2.inE("label1").toList() should have length 0
v2.outE("label2").toList() should have length 0
}
}

Expand Down

0 comments on commit 95be912

Please sign in to comment.