Skip to content

Commit

Permalink
GraphQuery work.
Browse files Browse the repository at this point in the history
  • Loading branch information
okram committed Mar 8, 2013
2 parents 74e1cfa + 961739b commit 5cee0a6
Show file tree
Hide file tree
Showing 45 changed files with 880 additions and 346 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.textile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ h3. Version 2.3.0 (NOT OFFICIALLY RELEASED YET)
```

* Bumped major versions of Ripple and SesameTools
* Added @GraphQuery@ in order to support intelligent indexing systems (@Vertex.query()@ and @Graph.query()@)
* Refactoring of wrappers to ensure proper propagation of @Graph@ reference
* Fixed a @ConcurrentModificationException@ in TinkerGraph
* @Element@ API changed to support @Element.remove()@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ public interface Graph {
*/
public Iterable<Edge> getEdges(String key, Object value);

/**
* Generate a query object that can be used to fine tune which edges/vertices are retrieved from the graph.
*
* @return a graph query object with methods for constraining which data is pulled from the underlying graph
*/
public GraphQuery query();

/**
* A shutdown function is required to properly close the graph.
* This is important for implementations that utilize disk-based serializations.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tinkerpop.blueprints;

/**
* (c) Matthias Broecheler (me@matthiasb.com)
*/

public interface GraphQuery extends Query {

@Override
public GraphQuery has(final String key, final Object value);

@Override
public <T extends Comparable<T>> GraphQuery has(final String key, final T value, final Compare compare);

@Override
public <T extends Comparable<T>> GraphQuery interval(final String key, final T startValue, final T endValue);

@Override
public GraphQuery limit(final long max);

}
43 changes: 7 additions & 36 deletions blueprints-core/src/main/java/com/tinkerpop/blueprints/Query.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.tinkerpop.blueprints;

/**
* A Query object defines a collection of filters and modifiers that are used to intelligently select edges from a vertex.
*
* @author Matthias Brocheler (http://matthiasb.com)
* @author Marko A. Rodriguez (http://markorodriguez.com)
* (c) Matthias Broecheler (me@matthiasb.com)
*/

public interface Query {

public enum Compare {
Expand Down Expand Up @@ -57,29 +55,6 @@ else if (this.equals(LESS_THAN))
*/
public <T extends Comparable<T>> Query interval(final String key, final T startValue, final T endValue);

/**
* The direction of the edges to retrieve.
*
* @param direction whether to retrieve the incoming, outgoing, or both directions
* @return the modified query object
*/
public Query direction(final Direction direction);

/**
* Filter out the edge if its label is not in set of provided labels.
*
* @param labels the labels to check against
* @return the modified query object
*/
public Query labels(final String... labels);

/**
* Filter out the edge if the max number of edges to retrieve has already been reached.
*
* @param max the max number of edges to return
* @return the modified query object
*/
public Query limit(final long max);

/**
* Execute the query and return the matching edges.
Expand All @@ -95,18 +70,14 @@ else if (this.equals(LESS_THAN))
*/
public Iterable<Vertex> vertices();

/**
* Execute the query and return the number of edges that are unfiltered.
*
* @return the number of unfiltered edges
*/
public long count();

/**
* Return the raw ids of the vertices on the other end of the edges.
* Filter out the edge if the max number of edges to retrieve has already been reached.
*
* @return the raw ids of the vertices on the other end of the edges
* @param max the max number of edges to return
* @return the modified query object
*/
public Object vertexIds();
public Query limit(final long max);


}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface Vertex extends Element {
*
* @return a vertex query object with methods for constraining which data is pulled from the underlying graph
*/
public Query query();
public VertexQuery query();

/**
* Add a new outgoing edge from this vertex to the parameter vertex with provided edge label.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.tinkerpop.blueprints;

/**
* A VertexQuery object defines a collection of filters and modifiers that are used to intelligently select edges from a vertex.
*
* @author Matthias Brocheler (http://matthiasb.com)
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public interface VertexQuery extends Query {


/**
* The direction of the edges to retrieve.
*
* @param direction whether to retrieve the incoming, outgoing, or both directions
* @return the modified query object
*/
public VertexQuery direction(final Direction direction);

/**
* Filter out the edge if its label is not in set of provided labels.
*
* @param labels the labels to check against
* @return the modified query object
*/
public VertexQuery labels(final String... labels);

/**
* Execute the query and return the number of edges that are unfiltered.
*
* @return the number of unfiltered edges
*/
public long count();

/**
* Return the raw ids of the vertices on the other end of the edges.
*
* @return the raw ids of the vertices on the other end of the edges
*/
public Object vertexIds();


@Override
public VertexQuery has(final String key, final Object value);

@Override
public <T extends Comparable<T>> VertexQuery has(final String key, final T value, final Compare compare);

@Override
public <T extends Comparable<T>> VertexQuery interval(final String key, final T startValue, final T endValue);

@Override
public VertexQuery limit(final long max);


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Features;
import com.tinkerpop.blueprints.GraphQuery;
import com.tinkerpop.blueprints.Index;
import com.tinkerpop.blueprints.IndexableGraph;
import com.tinkerpop.blueprints.KeyIndexableGraph;
import com.tinkerpop.blueprints.Parameter;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.DefaultGraphQuery;
import com.tinkerpop.blueprints.util.ExceptionFactory;
import com.tinkerpop.blueprints.util.KeyIndexableGraphHelper;
import com.tinkerpop.blueprints.util.PropertyFilteredIterable;
Expand Down Expand Up @@ -316,6 +318,10 @@ public void removeEdge(final Edge edge) {
this.edges.remove(edge.getId().toString());
}

public GraphQuery query() {
return new DefaultGraphQuery(this);
}


public String toString() {
if (null == this.directory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Query;
import com.tinkerpop.blueprints.VertexQuery;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.DefaultQuery;
import com.tinkerpop.blueprints.util.DefaultVertexQuery;
import com.tinkerpop.blueprints.util.MultiIterable;
import com.tinkerpop.blueprints.util.StringFactory;
import com.tinkerpop.blueprints.util.VerticesFromEdgesIterable;
Expand Down Expand Up @@ -98,8 +98,8 @@ private Iterable<Edge> getOutEdges(final String... labels) {
}
}

public Query query() {
return new DefaultQuery(this);
public VertexQuery query() {
return new DefaultVertexQuery(this);
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.tinkerpop.blueprints.util;

import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.GraphQuery;
import com.tinkerpop.blueprints.Vertex;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* For those graph engines that do not support the low-level querying of the vertices or edges, then DefaultQuery can be used.
* DefaultQuery assumes, at minimum, that Graph.getVertices() and Graph.getEdges() is implemented by the respective Graph.
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class DefaultGraphQuery extends DefaultQuery implements GraphQuery {

private final Graph graph;

public DefaultGraphQuery(final Graph graph) {
this.graph = graph;
}

public GraphQuery has(final String key, final Object value) {
this.hasContainers.add(new HasContainer(key, value, Compare.EQUAL));
return this;
}

public <T extends Comparable<T>> GraphQuery has(final String key, final T value, final Compare compare) {
this.hasContainers.add(new HasContainer(key, value, compare));
return this;
}

public <T extends Comparable<T>> GraphQuery interval(final String key, final T startValue, final T endValue) {
this.hasContainers.add(new HasContainer(key, startValue, Compare.GREATER_THAN_EQUAL));
this.hasContainers.add(new HasContainer(key, endValue, Compare.LESS_THAN));
return this;
}

public GraphQuery limit(final long max) {
this.limit = max;
return this;
}

public Iterable<Edge> edges() {
return new DefaultGraphQueryIterable<Edge>(false);
}

public Iterable<Vertex> vertices() {
return new DefaultGraphQueryIterable<Vertex>(true);
}

private class DefaultGraphQueryIterable<T extends Element> implements Iterable<T> {

private Iterable<T> iterable = null;

public DefaultGraphQueryIterable(final boolean forVertex) {
for (final HasContainer hasContainer : hasContainers) {
if (hasContainer.compare.equals(Compare.EQUAL)) {
if (forVertex)
this.iterable = (Iterable<T>) graph.getVertices(hasContainer.key, hasContainer.value);
else
this.iterable = (Iterable<T>) graph.getEdges(hasContainer.key, hasContainer.value);
break;
}
}
if (null == this.iterable) {
if (forVertex)
this.iterable = (Iterable<T>) graph.getVertices();
else
this.iterable = (Iterable<T>) graph.getEdges();
}
}

public Iterator<T> iterator() {
return new Iterator<T>() {
T nextElement = null;
final Iterator<T> itty = iterable.iterator();
long count = 0;

public boolean hasNext() {
if (null != this.nextElement) {
return true;
} else {
return this.loadNext();
}
}

public T next() {
while (true) {
if (this.nextElement != null) {
final T temp = this.nextElement;
this.nextElement = null;
return temp;
}

if (!this.loadNext())
throw new NoSuchElementException();
}
}

public void remove() {
throw new UnsupportedOperationException();
}

private boolean loadNext() {
this.nextElement = null;
if (count >= limit) return false;
while (this.itty.hasNext()) {
final T element = this.itty.next();
boolean filter = false;

/*if (!forVertex) {
filter = !containsLabel(((Edge) element).getLabel(), labels);
}*/

//if (!filter) {
for (final HasContainer hasContainer : hasContainers) {
if (!hasContainer.isLegal(element)) {
filter = true;
break;
}
}
//}
if (!filter) {
this.nextElement = element;
this.count++;
return true;
}
}
return false;
}
};
}

/*private boolean containsLabel(final String label, final String[] labels) {
if (labels.length == 0)
return true;
for (final String temp : labels) {
if (temp.equals(label))
return true;
}
return false;
}*/
}

}
Loading

0 comments on commit 5cee0a6

Please sign in to comment.