All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.tinkerpop.gremlin.tinkergraph.structure.TinkerHelper Maven / Gradle / Ivy
package com.tinkerpop.gremlin.tinkergraph.structure;
import com.tinkerpop.gremlin.process.computer.GraphComputer;
import com.tinkerpop.gremlin.process.util.FastNoSuchElementException;
import com.tinkerpop.gremlin.process.util.MultiIterator;
import com.tinkerpop.gremlin.structure.Direction;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Graph;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.Vertex;
import com.tinkerpop.gremlin.structure.util.ElementHelper;
import com.tinkerpop.gremlin.tinkergraph.process.computer.TinkerGraphView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class TinkerHelper {
protected static long getNextId(final TinkerGraph graph) {
return Stream.generate(() -> (++graph.currentId)).filter(id -> !graph.vertices.containsKey(id) && !graph.edges.containsKey(id)).findAny().get();
}
protected static Edge addEdge(final TinkerGraph graph, final TinkerVertex outVertex, final TinkerVertex inVertex, final String label, final Object... keyValues) {
ElementHelper.validateLabel(label);
ElementHelper.legalPropertyKeyValueArray(keyValues);
Object idValue = ElementHelper.getIdValue(keyValues).orElse(null);
final Edge edge;
if (null != idValue) {
if (graph.edges.containsKey(idValue))
throw Graph.Exceptions.edgeWithIdAlreadyExists(idValue);
} else {
idValue = TinkerHelper.getNextId(graph);
}
edge = new TinkerEdge(idValue, outVertex, label, inVertex, graph);
ElementHelper.attachProperties(edge, keyValues);
graph.edges.put(edge.id(), edge);
TinkerHelper.addOutEdge(outVertex, label, edge);
TinkerHelper.addInEdge(inVertex, label, edge);
return edge;
}
protected static void addOutEdge(final TinkerVertex vertex, final String label, final Edge edge) {
Set edges = vertex.outEdges.get(label);
if (null == edges) {
edges = new HashSet<>();
vertex.outEdges.put(label, edges);
}
edges.add(edge);
}
protected static void addInEdge(final TinkerVertex vertex, final String label, final Edge edge) {
Set edges = vertex.inEdges.get(label);
if (null == edges) {
edges = new HashSet<>();
vertex.inEdges.put(label, edges);
}
edges.add(edge);
}
public static void dropView(final TinkerGraph graph) {
graph.graphView = null;
}
public static Collection getVertices(final TinkerGraph graph) {
return graph.vertices.values();
}
public static Collection getEdges(final TinkerGraph graph) {
return graph.edges.values();
}
public static List queryVertexIndex(final TinkerGraph graph, final String key, final Object value) {
return graph.vertexIndex.get(key, value);
}
public static List queryEdgeIndex(final TinkerGraph graph, final String key, final Object value) {
return graph.edgeIndex.get(key, value);
}
public static boolean inComputerMode(final TinkerGraph graph) {
return null != graph.graphView;
}
public static TinkerGraphView createGraphView(final TinkerGraph graph, final GraphComputer.Isolation isolation, final Set computeKeys) {
return graph.graphView = new TinkerGraphView(isolation, computeKeys);
}
public static Map> getProperties(final TinkerElement element) {
return element.properties;
}
public static Iterator getEdges(final TinkerVertex vertex, final Direction direction, final int branchFactor, final String... labels) {
final MultiIterator edges = new MultiIterator<>();
if (direction.equals(Direction.OUT) || direction.equals(Direction.BOTH)) {
if (labels.length > 0) {
for (final String label : labels) {
edges.addIterator(vertex.outEdges.getOrDefault(label, Collections.emptySet()).iterator());
}
} else {
for (final Set set : vertex.outEdges.values()) {
edges.addIterator(set.iterator());
}
}
}
if (direction.equals(Direction.IN) || direction.equals(Direction.BOTH)) {
if (labels.length > 0) {
for (final String label : labels) {
edges.addIterator(vertex.inEdges.getOrDefault(label, Collections.emptySet()).iterator());
}
} else {
for (final Set set : vertex.inEdges.values()) {
edges.addIterator(set.iterator());
}
}
}
return new TinkerEdgeIterator((Iterator) edges, branchFactor);
}
public static Iterator getVertices(final TinkerVertex vertex, final Direction direction, final int branchFactor, final String... labels) {
if (direction != Direction.BOTH) {
return new TinkerVertexIterator(TinkerHelper.getEdges(vertex, direction, branchFactor, labels), direction);
} else {
final MultiIterator vertices = new MultiIterator<>(branchFactor);
vertices.addIterator(new TinkerVertexIterator(TinkerHelper.getEdges(vertex, Direction.OUT, branchFactor, labels), Direction.OUT));
vertices.addIterator(new TinkerVertexIterator(TinkerHelper.getEdges(vertex, Direction.IN, branchFactor, labels), Direction.IN));
return vertices;
}
}
public static Iterator getVertices(final TinkerEdge edge, final Direction direction) {
final List vertices = new ArrayList<>(2);
if (direction.equals(Direction.OUT) || direction.equals(Direction.BOTH))
vertices.add((TinkerVertex) edge.outVertex);
if (direction.equals(Direction.IN) || direction.equals(Direction.BOTH))
vertices.add((TinkerVertex) edge.inVertex);
return vertices.iterator();
}
private static class TinkerVertexIterator implements Iterator {
private final Iterator edges;
private final Direction direction;
public TinkerVertexIterator(final Iterator edges, final Direction direction) {
this.edges = edges;
this.direction = direction;
}
@Override
public boolean hasNext() {
return this.edges.hasNext();
}
@Override
public TinkerVertex next() {
if (this.direction.equals(Direction.IN))
return (TinkerVertex) this.edges.next().outVertex;
else
return (TinkerVertex) this.edges.next().inVertex;
}
}
private static class TinkerEdgeIterator implements Iterator {
private final Iterator edges;
private final int branchFactor;
private int currentCount = 0;
public TinkerEdgeIterator(final Iterator edges, final int branchFactor) {
this.edges = edges;
this.branchFactor = branchFactor;
}
@Override
public boolean hasNext() {
return this.currentCount < this.branchFactor && this.edges.hasNext();
}
@Override
public TinkerEdge next() {
if (this.currentCount++ < this.branchFactor) {
return this.edges.next();
} else {
throw FastNoSuchElementException.instance();
}
}
}
}