Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -592,7 +592,11 @@ private MatchedIndex collectMatchedIndex(SchemaLabel schemaLabel,
SchemaTransaction schema = this.graph().schemaTransaction();
Set<IndexLabel> ils = InsertionOrderUtil.newSet();
for (Id il : schemaLabel.indexLabels()) {
ils.add(schema.getIndexLabel(il));
IndexLabel indexLabel = schema.getIndexLabel(il);
if (indexLabel.indexType().isUnique()) {
continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not allowed to query by unique index

}
ils.add(indexLabel);
}
if (ils.isEmpty()) {
return null;
Expand Down Expand Up @@ -671,8 +675,8 @@ private boolean needIndexForLabel() {
private static Set<IndexLabel> matchSingleOrCompositeIndex(
ConditionQuery query,
Set<IndexLabel> indexLabels) {
boolean reqiureRange = query.hasRangeCondition();
boolean reqiureSearch = query.hasSearchCondition();
boolean requireRange = query.hasRangeCondition();
boolean requireSearch = query.hasSearchCondition();
Set<Id> queryPropKeys = query.userpropKeys();
for (IndexLabel indexLabel : indexLabels) {
List<Id> indexFields = indexLabel.indexFields();
Expand All @@ -690,11 +694,11 @@ private static Set<IndexLabel> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ private Set<Id> 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);
}
Expand All @@ -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<String> pks = this.transaction.graph()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public boolean isShard() {
return this == SHARD;
}

public boolean isUniuqe() {
public boolean isUnique() {
return this == UNIQUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -3556,6 +3585,7 @@ public void testRemoveVertexOfNotExists() {
graph.tx().commit();
}

@Test
public void testRemoveVertexAfterAddVertexWithTx() {
HugeGraph graph = graph();
GraphTransaction tx = graph.openTransaction();
Expand All @@ -3569,7 +3599,7 @@ public void testRemoveVertexAfterAddVertexWithTx() {

List<Vertex> 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
Expand Down