com.tinkerpop.blueprints.util.IndexableGraphHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blueprints-core Show documentation
Show all versions of blueprints-core Show documentation
Core interfaces and utilities for Blueprints
package com.tinkerpop.blueprints.util;
import com.tinkerpop.blueprints.Index;
import com.tinkerpop.blueprints.IndexableGraph;
import com.tinkerpop.blueprints.Vertex;
import java.util.Iterator;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class IndexableGraphHelper {
/**
* Add a vertex to a graph only if no other vertex in the provided Index is indexed by the property key/value pair.
* If a vertex already exists with that key/value pair, return the pre-existing vertex.
*
* @param graph the graph to add the vertex to
* @param id the id of the vertex to create (can be null)
* @param index the index to determine if another vertex with the same key/value exists
* @param uniqueKey the key to check on for uniqueness of the vertex
* @param uniqueValue the value to check on for uniqueness of the vertex
* @return the newly created vertex or the vertex that satisfies the uniqueness criteria
*/
public static Vertex addUniqueVertex(final IndexableGraph graph, final Object id, final Index index, final String uniqueKey, final Object uniqueValue) {
final Iterator results = index.get(uniqueKey, uniqueValue).iterator();
if (results.hasNext()) {
return results.next();
} else {
final Vertex vertex = graph.addVertex(id);
vertex.setProperty(uniqueKey, uniqueValue);
return vertex;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy