From c1e9ceb9823c0288ea2cf9af1f8d0643b272781a Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Wed, 2 Mar 2022 20:52:15 +0800 Subject: [PATCH] add updateSchema() api for schema tx Change-Id: I760db3c964f913d3c552c1d85193d9f3e0d401a4 --- .../hugegraph/auth/HugeGraphAuthProxy.java | 24 +++++++++ .../java/com/baidu/hugegraph/HugeGraph.java | 12 ++++- .../baidu/hugegraph/StandardHugeGraph.java | 36 ++++++++++--- .../backend/tx/SchemaTransaction.java | 52 +++++++++++++------ .../schema/builder/EdgeLabelBuilder.java | 4 +- .../schema/builder/IndexLabelBuilder.java | 6 +-- .../schema/builder/PropertyKeyBuilder.java | 4 +- .../schema/builder/VertexLabelBuilder.java | 4 +- 8 files changed, 108 insertions(+), 34 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java index d3efb5aa35..33c5cfa283 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeGraphAuthProxy.java @@ -195,6 +195,12 @@ public Id addPropertyKey(PropertyKey key) { return this.hugegraph.addPropertyKey(key); } + @Override + public void updatePropertyKey(PropertyKey key) { + verifySchemaPermission(HugePermission.WRITE, key); + this.hugegraph.updatePropertyKey(key); + } + @Override public Id removePropertyKey(Id key) { PropertyKey pkey = this.hugegraph.propertyKey(key); @@ -240,6 +246,12 @@ public void addVertexLabel(VertexLabel label) { this.hugegraph.addVertexLabel(label); } + @Override + public void updateVertexLabel(VertexLabel label) { + verifySchemaPermission(HugePermission.WRITE, label); + this.hugegraph.updateVertexLabel(label); + } + @Override public Id removeVertexLabel(Id id) { VertexLabel label = this.hugegraph.vertexLabel(id); @@ -293,6 +305,12 @@ public void addEdgeLabel(EdgeLabel label) { this.hugegraph.addEdgeLabel(label); } + @Override + public void updateEdgeLabel(EdgeLabel label) { + verifySchemaPermission(HugePermission.WRITE, label); + this.hugegraph.updateEdgeLabel(label); + } + @Override public Id removeEdgeLabel(Id id) { EdgeLabel label = this.hugegraph.edgeLabel(id); @@ -339,6 +357,12 @@ public void addIndexLabel(SchemaLabel schemaLabel, IndexLabel indexLabel) { this.hugegraph.addIndexLabel(schemaLabel, indexLabel); } + @Override + public void updateIndexLabel(IndexLabel label) { + verifySchemaPermission(HugePermission.WRITE, label); + this.hugegraph.updateIndexLabel(label); + } + @Override public Id removeIndexLabel(Id id) { IndexLabel label = this.hugegraph.indexLabel(id); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index ca388899ce..18d9f795e5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -72,6 +72,8 @@ public interface HugeGraph extends Graph { Id addPropertyKey(PropertyKey key); + void updatePropertyKey(PropertyKey key); + Id removePropertyKey(Id key); Id clearPropertyKey(PropertyKey propertyKey); @@ -84,7 +86,9 @@ public interface HugeGraph extends Graph { boolean existsPropertyKey(String key); - void addVertexLabel(VertexLabel vertexLabel); + void addVertexLabel(VertexLabel label); + + void updateVertexLabel(VertexLabel label); Id removeVertexLabel(Id label); @@ -100,7 +104,9 @@ public interface HugeGraph extends Graph { boolean existsLinkLabel(Id vertexLabel); - void addEdgeLabel(EdgeLabel edgeLabel); + void addEdgeLabel(EdgeLabel label); + + void updateEdgeLabel(EdgeLabel label); Id removeEdgeLabel(Id label); @@ -116,6 +122,8 @@ public interface HugeGraph extends Graph { void addIndexLabel(SchemaLabel schemaLabel, IndexLabel indexLabel); + void updateIndexLabel(IndexLabel label); + Id removeIndexLabel(Id label); Id rebuildIndex(SchemaElement schema); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java index 217fc5cbba..72f8838899 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/StandardHugeGraph.java @@ -714,6 +714,12 @@ public Id addPropertyKey(PropertyKey pkey) { return this.schemaTransaction().addPropertyKey(pkey); } + @Override + public void updatePropertyKey(PropertyKey pkey) { + assert this.name.equals(pkey.graph().name()); + this.schemaTransaction().updatePropertyKey(pkey); + } + @Override public Id removePropertyKey(Id pkey) { if (this.propertyKey(pkey).olap()) { @@ -756,9 +762,15 @@ public boolean existsPropertyKey(String name) { } @Override - public void addVertexLabel(VertexLabel vertexLabel) { - assert this.name.equals(vertexLabel.graph().name()); - this.schemaTransaction().addVertexLabel(vertexLabel); + public void addVertexLabel(VertexLabel label) { + assert this.name.equals(label.graph().name()); + this.schemaTransaction().addVertexLabel(label); + } + + @Override + public void updateVertexLabel(VertexLabel label) { + assert this.name.equals(label.graph().name()); + this.schemaTransaction().updateVertexLabel(label); } @Override @@ -812,9 +824,15 @@ public boolean existsLinkLabel(Id vertexLabel) { } @Override - public void addEdgeLabel(EdgeLabel edgeLabel) { - assert this.name.equals(edgeLabel.graph().name()); - this.schemaTransaction().addEdgeLabel(edgeLabel); + public void addEdgeLabel(EdgeLabel label) { + assert this.name.equals(label.graph().name()); + this.schemaTransaction().addEdgeLabel(label); + } + + @Override + public void updateEdgeLabel(EdgeLabel label) { + assert this.name.equals(label.graph().name()); + this.schemaTransaction().updateEdgeLabel(label); } @Override @@ -863,6 +881,12 @@ public void addIndexLabel(SchemaLabel schemaLabel, IndexLabel indexLabel) { this.schemaTransaction().addIndexLabel(schemaLabel, indexLabel); } + @Override + public void updateIndexLabel(IndexLabel label) { + assert this.name.equals(label.graph().name()); + this.schemaTransaction().updateIndexLabel(label); + } + @Override public Id removeIndexLabel(Id id) { return this.schemaTransaction().removeIndexLabel(id); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java index 340b66928c..7887fd5141 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/SchemaTransaction.java @@ -124,10 +124,15 @@ public List getIndexLabels() { @Watched(prefix = "schema") public Id addPropertyKey(PropertyKey propertyKey) { this.addSchema(propertyKey); - if (propertyKey.olap()) { - return this.createOlapPk(propertyKey); + if (!propertyKey.olap()) { + return IdGenerator.ZERO; } - return IdGenerator.ZERO; + return this.createOlapPk(propertyKey); + } + + @Watched(prefix = "schema") + public void updatePropertyKey(PropertyKey propertyKey) { + this.updateSchema(propertyKey); } @Watched(prefix = "schema") @@ -185,6 +190,11 @@ public void addVertexLabel(VertexLabel vertexLabel) { this.addSchema(vertexLabel); } + @Watched(prefix = "schema") + public void updateVertexLabel(VertexLabel vertexLabel) { + this.updateSchema(vertexLabel); + } + @Watched(prefix = "schema") public VertexLabel getVertexLabel(Id id) { E.checkArgumentNotNull(id, "Vertex label id can't be null"); @@ -217,6 +227,11 @@ public void addEdgeLabel(EdgeLabel edgeLabel) { this.addSchema(edgeLabel); } + @Watched(prefix = "schema") + public void updateEdgeLabel(EdgeLabel edgeLabel) { + this.updateSchema(edgeLabel); + } + @Watched(prefix = "schema") public EdgeLabel getEdgeLabel(Id id) { E.checkArgumentNotNull(id, "Edge label id can't be null"); @@ -239,49 +254,54 @@ public Id removeEdgeLabel(Id id) { } @Watched(prefix = "schema") - public void addIndexLabel(SchemaLabel schemaLabel, IndexLabel indexLabel) { + public void addIndexLabel(SchemaLabel baseLabel, IndexLabel indexLabel) { this.addSchema(indexLabel); /* * Update index name in base-label(VL/EL) * TODO: should wrap update base-label and create index in one tx. */ - if (schemaLabel.equals(VertexLabel.OLAP_VL)) { + if (baseLabel.equals(VertexLabel.OLAP_VL)) { return; } // FIXME: move schemaLabel update into updateSchema() lock block instead - synchronized (schemaLabel) { - schemaLabel.addIndexLabel(indexLabel.id()); - this.updateSchema(schemaLabel); + synchronized (baseLabel) { + baseLabel.addIndexLabel(indexLabel.id()); + this.updateSchema(baseLabel); } } + @Watched(prefix = "schema") + public void updateIndexLabel(IndexLabel indexLabel) { + this.updateSchema(indexLabel); + } + @Watched(prefix = "schema") public void removeIndexLabelFromBaseLabel(IndexLabel indexLabel) { HugeType baseType = indexLabel.baseType(); Id baseValue = indexLabel.baseValue(); - SchemaLabel schemaLabel; + SchemaLabel baseLabel; if (baseType == HugeType.VERTEX_LABEL) { - schemaLabel = this.getVertexLabel(baseValue); + baseLabel = this.getVertexLabel(baseValue); } else { assert baseType == HugeType.EDGE_LABEL; - schemaLabel = this.getEdgeLabel(baseValue); + baseLabel = this.getEdgeLabel(baseValue); } - if (schemaLabel == null) { + if (baseLabel == null) { LOG.info("The base label '{}' of index label '{}' " + "may be deleted before", baseValue, indexLabel); return; } - if (schemaLabel.equals(VertexLabel.OLAP_VL)) { + if (baseLabel.equals(VertexLabel.OLAP_VL)) { return; } // FIXME: move schemaLabel update into updateSchema() lock block instead - synchronized (schemaLabel) { - schemaLabel.removeIndexLabel(indexLabel.id()); - this.updateSchema(schemaLabel); + synchronized (baseLabel) { + baseLabel.removeIndexLabel(indexLabel.id()); + this.updateSchema(baseLabel); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java index 338b45fb2e..13b84d2be6 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/EdgeLabelBuilder.java @@ -262,7 +262,7 @@ public EdgeLabel append() { edgeLabel.nullableKey(propertyKey.id()); } edgeLabel.userdata(this.userdata); - this.graph().addEdgeLabel(edgeLabel); + this.graph().updateEdgeLabel(edgeLabel); return edgeLabel; } @@ -280,7 +280,7 @@ public EdgeLabel eliminate() { Userdata.check(this.userdata, Action.ELIMINATE); edgeLabel.removeUserdata(this.userdata); - this.graph().addEdgeLabel(edgeLabel); + this.graph().updateEdgeLabel(edgeLabel); return edgeLabel; } 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 44b21f4bec..ab00d7f81b 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 @@ -287,8 +287,7 @@ public IndexLabel append() { this.checkStableVars(); Userdata.check(this.userdata, Action.APPEND); indexLabel.userdata(this.userdata); - SchemaLabel schemaLabel = indexLabel.baseLabel(); - this.graph().addIndexLabel(schemaLabel, indexLabel); + this.graph().updateIndexLabel(indexLabel); return indexLabel; } @@ -303,8 +302,7 @@ public IndexLabel eliminate() { Userdata.check(this.userdata, Action.ELIMINATE); indexLabel.removeUserdata(this.userdata); - SchemaLabel schemaLabel = indexLabel.baseLabel(); - this.graph().addIndexLabel(schemaLabel, indexLabel); + this.graph().updateIndexLabel(indexLabel); return indexLabel; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java index 965b5b6fed..92941cfcc1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/PropertyKeyBuilder.java @@ -194,7 +194,7 @@ public PropertyKey append() { Userdata.check(this.userdata, Action.APPEND); propertyKey.userdata(this.userdata); - this.graph().addPropertyKey(propertyKey); + this.graph().updatePropertyKey(propertyKey); return propertyKey; } @@ -209,7 +209,7 @@ public PropertyKey eliminate() { Userdata.check(this.userdata, Action.ELIMINATE); propertyKey.removeUserdata(this.userdata); - this.graph().addPropertyKey(propertyKey); + this.graph().updatePropertyKey(propertyKey); return propertyKey; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java index 75fb3285c1..533245d717 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/builder/VertexLabelBuilder.java @@ -228,7 +228,7 @@ public VertexLabel append() { vertexLabel.nullableKey(propertyKey.id()); } vertexLabel.userdata(this.userdata); - this.graph().addVertexLabel(vertexLabel); + this.graph().updateVertexLabel(vertexLabel); return vertexLabel; } @@ -246,7 +246,7 @@ public VertexLabel eliminate() { Userdata.check(this.userdata, Action.ELIMINATE); vertexLabel.removeUserdata(this.userdata); - this.graph().addVertexLabel(vertexLabel); + this.graph().updateVertexLabel(vertexLabel); return vertexLabel; }