Skip to content

Commit b7768ad

Browse files
authored
feat(java,info): update validate and other methods (#769)
* add isValidated method * add isValidated test cases * rename prefix or path to uri * update * add and fix methods * update * update * update
1 parent ea9a324 commit b7768ad

File tree

12 files changed

+789
-119
lines changed

12 files changed

+789
-119
lines changed

maven-projects/info/src/main/java/org/apache/graphar/info/AdjacentList.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,23 @@ public String getPrefix() {
5353
public URI getBaseUri() {
5454
return baseUri;
5555
}
56+
57+
public boolean isValidated() {
58+
// Check if type is valid
59+
if (type == null) {
60+
return false;
61+
}
62+
63+
// Check if file type is valid
64+
if (fileType == null) {
65+
return false;
66+
}
67+
68+
// Check if base URI is not null
69+
if (baseUri == null || baseUri.toString().isEmpty()) {
70+
return false;
71+
}
72+
73+
return true;
74+
}
5675
}

maven-projects/info/src/main/java/org/apache/graphar/info/EdgeInfo.java

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.net.URI;
2323
import java.util.ArrayList;
2424
import java.util.HashMap;
25+
import java.util.HashSet;
2526
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Optional;
29+
import java.util.Set;
2830
import java.util.function.Function;
2931
import java.util.stream.Collectors;
3032
import java.util.stream.Stream;
@@ -426,6 +428,18 @@ public boolean hasProperty(String propertyName) {
426428
return propertyGroups.hasProperty(propertyName);
427429
}
428430

431+
public DataType getPropertyType(String propertyName) {
432+
return propertyGroups.getPropertyType(propertyName);
433+
}
434+
435+
public boolean isPrimaryKey(String propertyName) {
436+
return propertyGroups.isPrimaryKey(propertyName);
437+
}
438+
439+
public boolean isNullableKey(String propertyName) {
440+
return propertyGroups.isNullableKey(propertyName);
441+
}
442+
429443
public boolean hasPropertyGroup(PropertyGroup propertyGroup) {
430444
return propertyGroups.hasPropertyGroup(propertyGroup);
431445
}
@@ -446,50 +460,38 @@ public PropertyGroup getPropertyGroup(String property) {
446460
return propertyGroups.getPropertyGroup(property);
447461
}
448462

449-
public URI getPropertyGroupPrefix(PropertyGroup propertyGroup) {
463+
public URI getPropertyGroupUri(PropertyGroup propertyGroup) {
450464
checkPropertyGroupExist(propertyGroup);
451465
return getBaseUri().resolve(propertyGroup.getBaseUri());
452466
}
453467

454-
public URI getPropertyGroupChunkPath(PropertyGroup propertyGroup, long chunkIndex) {
468+
public URI getPropertyGroupChunkUri(PropertyGroup propertyGroup, long chunkIndex) {
455469
// PropertyGroup will be checked in getPropertyGroupPrefix
456-
return getPropertyGroupPrefix(propertyGroup).resolve("chunk" + chunkIndex);
470+
return getPropertyGroupUri(propertyGroup).resolve("chunk" + chunkIndex);
457471
}
458472

459-
public URI getAdjacentListPrefix(AdjListType adjListType) {
473+
public URI getAdjacentListUri(AdjListType adjListType) {
460474
return getBaseUri().resolve(getAdjacentList(adjListType).getBaseUri()).resolve("adj_list/");
461475
}
462476

463-
public URI getAdjacentListChunkPath(AdjListType adjListType, long vertexChunkIndex) {
464-
return getAdjacentListPrefix(adjListType).resolve("chunk" + vertexChunkIndex);
465-
}
466-
467-
public URI getOffsetPrefix(AdjListType adjListType) {
468-
return getAdjacentListPrefix(adjListType).resolve("offset/");
469-
}
470-
471-
public URI getOffsetChunkPath(AdjListType adjListType, long vertexChunkIndex) {
472-
return getOffsetPrefix(adjListType).resolve("chunk" + vertexChunkIndex);
477+
public URI getAdjacentListChunkUri(AdjListType adjListType, long vertexChunkIndex) {
478+
return getAdjacentListUri(adjListType).resolve("chunk" + vertexChunkIndex);
473479
}
474480

475-
public URI getVerticesNumFilePath(AdjListType adjListType) {
476-
return getAdjacentListPrefix(adjListType).resolve("vertex_count");
481+
public URI getOffsetUri(AdjListType adjListType) {
482+
return getAdjacentListUri(adjListType).resolve("offset/");
477483
}
478484

479-
public URI getEdgesNumFilePath(AdjListType adjListType, long vertexChunkIndex) {
480-
return getAdjacentListPrefix(adjListType).resolve("edge_count" + vertexChunkIndex);
485+
public URI getOffsetChunkUri(AdjListType adjListType, long vertexChunkIndex) {
486+
return getOffsetUri(adjListType).resolve("chunk" + vertexChunkIndex);
481487
}
482488

483-
public DataType getPropertyType(String propertyName) {
484-
return propertyGroups.getPropertyType(propertyName);
489+
public URI getVerticesNumFileUri(AdjListType adjListType) {
490+
return getAdjacentListUri(adjListType).resolve("vertex_count");
485491
}
486492

487-
public boolean isPrimaryKey(String propertyName) {
488-
return propertyGroups.isPrimaryKey(propertyName);
489-
}
490-
491-
public boolean isNullableKey(String propertyName) {
492-
return propertyGroups.isNullableKey(propertyName);
493+
public URI getEdgesNumFileUri(AdjListType adjListType, long vertexChunkIndex) {
494+
return getAdjacentListUri(adjListType).resolve("edge_count" + vertexChunkIndex);
493495
}
494496

495497
public String dump() {
@@ -576,6 +578,56 @@ private void checkPropertyGroupExist(PropertyGroup propertyGroup) {
576578
}
577579
}
578580

581+
public boolean isValidated() {
582+
// Check if source type, edge type, or destination type is empty
583+
if (getSrcType() == null
584+
|| getSrcType().isEmpty()
585+
|| getEdgeType() == null
586+
|| getEdgeType().isEmpty()
587+
|| getDstType() == null
588+
|| getDstType().isEmpty()) {
589+
return false;
590+
}
591+
592+
// Check if chunk sizes are positive
593+
if (chunkSize <= 0 || srcChunkSize <= 0 || dstChunkSize <= 0) {
594+
return false;
595+
}
596+
597+
// Check if prefix is null or empty
598+
if (baseUri == null || baseUri.toString().isEmpty()) {
599+
return false;
600+
}
601+
602+
// Check if adjacent lists are empty
603+
if (adjacentLists.isEmpty()) {
604+
return false;
605+
}
606+
607+
// Check if all adjacent lists are valid
608+
for (AdjacentList adjacentList : adjacentLists.values()) {
609+
if (adjacentList == null || !adjacentList.isValidated()) {
610+
return false;
611+
}
612+
}
613+
614+
// Check if all property groups are valid and property names are unique
615+
Set<String> propertyNameSet = new HashSet<>();
616+
for (PropertyGroup pg : propertyGroups.getPropertyGroupList()) {
617+
if (pg == null || !pg.isValidated()) {
618+
return false;
619+
}
620+
621+
for (Property p : pg.getPropertyList()) {
622+
if (propertyNameSet.contains(p.getName())) {
623+
return false;
624+
}
625+
propertyNameSet.add(p.getName());
626+
}
627+
}
628+
return true;
629+
}
630+
579631
private static class EdgeTriplet {
580632
private final String srcType;
581633
private final String edgeType;

maven-projects/info/src/main/java/org/apache/graphar/info/GraphInfo.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public boolean hasVertexInfo(String type) {
167167
}
168168

169169
public boolean hasEdgeInfo(String srcType, String edgeType, String dstType) {
170-
return edgeConcat2EdgeInfo.containsKey(EdgeInfo.concat(srcType, dstType, edgeType));
170+
return edgeConcat2EdgeInfo.containsKey(EdgeInfo.concat(srcType, edgeType, dstType));
171171
}
172172

173173
public VertexInfo getVertexInfo(String type) {
@@ -177,7 +177,7 @@ public VertexInfo getVertexInfo(String type) {
177177

178178
public EdgeInfo getEdgeInfo(String srcType, String edgeType, String dstType) {
179179
checkEdgeExist(srcType, edgeType, dstType);
180-
return edgeConcat2EdgeInfo.get(EdgeInfo.concat(srcType, dstType, edgeType));
180+
return edgeConcat2EdgeInfo.get(EdgeInfo.concat(srcType, edgeType, dstType));
181181
}
182182

183183
public int getVertexInfoNum() {
@@ -212,6 +212,35 @@ public VersionInfo getVersion() {
212212
return version;
213213
}
214214

215+
public boolean isValidated() {
216+
// Check if name is not empty and base URI is not null
217+
if (name == null || name.isEmpty() || baseUri == null) {
218+
return false;
219+
}
220+
221+
// Check if all vertex infos are valid
222+
for (VertexInfo vertexInfo : vertexInfos) {
223+
if (vertexInfo == null || !vertexInfo.isValidated()) {
224+
return false;
225+
}
226+
}
227+
228+
// Check if all edge infos are valid
229+
for (EdgeInfo edgeInfo : edgeInfos) {
230+
if (edgeInfo == null || !edgeInfo.isValidated()) {
231+
return false;
232+
}
233+
}
234+
235+
// Check if vertex/edge infos size matches vertex/edge type to index map size
236+
if (vertexInfos.size() != vertexType2VertexInfo.size()
237+
|| edgeInfos.size() != edgeConcat2EdgeInfo.size()) {
238+
return false;
239+
}
240+
241+
return true;
242+
}
243+
215244
private void checkVertexExist(String type) {
216245
if (!hasVertexInfo(type)) {
217246
throw new IllegalArgumentException(

maven-projects/info/src/main/java/org/apache/graphar/info/PropertyGroup.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,49 @@ public String getPrefix() {
9898
public URI getBaseUri() {
9999
return baseUri;
100100
}
101+
102+
public boolean isValidated() {
103+
// Check if base URI is not null or empty
104+
if (baseUri == null || baseUri.toString().isEmpty()) {
105+
return false;
106+
}
107+
108+
// Check if file type is valid
109+
if (fileType == null) {
110+
return false;
111+
}
112+
113+
// Check if properties are not empty
114+
if (propertyList.isEmpty()) {
115+
return false;
116+
}
117+
118+
// Check if all properties are valid and have unique names
119+
Map<String, Boolean> propertyNameSet = new HashMap<>();
120+
for (Property property : propertyList) {
121+
// Check if property name is not empty and data type is not null
122+
if (property == null
123+
|| property.getName() == null
124+
|| property.getName().isEmpty()
125+
|| property.getDataType() == null) {
126+
return false;
127+
}
128+
129+
// Check if property name is unique in the group
130+
String propertyName = property.getName();
131+
if (propertyNameSet.containsKey(propertyName)) {
132+
return false;
133+
}
134+
propertyNameSet.put(propertyName, true);
135+
136+
// TODO: support list type in csv file
137+
if (property.getDataType() == DataType.LIST && fileType == FileType.CSV) {
138+
return false;
139+
}
140+
}
141+
142+
return true;
143+
}
101144
}
102145

103146
class PropertyGroups {
@@ -192,26 +235,17 @@ List<PropertyGroup> getPropertyGroupList() {
192235
return propertyGroupList;
193236
}
194237

195-
Map<String, PropertyGroup> getPropertyGroupMap() {
196-
return propertyGroupMap;
197-
}
198-
199238
PropertyGroup getPropertyGroup(String propertyName) {
200239
checkPropertyExist(propertyName);
201240
return propertyGroupMap.get(propertyName);
202241
}
203242

204-
Map<String, Property> getProperties() {
205-
return properties;
206-
}
207-
208243
private void checkPropertyExist(String propertyName) {
209244
if (null == propertyName) {
210245
throw new IllegalArgumentException("Property name is null");
211246
}
212247
if (!hasProperty(propertyName)) {
213-
throw new IllegalArgumentException(
214-
"Property " + propertyName + " does not exist in the property group " + this);
248+
throw new IllegalArgumentException("Property " + propertyName + " does not exist");
215249
}
216250
}
217251
}

maven-projects/info/src/main/java/org/apache/graphar/info/VertexInfo.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
package org.apache.graphar.info;
2121

2222
import java.net.URI;
23+
import java.util.HashSet;
2324
import java.util.List;
2425
import java.util.Optional;
26+
import java.util.Set;
2527
import org.apache.graphar.info.type.DataType;
2628
import org.apache.graphar.info.yaml.GraphYaml;
2729
import org.apache.graphar.info.yaml.VertexYaml;
@@ -78,7 +80,7 @@ public Optional<VertexInfo> addPropertyGroupAsNew(PropertyGroup propertyGroup) {
7880
type, chunkSize, newPropertyGroups, baseUri, version));
7981
}
8082

81-
public int propertyGroupNum() {
83+
public int getPropertyGroupNum() {
8284
return propertyGroups.getPropertyGroupNum();
8385
}
8486

@@ -102,17 +104,21 @@ public boolean hasPropertyGroup(PropertyGroup propertyGroup) {
102104
return propertyGroups.hasPropertyGroup(propertyGroup);
103105
}
104106

105-
public URI getPropertyGroupPrefix(PropertyGroup propertyGroup) {
107+
public PropertyGroup getPropertyGroup(String property) {
108+
return propertyGroups.getPropertyGroup(property);
109+
}
110+
111+
public URI getPropertyGroupUri(PropertyGroup propertyGroup) {
106112
checkPropertyGroupExist(propertyGroup);
107113
return getBaseUri().resolve(propertyGroup.getBaseUri());
108114
}
109115

110-
public URI getPropertyGroupChunkPath(PropertyGroup propertyGroup, long chunkIndex) {
116+
public URI getPropertyGroupChunkUri(PropertyGroup propertyGroup, long chunkIndex) {
111117
// PropertyGroup will be checked in getPropertyGroupPrefix
112-
return getPropertyGroupPrefix(propertyGroup).resolve("chunk" + chunkIndex);
118+
return getPropertyGroupUri(propertyGroup).resolve("chunk" + chunkIndex);
113119
}
114120

115-
public URI getVerticesNumFilePath() {
121+
public URI getVerticesNumFileUri() {
116122
return getBaseUri().resolve("vertex_count");
117123
}
118124

@@ -135,7 +141,7 @@ public List<PropertyGroup> getPropertyGroups() {
135141
}
136142

137143
public String getPrefix() {
138-
return baseUri.toString();
144+
return baseUri == null ? null : baseUri.toString();
139145
}
140146

141147
public URI getBaseUri() {
@@ -158,4 +164,27 @@ private void checkPropertyGroupExist(PropertyGroup propertyGroup) {
158164
+ getType());
159165
}
160166
}
167+
168+
public boolean isValidated() {
169+
// Check if type and baseUri is not empty and chunkSize is positive
170+
if (type == null || type.isEmpty() || chunkSize <= 0 || baseUri == null) {
171+
return false;
172+
}
173+
// Check if property groups are valid
174+
Set<String> propertyNameSet = new HashSet<>();
175+
for (PropertyGroup pg : propertyGroups.getPropertyGroupList()) {
176+
// Check if property group is not null and not empty
177+
if (pg == null || !pg.isValidated()) {
178+
return false;
179+
}
180+
for (Property p : pg.getPropertyList()) {
181+
if (propertyNameSet.contains(p.getName())) {
182+
return false;
183+
}
184+
propertyNameSet.add(p.getName());
185+
}
186+
}
187+
188+
return true;
189+
}
161190
}

0 commit comments

Comments
 (0)