Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support aggregate property #693

Merged
merged 17 commits into from
Oct 12, 2019
2 changes: 1 addition & 1 deletion hugegraph-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Version>0.46.0.0</Implementation-Version>
<Implementation-Version>0.47.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.define.Checkable;
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.type.define.AggregateType;
import com.baidu.hugegraph.type.define.Cardinality;
import com.baidu.hugegraph.type.define.DataType;
import com.baidu.hugegraph.type.define.GraphMode;
Expand Down Expand Up @@ -155,6 +156,8 @@ private static class JsonPropertyKey implements Checkable {
public Cardinality cardinality;
@JsonProperty("data_type")
public DataType dataType;
@JsonProperty("aggregate_type")
public AggregateType aggregateType;
@JsonProperty("properties")
public String[] properties;
@JsonProperty("user_data")
Expand Down Expand Up @@ -191,6 +194,9 @@ private PropertyKey.Builder convert2Builder(HugeGraph g) {
if (this.dataType != null) {
builder.dataType(this.dataType);
}
if (this.aggregateType != null) {
builder.aggregateType(this.aggregateType);
}
if (this.userdata != null) {
builder.userdata(this.userdata);
}
Expand All @@ -203,9 +209,11 @@ private PropertyKey.Builder convert2Builder(HugeGraph g) {
@Override
public String toString() {
return String.format("JsonPropertyKey{name=%s, cardinality=%s, " +
"dataType=%s, properties=%s}",
"dataType=%s, aggregateType=%s, " +
"properties=%s}",
this.name, this.cardinality,
this.dataType, this.properties);
this.dataType, this.aggregateType,
this.properties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ public final class ApiVersion {
* [0.44] Issue-633: Support unique index
* [0.45] Issue-673: Add 'OVERRIDE' update strategy
* [0.46] Issue-618 & 694: Support UUID id type
* [0.47] Issue-691: Support aggregate property
*/

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

public static final void check() {
// Check version of hugegraph-core. Firstly do check from version 0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ public boolean supportsTransaction() {
public boolean supportsNumberType() {
return true;
}

@Override
public boolean supportsAggregateProperty() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,18 @@ protected void parseProperties(HugeElement element,
}

@Override
protected Object writeProperty(HugeProperty<?> property) {
protected Object writeProperty(PropertyKey propertyKey, Object value) {
BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_PROPERTY);
buffer.writeProperty(property.propertyKey(), property.value());
buffer.flip();
return buffer.asByteBuffer();
}

@Override
protected Object writeProperty(Object value) {
/*
* Since we can't know the type of the property value in some scenarios,
* so need to construct a fake property key to serialize to reuse code.
*/
PropertyKey pkey = new PropertyKey(null, IdGenerator.of(0L), "fake");
pkey.dataType(DataType.fromClass(value.getClass()));
BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_PROPERTY);
buffer.writeProperty(pkey, value);
if (propertyKey == null) {
/*
* Since we can't know the type of the property value in some
* scenarios so need to construct a fake property key to
* serialize to reuse code.
*/
propertyKey = new PropertyKey(null, IdGenerator.of(0L), "fake");
propertyKey.dataType(DataType.fromClass(value.getClass()));
}
buffer.writeProperty(propertyKey, value);
buffer.flip();
return buffer.asByteBuffer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public String version() {
* rangeLong and rangeDouble
* [1.5] #633: support unique index
* [1.6] #661 & #680: support bin serialization for cassandra
* [1.7] #691: support aggregate property
*/
return "1.6";
return "1.7";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public void init(CassandraSessionPool.Session session) {
.put(HugeKeys.NAME, DataType.text())
.put(HugeKeys.DATA_TYPE, DataType.tinyint())
.put(HugeKeys.CARDINALITY, DataType.tinyint())
.put(HugeKeys.AGGREGATE_TYPE, DataType.tinyint())
.put(HugeKeys.PROPERTIES, DataType.set(TYPE_PK))
.put(HugeKeys.USER_DATA, TYPE_UD)
.put(HugeKeys.STATUS, DataType.tinyint())
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.6.14</version>
<version>1.6.15</version>
</dependency>

<!-- tinkerpop -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ public List<Relation> userpropRelations() {
}

public void resetUserpropConditions() {
this.checkFlattened();
this.conditions.removeIf(condition -> !condition.isSysprop());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.structure.HugeVertexProperty;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.AggregateType;
import com.baidu.hugegraph.type.define.Cardinality;
import com.baidu.hugegraph.type.define.DataType;
import com.baidu.hugegraph.type.define.Directions;
Expand Down Expand Up @@ -950,6 +951,7 @@ public BinaryBackendEntry writePropertyKey(PropertyKey schema) {
writeString(HugeKeys.NAME, schema.name());
writeEnum(HugeKeys.DATA_TYPE, schema.dataType());
writeEnum(HugeKeys.CARDINALITY, schema.cardinality());
writeEnum(HugeKeys.AGGREGATE_TYPE, schema.aggregateType());
writeIds(HugeKeys.PROPERTIES, schema.properties());
writeEnum(HugeKeys.STATUS, schema.status());
writeUserdata(schema);
Expand All @@ -967,6 +969,8 @@ public PropertyKey readPropertyKey(HugeGraph graph,
propertyKey.dataType(readEnum(HugeKeys.DATA_TYPE, DataType.class));
propertyKey.cardinality(readEnum(HugeKeys.CARDINALITY,
Cardinality.class));
propertyKey.aggregateType(readEnum(HugeKeys.AGGREGATE_TYPE,
AggregateType.class));
propertyKey.properties(readIds(HugeKeys.PROPERTIES));
propertyKey.status(readEnum(HugeKeys.STATUS, SchemaStatus.class));
readUserdata(propertyKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.structure.HugeVertexProperty;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.AggregateType;
import com.baidu.hugegraph.type.define.Cardinality;
import com.baidu.hugegraph.type.define.DataType;
import com.baidu.hugegraph.type.define.Directions;
Expand Down Expand Up @@ -110,10 +111,10 @@ protected void parseProperty(Id key, Object colValue, HugeElement owner) {
}

protected Object writeProperty(HugeProperty<?> property) {
return this.writeProperty(property.value());
return this.writeProperty(property.propertyKey(), property.value());
}

protected Object writeProperty(Object value) {
protected Object writeProperty(PropertyKey propertyKey, Object value) {
return JsonUtil.toJson(value);
}

Expand Down Expand Up @@ -389,7 +390,7 @@ protected Query writeQueryCondition(Query query) {

if (query.resultType().isGraph() &&
r.relation() == Condition.RelationType.CONTAINS_VALUE) {
r.serialValue(this.writeProperty(r.serialValue()));
r.serialValue(this.writeProperty(null, r.serialValue()));
}
}

Expand Down Expand Up @@ -445,6 +446,8 @@ public BackendEntry writePropertyKey(PropertyKey propertyKey) {
entry.column(HugeKeys.NAME, propertyKey.name());
entry.column(HugeKeys.DATA_TYPE, propertyKey.dataType().code());
entry.column(HugeKeys.CARDINALITY, propertyKey.cardinality().code());
entry.column(HugeKeys.AGGREGATE_TYPE,
propertyKey.aggregateType().code());
entry.column(HugeKeys.PROPERTIES,
this.toLongSet(propertyKey.properties()));
this.writeUserdata(propertyKey, entry);
Expand Down Expand Up @@ -532,6 +535,7 @@ public PropertyKey readPropertyKey(HugeGraph graph,
String name = entry.column(HugeKeys.NAME);
Number dataType = entry.column(HugeKeys.DATA_TYPE);
Number cardinality = entry.column(HugeKeys.CARDINALITY);
Number aggregateType = entry.column(HugeKeys.AGGREGATE_TYPE);
Object properties = entry.column(HugeKeys.PROPERTIES);
Number status = entry.column(HugeKeys.STATUS);

Expand All @@ -540,6 +544,9 @@ public PropertyKey readPropertyKey(HugeGraph graph,
dataType.byteValue()));
propertyKey.cardinality(SerialEnum.fromCode(Cardinality.class,
cardinality.byteValue()));
propertyKey.aggregateType(SerialEnum.fromCode(
AggregateType.class,
aggregateType.byteValue()));
propertyKey.properties(this.toIdArray(properties));

this.readUserdata(propertyKey, entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.structure.HugeVertexProperty;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.AggregateType;
import com.baidu.hugegraph.type.define.Cardinality;
import com.baidu.hugegraph.type.define.DataType;
import com.baidu.hugegraph.type.define.Directions;
Expand Down Expand Up @@ -618,6 +619,8 @@ public BackendEntry writePropertyKey(PropertyKey propertyKey) {
JsonUtil.toJson(propertyKey.dataType()));
entry.column(HugeKeys.CARDINALITY,
JsonUtil.toJson(propertyKey.cardinality()));
entry.column(HugeKeys.AGGREGATE_TYPE,
JsonUtil.toJson(propertyKey.aggregateType()));
entry.column(HugeKeys.PROPERTIES, writeIds(propertyKey.properties()));
writeUserdata(propertyKey, entry);
entry.column(HugeKeys.STATUS,
Expand All @@ -638,13 +641,16 @@ public PropertyKey readPropertyKey(HugeGraph graph,
String.class);
String dataType = entry.column(HugeKeys.DATA_TYPE);
String cardinality = entry.column(HugeKeys.CARDINALITY);
String aggregateType = entry.column(HugeKeys.AGGREGATE_TYPE);
String properties = entry.column(HugeKeys.PROPERTIES);
String status = entry.column(HugeKeys.STATUS);

PropertyKey propertyKey = new PropertyKey(graph, id, name);
propertyKey.dataType(JsonUtil.fromJson(dataType, DataType.class));
propertyKey.cardinality(JsonUtil.fromJson(cardinality,
Cardinality.class));
propertyKey.aggregateType(JsonUtil.fromJson(aggregateType,
AggregateType.class));
propertyKey.properties(readIds(properties));
readUserdata(propertyKey, entry);
propertyKey.status(JsonUtil.fromJson(status, SchemaStatus.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ public default boolean supportsPersistence() {
public boolean supportsTransaction();

public boolean supportsNumberType();

public boolean supportsAggregateProperty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,10 @@ public boolean supportsTransaction() {
public boolean supportsNumberType() {
return false;
}

@Override
public boolean supportsAggregateProperty() {
return false;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ protected void prepareAdditions(Map<Id, HugeVertex> addedVertices,
for (HugeVertex v : addedVertices.values()) {
assert !v.removed();
v.committed();
this.checkAggregateProperty(v);
// Add vertex entry
this.doInsert(this.serializer.writeVertex(v));
// Update index of vertex(only include props)
Expand All @@ -248,6 +249,7 @@ protected void prepareAdditions(Map<Id, HugeVertex> addedVertices,
if (this.removingEdgeOwner(e)) {
continue;
}
this.checkAggregateProperty(e);
// Add edge entry of OUT and IN
this.doInsert(this.serializer.writeEdge(e));
this.doInsert(this.serializer.writeEdge(e.switchOwner()));
Expand All @@ -274,6 +276,7 @@ protected void prepareDeletions(Map<Id, HugeVertex> removedVertices,

// Remove vertices
for (HugeVertex v : removedVertices.values()) {
this.checkAggregateProperty(v);
/*
* If the backend stores vertex together with edges, it's edges
* would be removed after removing vertex. Otherwise, if the
Expand All @@ -287,6 +290,7 @@ protected void prepareDeletions(Map<Id, HugeVertex> removedVertices,

// Remove edges
for (HugeEdge e : removedEdges.values()) {
this.checkAggregateProperty(e);
// Update edge index
this.indexTx.updateEdgeIndex(e, true);
this.indexTx.updateLabelIndex(e, true);
Expand All @@ -300,6 +304,7 @@ protected void prepareDeletions(Map<Id, HugeVertex> removedVertices,
protected void prepareUpdates(Set<HugeProperty<?>> addedProps,
Set<HugeProperty<?>> removedProps) {
for (HugeProperty<?> p : removedProps) {
this.checkAggregateProperty(p);
if (p.element().type().isVertex()) {
HugeVertexProperty<?> prop = (HugeVertexProperty<?>) p;
if (this.store().features().supportsUpdateVertexProperty()) {
Expand Down Expand Up @@ -328,6 +333,7 @@ protected void prepareUpdates(Set<HugeProperty<?>> addedProps,
}
}
for (HugeProperty<?> p : addedProps) {
this.checkAggregateProperty(p);
if (p.element().type().isVertex()) {
HugeVertexProperty<?> prop = (HugeVertexProperty<?>) p;
if (this.store().features().supportsUpdateVertexProperty()) {
Expand Down Expand Up @@ -355,7 +361,6 @@ protected void prepareUpdates(Set<HugeProperty<?>> addedProps,
}
}
}

}

@Override
Expand Down Expand Up @@ -1016,6 +1021,20 @@ public static void verifyEdgesConditionQuery(ConditionQuery query) {
}
}

private void checkAggregateProperty(HugeElement element) {
E.checkArgument(element.getAggregateProperties().isEmpty() ||
this.store().features().supportsAggregateProperty(),
"The %s store does not support aggregate property",
this.store().provider().type());
}

private void checkAggregateProperty(HugeProperty property) {
E.checkArgument(!property.isAggregateType() ||
this.store().features().supportsAggregateProperty(),
"The %s store does not support aggregate property",
this.store().provider().type());
}

private Query optimizeQuery(ConditionQuery query) {
Id label = (Id) query.condition(HugeKeys.LABEL);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public Map<HugeKeys, Object> writePropertyKey(PropertyKey propertyKey) {
map.put(HugeKeys.NAME, propertyKey.name());
map.put(HugeKeys.DATA_TYPE, propertyKey.dataType());
map.put(HugeKeys.CARDINALITY, propertyKey.cardinality());
map.put(HugeKeys.AGGREGATE_TYPE, propertyKey.aggregateType());
map.put(HugeKeys.PROPERTIES,
graph.mapPkId2Name(propertyKey.properties()));
map.put(HugeKeys.USER_DATA, propertyKey.userdata());
Expand Down
Loading