Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<PropertyKey> {
Expand Down Expand Up @@ -95,6 +103,16 @@ public interface Builder extends SchemaBuilder<PropertyKey> {

Builder valueSet();

Builder aggregateType(AggregateType aggregateType);

Builder calcSum();

Builder calcMax();

Builder calcMin();

Builder calcOld();

Builder userdata(String key, Object val);

Builder ifNotExist();
Expand Down Expand Up @@ -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");
Expand Down
75 changes: 75 additions & 0 deletions src/test/java/com/baidu/hugegraph/api/PropertyKeyApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, () -> {
Expand Down