Search in sources :

Example 1 with IdStrategy

use of com.baidu.hugegraph.type.define.IdStrategy in project incubator-hugegraph by apache.

the class TableSerializer method readVertexLabel.

@Override
public VertexLabel readVertexLabel(HugeGraph graph, BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }
    TableBackendEntry entry = this.convertEntry(backendEntry);
    Number id = schemaColumn(entry, HugeKeys.ID);
    String name = schemaColumn(entry, HugeKeys.NAME);
    IdStrategy idStrategy = schemaEnum(entry, HugeKeys.ID_STRATEGY, IdStrategy.class);
    Object properties = schemaColumn(entry, HugeKeys.PROPERTIES);
    Object primaryKeys = schemaColumn(entry, HugeKeys.PRIMARY_KEYS);
    Object nullableKeys = schemaColumn(entry, HugeKeys.NULLABLE_KEYS);
    Object indexLabels = schemaColumn(entry, HugeKeys.INDEX_LABELS);
    SchemaStatus status = schemaEnum(entry, HugeKeys.STATUS, SchemaStatus.class);
    Number ttl = schemaColumn(entry, HugeKeys.TTL);
    Number ttlStartTime = schemaColumn(entry, HugeKeys.TTL_START_TIME);
    VertexLabel vertexLabel = new VertexLabel(graph, this.toId(id), name);
    vertexLabel.idStrategy(idStrategy);
    vertexLabel.properties(this.toIdArray(properties));
    vertexLabel.primaryKeys(this.toIdArray(primaryKeys));
    vertexLabel.nullableKeys(this.toIdArray(nullableKeys));
    vertexLabel.addIndexLabels(this.toIdArray(indexLabels));
    vertexLabel.status(status);
    vertexLabel.ttl(ttl.longValue());
    vertexLabel.ttlStartTime(this.toId(ttlStartTime));
    this.readEnableLabelIndex(vertexLabel, entry);
    this.readUserdata(vertexLabel, entry);
    return vertexLabel;
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus)

Example 2 with IdStrategy

use of com.baidu.hugegraph.type.define.IdStrategy in project incubator-hugegraph by apache.

the class TextSerializer method readVertexLabel.

@Override
public VertexLabel readVertexLabel(HugeGraph graph, BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }
    TextBackendEntry entry = this.convertEntry(backendEntry);
    Id id = readId(entry.id());
    String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME), String.class);
    String idStrategy = entry.column(HugeKeys.ID_STRATEGY);
    String properties = entry.column(HugeKeys.PROPERTIES);
    String primaryKeys = entry.column(HugeKeys.PRIMARY_KEYS);
    String nullableKeys = entry.column(HugeKeys.NULLABLE_KEYS);
    String indexLabels = entry.column(HugeKeys.INDEX_LABELS);
    String enableLabelIndex = entry.column(HugeKeys.ENABLE_LABEL_INDEX);
    String status = entry.column(HugeKeys.STATUS);
    VertexLabel vertexLabel = new VertexLabel(graph, id, name);
    vertexLabel.idStrategy(JsonUtil.fromJson(idStrategy, IdStrategy.class));
    vertexLabel.properties(readIds(properties));
    vertexLabel.primaryKeys(readIds(primaryKeys));
    vertexLabel.nullableKeys(readIds(nullableKeys));
    vertexLabel.addIndexLabels(readIds(indexLabels));
    vertexLabel.enableLabelIndex(JsonUtil.fromJson(enableLabelIndex, Boolean.class));
    readUserdata(vertexLabel, entry);
    vertexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
    return vertexLabel;
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus)

Example 3 with IdStrategy

use of com.baidu.hugegraph.type.define.IdStrategy in project incubator-hugegraph by apache.

the class GraphTransaction method checkId.

