use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class GraphTransaction method checkVertexLabel.
private VertexLabel checkVertexLabel(Object label, boolean verifyLabel) {
HugeVertexFeatures features = graph().features().vertex();
// Check Vertex label
if (label == null && features.supportsDefaultLabel()) {
label = features.defaultLabel();
}
if (label == null) {
throw Element.Exceptions.labelCanNotBeNull();
}
E.checkArgument(label instanceof String || label instanceof VertexLabel, "Expect a string or a VertexLabel object " + "as the vertex label argument, but got: '%s'", label);
// The label must be an instance of String or VertexLabel
if (label instanceof String) {
if (verifyLabel) {
ElementHelper.validateLabel((String) label);
}
label = graph().vertexLabel((String) label);
}
assert (label instanceof VertexLabel);
return (VertexLabel) label;
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class GraphTransaction method optimizeQuery.
private Query optimizeQuery(ConditionQuery query) {
if (query.idsSize() > 0) {
throw new HugeException("Not supported querying by id and conditions: %s", query);
}
Id label = (Id) query.condition(HugeKeys.LABEL);
// Optimize vertex query
if (label != null && query.resultType().isVertex()) {
VertexLabel vertexLabel = this.graph().vertexLabel(label);
if (vertexLabel.idStrategy() == IdStrategy.PRIMARY_KEY) {
List<Id> keys = vertexLabel.primaryKeys();
E.checkState(!keys.isEmpty(), "The primary keys can't be empty when using " + "'%s' id strategy for vertex label '%s'", IdStrategy.PRIMARY_KEY, vertexLabel.name());
if (query.matchUserpropKeys(keys)) {
// Query vertex by label + primary-values
query.optimized(OptimizedType.PRIMARY_KEY);
String primaryValues = query.userpropValuesString(keys);
LOG.debug("Query vertices by primaryKeys: {}", query);
// Convert {vertex-label + primary-key} to vertex-id
Id id = SplicingIdGenerator.splicing(label.asString(), primaryValues);
/*
* Just query by primary-key(id), ignore other userprop(if
* exists) that it will be filtered by queryVertices(Query)
*/
return new IdQuery(query, id);
}
}
}
// Optimize edge query
if (query.resultType().isEdge() && label != null && query.condition(HugeKeys.OWNER_VERTEX) != null && query.condition(HugeKeys.DIRECTION) != null && matchEdgeSortKeys(query, false, this.graph())) {
// Query edge by sourceVertex + direction + label + sort-values
query.optimized(OptimizedType.SORT_KEYS);
query = query.copy();
// Serialize sort-values
List<Id> keys = this.graph().edgeLabel(label).sortKeys();
List<Condition> conditions = GraphIndexTransaction.constructShardConditions(query, keys, HugeKeys.SORT_VALUES);
query.query(conditions);
/*
* Reset all userprop since transferred to sort-keys, ignore other
* userprop(if exists) that it will be filtered by queryEdges(Query)
*/
query.resetUserpropConditions();
LOG.debug("Query edges by sortKeys: {}", query);
return query;
}
/*
* Query only by sysprops, like: by vertex label, by edge label.
* NOTE: we assume sysprops would be indexed by backend store
* but we don't support query edges only by direction/target-vertex.
*/
if (query.allSysprop()) {
if (query.resultType().isVertex()) {
verifyVerticesConditionQuery(query);
} else if (query.resultType().isEdge()) {
verifyEdgesConditionQuery(query);
}
/*
* Just support:
* 1.not query by label
* 2.or query by label and store supports this feature
*/
boolean byLabel = (label != null && query.conditionsSize() == 1);
if (!byLabel || this.store().features().supportsQueryByLabel()) {
return query;
}
}
return null;
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class GraphTransaction method constructVertex.
@Watched(prefix = "graph")
public HugeVertex constructVertex(boolean verifyVL, Object... keyValues) {
HugeElement.ElementKeys elemKeys = HugeElement.classifyKeys(keyValues);
if (possibleOlapVertex(elemKeys)) {
Id id = HugeVertex.getIdValue(elemKeys.id());
HugeVertex vertex = HugeVertex.create(this, id, VertexLabel.OLAP_VL);
ElementHelper.attachProperties(vertex, keyValues);
Iterator<HugeProperty<?>> iterator = vertex.getProperties().iterator();
assert iterator.hasNext();
if (iterator.next().propertyKey().olap()) {
return vertex;
}
}
VertexLabel vertexLabel = this.checkVertexLabel(elemKeys.label(), verifyVL);
Id id = HugeVertex.getIdValue(elemKeys.id());
List<Id> keys = this.graph().mapPkName2Id(elemKeys.keys());
// Check whether id match with id strategy
this.checkId(id, keys, vertexLabel);
// Create HugeVertex
HugeVertex vertex = HugeVertex.create(this, null, vertexLabel);
// Set properties
ElementHelper.attachProperties(vertex, keyValues);
// Assign vertex id
if (this.params().mode().maintaining() && vertexLabel.idStrategy() == IdStrategy.AUTOMATIC) {
// Resume id for AUTOMATIC id strategy in restoring mode
vertex.assignId(id, true);
} else {
vertex.assignId(id);
}
return vertex;
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class GraphTransaction method checkNonnullProperty.
private void checkNonnullProperty(HugeVertex vertex) {
Set<Id> keys = vertex.getPropertyKeys();
VertexLabel vertexLabel = vertex.schemaLabel();
// Check whether passed all non-null property
@SuppressWarnings("unchecked") Collection<Id> nonNullKeys = CollectionUtils.subtract(vertexLabel.properties(), vertexLabel.nullableKeys());
if (!keys.containsAll(nonNullKeys)) {
@SuppressWarnings("unchecked") Collection<Id> missed = CollectionUtils.subtract(nonNullKeys, keys);
HugeGraph graph = this.graph();
E.checkArgument(false, "All non-null property keys %s of " + "vertex label '%s' must be set, missed keys %s", graph.mapPkId2Name(nonNullKeys), vertexLabel.name(), graph.mapPkId2Name(missed));
}
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class EdgeLabelBuilder method checkTtl.
private void checkTtl() {
E.checkArgument(this.ttl >= 0, "The ttl must be >= 0, but got: %s", this.ttl);
if (this.ttl == 0L) {
E.checkArgument(this.ttlStartTime == null, "Can't set ttl start time if ttl is not set");
return;
}
VertexLabel source = this.graph().vertexLabel(this.sourceLabel);
VertexLabel target = this.graph().vertexLabel(this.targetLabel);
E.checkArgument((source.ttl() == 0L || this.ttl <= source.ttl()) && (target.ttl() == 0L || this.ttl <= target.ttl()), "The ttl(%s) of edge label '%s' should less than " + "ttl(%s) of source label '%s' and ttl(%s) of target " + "label '%s'", this.ttl, this.name, source.ttl(), this.sourceLabel, target.ttl(), this.targetLabel);
if (this.ttlStartTime == null) {
return;
}
// Check whether the properties contains the specified keys
E.checkArgument(!this.properties.isEmpty(), "The properties can't be empty when exist " + "ttl start time for edge label '%s'", this.name);
E.checkArgument(this.properties.contains(this.ttlStartTime), "The ttl start time '%s' must be contained in " + "properties '%s' for edge label '%s'", this.ttlStartTime, this.name, this.properties);
PropertyKey pkey = this.graph().propertyKey(this.ttlStartTime);
E.checkArgument(pkey.dataType().isDate(), "The ttl start time property must be date type," + "but got '%s(%s)'", this.ttlStartTime, pkey.dataType());
}
Aggregations