This is an automated email from the ASF dual-hosted git repository. xiaokang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-graphar.git
The following commit(s) were added to refs/heads/main by this push: new bc60fe47 feat(java,info): refactor `EdgeInfo` creation using access-level compliant builder, a… (#743) bc60fe47 is described below commit bc60fe47453bdeb27cd90785d322b2aae688363d Author: Bigu Cezar <jarvx...@gmail.com> AuthorDate: Mon Sep 8 05:05:01 2025 +0300 feat(java,info): refactor `EdgeInfo` creation using access-level compliant builder, a… (#743) * refactor: `EdgeInfo` creation using access-level compliant builder, added in `TestUtil` * style: Fixed formatting with spotless * refactor: added static factory to builder * feat: Added `add` methods inside builder for list types * feat: added `edgeTriplet` variable init support and null safety when streaming * style: Removed bulk dependency import * refactor: Added static factory for builder in wrapper class * fix: Added exception throwing for invalid arguments * test: Added testing for erroneous properties for `EdgeInfoBuilder` * fix: changed access modifiers to public in builder * chore: added license * test: Added tests for adding methods and appending to existing * refactor: added final to sample path * style: Applied spotless --- .../java/org/apache/graphar/info/EdgeInfo.java | 181 +++++++++++++++++++++ .../java/org/apache/graphar/info/EdgeInfoTest.java | 97 +++++++++++ .../java/org/apache/graphar/info/TestUtil.java | 64 ++++---- 3 files changed, 312 insertions(+), 30 deletions(-) diff --git a/maven-projects/info/src/main/java/org/apache/graphar/info/EdgeInfo.java b/maven-projects/info/src/main/java/org/apache/graphar/info/EdgeInfo.java index 07ecf619..31588c6a 100644 --- a/maven-projects/info/src/main/java/org/apache/graphar/info/EdgeInfo.java +++ b/maven-projects/info/src/main/java/org/apache/graphar/info/EdgeInfo.java @@ -20,6 +20,8 @@ package org.apache.graphar.info; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -51,6 +53,185 @@ public class EdgeInfo { private final PropertyGroups propertyGroups; private final VersionInfo version; + public static EdgeInfoBuilder builder() { + return new EdgeInfoBuilder(); + } + + public static final class EdgeInfoBuilder { + private EdgeTriplet edgeTriplet; + private long chunkSize; + private long srcChunkSize; + private long dstChunkSize; + private boolean directed; + private String prefix; + private Map<AdjListType, AdjacentList> adjacentLists; + private PropertyGroups propertyGroups; + private VersionInfo version; + + private List<AdjacentList> adjacentListsAsListTemp; + private List<PropertyGroup> propertyGroupsAsListTemp; + + private String srcType; + private String edgeType; + private String dstType; + + private EdgeInfoBuilder() {} + + public EdgeInfoBuilder srcType(String srcType) { + this.srcType = srcType; + return this; + } + + public EdgeInfoBuilder edgeType(String edgeType) { + this.edgeType = edgeType; + return this; + } + + public EdgeInfoBuilder dstType(String dstType) { + this.dstType = dstType; + return this; + } + + public EdgeInfoBuilder edgeTriplet(String srcType, String edgeType, String dstType) { + this.edgeTriplet = new EdgeTriplet(srcType, edgeType, dstType); + return this; + } + + public EdgeInfoBuilder edgeTriplet(EdgeTriplet edgeTriplet) { + this.edgeTriplet = edgeTriplet; + return this; + } + + public EdgeInfoBuilder chunkSize(long chunkSize) { + this.chunkSize = chunkSize; + return this; + } + + public EdgeInfoBuilder srcChunkSize(long srcChunkSize) { + this.srcChunkSize = srcChunkSize; + return this; + } + + public EdgeInfoBuilder dstChunkSize(long dstChunkSize) { + this.dstChunkSize = dstChunkSize; + return this; + } + + public EdgeInfoBuilder directed(boolean directed) { + this.directed = directed; + return this; + } + + public EdgeInfoBuilder prefix(String prefix) { + this.prefix = prefix; + return this; + } + + public EdgeInfoBuilder addAdjacentList(AdjacentList adjacentList) { + if (adjacentListsAsListTemp == null) { + adjacentListsAsListTemp = new ArrayList<>(); + } + adjacentListsAsListTemp.add(adjacentList); + return this; + } + + public EdgeInfoBuilder adjacentLists(List<AdjacentList> adjacentListsAsList) { + if (adjacentListsAsListTemp == null) { + adjacentListsAsListTemp = new ArrayList<>(); + } + this.adjacentListsAsListTemp.addAll(adjacentListsAsList); + return this; + } + + public EdgeInfoBuilder adjacentLists(Map<AdjListType, AdjacentList> adjacentLists) { + this.adjacentLists = adjacentLists; + return this; + } + + public EdgeInfoBuilder addPropertyGroup(PropertyGroup propertyGroup) { + if (propertyGroupsAsListTemp == null) propertyGroupsAsListTemp = new ArrayList<>(); + propertyGroupsAsListTemp.add(propertyGroup); + return this; + } + + public EdgeInfoBuilder addPropertyGroups(List<PropertyGroup> propertyGroups) { + if (propertyGroupsAsListTemp == null) propertyGroupsAsListTemp = new ArrayList<>(); + propertyGroupsAsListTemp.addAll(propertyGroups); + return this; + } + + public EdgeInfoBuilder propertyGroups(PropertyGroups propertyGroups) { + this.propertyGroups = propertyGroups; + return this; + } + + public EdgeInfoBuilder version(String version) { + this.version = VersionParser.getVersion(version); + return this; + } + + public EdgeInfoBuilder version(VersionInfo version) { + this.version = version; + return this; + } + + public EdgeInfo build() { + if (adjacentLists == null) { + adjacentLists = new HashMap<>(); + } + + if (adjacentListsAsListTemp != null) { + adjacentLists.putAll( + adjacentListsAsListTemp.stream() + .collect( + Collectors.toUnmodifiableMap( + AdjacentList::getType, Function.identity()))); + } + + if (propertyGroups == null && propertyGroupsAsListTemp != null) { + propertyGroups = new PropertyGroups(propertyGroupsAsListTemp); + } else if (propertyGroupsAsListTemp != null) { + propertyGroups = + propertyGroupsAsListTemp.stream() + .map(propertyGroups::addPropertyGroupAsNew) + .filter(Optional::isPresent) + .map(Optional::get) + .reduce((first, second) -> second) + .orElse(new PropertyGroups(new ArrayList<>())); + } + + if (edgeTriplet == null && srcType != null && edgeType != null && dstType != null) { + edgeTriplet = new EdgeTriplet(srcType, edgeType, dstType); + } + + if (edgeTriplet == null) { + throw new IllegalArgumentException("Edge triplet is null"); + } + + if (propertyGroups == null) { + throw new IllegalArgumentException("PropertyGroups is empty"); + } + + if (adjacentLists.isEmpty()) { + throw new IllegalArgumentException("AdjacentLists is empty"); + } + + return new EdgeInfo(this); + } + } + + private EdgeInfo(EdgeInfoBuilder builder) { + this.edgeTriplet = builder.edgeTriplet; + this.chunkSize = builder.chunkSize; + this.srcChunkSize = builder.srcChunkSize; + this.dstChunkSize = builder.dstChunkSize; + this.directed = builder.directed; + this.prefix = builder.prefix; + this.adjacentLists = builder.adjacentLists; + this.propertyGroups = builder.propertyGroups; + this.version = builder.version; + } + public EdgeInfo( String srcType, String edgeType, diff --git a/maven-projects/info/src/test/java/org/apache/graphar/info/EdgeInfoTest.java b/maven-projects/info/src/test/java/org/apache/graphar/info/EdgeInfoTest.java new file mode 100644 index 00000000..10e6125b --- /dev/null +++ b/maven-projects/info/src/test/java/org/apache/graphar/info/EdgeInfoTest.java @@ -0,0 +1,97 @@ +/* + * 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 org.apache.graphar.info; + +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +public class EdgeInfoTest { + + private EdgeInfo.EdgeInfoBuilder e = + EdgeInfo.builder() + .srcType("person") + .edgeType("knows") + .chunkSize(1024) + .srcChunkSize(100) + .dstChunkSize(100) + .directed(false) + .prefix("edge/person_knows_person/") + .version("gar/v1"); + + @Test + public void erroneousTripletEdgeBuilderTest() { + try { + e.adjacentLists(List.of(TestUtil.orderedBySource, TestUtil.orderedByDest)) + .addPropertyGroups(List.of(TestUtil.pg3)) + .build(); + } catch (IllegalArgumentException e) { + System.err.println(e.getMessage()); + } + } + + @Test + public void emptyAdjacentListEdgeBuilderTest() { + try { + e.dstType("person").addPropertyGroups(List.of(TestUtil.pg3)).build(); + } catch (IllegalArgumentException e) { + System.err.println(e.getMessage()); + } + } + + @Test + public void emptyPropertyGroupsEdgeBuilderTest() { + try { + e.adjacentLists(List.of(TestUtil.orderedBySource, TestUtil.orderedByDest)) + .dstType("person") + .build(); + } catch (IllegalArgumentException e) { + System.err.println(e.getMessage()); + } + } + + @Test + public void addMethodsTest() { + + EdgeInfo edgeInfo = + e.addPropertyGroup(TestUtil.pg3) + .addAdjacentList(TestUtil.orderedBySource) + .addAdjacentList(TestUtil.orderedByDest) + .dstType("person") + .build(); + + Assert.assertEquals(2, edgeInfo.getAdjacentLists().size()); + Assert.assertEquals(1, edgeInfo.getPropertyGroups().size()); + } + + @Test + public void appendMethodsTest() { + EdgeInfo edgeInfo = + e.propertyGroups(new PropertyGroups(List.of(TestUtil.pg3))) + .adjacentLists(List.of(TestUtil.orderedBySource)) + .addAdjacentList(TestUtil.orderedByDest) + .addPropertyGroups(List.of(TestUtil.pg2)) + .dstType("person") + .build(); + + Assert.assertEquals(2, edgeInfo.getAdjacentLists().size()); + Assert.assertEquals(2, edgeInfo.getPropertyGroups().size()); + } +} diff --git a/maven-projects/info/src/test/java/org/apache/graphar/info/TestUtil.java b/maven-projects/info/src/test/java/org/apache/graphar/info/TestUtil.java index 899f6a78..ab56dc1c 100644 --- a/maven-projects/info/src/test/java/org/apache/graphar/info/TestUtil.java +++ b/maven-projects/info/src/test/java/org/apache/graphar/info/TestUtil.java @@ -30,7 +30,7 @@ public class TestUtil { static final String SAVE_DIR = "/tmp/graphar/test/"; - private static String LDBC_SAMPLE_GRAPH_PATH = "/ldbc_sample/csv/ldbc_sample.graph.yml"; + private static final String LDBC_SAMPLE_GRAPH_PATH = "/ldbc_sample/csv/ldbc_sample.graph.yml"; public static String getTestData() { return GAR_TEST_DATA; @@ -40,6 +40,27 @@ public class TestUtil { return getTestData() + "/" + LDBC_SAMPLE_GRAPH_PATH; } + public static final AdjacentList orderedBySource = + new AdjacentList(AdjListType.ordered_by_source, FileType.CSV, "ordered_by_source/"); + public static final AdjacentList orderedByDest = + new AdjacentList(AdjListType.ordered_by_dest, FileType.CSV, "ordered_by_dest/"); + public static final Property creationDate = + new Property("creationDate", DataType.STRING, false, false); + public static final PropertyGroup pg3 = + new PropertyGroup(List.of(creationDate), FileType.CSV, "creationDate/"); + + public static final Property id = new Property("id", DataType.INT64, true, false); + public static final Property firstName = + new Property("firstName", DataType.STRING, false, false); + public static final Property lastName = new Property("lastName", DataType.STRING, false, false); + public static final Property gender = new Property("gender", DataType.STRING, false, true); + public static final PropertyGroup pg1 = new PropertyGroup(List.of(id), FileType.CSV, "id/"); + public static final PropertyGroup pg2 = + new PropertyGroup( + List.of(firstName, lastName, gender), FileType.CSV, "firstName_lastName"); + public static final VertexInfo person = + new VertexInfo("person", 100, List.of(pg1, pg2), "vertex/person/", "gar/v1"); + public static GraphInfo getLdbcSampleDataSet() { // create vertex info of yaml: // type: person @@ -68,17 +89,6 @@ public class TestUtil { // file_type: csv // version: gar/v1 - Property id = new Property("id", DataType.INT64, true, false); - Property firstName = new Property("firstName", DataType.STRING, false, false); - Property lastName = new Property("lastName", DataType.STRING, false, false); - Property gender = new Property("gender", DataType.STRING, false, true); - PropertyGroup pg1 = new PropertyGroup(List.of(id), FileType.CSV, "id/"); - PropertyGroup pg2 = - new PropertyGroup( - List.of(firstName, lastName, gender), FileType.CSV, "firstName_lastName"); - VertexInfo person = - new VertexInfo("person", 100, List.of(pg1, pg2), "vertex/person/", "gar/v1"); - // create edge info of yaml: // src_type: person // edge_type: knows @@ -105,25 +115,19 @@ public class TestUtil { // data_type: string // is_primary: false // version: gar/v1 - AdjacentList orderedBySource = - new AdjacentList(AdjListType.ordered_by_source, FileType.CSV, "ordered_by_source/"); - AdjacentList orderedByDest = - new AdjacentList(AdjListType.ordered_by_dest, FileType.CSV, "ordered_by_dest/"); - Property creationDate = new Property("creationDate", DataType.STRING, false, false); - PropertyGroup pg3 = new PropertyGroup(List.of(creationDate), FileType.CSV, "creationDate/"); + EdgeInfo knows = - new EdgeInfo( - "person", - "knows", - "person", - 1024, - 100, - 100, - false, - "edge/person_knows_person/", - "gar/v1", - List.of(orderedBySource, orderedByDest), - List.of(pg3)); + EdgeInfo.builder() + .edgeTriplet("person", "knows", "person") + .chunkSize(1024) + .srcChunkSize(100) + .dstChunkSize(100) + .directed(false) + .prefix("edge/person_knows_person/") + .version("gar/v1") + .adjacentLists(List.of(orderedBySource, orderedByDest)) + .addPropertyGroups(List.of(pg3)) + .build(); // create graph info of yaml: // name: ldbc_sample --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@graphar.apache.org For additional commands, e-mail: commits-h...@graphar.apache.org