Skip to content
Permalink
Browse files
Record create time when create schema (#746)
Change-Id: I8a1156d4262969c1b287f1eac999614549d81b3a
  • Loading branch information
Linary authored and zhoney committed Dec 24, 2019
1 parent e2f2d5a commit ef6828963333f38988ad6c4a889529b2e9605f91
Showing 36 changed files with 628 additions and 183 deletions.
@@ -86,7 +86,7 @@
</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>0.49.0.0</Implementation-Version>
<Implementation-Version>0.50.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
@@ -45,6 +45,7 @@
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.define.Checkable;
import com.baidu.hugegraph.schema.EdgeLabel;
import com.baidu.hugegraph.schema.Userdata;
import com.baidu.hugegraph.type.define.Frequency;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.util.E;
@@ -184,7 +185,7 @@ private static class JsonEdgeLabel implements Checkable {
@JsonProperty("enable_label_index")
public Boolean enableLabelIndex;
@JsonProperty("user_data")
public Map<String, Object> userdata;
public Userdata userdata;
@JsonProperty("check_exist")
public Boolean checkExist;

@@ -28,6 +28,7 @@
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@@ -44,6 +45,7 @@
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.define.Checkable;
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.schema.Userdata;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.IndexType;
@@ -77,6 +79,31 @@ public String create(@Context GraphManager manager,
return manager.serializer(g).writeCreatedIndexLabel(il);
}

@PUT
@Timed
@Path("{name}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String update(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String name,
@QueryParam("action") String action,
IndexLabelAPI.JsonIndexLabel jsonIndexLabel) {
LOG.debug("Graph [{}] {} index label: {}",
graph, action, jsonIndexLabel);
checkUpdatingBody(jsonIndexLabel);
E.checkArgument(name.equals(jsonIndexLabel.name),
"The name in url(%s) and body(%s) are different",
name, jsonIndexLabel.name);
// Parse action parameter
boolean append = checkAndParseAction(action);

HugeGraph g = graph(manager, graph);
IndexLabel.Builder builder = jsonIndexLabel.convert2Builder(g);
IndexLabel IndexLabel = append ? builder.append() : builder.eliminate();
return manager.serializer(g).writeIndexlabel(mapIndexLabel(IndexLabel));
}

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
@@ -171,6 +198,8 @@ private static class JsonIndexLabel implements Checkable {
public IndexType indexType;
@JsonProperty("fields")
public String[] fields;
@JsonProperty("user_data")
public Userdata userdata;
@JsonProperty("check_exist")
public Boolean checkExist;

@@ -188,6 +217,24 @@ public void checkCreate(boolean isBatch) {
E.checkArgumentNotNull(this.baseValue,
"The base value of index label '%s' " +
"can't be null", this.name);
E.checkArgumentNotNull(this.indexType,
"The index type of index label '%s' " +
"can't be null", this.name);
}

@Override
public void checkUpdate() {
E.checkArgumentNotNull(this.name,
"The name of index label can't be null");
E.checkArgument(this.baseType == null,
"The base type of index label '%s' must be null",
this.name);
E.checkArgument(this.baseValue == null,
"The base value of index label '%s' must be null",
this.name);
E.checkArgument(this.indexType == null,
"The index type of index label '%s' must be null",
this.name);
}

private IndexLabel.Builder convert2Builder(HugeGraph g) {
@@ -202,13 +249,19 @@ private IndexLabel.Builder convert2Builder(HugeGraph g) {
g, g.mode());
builder.id(this.id);
}
builder.on(this.baseType, this.baseValue);
if (this.baseType != null) {
assert this.baseValue != null;
builder.on(this.baseType, this.baseValue);
}
if (this.indexType != null) {
builder.indexType(this.indexType);
}
if (this.fields != null) {
if (this.fields != null && this.fields.length > 0) {
builder.by(this.fields);
}
if (this.userdata != null) {
builder.userdata(this.userdata);
}
if (this.checkExist != null) {
builder.checkExist(this.checkExist);
}
@@ -21,7 +21,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Singleton;
import javax.ws.rs.Consumes;
@@ -44,6 +43,7 @@
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.define.Checkable;
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.schema.Userdata;
import com.baidu.hugegraph.type.define.AggregateType;
import com.baidu.hugegraph.type.define.Cardinality;
import com.baidu.hugegraph.type.define.DataType;
@@ -177,7 +177,7 @@ private static class JsonPropertyKey implements Checkable {
@JsonProperty("properties")
public String[] properties;
@JsonProperty("user_data")
public Map<String, Object> userdata;
public Userdata userdata;
@JsonProperty("check_exist")
public Boolean checkExist;

@@ -44,6 +44,7 @@
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.define.Checkable;
import com.baidu.hugegraph.schema.Userdata;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.IdStrategy;
@@ -183,7 +184,7 @@ private static class JsonVertexLabel implements Checkable {
@JsonProperty("enable_label_index")
public Boolean enableLabelIndex;
@JsonProperty("user_data")
public Map<String, Object> userdata;
public Userdata userdata;
@JsonProperty("check_exist")
public Boolean checkExist;

@@ -98,10 +98,11 @@ public final class ApiVersion {
*
* version 0.11:
* [0.49] Issue-670: Support fusiform similarity API
* [0.50] Issue-746: Support userdata for index label
*/

// The second parameter of Version.of() is for IDE running without JAR
public static final Version VERSION = Version.of(ApiVersion.class, "0.49");
public static final Version VERSION = Version.of(ApiVersion.class, "0.50");

public static final void check() {
// Check version of hugegraph-core. Firstly do check from version 0.3
@@ -59,7 +59,8 @@ public String version() {
* [1.5] #633: support unique index
* [1.6] #661 & #680: support bin serialization for cassandra
* [1.7] #691: support aggregate property
* [1.8] #746: support userdata for indexlabel
*/
return "1.7";
return "1.8";
}
}
@@ -225,6 +225,7 @@ public void init(CassandraSessionPool.Session session) {
.put(HugeKeys.BASE_VALUE, TYPE_SL)
.put(HugeKeys.INDEX_TYPE, DataType.tinyint())
.put(HugeKeys.FIELDS, DataType.list(TYPE_PK))
.put(HugeKeys.USER_DATA, TYPE_UD)
.put(HugeKeys.STATUS, DataType.tinyint())
.build();

@@ -985,6 +985,7 @@ public BinaryBackendEntry writeIndexLabel(IndexLabel schema) {
writeEnum(HugeKeys.INDEX_TYPE, schema.indexType());
writeIds(HugeKeys.FIELDS, schema.indexFields());
writeEnum(HugeKeys.STATUS, schema.status());
writeUserdata(schema);
return this.entry;
}

@@ -1002,6 +1003,7 @@ public IndexLabel readIndexLabel(HugeGraph graph,
IndexType.class));
indexLabel.indexFields(readIds(HugeKeys.FIELDS));
indexLabel.status(readEnum(HugeKeys.STATUS, SchemaStatus.class));
readUserdata(indexLabel);
return indexLabel;
}

@@ -565,6 +565,7 @@ public BackendEntry writeIndexLabel(IndexLabel indexLabel) {
entry.column(HugeKeys.INDEX_TYPE, indexLabel.indexType().code());
entry.column(HugeKeys.FIELDS,
this.toLongList(indexLabel.indexFields()));
this.writeUserdata(indexLabel, entry);
entry.column(HugeKeys.STATUS, indexLabel.status().code());
return entry;
}
@@ -593,6 +594,7 @@ public IndexLabel readIndexLabel(HugeGraph graph,
indexLabel.indexType(SerialEnum.fromCode(IndexType.class,
indexType.byteValue()));
indexLabel.indexFields(this.toIdArray(indexFields));
this.readUserdata(indexLabel, entry);
indexLabel.status(SerialEnum.fromCode(SchemaStatus.class,
status.byteValue()));
return indexLabel;
@@ -667,6 +667,7 @@ public BackendEntry writeIndexLabel(IndexLabel indexLabel) {
entry.column(HugeKeys.INDEX_TYPE,
JsonUtil.toJson(indexLabel.indexType()));
entry.column(HugeKeys.FIELDS, writeIds(indexLabel.indexFields()));
writeUserdata(indexLabel, entry);
entry.column(HugeKeys.STATUS,
JsonUtil.toJson(indexLabel.status()));
return entry;
@@ -694,6 +695,7 @@ public IndexLabel readIndexLabel(HugeGraph graph,
indexLabel.baseValue(readId(baseValue));
indexLabel.indexType(JsonUtil.fromJson(indexType, IndexType.class));
indexLabel.indexFields(readIds(indexFields));
readUserdata(indexLabel, entry);
indexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
return indexLabel;
}
@@ -90,7 +90,8 @@ public String version() {
* [1.3] #270 & #398: support shard-index and vertex + sortkey prefix,
* also split range table to rangeInt, rangeFloat,
* rangeLong and rangeDouble
* [1.4] #746: support userdata for indexlabel
*/
return "1.3";
return "1.4";
}
}
@@ -521,6 +521,9 @@ public void delete(BackendSession session, TextBackendEntry entry) {
String indexLabel = entry.column(HugeKeys.INDEX_LABEL_ID);
E.checkState(indexLabel != null, "Expect index label");

if (this.store().isEmpty()) {
return;
}
Id indexLabelId = IdGenerator.of(indexLabel);
Id min = HugeIndex.formatIndexId(entry.type(), indexLabelId, 0L);
indexLabelId = IdGenerator.of(indexLabelId.asLong() + 1L);
@@ -51,12 +51,14 @@
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.schema.SchemaElement;
import com.baidu.hugegraph.schema.SchemaLabel;
import com.baidu.hugegraph.schema.Userdata;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.task.HugeTask;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.GraphMode;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.type.define.SchemaStatus;
import com.baidu.hugegraph.util.DateUtil;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.LockUtil;
import com.google.common.collect.ImmutableSet;
@@ -267,6 +269,8 @@ protected void updateSchema(SchemaElement schema) {
protected void addSchema(SchemaElement schema) {
LOG.debug("SchemaTransaction add {} with id '{}'",
schema.type(), schema.id());
setCreateTimeIfNeeded(schema);

LockUtil.Locks locks = new LockUtil.Locks(this.graph().name());
try {
locks.lockWrites(LockUtil.hugeType2Group(schema.type()),
@@ -444,6 +448,12 @@ public void checkIdIfRestoringMode(HugeType type, Id id) {
}
}

private static void setCreateTimeIfNeeded(SchemaElement schema) {
if (!schema.userdata().containsKey(Userdata.CREATE_TIME)) {
schema.userdata(Userdata.CREATE_TIME, DateUtil.now());
}
}

private static Id asyncRun(HugeGraph graph, HugeType schemaType,
Id schemaId, SchemaCallable callable) {
return asyncRun(graph, schemaType, schemaId,
@@ -52,7 +52,7 @@ public Map<HugeKeys, Object> writeVertexLabel(VertexLabel vertexLabel) {
map.put(HugeKeys.USER_DATA, vertexLabel.userdata());
return map;
}

public Map<HugeKeys, Object> writeEdgeLabel(EdgeLabel edgeLabel) {
HugeGraph graph = edgeLabel.graph();
assert graph != null;
@@ -77,7 +77,7 @@ public Map<HugeKeys, Object> writeEdgeLabel(EdgeLabel edgeLabel) {
map.put(HugeKeys.USER_DATA, edgeLabel.userdata());
return map;
}

public Map<HugeKeys, Object> writePropertyKey(PropertyKey propertyKey) {
HugeGraph graph = propertyKey.graph();
assert graph != null;
@@ -112,6 +112,7 @@ public Map<HugeKeys, Object> writeIndexLabel(IndexLabel indexLabel) {
}
map.put(HugeKeys.INDEX_TYPE, indexLabel.indexType());
map.put(HugeKeys.FIELDS, graph.mapPkId2Name(indexLabel.indexFields()));
map.put(HugeKeys.USER_DATA, indexLabel.userdata());
return map;
}
}
@@ -28,7 +28,6 @@
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.exception.NotSupportException;
import com.baidu.hugegraph.schema.builder.SchemaBuilder;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.IndexType;
@@ -121,16 +120,6 @@ public Id indexField() {
return this.indexFields.get(0);
}

@Override
public Map<String, Object> userdata() {
throw new NotSupportException("user data for index label");
}

@Override
public void userdata(String key, Object value) {
throw new NotSupportException("user data for index label");
}

// ABS of System index id must be below SchemaElement.MAX_PRIMITIVE_SYS_ID
private static final int VL_IL_ID = -1;
private static final int EL_IL_ID = -2;
@@ -220,6 +209,10 @@ public interface Builder extends SchemaBuilder<IndexLabel> {
Builder on(HugeType baseType, String baseValue);

Builder indexType(IndexType indexType);

Builder userdata(String key, Object value);

Builder userdata(Map<String, Object> userdata);
}

public static class CreatedIndexLabel {

0 comments on commit ef68289

Please sign in to comment.