From 1362d785afd475e54949bdc64cde8cef7bb50309 Mon Sep 17 00:00:00 2001 From: liningrui Date: Tue, 13 Nov 2018 11:53:43 +0800 Subject: [PATCH] Check exist same id vertex with customized id strategy Implement #185 Change-Id: Iabac4af7804aa07c449280cf30eb65ab17d9bd97 --- .../backend/tx/GraphTransaction.java | 34 +++++++++++++++++++ .../baidu/hugegraph/config/CoreOptions.java | 9 +++++ .../hugegraph/type/define/IdStrategy.java | 12 +++++++ .../baidu/hugegraph/core/VertexCoreTest.java | 30 ++++++++++++++++ 4 files changed, 85 insertions(+) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java index 750b74e037..67a661bb31 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java @@ -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; @@ -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(); @@ -208,6 +212,9 @@ protected BackendMutation prepareCommit() { protected void prepareAdditions(Map addedVertexes, Map addedEdges) { + if (this.checkVertexExist) { + this.checkVertexExistIfCustomizedId(addedVertexes); + } // Do vertex update for (HugeVertex v : addedVertexes.values()) { assert !v.removed(); @@ -236,6 +243,33 @@ protected void prepareAdditions(Map addedVertexes, } } + private void checkVertexExistIfCustomizedId(Map vertices) { + Set 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 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 removedVertexes, Map removedEdges) { // Remove related edges of each vertex diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java index cf28893ccf..ffe41771b0 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java @@ -111,6 +111,15 @@ public static synchronized CoreOptions instance() { "vertex" ); + public static final ConfigOption 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 VERTEX_TX_CAPACITY = new ConfigOption<>( "vertex.tx_capacity", diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IdStrategy.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IdStrategy.java index 1ecc632902..c85bc82422 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IdStrategy.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/IdStrategy.java @@ -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; + } } 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 4758360849..827ad4a59f 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 @@ -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; @@ -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();