Skip to content

Commit

Permalink
Check exist same id vertex with customized id strategy
Browse files Browse the repository at this point in the history
Implement #185

Change-Id: Iabac4af7804aa07c449280cf30eb65ab17d9bd97
  • Loading branch information
Linary authored and zhoney committed Nov 16, 2018
1 parent 30fbc72 commit 1362d78
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public class GraphTransaction extends IndexableTransaction {

private LockUtil.LocksTable locksTable;

private final boolean checkVertexExist;

private final int vertexesCapacity;
private final int edgesCapacity;

Expand All @@ -113,6 +115,8 @@ public GraphTransaction(HugeGraph graph, BackendStore store) {
assert !this.indexTx.autoCommit();

final HugeConfig conf = graph.configuration();
this.checkVertexExist = conf.get(
CoreOptions.VERTEX_CHECK_CUSTOMIZED_ID_EXIST);
this.vertexesCapacity = conf.get(CoreOptions.VERTEX_TX_CAPACITY);
this.edgesCapacity = conf.get(CoreOptions.EDGE_TX_CAPACITY);
this.locksTable = new LockUtil.LocksTable();
Expand Down Expand Up @@ -208,6 +212,9 @@ protected BackendMutation prepareCommit() {

protected void prepareAdditions(Map<Id, HugeVertex> addedVertexes,
Map<Id, HugeEdge> addedEdges) {
if (this.checkVertexExist) {
this.checkVertexExistIfCustomizedId(addedVertexes);
}
// Do vertex update
for (HugeVertex v : addedVertexes.values()) {
assert !v.removed();
Expand Down Expand Up @@ -236,6 +243,33 @@ protected void prepareAdditions(Map<Id, HugeVertex> addedVertexes,
}
}

private void checkVertexExistIfCustomizedId(Map<Id, HugeVertex> vertices) {
Set<Id> ids = new HashSet<>();
for (HugeVertex vertex : vertices.values()) {
VertexLabel vl = vertex.schemaLabel();
if (!vl.hidden() && vl.idStrategy().isCustomized()) {
ids.add(vertex.id());
}
}
if (ids.isEmpty()) {
return;
}
IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids);
Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery);
if (results.hasNext()) {
HugeVertex existedVertex = results.next();
HugeVertex newVertex = vertices.get(existedVertex.id());
if (!existedVertex.label().equals(newVertex.label())) {
throw new HugeException(
"The newly added vertex with id:'%s' label:'%s' " +
"is not allowed to insert, because already exist " +
"a vertex with same id and different label:'%s'",
newVertex.id(), newVertex.label(),
existedVertex.label());
}
}
}

protected void prepareDeletions(Map<Id, HugeVertex> removedVertexes,
Map<Id, HugeEdge> removedEdges) {
// Remove related edges of each vertex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public static synchronized CoreOptions instance() {
"vertex"
);

public static final ConfigOption<Boolean> VERTEX_CHECK_CUSTOMIZED_ID_EXIST =
new ConfigOption<>(
"vertex.check_customzied_id_exist",
"Whether to check the vertices exist for those using " +
"customized id strategy",
disallowEmpty(),
true
);

public static final ConfigOption<Integer> VERTEX_TX_CAPACITY =
new ConfigOption<>(
"vertex.tx_capacity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,16 @@ public byte code() {
public String string() {
return this.name;
}

public boolean isAutomatic() {
return this == AUTOMATIC;
}

public boolean isPrimaryKey() {
return this == PRIMARY_KEY;
}

public boolean isCustomized() {
return this == CUSTOMIZE_STRING || this == CUSTOMIZE_NUMBER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.junit.Before;
import org.junit.Test;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.id.Id;
Expand Down Expand Up @@ -3194,6 +3195,35 @@ public void testQueryByLabelIndexWithLimitAndOffset() {
Assert.assertTrue(vertices.containsAll(vertices1));
}

@Test
public void testAddCustomizedIdVerticesContainsExisted() {
HugeGraph graph = graph();
SchemaManager schema = graph.schema();

schema.vertexLabel("programmer")
.useCustomizeStringId()
.properties("name", "age", "city")
.create();
schema.vertexLabel("designer")
.useCustomizeStringId()
.properties("name", "age", "city")
.create();

graph.addVertex(T.label, "programmer", T.id, "123456", "name", "marko",
"age", 18, "city", "Beijing");
graph.tx().commit();

graph.addVertex(T.label, "programmer", T.id, "123456", "name", "marko",
"age", 19, "city", "Beijing");
graph.tx().commit();

graph.addVertex(T.label, "designer", T.id, "123456", "name", "marko",
"age", 18, "city", "Beijing");
Assert.assertThrows(HugeException.class, () -> {
graph.tx().commit();
});
}

private void init10Vertices() {
HugeGraph graph = graph();

Expand Down

0 comments on commit 1362d78

Please sign in to comment.