private void checkId(Id id, List<Id> keys, VertexLabel vertexLabel) {
    // Check whether id match with id strategy
    IdStrategy strategy = vertexLabel.idStrategy();
    switch(strategy) {
        case PRIMARY_KEY:
            E.checkArgument(id == null, "Can't customize vertex id when " + "id strategy is '%s' for vertex label '%s'", strategy, vertexLabel.name());
            // Check whether primaryKey exists
            List<Id> primaryKeys = vertexLabel.primaryKeys();
            E.checkArgument(keys.containsAll(primaryKeys), "The primary keys: %s of vertex label '%s' " + "must be set when using '%s' id strategy", this.graph().mapPkId2Name(primaryKeys), vertexLabel.name(), strategy);
            break;
        case AUTOMATIC:
            if (this.params().mode().maintaining()) {
                E.checkArgument(id != null && id.number(), "Must customize vertex number id when " + "id strategy is '%s' for vertex label " + "'%s' in restoring mode", strategy, vertexLabel.name());
            } else {
                E.checkArgument(id == null, "Can't customize vertex id when " + "id strategy is '%s' for vertex label '%s'", strategy, vertexLabel.name());
            }
            break;
        case CUSTOMIZE_STRING:
        case CUSTOMIZE_UUID:
            E.checkArgument(id != null && !id.number(), "Must customize vertex string id when " + "id strategy is '%s' for vertex label '%s'", strategy, vertexLabel.name());
            break;
        case CUSTOMIZE_NUMBER:
            E.checkArgument(id != null && id.number(), "Must customize vertex number id when " + "id strategy is '%s' for vertex label '%s'", strategy, vertexLabel.name());
            break;
        default:
            throw new AssertionError("Unknown id strategy: " + strategy);
    }
}
Also used : IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId)

Example 4 with IdStrategy

use of com.baidu.hugegraph.type.define.IdStrategy in project incubator-hugegraph by apache.

the class VertexAPI method getVertexId.

private static Id getVertexId(HugeGraph g, JsonVertex vertex) {
    VertexLabel vertexLabel = g.vertexLabel(vertex.label);
    String labelId = vertexLabel.id().asString();
    IdStrategy idStrategy = vertexLabel.idStrategy();
    E.checkArgument(idStrategy != IdStrategy.AUTOMATIC, "Automatic Id strategy is not supported now");
    if (idStrategy == IdStrategy.PRIMARY_KEY) {
        List<Id> pkIds = vertexLabel.primaryKeys();
        List<Object> pkValues = new ArrayList<>(pkIds.size());
        for (Id pkId : pkIds) {
            String propertyKey = g.propertyKey(pkId).name();
            Object propertyValue = vertex.properties.get(propertyKey);
            E.checkArgument(propertyValue != null, "The value of primary key '%s' can't be null", propertyKey);
            pkValues.add(propertyValue);
        }
        String value = ConditionQuery.concatValues(pkValues);
        return SplicingIdGenerator.splicing(labelId, value);
    } else if (idStrategy == IdStrategy.CUSTOMIZE_UUID) {
        return Text.uuid(String.valueOf(vertex.id));
    } else {
        assert idStrategy == IdStrategy.CUSTOMIZE_NUMBER || idStrategy == IdStrategy.CUSTOMIZE_STRING;
        return HugeVertex.getIdValue(vertex.id);
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id)

Example 5 with IdStrategy

use of com.baidu.hugegraph.type.define.IdStrategy in project incubator-hugegraph by apache.

the class HugeVertex method assignId.

@Watched(prefix = "vertex")
public void assignId(Id id, boolean force) {
    IdStrategy strategy = this.label.idStrategy();
    // Generate an id and assign
    switch(strategy) {
        case CUSTOMIZE_STRING:
            assert !id.number();
            this.id = id;
            break;
        case CUSTOMIZE_NUMBER:
            assert id.number();
            this.id = id;
            break;
        case CUSTOMIZE_UUID:
            this.id = id.uuid() ? id : IdGenerator.of(id.asString(), true);
            break;
        case PRIMARY_KEY:
            this.id = SplicingIdGenerator.instance().generate(this);
            break;
        case AUTOMATIC:
            if (force) {
                // Resume id for AUTOMATIC id strategy in restoring mode
                assert id.number();
                this.id = id;
            } else {
                this.id = SnowflakeIdGenerator.instance(this.graph()).generate(this);
            }
            break;
        default:
            throw new AssertionError(String.format("Unknown id strategy '%s'", strategy));
    }
    this.checkIdLength();
}
Also used : IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

IdStrategy (com.baidu.hugegraph.type.define.IdStrategy)9 Id (com.baidu.hugegraph.backend.id.Id)3 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)3 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)3 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)2 SchemaStatus (com.baidu.hugegraph.type.define.SchemaStatus)2 ArrayList (java.util.ArrayList)2