Skip to content

Commit

Permalink
support has filter unable to use index (apache#24)
Browse files Browse the repository at this point in the history
Change-Id: Idda255970506c07d19ac373b3b2c2cd6ee9200d1
  • Loading branch information
zhoney authored Sep 23, 2021
1 parent 08aafc8 commit a0b72f4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.baidu.hugegraph.backend.query.ConditionQuery;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.exception.NoIndexException;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.util.Log;

Expand Down Expand Up @@ -98,6 +99,7 @@ private long edgesCount() {
return IteratorUtils.count(this.edges());
}

@SuppressWarnings("unchecked")
private Iterator<E> vertices() {
LOG.debug("HugeGraphStep.vertices(): {}", this);

Expand All @@ -113,11 +115,19 @@ private Iterator<E> vertices() {
}

Query query = this.makeQuery(graph, HugeType.VERTEX);
@SuppressWarnings("unchecked")
Iterator<E> result = (Iterator<E>) graph.vertices(query);
return result;
Iterator<E> result;
try {
return (Iterator<E>) graph.vertices(query);
} catch (NoIndexException e) {
LOG.warn("Can't query vertex by index, will query all and filter:" +
" {}", e);
}
query = new Query(HugeType.VERTEX);
result = (Iterator<E>) graph.vertices(query);
return TraversalUtil.filterResult(this.hasContainers, result);
}

@SuppressWarnings("unchecked")
private Iterator<E> edges() {
LOG.debug("HugeGraphStep.edges(): {}", this);

Expand All @@ -134,9 +144,17 @@ private Iterator<E> edges() {
}

Query query = this.makeQuery(graph, HugeType.EDGE);
@SuppressWarnings("unchecked")
Iterator<E> result = (Iterator<E>) graph.edges(query);
return result;

Iterator<E> result;
try {
return (Iterator<E>) graph.edges(query);
} catch (NoIndexException e) {
LOG.warn("Can't query edge by index, will query all and filter:" +
" {}", e);
}
query = new Query(HugeType.EDGE);
result = (Iterator<E>) graph.edges(query);
return TraversalUtil.filterResult(this.hasContainers, result);
}

private boolean hasIds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5913,14 +5913,7 @@ public void testQueryEdgeByUniqueIndex() {
marko.addEdge("like", vadas, "weight", 0.5);
graph().tx().commit();

Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().E().hasLabel("like").has("weight", 0.5).next();
}, e -> {
Assert.assertEquals("Don't accept query based on properties " +
"[weight] that are not indexed in label " +
"'like', may not match secondary condition",
e.getMessage());
});
graph().traversal().E().hasLabel("like").has("weight", 0.5).next();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,7 @@ public void testAddEdgeLabelWithDisableLabelIndex() {
graph.tx().commit();

if (!storeFeatures().supportsQueryByLabel()) {
Assert.assertThrows(NoIndexException.class, () -> {
graph.traversal().E().hasLabel("write").toList();
});
graph.traversal().E().hasLabel("write").toList();
} else {
List<Edge> edges = graph.traversal().E().hasLabel("write")
.toList();
Expand Down Expand Up @@ -991,13 +989,7 @@ public void testRebuildIndexOfEdgeLabelWithoutLabelIndex() {
initDataWithoutLabelIndex();

// Not support query by label
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().E().hasLabel("read").toList();
}, e -> {
Assert.assertTrue(
e.getMessage().startsWith("Don't accept query by label") &&
e.getMessage().endsWith("label index is disabled"));
});
graph().traversal().E().hasLabel("read").toList();

// Query by property index is ok
List<Edge> edges = graph().traversal().E()
Expand All @@ -1020,13 +1012,7 @@ public void testRemoveEdgeLabelWithoutLabelIndex() {
initDataWithoutLabelIndex();

// Not support query by label
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().E().hasLabel("read").toList();
}, e -> {
Assert.assertTrue(
e.getMessage().startsWith("Don't accept query by label") &&
e.getMessage().endsWith("label index is disabled"));
});
graph().traversal().E().hasLabel("read").toList();

// Query by property index is ok
List<Edge> edges = graph().traversal().E()
Expand All @@ -1036,10 +1022,8 @@ public void testRemoveEdgeLabelWithoutLabelIndex() {

graph().schema().edgeLabel("read").remove();

Assert.assertThrows(NoIndexException.class, () ->
graph().traversal().E()
.has("date", P.lt("2019-12-30 13:00:00")).toList()
);
graph().traversal().E()
.has("date", P.lt("2019-12-30 13:00:00")).toList();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,8 @@ public void testAddIndexLabelOfVertexWithVertexExist() {
"city", "Hongkong", "age", 3);
graph().tx().commit();

Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("person")
.has("city", "Hongkong").next();
});
graph().traversal().V().hasLabel("person")
.has("city", "Hongkong").next();

