Skip to content
Permalink
Browse files
add Combiner (#12)
* add Combiner, DoubleValueSumCombiner, IdValueMinimumCombiner, LongValueSumCombiner
* Value implements Comparable<Value>
  • Loading branch information
houzhizhen committed Feb 5, 2021
1 parent 90a9656 commit 3773c5f4c8f6fb94912025aa9bb0971913bc7a6d
Showing 31 changed files with 568 additions and 11 deletions.
@@ -0,0 +1,30 @@
/*
* 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.computer.core.combiner;

public interface Combiner<T> {

/**
* Combine v1 and v2, return the combined value. The combined value may
* take use v1 or v2. The value of v1 and v2 may be updated. Should not
* use v1 and v2 after combine them.
*/
T combine(T v1, T v2);
}
@@ -0,0 +1,31 @@
/*
* 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.computer.core.combiner;

import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;

public class DoubleValueSumCombiner implements Combiner<DoubleValue> {

@Override
public DoubleValue combine(DoubleValue v1, DoubleValue v2) {
v2.value(v1.value() + v2.value());
return v2;
}
}
@@ -0,0 +1,31 @@
/*
* 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.computer.core.combiner;

import com.baidu.hugegraph.computer.core.graph.value.LongValue;

public class LongValueSumCombiner implements Combiner<LongValue> {

@Override
public LongValue combine(LongValue v1, LongValue v2) {
v2.value(v1.value() + v2.value());
return v2;
}
}
@@ -0,0 +1,34 @@
/*
* 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.computer.core.combiner;

import com.baidu.hugegraph.computer.core.graph.value.Value;

public class ValueMaxCombiner <T extends Value> implements Combiner<T> {

@Override
public T combine(T v1, T v2) {
if (v1.compareTo(v2) >= 0) {
return v1;
} else {
return v2;
}
}
}
@@ -0,0 +1,34 @@
/*
* 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.computer.core.combiner;

import com.baidu.hugegraph.computer.core.graph.value.Value;

public class ValueMinCombiner<T extends Value> implements Combiner<T> {

@Override
public T combine(T v1, T v2) {
if (v1.compareTo(v2) <= 0) {
return v1;
} else {
return v2;
}
}
}
@@ -23,8 +23,9 @@

import com.baidu.hugegraph.computer.core.io.GraphInput;
import com.baidu.hugegraph.computer.core.io.GraphOutput;
import com.baidu.hugegraph.util.E;

public class BooleanValue implements Value {
public class BooleanValue implements Value<BooleanValue> {

public static final BooleanValue TRUE = new BooleanValue(true);
public static final BooleanValue FALSE = new BooleanValue(false);
@@ -66,6 +67,12 @@ public void write(GraphOutput out) throws IOException {
out.writeBoolean(this.value);
}

@Override
public int compareTo(BooleanValue obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
return Boolean.compare(this.value, obj.value);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BooleanValue)) {
@@ -23,8 +23,9 @@

import com.baidu.hugegraph.computer.core.io.GraphInput;
import com.baidu.hugegraph.computer.core.io.GraphOutput;
import com.baidu.hugegraph.util.E;

public class DoubleValue implements Value {
public class DoubleValue implements Value<DoubleValue> {

private double value;

@@ -63,6 +64,12 @@ public void write(GraphOutput out) throws IOException {
out.writeDouble(this.value);
}

@Override
public int compareTo(DoubleValue obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
return Double.compare(this.value, obj.value);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof DoubleValue)) {
@@ -23,8 +23,9 @@

import com.baidu.hugegraph.computer.core.io.GraphInput;
import com.baidu.hugegraph.computer.core.io.GraphOutput;
import com.baidu.hugegraph.util.E;

public class FloatValue implements Value {
public class FloatValue implements Value<FloatValue> {

private float value;

@@ -63,6 +64,12 @@ public void write(GraphOutput out) throws IOException {
out.writeFloat(this.value);
}

@Override
public int compareTo(FloatValue obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
return Float.compare(this.value, obj.value);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof FloatValue)) {
@@ -32,8 +32,9 @@
import com.baidu.hugegraph.computer.core.io.OptimizedStreamGraphInput;
import com.baidu.hugegraph.computer.core.io.StreamGraphInput;
import com.baidu.hugegraph.computer.core.util.ByteArrayUtil;
import com.baidu.hugegraph.util.E;

public class IdValue implements Value, Comparable<IdValue> {
public class IdValue implements Value<IdValue> {

private byte[] bytes;
private int length;
@@ -79,6 +80,7 @@ public void write(GraphOutput out) throws IOException {

@Override
public int compareTo(IdValue obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
return ByteArrayUtil.compare(this.bytes, this.length,
obj.bytes, obj.length);
}
@@ -23,8 +23,9 @@

import com.baidu.hugegraph.computer.core.io.GraphInput;
import com.baidu.hugegraph.computer.core.io.GraphOutput;
import com.baidu.hugegraph.util.E;

public class IntValue implements Value {
public class IntValue implements Value<IntValue> {

private int value;

@@ -63,6 +64,12 @@ public void write(GraphOutput out) throws IOException {
out.writeInt(this.value);
}

@Override
public int compareTo(IntValue obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
return Integer.compare(this.value, obj.value);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof IntValue)) {
@@ -31,7 +31,7 @@
import com.baidu.hugegraph.computer.core.io.GraphOutput;
import com.baidu.hugegraph.util.E;

public class ListValue<T extends Value> implements Value {
public class ListValue<T extends Value> implements Value<ListValue<T>> {

private ValueType elemType;
private List<T> values;
@@ -117,6 +117,22 @@ protected void write(GraphOutput out, boolean writeElemType)
}
}

@Override
public int compareTo(ListValue<T> obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
int cmp = this.size() - obj.size();
if (cmp != 0) {
return cmp;
}
for (int i = 0; i < this.size(); i++) {
cmp = this.values.get(i).compareTo(obj.values.get(i));
if (cmp != 0) {
return cmp;
}
}
return 0;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof ListValue)) {
@@ -23,8 +23,9 @@

import com.baidu.hugegraph.computer.core.io.GraphInput;
import com.baidu.hugegraph.computer.core.io.GraphOutput;
import com.baidu.hugegraph.util.E;

public class LongValue implements Value {
public class LongValue implements Value<LongValue> {

private long value;

@@ -63,6 +64,12 @@ public void write(GraphOutput out) throws IOException {
out.writeLong(this.value);
}

@Override
public int compareTo(LongValue obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
return Long.compare(this.value, obj.value);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof LongValue)) {
@@ -23,8 +23,9 @@

import com.baidu.hugegraph.computer.core.io.GraphInput;
import com.baidu.hugegraph.computer.core.io.GraphOutput;
import com.baidu.hugegraph.util.E;

public class NullValue implements Value {
public class NullValue implements Value<NullValue> {

private static final NullValue INSTANCE = new NullValue();

@@ -53,6 +54,12 @@ public void read(GraphInput in) throws IOException {
// Do nothing
}

@Override
public int compareTo(NullValue obj) {
E.checkArgumentNotNull(obj, "The compare argument can't be null");
return 0;
}

@Override
public boolean equals(Object obj) {
return obj == INSTANCE || obj instanceof NullValue;
@@ -22,7 +22,7 @@
import com.baidu.hugegraph.computer.core.io.Readable;
import com.baidu.hugegraph.computer.core.io.Writable;

public interface Value extends Writable, Readable {
public interface Value<T> extends Writable, Readable, Comparable<T> {

ValueType type();
}
@@ -26,8 +26,8 @@

import com.baidu.hugegraph.computer.core.allocator.AllocatorTestSuite;
import com.baidu.hugegraph.computer.core.bsp.BspTestSuite;
import com.baidu.hugegraph.computer.core.combiner.CombinerTestSuite;
import com.baidu.hugegraph.computer.core.common.CommonTestSuite;
import com.baidu.hugegraph.computer.core.common.ExceptionTest;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.GraphTestSuite;
import com.baidu.hugegraph.computer.core.io.IOTestSuite;
@@ -40,7 +40,7 @@
AllocatorTestSuite.class,
CommonTestSuite.class,
BspTestSuite.class,
ExceptionTest.class,
CombinerTestSuite.class,
GraphTestSuite.class,
IOTestSuite.class,
BspTestSuite.class,

0 comments on commit 3773c5f

Please sign in to comment.