This is an automated email from the ASF dual-hosted git repository.

jermy pushed a commit to branch schema-update-bug-rebase
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git

commit c1e9ceb9823c0288ea2cf9af1f8d0643b272781a
Author: Zhangmei Li <[email protected]>
AuthorDate: Wed Mar 2 20:52:15 2022 +0800

    add updateSchema() api for schema tx
    
    Change-Id: I760db3c964f913d3c552c1d85193d9f3e0d401a4
---
 .../baidu/hugegraph/auth/HugeGraphAuthProxy.java   | 24 ++++++++++
 .../main/java/com/baidu/hugegraph/HugeGraph.java   | 12 ++++-
 .../com/baidu/hugegraph/StandardHugeGraph.java     | 36 ++++++++++++---
 .../hugegraph/backend/tx/SchemaTransaction.java    | 52 +++++++++++++++-------
 .../hugegraph/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 d3efb5aa3..33c5cfa28 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 final class HugeGraphAuthProxy implements HugeGraph 
{
         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 final class HugeGraphAuthProxy implements HugeGraph 
{
         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 final class HugeGraphAuthProxy implements HugeGraph 
{
         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 final class HugeGraphAuthProxy implements HugeGraph 
{
         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 ca388899c..18d9f795e 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 217fc5cbb..72f883889 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 class StandardHugeGraph implements HugeGraph {
         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 class StandardHugeGraph implements HugeGraph {
     }
 
     @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 class StandardHugeGraph implements HugeGraph {
     }
 
     @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 class StandardHugeGraph implements HugeGraph {
         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 340b66928..7887fd514 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 class SchemaTransaction extends 
IndexableTransaction {
     @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 class SchemaTransaction extends 
IndexableTransaction {
         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 class SchemaTransaction extends 
IndexableTransaction {
         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 class SchemaTransaction extends 
IndexableTransaction {
     }
 
     @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 338b45fb2..13b84d2be 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 class EdgeLabelBuilder extends AbstractBuilder
             edgeLabel.nullableKey(propertyKey.id());
         }
         edgeLabel.userdata(this.userdata);
-        this.graph().addEdgeLabel(edgeLabel);
+        this.graph().updateEdgeLabel(edgeLabel);
         return edgeLabel;
     }
 
@@ -280,7 +280,7 @@ public class EdgeLabelBuilder extends AbstractBuilder
         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 44b21f4be..ab00d7f81 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 class IndexLabelBuilder extends AbstractBuilder
         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 class IndexLabelBuilder extends AbstractBuilder
         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 965b5b6fe..92941cfcc 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 class PropertyKeyBuilder extends AbstractBuilder
         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 class PropertyKeyBuilder extends AbstractBuilder
         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 75fb3285c..533245d71 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 class VertexLabelBuilder extends AbstractBuilder
             vertexLabel.nullableKey(propertyKey.id());
         }
         vertexLabel.userdata(this.userdata);
-        this.graph().addVertexLabel(vertexLabel);
+        this.graph().updateVertexLabel(vertexLabel);
         return vertexLabel;
     }
 
@@ -246,7 +246,7 @@ public class VertexLabelBuilder extends AbstractBuilder
         Userdata.check(this.userdata, Action.ELIMINATE);
 
         vertexLabel.removeUserdata(this.userdata);
-        this.graph().addVertexLabel(vertexLabel);
+        this.graph().updateVertexLabel(vertexLabel);
         return vertexLabel;
     }
 

Reply via email to