diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java index 211cbff7a9..e5d300b075 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java @@ -182,7 +182,7 @@ protected void updateIndex(Id ilId, HugeElement element, boolean removed) { } } - if (firstNullField == 0 && !indexLabel.indexType().isUniuqe()) { + if (firstNullField == 0 && !indexLabel.indexType().isUnique()) { // The property value of first index field is null return; } @@ -592,7 +592,11 @@ private MatchedIndex collectMatchedIndex(SchemaLabel schemaLabel, SchemaTransaction schema = this.graph().schemaTransaction(); Set ils = InsertionOrderUtil.newSet(); for (Id il : schemaLabel.indexLabels()) { - ils.add(schema.getIndexLabel(il)); + IndexLabel indexLabel = schema.getIndexLabel(il); + if (indexLabel.indexType().isUnique()) { + continue; + } + ils.add(indexLabel); } if (ils.isEmpty()) { return null; @@ -671,8 +675,8 @@ private boolean needIndexForLabel() { private static Set matchSingleOrCompositeIndex( ConditionQuery query, Set indexLabels) { - boolean reqiureRange = query.hasRangeCondition(); - boolean reqiureSearch = query.hasSearchCondition(); + boolean requireRange = query.hasRangeCondition(); + boolean requireSearch = query.hasSearchCondition(); Set queryPropKeys = query.userpropKeys(); for (IndexLabel indexLabel : indexLabels) { List indexFields = indexLabel.indexFields(); @@ -690,11 +694,11 @@ private static Set matchSingleOrCompositeIndex( * 4.secondary (composite) index */ IndexType indexType = indexLabel.indexType(); - if ((reqiureSearch && !indexType.isSearch()) || - (!reqiureSearch && indexType.isSearch())) { + if ((requireSearch && !indexType.isSearch()) || + (!requireSearch && indexType.isSearch())) { continue; } - if (reqiureRange && !indexType.isNumeric()) { + if (requireRange && !indexType.isNumeric()) { continue; } return ImmutableSet.of(indexLabel); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java index 0320c9ff8c..0901960cbc 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/IndexLabelBuilder.java @@ -507,8 +507,8 @@ private Set removeSubIndex(SchemaLabel schemaLabel) { * 2. existed index label is prefix of new created index label * (except for unique index) */ - if (this.indexType.isUniuqe() && oldFields.containsAll(newFields) || - !this.indexType.isUniuqe() && + if (this.indexType.isUnique() && oldFields.containsAll(newFields) || + !this.indexType.isUnique() && CollectionUtil.prefixOf(oldFields, newFields)) { overrideIndexLabelIds.add(id); } @@ -528,7 +528,7 @@ private void checkPrimaryKeyIndex(SchemaLabel schemaLabel) { VertexLabel vl = (VertexLabel) schemaLabel; if (vl.idStrategy().isPrimaryKey()) { if (this.indexType.isSecondary() || - this.indexType.isUniuqe() || + this.indexType.isUnique() || this.indexType.isShard() && this.allStringIndex(this.indexFields)) { List pks = this.transaction.graph() diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IndexType.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IndexType.java index 01ce214053..445b522d0e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IndexType.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IndexType.java @@ -116,7 +116,7 @@ public boolean isShard() { return this == SHARD; } - public boolean isUniuqe() { + public boolean isUnique() { return this == UNIQUE; } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java index cff23096cc..1201748a44 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java @@ -57,6 +57,7 @@ import com.baidu.hugegraph.backend.tx.GraphTransaction; import com.baidu.hugegraph.config.CoreOptions; import com.baidu.hugegraph.exception.LimitExceedException; +import com.baidu.hugegraph.exception.NoIndexException; import com.baidu.hugegraph.exception.NotFoundException; import com.baidu.hugegraph.schema.SchemaManager; import com.baidu.hugegraph.structure.HugeEdge; @@ -3533,6 +3534,43 @@ public void testUpdatePropertyToValueOfRemovedEdgeWithUniqueIndex() { graph().tx().commit(); } + @Test + public void testQueryEdgeByUniqueIndex() { + SchemaManager schema = graph().schema(); + schema.propertyKey("weight").asDouble().ifNotExist().create(); + schema.vertexLabel("user") + .properties("name") + .primaryKeys("name") + .ifNotExist() + .create(); + schema.edgeLabel("like") + .sourceLabel("user") + .targetLabel("user") + .properties("weight") + .ifNotExist() + .create(); + schema.indexLabel("likeByWeight") + .onE("like") + .by("weight") + .unique() + .ifNotExist() + .create(); + + Vertex marko = graph().addVertex(T.label, "user", "name", "marko"); + Vertex vadas = graph().addVertex(T.label, "user", "name", "vadas"); + 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()); + }); + } + @Test public void testUpdateEdgePropertyOfAggregateType() { Assume.assumeTrue("Not support aggregate property", diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java index efba4c8643..33e44059e6 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java @@ -3508,6 +3508,35 @@ public void testAddVerticesWithUniqueIndexForNullableProperties() { }); } + @Test + public void testQueryByUniqueIndex() { + HugeGraph graph = this.graph(); + SchemaManager schema = graph.schema(); + schema.vertexLabel("node") + .properties("name") + .useAutomaticId() + .ifNotExist() + .create(); + schema.indexLabel("nodeByName") + .unique() + .onV("node") + .by("name") + .ifNotExist() + .create(); + + graph.addVertex(T.label, "node", "name", "tom"); + graph.tx().commit(); + + Assert.assertThrows(NoIndexException.class, () -> { + graph.traversal().V().hasLabel("node").has("name", "tom").next(); + }, (e) -> { + Assert.assertEquals("Don't accept query based on properties " + + "[name] that are not indexed in label 'node'," + + " may not match secondary condition", + e.getMessage()); + }); + } + @Test public void testRemoveVertex() { HugeGraph graph = graph(); @@ -3556,6 +3585,7 @@ public void testRemoveVertexOfNotExists() { graph.tx().commit(); } + @Test public void testRemoveVertexAfterAddVertexWithTx() { HugeGraph graph = graph(); GraphTransaction tx = graph.openTransaction(); @@ -3569,7 +3599,7 @@ public void testRemoveVertexAfterAddVertexWithTx() { List vertices = graph.traversal().V().toList(); Assert.assertEquals(1, vertices.size()); - assertNotContains(vertices, T.label, "book", "name", "java-2"); + assertNotContains(vertices, T.label, "book", "name", "java-1"); } @Test