diff --git a/src/main/java/com/baidu/hugegraph/structure/constant/AggregateType.java b/src/main/java/com/baidu/hugegraph/structure/constant/AggregateType.java new file mode 100644 index 00000000..03d13de3 --- /dev/null +++ b/src/main/java/com/baidu/hugegraph/structure/constant/AggregateType.java @@ -0,0 +1,74 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.structure.constant; + +public enum AggregateType { + + NONE(0, "none"), + MAX(1, "max"), + MIN(2, "min"), + SUM(3, "sum"), + OLD(4, "old"); + + private byte code = 0; + private String name = null; + + AggregateType(int code, String name) { + assert code < 256; + this.code = (byte) code; + this.name = name; + } + + public byte code() { + return this.code; + } + + public String string() { + return this.name; + } + + public boolean isNone() { + return this == NONE; + } + + public boolean isMax() { + return this == MAX; + } + + public boolean isMin() { + return this == MIN; + } + + public boolean isSum() { + return this == SUM; + } + + public boolean isNumber() { + return this.isMax() || this.isMin() || this.isSum(); + } + + public boolean isOld() { + return this == OLD; + } + + public boolean isIndexable() { + return this == NONE || this == MAX || this == MIN || this == OLD; + } +} diff --git a/src/main/java/com/baidu/hugegraph/structure/schema/PropertyKey.java b/src/main/java/com/baidu/hugegraph/structure/schema/PropertyKey.java index 735c6c29..e05cb090 100644 --- a/src/main/java/com/baidu/hugegraph/structure/schema/PropertyKey.java +++ b/src/main/java/com/baidu/hugegraph/structure/schema/PropertyKey.java @@ -21,6 +21,7 @@ import com.baidu.hugegraph.driver.SchemaManager; import com.baidu.hugegraph.structure.SchemaElement; +import com.baidu.hugegraph.structure.constant.AggregateType; import com.baidu.hugegraph.structure.constant.Cardinality; import com.baidu.hugegraph.structure.constant.DataType; import com.baidu.hugegraph.structure.constant.HugeType; @@ -34,12 +35,15 @@ public class PropertyKey extends SchemaElement { private DataType dataType; @JsonProperty("cardinality") private Cardinality cardinality; + @JsonProperty("aggregate_type") + private AggregateType aggregateType; @JsonCreator public PropertyKey(@JsonProperty("name") String name) { super(name); this.dataType = DataType.TEXT; this.cardinality = Cardinality.SINGLE; + this.aggregateType = AggregateType.NONE; } @Override @@ -55,12 +59,16 @@ public Cardinality cardinality() { return this.cardinality; } + public AggregateType aggregateType() { + return this.aggregateType; + } + @Override public String toString() { - return String.format("{name=%s, cardinality=%s, " + - "dataType=%s, properties=%s}", - this.name, this.cardinality, - this.dataType, this.properties); + return String.format("{name=%s, cardinality=%s, dataType=%s, " + + "aggregateType=%s, properties=%s}", + this.name, this.cardinality, this.dataType, + this.aggregateType, this.properties); } public interface Builder extends SchemaBuilder { @@ -95,6 +103,16 @@ public interface Builder extends SchemaBuilder { Builder valueSet(); + Builder aggregateType(AggregateType aggregateType); + + Builder calcSum(); + + Builder calcMax(); + + Builder calcMin(); + + Builder calcOld(); + Builder userdata(String key, Object val); Builder ifNotExist(); @@ -225,6 +243,32 @@ public Builder valueSet() { return this; } + @Override + public Builder aggregateType(AggregateType aggregateType) { + this.propertyKey.aggregateType = aggregateType; + return this; + } + + @Override public Builder calcSum() { + this.propertyKey.aggregateType = AggregateType.SUM; + return this; + } + + @Override public Builder calcMax() { + this.propertyKey.aggregateType = AggregateType.MAX; + return this; + } + + @Override public Builder calcMin() { + this.propertyKey.aggregateType = AggregateType.MIN; + return this; + } + + @Override public Builder calcOld() { + this.propertyKey.aggregateType = AggregateType.OLD; + return this; + } + @Override public Builder userdata(String key, Object val) { E.checkArgumentNotNull(key, "The user data key can't be null"); diff --git a/src/test/java/com/baidu/hugegraph/api/PropertyKeyApiTest.java b/src/test/java/com/baidu/hugegraph/api/PropertyKeyApiTest.java index 37333784..a5f3ad12 100644 --- a/src/test/java/com/baidu/hugegraph/api/PropertyKeyApiTest.java +++ b/src/test/java/com/baidu/hugegraph/api/PropertyKeyApiTest.java @@ -24,6 +24,7 @@ import org.junit.After; import org.junit.Test; +import com.baidu.hugegraph.structure.constant.AggregateType; import com.baidu.hugegraph.structure.constant.Cardinality; import com.baidu.hugegraph.structure.constant.DataType; import com.baidu.hugegraph.structure.schema.PropertyKey; @@ -80,6 +81,80 @@ public void testCreateWithCardinality() { Assert.assertEquals(Cardinality.SET, propertyKey.cardinality()); } + @Test + public void testCreateWithAggregateType() { + PropertyKey propertyKey = schema().propertyKey("name") + .asText().valueSingle() + .build(); + + propertyKey = propertyKeyAPI.create(propertyKey); + + Assert.assertEquals("name", propertyKey.name()); + Assert.assertEquals(DataType.TEXT, propertyKey.dataType()); + Assert.assertEquals(Cardinality.SINGLE, propertyKey.cardinality()); + Assert.assertEquals(AggregateType.NONE, propertyKey.aggregateType()); + + propertyKey = schema().propertyKey("no") + .asText().valueSingle() + .aggregateType(AggregateType.OLD) + .build(); + + propertyKey = propertyKeyAPI.create(propertyKey); + + Assert.assertEquals("no", propertyKey.name()); + Assert.assertEquals(DataType.TEXT, propertyKey.dataType()); + Assert.assertEquals(Cardinality.SINGLE, propertyKey.cardinality()); + Assert.assertEquals(AggregateType.OLD, propertyKey.aggregateType()); + + propertyKey = schema().propertyKey("max") + .asInt().valueSingle() + .aggregateType(AggregateType.MAX) + .build(); + + propertyKey = propertyKeyAPI.create(propertyKey); + + Assert.assertEquals("max", propertyKey.name()); + Assert.assertEquals(DataType.INT, propertyKey.dataType()); + Assert.assertEquals(Cardinality.SINGLE, propertyKey.cardinality()); + Assert.assertEquals(AggregateType.MAX, propertyKey.aggregateType()); + + propertyKey = schema().propertyKey("min") + .asInt().valueSingle() + .aggregateType(AggregateType.MIN) + .build(); + + propertyKey = propertyKeyAPI.create(propertyKey); + + Assert.assertEquals("min", propertyKey.name()); + Assert.assertEquals(DataType.INT, propertyKey.dataType()); + Assert.assertEquals(Cardinality.SINGLE, propertyKey.cardinality()); + Assert.assertEquals(AggregateType.MIN, propertyKey.aggregateType()); + + propertyKey = schema().propertyKey("sum") + .asInt().valueSingle() + .aggregateType(AggregateType.SUM) + .build(); + + propertyKey = propertyKeyAPI.create(propertyKey); + + Assert.assertEquals("sum", propertyKey.name()); + Assert.assertEquals(DataType.INT, propertyKey.dataType()); + Assert.assertEquals(Cardinality.SINGLE, propertyKey.cardinality()); + Assert.assertEquals(AggregateType.SUM, propertyKey.aggregateType()); + + propertyKey = schema().propertyKey("total") + .asInt().valueSingle() + .aggregateType(AggregateType.SUM) + .build(); + + propertyKey = propertyKeyAPI.create(propertyKey); + + Assert.assertEquals("total", propertyKey.name()); + Assert.assertEquals(DataType.INT, propertyKey.dataType()); + Assert.assertEquals(Cardinality.SINGLE, propertyKey.cardinality()); + Assert.assertEquals(AggregateType.SUM, propertyKey.aggregateType()); + } + @Test public void testCreateWithInvalidName() { Utils.assertResponseError(400, () -> {