Permalink
Browse files

Speed improvements to MultiIterable. Simplification of TinkerGraph in…

…ternal code. Moved Parameter to base pgm interfaces along with Filter. Taking advantage of Neo4js var args on RelationshipType.
  • Loading branch information...
okram committed Apr 26, 2012
1 parent 5841312 commit a84a39142f3fcc17c7f247c622dd0fece84ebfef
Showing with 104 additions and 91 deletions.
  1. +3 −0 CHANGELOG.textile
  2. +0 −2 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/IndexableGraph.java
  3. +4 −1 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/{impls → }/Parameter.java
  4. +25 −18 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/impls/MultiIterable.java
  5. +1 −1 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/impls/tg/TinkerGraph.java
  6. +16 −31 blueprints-core/src/main/java/com/tinkerpop/blueprints/pgm/impls/tg/TinkerVertex.java
  7. +1 −1 ...ints-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/event/EventIndexableGraph.java
  8. +1 −1 ...e/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/partition/PartitionIndexableGraph.java
  9. +1 −1 ...ore/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/readonly/ReadOnlyIndexableGraph.java
  10. +1 −1 ...-core/src/main/java/com/tinkerpop/blueprints/pgm/util/wrappers/wrapped/WrappedIndexableGraph.java
  11. +1 −1 blueprints-dex-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/dex/DexGraph.java
  12. +1 −1 ...rints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jAutomaticIndex.java
  13. +5 −10 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jElement.java
  14. +1 −1 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jGraph.java
  15. +1 −1 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jIndex.java
  16. +11 −11 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jVertex.java
  17. +1 −1 blueprints-neo4j-graph/src/test/java/com/tinkerpop/blueprints/pgm/impls/neo4j/Neo4jGraphTest.java
  18. +1 −1 ...neo4jbatch-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/neo4jbatch/Neo4jBatchGraph.java
  19. +1 −1 ...jbatch-graph/src/test/java/com/tinkerpop/blueprints/pgm/impls/neo4jbatch/Neo4jBatchGraphTest.java
  20. +1 −1 blueprints-orient-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/orientdb/OrientGraph.java
  21. +4 −4 ...t-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/orientdb/util/OrientElementSequence.java
  22. +1 −1 blueprints-rexster-graph/src/main/java/com/tinkerpop/blueprints/pgm/impls/rexster/RexsterGraph.java
  23. +22 −0 blueprints-test/src/test/java/com/tinkerpop/blueprints/pgm/impls/MultiIterableTest.java
