Skip to content

Commit

Permalink
add updateSchema() api for schema tx
Browse files Browse the repository at this point in the history
Change-Id: I760db3c964f913d3c552c1d85193d9f3e0d401a4
  • Loading branch information
javeme committed Jun 6, 2022
1 parent 40a5196 commit c1e9ceb
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,15 @@ public List<IndexLabel> 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")
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit c1e9ceb

Please sign in to comment.