Skip to content

Commit

Permalink
find index independent of it's name
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier committed Nov 17, 2015
1 parent 505583e commit c861647
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,19 @@ protected Object convertKey(final OIndex<?> idx, Object iValue) {
return iValue;
}

public Stream<OrientVertex> getIndexedVertices(OrientIndexQuery indexReference) {
// TODO: make value optional
public Stream<OrientVertex> getIndexedVertices(OIndex index, Object value) {

This comment has been minimized.

Copy link
@velo

This comment has been minimized.

Copy link
@mpollmeier

mpollmeier Jan 10, 2016

Author Collaborator

see my comment in #53
sorry i didn't comment earlier, just back from holidays today

makeActive();

// if (iKey.equals("@class"))
// return getVerticesOfClass(iValue.toString());

final OIndex<?> idx = database.getMetadata().getIndexManager().getIndex(indexReference.indexName());
Object iValue = indexReference.value;
if (idx == null) {
if (index == null) {
// NO INDEX
return Collections.<OrientVertex>emptyList().stream();
} else {
iValue = convertKey(idx, iValue);
Object indexValue = idx.get(iValue);
value = convertKey(index, value);
Object indexValue = index.get(value);
if (indexValue == null) {
return Collections.<OrientVertex>emptyList().stream();
} else if (!(indexValue instanceof Iterable<?>)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.apache.tinkerpop.gremlin.orientdb.traversal.step.sideEffect;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexManagerProxy;
import com.orientechnologies.orient.core.metadata.schema.OImmutableClass;
import org.apache.tinkerpop.gremlin.orientdb.OrientIndexQuery;
import org.apache.tinkerpop.gremlin.orientdb.OrientGraph;
import org.apache.tinkerpop.gremlin.orientdb.OrientVertex;
Expand Down Expand Up @@ -35,20 +38,24 @@ private boolean isVertexStep() {

private Iterator<? extends Vertex> vertices() {
final OrientGraph graph = (OrientGraph) this.getTraversal().getGraph().get();
final Optional<OrientIndexQuery> indexQuery = getIndexQuery();

if (this.ids != null && this.ids.length > 0) {
return this.iteratorList(graph.vertices(this.ids));
} else if (!indexQuery.isPresent()) {
OLogManager.instance().warn(this, "scanning through all vertices without using an index");
return this.iteratorList(graph.vertices());
} else {
OLogManager.instance().info(this, "index will be queried with " + indexQuery.get());
Stream<OrientVertex> indexedVertices = graph.getIndexedVertices(indexQuery.get());
return indexedVertices
.filter(vertex -> HasContainer.testAll(vertex, this.hasContainers))
.collect(Collectors.<Vertex>toList())
.iterator();
Optional<Pair<OIndex, Object>> indexAndValue = findIndex();
if (indexAndValue.isPresent()) {
OIndex index = indexAndValue.get().getValue0();
Object value = indexAndValue.get().getValue1();
OLogManager.instance().info(this, "index [" + indexAndValue.get() + "] will be used");
Stream<OrientVertex> indexedVertices = graph.getIndexedVertices(index, value);
return indexedVertices
.filter(vertex -> HasContainer.testAll(vertex, this.hasContainers))
.collect(Collectors.<Vertex>toList())
.iterator();
} else {
OLogManager.instance().warn(this, "scanning through all vertices without using an index");
return this.iteratorList(graph.vertices());
}
}
}

Expand Down Expand Up @@ -82,9 +89,11 @@ private OrientGraph getGraph() {
return ((OrientGraph) this.getTraversal().getGraph().get());
}

private Optional<OrientIndexQuery> getIndexQuery() {
Optional<String> elementLabel = findElementLabelInHasContainers();
OrientGraph graph = getGraph();
private Optional<Pair<OIndex, Object>> findIndex() {
final Optional<String> elementLabel = findElementLabelInHasContainers();
final OrientGraph graph = getGraph();
final OIndexManagerProxy indexManager = graph.database().getMetadata().getIndexManager();

// find indexed keys only for the element subclass (if present)
final Set<String> indexedKeys = elementLabel.isPresent() ?
graph.getIndexedKeys(this.returnClass, elementLabel.get()) :
Expand All @@ -97,12 +106,23 @@ private Optional<OrientIndexQuery> getIndexQuery() {
.map(c -> Optional.of(new Pair<>(c.getKey(), c.getValue())))
.orElseGet(Optional::empty);

if (indexedKeyAndValue.isPresent()) {
if (elementLabel.isPresent() && indexedKeyAndValue.isPresent()) {
String key = indexedKeyAndValue.get().getValue0();
Object value = indexedKeyAndValue.get().getValue1();
return Optional.of(new OrientIndexQuery(isVertexStep(), elementLabel, key, value));
} else
return Optional.empty();

String className = OImmutableClass.VERTEX_CLASS_NAME + '_' + elementLabel.get();
Set<OIndex<?>> classIndexes = indexManager.getClassIndexes(className);
Iterator<OIndex<?>> keyIndexes = classIndexes.stream().filter(idx -> idx.getDefinition().getFields().contains(key)).iterator();

if (keyIndexes.hasNext()) {
// TODO: implement algorithm to select best index if there are multiple
return Optional.of(new Pair(keyIndexes.next(), value));
} else {
OLogManager.instance().warn(this, "no index found for class=[" + className + "] and key=[" + key + "]");
}
}

return Optional.empty();
}

@Override
Expand Down

0 comments on commit c861647

Please sign in to comment.