View
@@ -20,6 +20,9 @@ h3. Version 1.3 (NOT OFFICIALLY RELEASED YET)
* @RexsterGraph@ supports Rexster Authentication
* @Neo4jElement@ will accept a @Collection@ as a property (as long as all items within it are of the same data type), internally converting it to an array for storage in Neo4j
* Fixed a couple of API naming convention errors in @PartitionGraph@
+* Updated core @Vertex@ API to support the notion of 'push down predicates' (lower level element filtering)
+* Added internal support for Neo4j's @Node@ var args of @RelationshipTypes@
+* Speed improvements to the much used @MultiIterable@
==<hr/>==
@@ -1,7 +1,5 @@
package com.tinkerpop.blueprints.pgm;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
-
import java.util.Set;
/**
@@ -1,8 +1,11 @@
-package com.tinkerpop.blueprints.pgm.impls;
+package com.tinkerpop.blueprints.pgm;
import java.util.Map;
/**
+ * A Parameter is simply a pair of objects of type K and of type V.
+ * This is used in situations where a key/value pair is needed for configuration.
+ *
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class Parameter<K, V> implements Map.Entry<K, V> {
@@ -1,11 +1,13 @@
package com.tinkerpop.blueprints.pgm.impls;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
+ * A helper class that is used to combine multiple iterables into a single iterable.
+ *
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class MultiIterable<S> implements Iterable<S> {
@@ -18,44 +20,49 @@ public MultiIterable(final List<Iterable<S>> iterables) {
public Iterator<S> iterator() {
if (this.iterables.size() == 0) {
- return new ArrayList<S>().iterator();
+ return (Iterator<S>) Collections.emptyList().iterator();
} else {
- return new MultiIterator<S>(iterables);
+ return new MultiIterator();
}
}
- protected class MultiIterator<S> implements Iterator<S> {
+ protected class MultiIterator implements Iterator<S> {
- private final List<Iterator<S>> iterators = new ArrayList<Iterator<S>>();
+ private Iterator<S> currentIterator;
private int current = 0;
- public MultiIterator(final List<Iterable<S>> iterables) {
- for (final Iterable<S> iterable : iterables) {
- this.iterators.add(iterable.iterator());
- }
+ public MultiIterator() {
+ currentIterator = iterables.get(0).iterator();
}
public void remove() {
- this.iterators.get(this.current).remove();
+ currentIterator.remove();
}
public boolean hasNext() {
- while (this.current < this.iterators.size()) {
- if (this.iterators.get(this.current).hasNext()) {
+ while (true) {
+ if (currentIterator.hasNext()) {
return true;
+ } else {
+ this.current++;
+ if (this.current >= iterables.size())
+ break;
+ this.currentIterator = iterables.get(this.current).iterator();
}
- this.current++;
}
return false;
}
public S next() {
- while (this.current < this.iterators.size()) {
- final Iterator<S> temp = this.iterators.get(this.current);
- if (temp.hasNext()) {
- return temp.next();
+ while (true) {
+ if (currentIterator.hasNext()) {
+ return currentIterator.next();
+ } else {
+ this.current++;
+ if (this.current >= iterables.size())
+ break;
+ this.currentIterator = iterables.get(current).iterator();
}
- this.current++;
}
throw new NoSuchElementException();
}
@@ -6,8 +6,8 @@
import com.tinkerpop.blueprints.pgm.Element;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.Vertex;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
import com.tinkerpop.blueprints.pgm.util.AutomaticIndexHelper;
@@ -16,7 +16,6 @@
import java.util.Map;
import java.util.Set;
-
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
@@ -33,13 +32,17 @@ 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 Object... filters) {
if (filters.length == 0) {
- final List<Edge> totalEdges = new LinkedList<Edge>();
- for (final Collection<Edge> edges : this.inEdges.values()) {
- totalEdges.addAll(edges);
- }
- return totalEdges;
+ return getAllEdges(this.inEdges);
} else if (filters.length == 1) {
if (filters[0] instanceof String) {
final Set<Edge> edges = this.inEdges.get(filters[0]);
@@ -49,20 +52,15 @@ public TinkerVertex getRawVertex() {
return new LinkedList<Edge>(edges);
}
} else if (filters[0] instanceof Filter) {
- final List<Edge> totalEdges = new LinkedList<Edge>();
- for (final Collection<Edge> edges : this.inEdges.values()) {
- totalEdges.addAll(edges);
- }
-
- return new FilteredEdgeIterable(totalEdges, FilteredEdgeIterable.getFilter(filters));
+ return new FilteredEdgeIterable(getAllEdges(this.inEdges), (Filter) filters[0]);
} else {
throw new IllegalArgumentException(Vertex.TYPE_ERROR_MESSAGE);
}
} else {
final Filter filter = FilteredEdgeIterable.getFilter(filters);
final List<String> labels = FilteredEdgeIterable.getLabels(filters);
- final List<Edge> totalEdges = new LinkedList<Edge>();
+ List<Edge> totalEdges = new LinkedList<Edge>();
for (final String label : labels) {
final Set<Edge> edges = this.inEdges.get(label);
if (null != edges) {
@@ -74,9 +72,7 @@ public TinkerVertex getRawVertex() {
return totalEdges;
if (labels.isEmpty()) {
- for (final Collection<Edge> edges : this.inEdges.values()) {
- totalEdges.addAll(edges);
- }
+ totalEdges = getAllEdges(this.inEdges);
}
if (null == filter)
@@ -88,11 +84,7 @@ public TinkerVertex getRawVertex() {
public Iterable<Edge> getOutEdges(final Object... filters) {
if (filters.length == 0) {
- final List<Edge> totalEdges = new LinkedList<Edge>();
- for (final Collection<Edge> edges : this.outEdges.values()) {
- totalEdges.addAll(edges);
- }
- return totalEdges;
+ return getAllEdges(this.outEdges);
} else if (filters.length == 1) {
if (filters[0] instanceof String) {
final Set<Edge> edges = this.outEdges.get(filters[0]);
@@ -102,20 +94,15 @@ public TinkerVertex getRawVertex() {
return new LinkedList<Edge>(edges);
}
} else if (filters[0] instanceof Filter) {
- final List<Edge> totalEdges = new LinkedList<Edge>();
- for (final Collection<Edge> edges : this.outEdges.values()) {
- totalEdges.addAll(edges);
- }
-
- return new FilteredEdgeIterable(totalEdges, FilteredEdgeIterable.getFilter(filters));
+ return new FilteredEdgeIterable(getAllEdges(this.outEdges), (Filter) filters[0]);
} else {
throw new IllegalArgumentException(Vertex.TYPE_ERROR_MESSAGE);
}
} else {
final Filter filter = FilteredEdgeIterable.getFilter(filters);
final List<String> labels = FilteredEdgeIterable.getLabels(filters);
- final List<Edge> totalEdges = new LinkedList<Edge>();
+ List<Edge> totalEdges = new LinkedList<Edge>();
for (final String label : labels) {
final Set<Edge> edges = this.outEdges.get(label);
if (null != edges) {
@@ -127,9 +114,7 @@ public TinkerVertex getRawVertex() {
return totalEdges;
if (labels.isEmpty()) {
- for (final Collection<Edge> edges : this.outEdges.values()) {
- totalEdges.addAll(edges);
- }
+ totalEdges = getAllEdges(this.outEdges);
}
if (null == filter)
@@ -5,8 +5,8 @@
import com.tinkerpop.blueprints.pgm.Element;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.WrappableGraph;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.util.wrappers.event.util.EventIndexSequence;
import java.util.Set;
@@ -4,8 +4,8 @@
import com.tinkerpop.blueprints.pgm.Element;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.WrappableGraph;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.util.wrappers.partition.util.PartitionIndexSequence;
import java.util.Set;
@@ -4,8 +4,8 @@
import com.tinkerpop.blueprints.pgm.Element;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.WrappableGraph;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.util.wrappers.readonly.util.ReadOnlyIndexSequence;
import java.util.Set;
@@ -4,8 +4,8 @@
import com.tinkerpop.blueprints.pgm.Element;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.WrappableGraph;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.util.wrappers.wrapped.util.WrappedIndexSequence;
import java.util.Set;
@@ -6,9 +6,9 @@
import com.tinkerpop.blueprints.pgm.Element;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.WrappableGraph;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
import com.tinkerpop.blueprints.pgm.impls.dex.util.DexAttributes;
import com.tinkerpop.blueprints.pgm.impls.dex.util.DexTypes;
@@ -1,7 +1,7 @@
package com.tinkerpop.blueprints.pgm.impls.neo4j;
import com.tinkerpop.blueprints.pgm.AutomaticIndex;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
+import com.tinkerpop.blueprints.pgm.Parameter;
import org.neo4j.graphdb.PropertyContainer;
import java.util.HashSet;
@@ -40,10 +40,9 @@ public void setProperty(final String key, final Object value) {
throw new RuntimeException(key + StringFactory.PROPERTY_EXCEPTION_MESSAGE);
try {
-
// attempts to take a collection and convert it to an array so that Neo4j can consume it
final Object convertedValue = tryConvertCollectionToArray(value);
-
+
this.graph.autoStartTransaction();
Object oldValue = this.getProperty(key);
@@ -116,18 +115,16 @@ public Object getId() {
public boolean equals(final Object object) {
return (null != object) && (this.getClass().equals(object.getClass()) && this.getId().equals(((Element) object).getId()));
}
-
+
private Object tryConvertCollectionToArray(final Object value) {
- if (value instanceof Collection<?>)
- {
+ if (value instanceof Collection<?>) {
// convert this collection to an array. the collection must
// be all of the same type.
try {
final Collection<?> collection = (Collection<?>) value;
Object[] array = null;
final Iterator<?> objects = collection.iterator();
- for (int i = 0; objects.hasNext(); i++)
- {
+ for (int i = 0; objects.hasNext(); i++) {
Object object = objects.next();
if (array == null) {
array = (Object[]) Array.newInstance(object.getClass(), collection.size());
@@ -140,9 +137,7 @@ private Object tryConvertCollectionToArray(final Object value) {
// this fires off if the collection is not all of the same type
return value;
}
- }
- else
- {
+ } else {
return value;
}
}
@@ -5,10 +5,10 @@
import com.tinkerpop.blueprints.pgm.Element;
import com.tinkerpop.blueprints.pgm.Index;
import com.tinkerpop.blueprints.pgm.IndexableGraph;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.TransactionalGraph;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.WrappableGraph;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
import com.tinkerpop.blueprints.pgm.impls.neo4j.util.Neo4jEdgeSequence;
import com.tinkerpop.blueprints.pgm.impls.neo4j.util.Neo4jVertexSequence;
@@ -4,9 +4,9 @@
import com.tinkerpop.blueprints.pgm.CloseableSequence;
import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Index;
+import com.tinkerpop.blueprints.pgm.Parameter;
import com.tinkerpop.blueprints.pgm.TransactionalGraph;
import com.tinkerpop.blueprints.pgm.Vertex;
-import com.tinkerpop.blueprints.pgm.impls.Parameter;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
import com.tinkerpop.blueprints.pgm.impls.neo4j.util.Neo4jEdgeSequence;
import com.tinkerpop.blueprints.pgm.impls.neo4j.util.Neo4jVertexSequence;
Oops, something went wrong.

0 comments on commit a84a391

Please sign in to comment.