From f9d2f695df7e1b17aee6e648174b91a5ce025d0f Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Thu, 26 Dec 2019 22:25:00 +0800 Subject: [PATCH 1/6] fix mismatched exception type of unique index query Change-Id: I9977a4cc05e3ee30d869ea32a043eddce216fa22 fixed: #795 --- .../backend/tx/GraphIndexTransaction.java | 16 ++++--- .../baidu/hugegraph/core/EdgeCoreTest.java | 43 +++++++++++++++++++ .../baidu/hugegraph/core/VertexCoreTest.java | 32 ++++++++++++++ 3 files changed, 85 insertions(+), 6 deletions(-) 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..78d3b1cfe3 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 @@ -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().isUniuqe()) { + 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-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java index cff23096cc..c8f2f6ecba 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,48 @@ 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.assertTrue(e.getMessage().equals( + "Don't accept query based on properties " + + "[weight] that are not indexed in label 'like'," + + " may not match secondary condition")); + }); + } + @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..89b9a2d02e 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,38 @@ 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.assertTrue(e.getMessage().equals( + "Don't accept query based on properties [name] " + + "that are not indexed in label 'node', " + + "may not match secondary condition")); + }); + } + @Test public void testRemoveVertex() { HugeGraph graph = graph(); From e1eac578a2409348acb2a2b8928ccc505e4989d2 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Thu, 26 Dec 2019 22:51:09 +0800 Subject: [PATCH 2/6] improve Change-Id: I11ec1986e51e2a957758892cf59ffb4912d5432d --- .../baidu/hugegraph/backend/tx/GraphIndexTransaction.java | 4 ++-- .../baidu/hugegraph/schema/builder/IndexLabelBuilder.java | 6 +++--- .../java/com/baidu/hugegraph/type/define/IndexType.java | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) 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 78d3b1cfe3..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; } @@ -593,7 +593,7 @@ private MatchedIndex collectMatchedIndex(SchemaLabel schemaLabel, Set ils = InsertionOrderUtil.newSet(); for (Id il : schemaLabel.indexLabels()) { IndexLabel indexLabel = schema.getIndexLabel(il); - if (indexLabel.indexType().isUniuqe()) { + if (indexLabel.indexType().isUnique()) { continue; } ils.add(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; } } From 932473e48b3b6d2b14556361d2a1cf54fe44f741 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Mon, 6 Jan 2020 20:29:57 +0800 Subject: [PATCH 3/6] improve Change-Id: I4cff5f7b914b3b62ad4e351bed60fed1e706ffe2 --- .../main/java/com/baidu/hugegraph/core/EdgeCoreTest.java | 8 ++++---- .../java/com/baidu/hugegraph/core/VertexCoreTest.java | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) 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 c8f2f6ecba..1e3053cf6f 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 @@ -3569,10 +3569,10 @@ public void testQueryEdgeByUniqueIndex() { Assert.assertThrows(NoIndexException.class, () -> { graph().traversal().E().hasLabel("like").has("weight", 0.5).next(); }, (e) -> { - Assert.assertTrue(e.getMessage().equals( - "Don't accept query based on properties " + - "[weight] that are not indexed in label 'like'," + - " may not match secondary condition")); + Assert.assertEquals("Don't accept query based on properties " + + "[weight] that are not indexed in label " + + "'like', may not match secondary condition", + e.getMessage()); }); } 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 89b9a2d02e..58c854361c 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 @@ -3533,10 +3533,10 @@ public void testQueryByUniqueIndex() { Assert.assertThrows(NoIndexException.class, () -> { graph.traversal().V().hasLabel("node").has("name", "tom").next(); }, (e) -> { - Assert.assertTrue(e.getMessage().equals( - "Don't accept query based on properties [name] " + - "that are not indexed in label 'node', " + - "may not match secondary condition")); + Assert.assertEquals("Don't accept query based on properties " + + "[name] that are not indexed in label 'node'," + + " may not match secondary condition", + e.getMessage()); }); } @@ -3588,6 +3588,7 @@ public void testRemoveVertexOfNotExists() { graph.tx().commit(); } + @Test public void testRemoveVertexAfterAddVertexWithTx() { HugeGraph graph = graph(); GraphTransaction tx = graph.openTransaction(); From 7f6bb1f93ee5d1dc0d6ab4a4f752a62ce11cd7b2 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Mon, 6 Jan 2020 20:37:27 +0800 Subject: [PATCH 4/6] still comment the failed test Change-Id: Iba850b598f05a652c10659efa251fb8a7bdf20a3 --- .../src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java | 1 - 1 file changed, 1 deletion(-) 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 58c854361c..7282c2019d 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 @@ -3588,7 +3588,6 @@ public void testRemoveVertexOfNotExists() { graph.tx().commit(); } - @Test public void testRemoveVertexAfterAddVertexWithTx() { HugeGraph graph = graph(); GraphTransaction tx = graph.openTransaction(); From 64e06b903836820b494b2622aeccb2b42ec4ad43 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 7 Jan 2020 10:56:18 +0800 Subject: [PATCH 5/6] tiny improve Change-Id: Id1f469218e6d537b221f6d930fdd0bc9dc344c85 --- .../src/main/java/com/baidu/hugegraph/core/EdgeCoreTest.java | 5 ----- .../main/java/com/baidu/hugegraph/core/VertexCoreTest.java | 3 --- 2 files changed, 8 deletions(-) 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 1e3053cf6f..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 @@ -3538,20 +3538,17 @@ public void testUpdatePropertyToValueOfRemovedEdgeWithUniqueIndex() { 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") @@ -3561,9 +3558,7 @@ public void testQueryEdgeByUniqueIndex() { 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, () -> { 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 7282c2019d..ad7d8f4c9f 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 @@ -3512,13 +3512,11 @@ public void testAddVerticesWithUniqueIndexForNullableProperties() { 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") @@ -3527,7 +3525,6 @@ public void testQueryByUniqueIndex() { .create(); graph.addVertex(T.label, "node", "name", "tom"); - graph.tx().commit(); Assert.assertThrows(NoIndexException.class, () -> { From 8efe9366d7aa7d32837470dd7f50e1c8c9aeb550 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 7 Jan 2020 11:57:04 +0800 Subject: [PATCH 6/6] uncomment the test Change-Id: Iaddf6e070a083002b8b17e238384cdd2f7e43a1e --- .../src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 ad7d8f4c9f..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 @@ -3585,6 +3585,7 @@ public void testRemoveVertexOfNotExists() { graph.tx().commit(); } + @Test public void testRemoveVertexAfterAddVertexWithTx() { HugeGraph graph = graph(); GraphTransaction tx = graph.openTransaction(); @@ -3598,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