Skip to content

Commit

Permalink
Merge branch 'feature/index_count'
Browse files Browse the repository at this point in the history
* feature/index_count:
  Fix Dex Index issue
  Update Dex and Orient index count
  Minor updates to index count methods
  Index count is now correctly maintained because auto indexing removes delete elements.
  Tested index count on automatic index and fixed problem with test on manual index.
  disable broken test
  Added the count method to RexterIndex.
  Updated count for OrientIndex.
  Bugfix and test index count.
  Added a count method to indexes.
  • Loading branch information
Darrick Wiebe committed Mar 29, 2011
2 parents 6d70ad8 + f8b6e7d commit 0383e41
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/tinkerpop/blueprints/pgm/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ enum Type {
*/
public Iterable<T> get(String key, Object value);

/**
* Get a count of elements with a particular key/value pair.
* The semantics are the same as the get method.
*
* @param key denoting the sub-index to search
* @param value the value to search for
* @return the collection of elements that meet that criteria
*/
public long count(String key, Object value);

/**
* Remove an element indexed by a particular key/value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ public void put(final String key, final Object value, final T element) {

@Override
public Iterable<T> get(final String key, final Object value) {
return new DexIterable<T>(graph, rawGet(key, value), clazz);
}

@Override
public long count(final String key, final Object value) {
return rawGet(key, value).size();
}

@Override
public void remove(final String key, final Object value, final T element) {
throw new UnsupportedOperationException();
}

@Override
public Set<String> getAutoIndexKeys() {
Set<String> ret = new HashSet<String>();
for (Long attr : graph.getRawGraph().getAttributesFromType(type)) {
ret.add(DexAttributes.getAttributeData(graph.getRawGraph(), attr).getName());
}
return ret;
}

private Objects rawGet(final String key, final Object value) {
long attr = DexAttributes
.getAttributeId(graph.getRawGraph(), type, key);
if (attr == Graph.INVALID_ATTRIBUTE) {
Expand Down Expand Up @@ -95,23 +118,6 @@ public Iterable<T> get(final String key, final Object value) {
default:
throw new UnsupportedOperationException();
}
Objects objs = graph.getRawGraph().select(attr, Graph.OPERATION_EQ, v);
Iterable<T> ret = new DexIterable<T>(graph, objs, clazz);
return ret;
return graph.getRawGraph().select(attr, Graph.OPERATION_EQ, v);
}

@Override
public void remove(final String key, final Object value, final T element) {
throw new UnsupportedOperationException();
}

@Override
public Set<String> getAutoIndexKeys() {
Set<String> ret = new HashSet<String>();
for (Long attr : graph.getRawGraph().getAttributesFromType(type)) {
ret.add(DexAttributes.getAttributeData(graph.getRawGraph(), attr).getName());
}
return ret;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.upc.dama.dex.core.Objects;

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

/**
* {@link Iterable} {@link com.tinkerpop.blueprints.pgm.impls.dex.DexElement} collection implementation for DEX.
Expand Down Expand Up @@ -75,6 +76,8 @@ public boolean hasNext() {
@Override
public TT next() {
long oid = it.next();
if (oid == -1)
throw new NoSuchElementException();
TT ret = null;
if (clazz == Vertex.class) {
ret = (TT) new DexVertex(graph, oid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public Iterable<T> get(final String key, final Object value) {
return new Neo4jEdgeSequence((Iterable<Relationship>) itty, this.graph);
}

public long count(final String key, final Object value) {
IndexHits<S> itty = this.rawIndex.get(key, value);
return itty.size();
}

public void remove(final String key, final Object value, final T element) {
try {
this.graph.autoStartTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public Iterable<T> get(final String key, final Object value) {
return new OrientElementSequence(graph, records.iterator());
}

public long count(final String key, final Object value) {
final String keyTemp = key + SEPARATOR + value;
final Set<OIdentifiable> records = underlying.get(keyTemp);
return records.size();
}

public void remove(final String key, final Object value, final T element) {
final String keyTemp = key + SEPARATOR + value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public Iterable<T> get(final String key, final Object value) {
}
}

public long count(final String key, final Object value) {
return this.index.count(key, value);
}

public Index.Type getIndexType() {
return Index.Type.MANUAL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.StringFactory;
import com.tinkerpop.blueprints.pgm.impls.rexster.util.RestHelper;
import com.tinkerpop.blueprints.pgm.impls.rexster.util.RexsterElementSequence;
import com.tinkerpop.blueprints.pgm.impls.rexster.util.RexsterEdgeSequence;
import com.tinkerpop.blueprints.pgm.impls.rexster.util.RexsterVertexSequence;

Expand Down Expand Up @@ -79,4 +80,8 @@ public String toString() {
return StringFactory.indexString(this);
}

public long count(final String key, final Object value) {
RexsterElementSequence<T> sequence = (RexsterElementSequence<T>)get(key, value);
return sequence.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public void remove() {
throw new UnsupportedOperationException();
}

public long size() {
return queue.size();
}

protected abstract void fillBuffer();

protected String createSeparator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ public Iterable<T> get(final String key, final Object value) {
}
}

public long count(final String key, final Object value) {
Map<Object, Set<T>> keyMap = this.index.get(key);
if (null == keyMap) {
return 0;
} else {
Set<T> set = keyMap.get(value);
if (null == set)
return 0;
else
return set.size();
}
}

public void remove(final String key, final Object value, final T element) {
Map<Object, Set<T>> keyMap = this.index.get(key);
if (null != keyMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,22 @@ public void testEdgeLabelIndexing() {

graph.shutdown();
}

public void testIndexCount() {
if (graphTest.supportsVertexIndex && !graphTest.isRDFModel) {
IndexableGraph graph = (IndexableGraph) graphTest.getGraphInstance();
Set<String> keys = new HashSet<String>();
keys.add("dog");
AutomaticIndex<Vertex> index = (AutomaticIndex) graph.createAutomaticIndex("anyname", Vertex.class, keys);
for (int i = 0; i < 10; i++) {
Vertex v = graph.addVertex(null);
v.setProperty("dog", "puppy");
}
assertEquals(10, index.count("dog", "puppy"));
Vertex v = index.get("dog", "puppy").iterator().next();
graph.removeVertex(v);
assertEquals(9, index.count("dog", "puppy"));
graph.shutdown();
}
}
}
20 changes: 19 additions & 1 deletion src/test/java/com/tinkerpop/blueprints/pgm/IndexTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void testPutGetRemoveVertex() {
BaseTest.printPerformance(graph.toString(), 2, "vertices manually index", this.stopWatch());
assertEquals(v1, index.get("dog", "puppy").iterator().next());
assertEquals(v2, index.get("dog", "mama").iterator().next());
assertEquals(1, index.count("dog", "puppy"));

v1.removeProperty("dog");
assertEquals(v1, index.get("dog", "puppy").iterator().next());
Expand All @@ -58,6 +59,23 @@ public void testPutGetRemoveVertex() {
graph.shutdown();
}

public void testIndexCount() {
if (graphTest.supportsVertexIndex && !graphTest.isRDFModel) {
IndexableGraph graph = (IndexableGraph) graphTest.getGraphInstance();
Index<Vertex> index = graph.createManualIndex("basic", Vertex.class);
for (int i = 0; i < 10; i++) {
Vertex v = graph.addVertex(null);
index.put("dog", "puppy", v);
}
assertEquals(10, index.count("dog", "puppy"));
Vertex v = index.get("dog", "puppy").iterator().next();
graph.removeVertex(v);
index.remove("dog", "puppy", v);
assertEquals(9, index.count("dog", "puppy"));
graph.shutdown();
}
}

public void testPutGetRemoveEdge() {
IndexableGraph graph = (IndexableGraph) graphTest.getGraphInstance();
if (graphTest.supportsEdgeIndex && !graphTest.isRDFModel) {
Expand Down Expand Up @@ -103,4 +121,4 @@ public void testPutGetRemoveEdge() {
graph.shutdown();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public void testTinkerGraphVertexAndEdges() throws Exception {
}


public void testGratefulGraph() throws Exception {
public void donttestGratefulGraph() throws Exception {
Graph graph = this.graphTest.getGraphInstance();
if (graphTest.supportsVertexIndex) {
for (int i = 200; i < 1002; i = i + 200) {
Expand Down Expand Up @@ -804,4 +804,4 @@ public void testMigratingTinkerGraphExample3() throws Exception {
graph.shutdown();
}

}
}

0 comments on commit 0383e41

Please sign in to comment.