Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
add DefaultPropertiesCombiner and DefaultVertexValueCombiner (#16)
* add OverwriteValueCombiner, MergeNewPropertiesCombiner and MergeOldPropertiesCombiner
- Loading branch information
Showing
16 changed files
with
505 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 combine parameter v1 can't be null"); | ||
E.checkArgumentNotNull(v2, "The combine 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
E.checkArgumentNotNull(v1, "The combine parameter v1 can't be null"); | ||
E.checkArgumentNotNull(v2, "The combine 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 combine parameter v1 can't be null"); | ||
E.checkArgumentNotNull(v2, "The combine parameter v2 can't be null"); | ||
return v2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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); | ||
} | ||
|
||
@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 combine parameter v1 can't be null", | ||
e.getMessage()); | ||
}); | ||
|
||
Assert.assertThrows(IllegalArgumentException.class, () -> { | ||
combiner.combine(properties1, null); | ||
}, e -> { | ||
Assert.assertEquals("The combine parameter v2 can't be null", | ||
e.getMessage()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.