schema.indexLabel("personByCity").onV("person").secondary()
.by("city").create();
Expand All @@ -253,10 +251,8 @@ public void testAddIndexLabelOfVertexWithVertexExist() {
.has("city", "Hongkong").next();
Assert.assertNotNull(vertex);

Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("person")
.has("age", P.inside(2, 4)).next();
});
graph().traversal().V().hasLabel("person")
.has("age", P.inside(2, 4)).next();
schema.indexLabel("personByAge").onV("person").range()
.by("age").create();

Expand Down Expand Up @@ -284,10 +280,9 @@ public void testAddIndexLabelOfEdgeWithEdgeExist() {

graph().tx().commit();

Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().E().hasLabel("authored")
.has("contribution", "test").next();
});

graph().traversal().E().hasLabel("authored")
.has("contribution", "test").next();

schema.indexLabel("authoredByContri").onE("authored")
.secondary().by("contribution").create();
Expand Down Expand Up @@ -1276,10 +1271,8 @@ public void testRemoveIndexLabelOfVertex() {
assertNotContainsIl(person.indexLabels(), "personByCity");
assertContainsIl(person.indexLabels(), "personByAge");

Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("person")
.has("city", "Hongkong").next();
});
graph().traversal().V().hasLabel("person")
.has("city", "Hongkong").next();
vertex = graph().traversal().V().hasLabel("person")
.has("age", P.inside(2, 4)).next();
Assert.assertNotNull(vertex);
Expand All @@ -1293,10 +1286,8 @@ public void testRemoveIndexLabelOfVertex() {
person = schema.getVertexLabel("person");
Assert.assertEquals(0, person.indexLabels().size());

Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("person")
.has("age", P.inside(2, 4)).next();
});
graph().traversal().V().hasLabel("person")
.has("age", P.inside(2, 4)).next();
}

@Test
Expand Down Expand Up @@ -1344,10 +1335,9 @@ public void testRemoveIndexLabelOfEdge() {
authored = schema.getEdgeLabel("authored");
Assert.assertEquals(0, authored.indexLabels().size());

Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().E().hasLabel("authored")
.has("contribution", "test").next();
});
graph().traversal().E().hasLabel("authored")
.has("contribution", "test").next();

}

@Test
Expand Down Expand Up @@ -1520,13 +1510,7 @@ public void testRebuildIndexOfVertexWithoutLabelIndex() {
initDataWithoutLabelIndex();

// Not support query by label
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("reader").toList();
}, e -> {
Assert.assertTrue(
e.getMessage().startsWith("Don't accept query by label") &&
e.getMessage().endsWith("label index is disabled"));
});
graph().traversal().V().hasLabel("reader").toList();

// Query by property index is ok
List<Vertex> vertices = graph().traversal().V()
Expand All @@ -1548,13 +1532,7 @@ public void testRebuildIndexOfEdgeWithoutLabelIndex() {
initDataWithoutLabelIndex();

// Not support query by label
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().E().hasLabel("read").toList();
}, e -> {
Assert.assertTrue(
e.getMessage().startsWith("Don't accept query by label") &&
e.getMessage().endsWith("label index is disabled"));
});
graph().traversal().E().hasLabel("read").toList();

// Query by property index is ok
List<Edge> edges = graph().traversal().E()
Expand All @@ -1577,13 +1555,7 @@ public void testRemoveIndexLabelOfVertexWithoutLabelIndex() {
initDataWithoutLabelIndex();

// Not support query by label
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("reader").toList();
}, e -> {
Assert.assertTrue(
e.getMessage().startsWith("Don't accept query by label") &&
e.getMessage().endsWith("label index is disabled"));
});
graph().traversal().V().hasLabel("reader").toList();

// Query by property index is ok
List<Vertex> vertices = graph().traversal().V()
Expand All @@ -1592,9 +1564,7 @@ public void testRemoveIndexLabelOfVertexWithoutLabelIndex() {

graph().schema().indexLabel("readerByCity").remove();

Assert.assertThrows(NoIndexException.class, () ->
graph().traversal().V().has("city", "Shanghai").toList()
);
graph().traversal().V().has("city", "Shanghai").toList();
}

@Test
Expand All @@ -1605,13 +1575,7 @@ public void testRemoveIndexLabelOfEdgeWithoutLabelIndex() {
initDataWithoutLabelIndex();

// Not support query by label
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().E().hasLabel("read").toList();
}, e -> {
Assert.assertTrue(
e.getMessage().startsWith("Don't accept query by label") &&
e.getMessage().endsWith("label index is disabled"));
});
graph().traversal().E().hasLabel("read").toList();

// Query by property index is ok
List<Edge> edges = graph().traversal().E()
Expand All @@ -1621,10 +1585,9 @@ public void testRemoveIndexLabelOfEdgeWithoutLabelIndex() {

graph().schema().indexLabel("readByDate").remove();

Assert.assertThrows(NoIndexException.class, () ->
graph().traversal().E()
.has("date", P.lt("2019-12-30 13:00:00")).toList()
);
graph().traversal().E()
.has("date", P.lt("2019-12-30 13:00:00")).toList();

}

@Test
Expand Down
Loading

0 comments on commit a0b72f4

Please sign in to comment.