Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
package com.baidu.hugegraph.computer.core.combiner;

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

public class DoubleValueSumCombiner implements Combiner<DoubleValue> {

@Override
public DoubleValue combine(DoubleValue v1, DoubleValue v2) {
E.checkArgumentNotNull(v1, "The parameter v1 can't be null");
E.checkArgumentNotNull(v2, "The parameter v2 can't be null");
v2.value(v1.value() + v2.value());
return v2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
package com.baidu.hugegraph.computer.core.combiner;

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

public class LongValueSumCombiner implements Combiner<LongValue> {

@Override
public LongValue combine(LongValue v1, LongValue v2) {
E.checkArgumentNotNull(v1, "The parameter v1 can't be null");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The combine parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unified with others not null checks. If it fails, the stack trance will show the information.

E.checkArgumentNotNull(v2, "The parameter v2 can't be null");
v2.value(v1.value() + v2.value());
return v2;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 java.util.Map;

import com.baidu.hugegraph.computer.core.graph.properties.Properties;
import com.baidu.hugegraph.computer.core.graph.value.Value;
import com.baidu.hugegraph.util.E;

public class MergeNewPropertiesCombiner implements PropertiesCombiner {

/**
* Merge properties v2 into v1. If a property exists in both v1 and v2,
* remain the value in v1.
*/
@Override
public Properties combine(Properties v1, Properties v2) {
E.checkArgumentNotNull(v1, "The parameter v1 can't be null");
E.checkArgumentNotNull(v2, "The parameter v2 can't be null");
Map<String, Value> v1Map = v1.get();
Map<String, Value> v2Map = v2.get();
for (Map.Entry<String, Value> entry : v2Map.entrySet()) {
v1Map.putIfAbsent(entry.getKey(), entry.getValue());
}
return v1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 java.util.Map;

import com.baidu.hugegraph.computer.core.graph.properties.Properties;
import com.baidu.hugegraph.computer.core.graph.value.Value;
import com.baidu.hugegraph.util.E;

public class MergeOldPropertiesCombiner implements PropertiesCombiner {

/**
* Merge properties v1 into v2. If a property exists in both v1 and v2,
* remain the value in v2.
*/
@Override
public Properties combine(Properties v1, Properties v2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check v1 and v2 not null, and add test case.
also improve Value*Combiner

E.checkArgumentNotNull(v1, "The parameter v1 can't be null");
E.checkArgumentNotNull(v2, "The parameter v2 can't be null");
Map<String, Value> v1Map = v1.get();
Map<String, Value> v2Map = v2.get();
for (Map.Entry<String, Value> entry : v1Map.entrySet()) {
v2Map.putIfAbsent(entry.getKey(), entry.getValue());
}
return v2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.util.E;

public class OverwriteCombiner<T> implements Combiner<T> {

@Override
public T combine(T v1, T v2) {
E.checkArgumentNotNull(v1, "The parameter v1 can't be null");
E.checkArgumentNotNull(v2, "The parameter v2 can't be null");
return v2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.properties.Properties;

/**
* When vertex properties with the same vertex id are loaded, this class
* specifies how to combine their properties.
*/
public interface PropertiesCombiner extends Combiner<Properties> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
package com.baidu.hugegraph.computer.core.combiner;

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

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

@Override
public T combine(T v1, T v2) {
E.checkArgumentNotNull(v1, "The parameter v1 can't be null");
E.checkArgumentNotNull(v2, "The parameter v2 can't be null");
if (v1.compareTo(v2) >= 0) {
return v1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
package com.baidu.hugegraph.computer.core.combiner;

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

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

@Override
public T combine(T v1, T v2) {
E.checkArgumentNotNull(v1, "The parameter v1 can't be null");
E.checkArgumentNotNull(v2, "The parameter v2 can't be null");
if (v1.compareTo(v2) <= 0) {
return v1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
OverwriteCombinerTest.class,
MergeOldPropertiesCombinerTest.class,
MergeNewPropertiesCombinerTest.class,
DoubleValueSumCombinerTest.class,
LongValueSumCombinerTest.class,
ValueMinCombinerTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class DoubleValueSumCombinerTest {

@Test
public void test() {
public void testCombine() {
DoubleValue sum = new DoubleValue(0.0D);
DoubleValueSumCombiner combiner = new DoubleValueSumCombiner();
for (int i = 1; i <= 10; i++) {
Expand All @@ -36,4 +36,24 @@ public void test() {
}
Assert.assertEquals(55.0D, sum.value(), 0.0D);
}

@Test
public void testCombineNull() {
DoubleValue value1 = new DoubleValue(0.0D);
DoubleValue value2 = new DoubleValue(0.0D);
DoubleValueSumCombiner combiner = new DoubleValueSumCombiner();
Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(null, value2);
}, e -> {
Assert.assertEquals("The parameter v1 can't be null",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(value1, null);
}, e -> {
Assert.assertEquals("The parameter v2 can't be null",
e.getMessage());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class LongValueSumCombinerTest {

@Test
public void test() {
public void testCombine() {
LongValue sum = new LongValue(0L);
LongValueSumCombiner combiner = new LongValueSumCombiner();
for (int i = 1; i <= 10; i++) {
Expand All @@ -36,4 +36,24 @@ public void test() {
}
Assert.assertEquals(55L, sum.value());
}

@Test
public void testCombineNull() {
LongValue value1 = new LongValue(1L);
LongValue value2 = new LongValue(2L);
LongValueSumCombiner combiner = new LongValueSumCombiner();
Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(null, value2);
}, e -> {
Assert.assertEquals("The parameter v1 can't be null",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(value1, null);
}, e -> {
Assert.assertEquals("The parameter v2 can't be null",
e.getMessage());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 org.junit.Test;

import com.baidu.hugegraph.computer.core.graph.id.Utf8Id;
import com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties;
import com.baidu.hugegraph.computer.core.graph.properties.Properties;
import com.baidu.hugegraph.testutil.Assert;

public class MergeNewPropertiesCombinerTest {

@Test
public void testCombine() {
Properties properties1 = new DefaultProperties();
properties1.put("name", new Utf8Id("marko").idValue());
properties1.put("city", new Utf8Id("Beijing").idValue());

Properties properties2 = new DefaultProperties();
properties2.put("name", new Utf8Id("josh").idValue());
properties2.put("age", new Utf8Id("18").idValue());

Properties expect = new DefaultProperties();
expect.put("name", new Utf8Id("marko").idValue());
expect.put("age", new Utf8Id("18").idValue());
expect.put("city", new Utf8Id("Beijing").idValue());

PropertiesCombiner combiner = new MergeNewPropertiesCombiner();
Properties properties = combiner.combine(properties1, properties2);
Assert.assertEquals(expect, properties);

Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(null, properties2);
}, e -> {
Assert.assertEquals("The parameter v1 can't be null",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(properties1, null);
}, e -> {
Assert.assertEquals("The parameter v2 can't be null",
e.getMessage());
});
}

@Test
public void testCombineNull() {
Properties properties1 = new DefaultProperties();
properties1.put("name", new Utf8Id("marko").idValue());
properties1.put("city", new Utf8Id("Beijing").idValue());

Properties properties2 = new DefaultProperties();
properties2.put("name", new Utf8Id("josh").idValue());
properties2.put("age", new Utf8Id("18").idValue());


PropertiesCombiner combiner = new MergeNewPropertiesCombiner();

Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(null, properties2);
}, e -> {
Assert.assertEquals("The parameter v1 can't be null",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
combiner.combine(properties1, null);
}, e -> {
Assert.assertEquals("The parameter v2 can't be null",
e.getMessage());
});
}
}
Loading