Permalink
Browse files

more work on BasicQuery. Added a full gamut test case to GraphTestSui…

…te to validate the query() behavior.
  • Loading branch information...
1 parent 244bcdc commit 1a19b55d03eaf1b5d513612104560909fa7e69be @okram okram committed Apr 28, 2012
@@ -1,6 +1,8 @@
package com.tinkerpop.blueprints.pgm;
/**
+ * A Query object defines a collection of filters and modifies that are used to intelligent select edges from a vertex.
+ *
* @author Matthias Brocheler (http://matthiasb.com)
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
@@ -10,24 +12,85 @@
public enum Direction {OUT, IN, BOTH}
+ /**
+ * Filter out the edge if it does not have a property with the specified value.
+ *
+ * @param key the key of the property
+ * @param value the value to check against
+ * @return the modified query object
+ */
public Query has(final String key, final Object value);
+ /**
+ * Filter out the edge if it does not have a property with a comparable value.
+ *
+ * @param key the key of the property
+ * @param value the value to check against
+ * @param compare the comparator to use for comparison
+ * @return the modified query object
+ */
public Query has(final String key, final Object value, final Compare compare);
+ /**
+ * Filter out the edge of its property value is not within the provided interval.
+ *
+ * @param key the key of the property
+ * @param startValue the inclusive start value of the interval
+ * @param endValue the exclusive end value of the interval
+ * @return the modified query object
+ */
public Query interval(final String key, final Object startValue, final Object 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.
+ *
+ * @return the unfiltered edges
+ */
public Iterable<Edge> edges();
+ /**
+ * Execute the query and return the vertices on the other end of the matching edges.
+ *
+ * @return the unfiltered edge's vertices
+ */
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.
+ *
+ * @return the raw ids of the vertices on the other end of the edges
+ */
public Object vertexIds();
}
@@ -16,9 +16,11 @@
*/
public class BasicQuery implements Query {
+ private static final String[] EMPTY_LABELS = new String[]{};
+
private final Vertex vertex;
public Direction direction = Direction.BOTH;
- public String[] labels;
+ public String[] labels = EMPTY_LABELS;
public long limit = Long.MAX_VALUE;
public List<HasContainer> hasContainers = new LinkedList<HasContainer>();
@@ -94,7 +96,6 @@ public HasContainer(final String key, final Object value, final Compare compare)
public boolean isLegal(final Element element) {
final Object elementValue = element.getProperty(key);
-
switch (compare) {
case EQUAL:
if (null == elementValue)
@@ -129,11 +130,11 @@ public boolean isLegal(final Element element) {
private class QueryIterable<T extends Element> implements Iterable<T> {
private Iterable<Edge> iterable;
- private boolean isVertex;
+ private boolean forVertex;
- public QueryIterable(boolean isVertex) {
- this.isVertex = isVertex;
- List<Iterable<Edge>> temp = new ArrayList<Iterable<Edge>>(2);
+ public QueryIterable(final boolean forVertex) {
+ this.forVertex = forVertex;
+ final List<Iterable<Edge>> temp = new ArrayList<Iterable<Edge>>(2);
if (direction == Direction.OUT || direction == Direction.BOTH) {
temp.add(vertex.getOutEdges(labels));
}
@@ -152,51 +153,58 @@ public QueryIterable(boolean isVertex) {
private class QueryIterator<T extends Element> implements Iterator<T> {
Edge nextEdge = null;
- Iterator<Edge> itty;
+ final Iterator<Edge> itty;
long count = 0;
public QueryIterator() {
this.itty = iterable.iterator();
- this.loadNext();
}
public boolean hasNext() {
- return null != nextEdge;
+ if (null != this.nextEdge) {
+ return true;
+ } else {
+ return this.loadNext();
+ }
}
public T next() {
- if (nextEdge != null) {
- final Edge temp = nextEdge;
- this.loadNext();
- if (isVertex) {
- if (direction == Direction.OUT)
- return (T) temp.getInVertex();
- else if (direction == Direction.IN)
- return (T) temp.getOutVertex();
- else {
- if (temp.getInVertex() == vertex) {
- return (T) temp.getOutVertex();
- } else {
+ while (true) {
+ if (this.nextEdge != null) {
+ final Edge temp = this.nextEdge;
+ this.nextEdge = null;
+ if (forVertex) {
+ if (direction == Direction.OUT)
return (T) temp.getInVertex();
+ else if (direction == Direction.IN)
+ return (T) temp.getOutVertex();
+ else {
+ if (temp.getInVertex() == vertex) {
+ return (T) temp.getOutVertex();
+ } else {
+ return (T) temp.getInVertex();
+ }
}
- }
- } else {
- return (T) temp;
+ } else {
+ return (T) temp;
+ }
}
- } else
- throw new NoSuchElementException();
+
+ if (!this.loadNext())
+ throw new NoSuchElementException();
+ }
}
public void remove() {
throw new UnsupportedOperationException();
}
- private void loadNext() {
- nextEdge = null;
- if (count >= limit) return;
- while (itty.hasNext() && nextEdge == null) {
- Edge edge = itty.next();
+ private boolean loadNext() {
+ this.nextEdge = null;
+ if (count >= limit) return false;
+ while (this.itty.hasNext()) {
+ final Edge edge = this.itty.next();
boolean filter = false;
for (final HasContainer hasContainer : hasContainers) {
if (!hasContainer.isLegal(edge)) {
@@ -207,11 +215,11 @@ private void loadNext() {
if (!filter) {
this.nextEdge = edge;
this.count++;
+ return true;
}
}
+ return false;
}
-
-
}
}
}
@@ -31,14 +31,6 @@ public TinkerVertex getRawVertex() {
return this;
}
- private static List<Edge> getAllEdges(final Map<String, Set<Edge>> theEdges) {
- final List<Edge> totalEdges = new LinkedList<Edge>();
- for (final Collection<Edge> edges : theEdges.values()) {
- totalEdges.addAll(edges);
- }
- return totalEdges;
- }
-
public Iterable<Edge> getInEdges(final String... labels) {
if (labels.length == 0) {
final List<Edge> totalEdges = new LinkedList<Edge>();
@@ -50,6 +50,6 @@ public int hashCode() {
}
public boolean equals(Object object) {
- return (object.getClass().equals(this.getClass())) && this.rawElement.getId().equals(((ReadOnlyElement) object).getId());
+ return null != object && (object.getClass().equals(this.getClass())) && this.rawElement.getId().equals(((ReadOnlyElement) object).getId());
}
}
@@ -36,7 +36,7 @@ public Object getId() {
}
public boolean equals(final Object object) {
- return null != object && this.getClass().equals(object.getClass()) && this.getId().equals(((Element) object).getId());
+ return null != object && (object.getClass().equals(this.getClass())) && this.rawElement.getId().equals(((WrappedElement) object).getId());
}
public int hashCode() {
@@ -16,7 +16,7 @@ protected IndexableGraph createGraph() {
System.out.println(b.stringValue());
System.exit(1);
*/
-
+
return new TinkerGraph();
}
}
Oops, something went wrong.

0 comments on commit 1a19b55

Please sign in to comment.