Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.graphar.info.type.AdjListType;
import org.apache.graphar.info.type.Cardinality;
import org.apache.graphar.info.type.DataType;
import org.apache.graphar.info.yaml.EdgeYaml;
import org.apache.graphar.info.yaml.GraphYaml;
Expand Down Expand Up @@ -496,12 +497,14 @@ public URI getEdgesNumFileUri(AdjListType adjListType, long vertexChunkIndex) {
}

public void dump(Writer output) {
isValidated();
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
EdgeYaml edgeYaml = new EdgeYaml(this);
yaml.dump(edgeYaml, output);
}

public String dump() {
isValidated();
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
EdgeYaml edgeYaml = new EdgeYaml(this);
return yaml.dump(edgeYaml);
Expand Down Expand Up @@ -626,6 +629,10 @@ public boolean isValidated() {
}

for (Property p : pg.getPropertyList()) {
if (p.getCardinality() != Cardinality.SINGLE) {
// edge property only supports single cardinality
return false;
}
if (propertyNameSet.contains(p.getName())) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,21 @@ private GraphInfo(
}

public void dump(URI storeUri, Writer output) {
isValidated();
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
GraphYaml graphYaml = new GraphYaml(storeUri, this);
yaml.dump(graphYaml, output);
}

public String dump(URI storeUri) {
isValidated();
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
GraphYaml graphYaml = new GraphYaml(storeUri, this);
return yaml.dump(graphYaml);
}

public String dump() {
isValidated();
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
GraphYaml graphYaml = new GraphYaml(this);
return yaml.dump(graphYaml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,44 @@

package org.apache.graphar.info;

import org.apache.graphar.info.type.Cardinality;
import org.apache.graphar.info.type.DataType;
import org.apache.graphar.info.yaml.PropertyYaml;

public class Property {
private final String name;
private final DataType dataType;
private final Cardinality cardinality;
private final boolean primary;
private final boolean nullable;

public Property(String name, DataType dataType, boolean primary, boolean nullable) {
this(name, dataType, Cardinality.SINGLE, primary, nullable);
}

public Property(
String name,
DataType dataType,
Cardinality cardinality,
boolean primary,
boolean nullable) {
this.name = name;
this.dataType = dataType;
this.cardinality = cardinality;
this.primary = primary;
this.nullable = nullable;
}

Property(PropertyYaml yamlParser) {
public Property(PropertyYaml yamlParser) {
this.name = yamlParser.getName();
this.dataType = DataType.fromString(yamlParser.getData_type());
this.primary = yamlParser.getIs_primary();
this.nullable = yamlParser.getIs_nullable();
Cardinality cardinality = Cardinality.SINGLE;
if (yamlParser.getCardinality() != null && !yamlParser.getCardinality().isEmpty()) {
cardinality = Cardinality.fromString(yamlParser.getCardinality());
}
this.cardinality = cardinality;
}

public String getName() {
Expand All @@ -57,4 +74,8 @@ public boolean isPrimary() {
public boolean isNullable() {
return nullable;
}

public Cardinality getCardinality() {
return cardinality;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.graphar.info.type.Cardinality;
import org.apache.graphar.info.type.DataType;
import org.apache.graphar.info.type.FileType;
import org.apache.graphar.util.GeneralParams;
Expand Down Expand Up @@ -137,6 +138,9 @@ public boolean isValidated() {
if (property.getDataType() == DataType.LIST && fileType == FileType.CSV) {
return false;
}
if (property.getCardinality() != Cardinality.SINGLE && fileType == FileType.CSV) {
return false;
}
}

return true;
Expand Down Expand Up @@ -216,6 +220,11 @@ int getPropertyGroupNum() {
return propertyGroupList.size();
}

public Cardinality getCardinality(String propertyName) {
checkPropertyExist(propertyName);
return properties.get(propertyName).getCardinality();
}

DataType getPropertyType(String propertyName) {
checkPropertyExist(propertyName);
return properties.get(propertyName).getDataType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.apache.graphar.info.type.Cardinality;
import org.apache.graphar.info.type.DataType;
import org.apache.graphar.info.yaml.GraphYaml;
import org.apache.graphar.info.yaml.VertexYaml;
Expand Down Expand Up @@ -85,6 +86,10 @@ public int getPropertyGroupNum() {
return propertyGroups.getPropertyGroupNum();
}

public Cardinality getCardinality(String propertyName) {
return propertyGroups.getCardinality(propertyName);
}

public DataType getPropertyType(String propertyName) {
return propertyGroups.getPropertyType(propertyName);
}
Expand Down Expand Up @@ -124,12 +129,14 @@ public URI getVerticesNumFileUri() {
}

public void dump(Writer output) {
isValidated();
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
VertexYaml vertexYaml = new VertexYaml(this);
yaml.dump(vertexYaml, output);
}

public String dump() {
isValidated();
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
VertexYaml vertexYaml = new VertexYaml(this);
return yaml.dump(vertexYaml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public abstract class BaseGraphInfoSaver implements GraphInfoSaver {

@Override
public void save(URI graphInfoUri, GraphInfo graphInfo) throws IOException {
// if graphInfoUri is a directory then save to ${graphInfo.name}.graph.yml
if (graphInfoUri.getPath().endsWith("/")) {
graphInfoUri =
URI.create(graphInfoUri.toString() + graphInfo.getName() + ".graph.yaml");
}
Writer writer = writeYaml(graphInfoUri);
graphInfo.dump(graphInfoUri, writer);
writer.close();
Expand All @@ -48,13 +53,22 @@ public void save(URI graphInfoUri, GraphInfo graphInfo) throws IOException {

@Override
public void save(URI vertexInfoUri, VertexInfo vertexInfo) throws IOException {
// if vertexInfoUri is a directory then save to ${vertexInfo.type}.vertex.yml
if (vertexInfoUri.getPath().endsWith("/")) {
vertexInfoUri =
URI.create(vertexInfoUri.toString() + vertexInfo.getType() + ".vertex.yaml");
}
Writer vertexWriter = writeYaml(vertexInfoUri);
vertexInfo.dump(vertexWriter);
vertexWriter.close();
}

@Override
public void save(URI edgeInfoUri, EdgeInfo edgeInfo) throws IOException {
// if edgeInfoUri is a directory then save to ${edgeInfo.getConcat()}.edge.yml
if (edgeInfoUri.getPath().endsWith("/")) {
edgeInfoUri = URI.create(edgeInfoUri.toString() + edgeInfo.getConcat() + ".edge.yaml");
}
Writer edgeWriter = writeYaml(edgeInfoUri);
edgeInfo.dump(edgeWriter);
edgeWriter.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.type;

/** Defines how multiple values are handled for a given property key. */
public enum Cardinality {
/** Single value property */
SINGLE,
/** List of values property */
LIST,
/** Set of values property (no duplicates) */
SET;

public String toString() {
switch (this) {
case SINGLE:
return "single";
case LIST:
return "list";
case SET:
return "set";
default:
throw new IllegalArgumentException("Unknown cardinality: " + this);
}
}

public static Cardinality fromString(String cardinality) {
if (cardinality == null) {
return null;
}
switch (cardinality) {
case "single":
return SINGLE;
case "list":
return LIST;
case "set":
return SET;
default:
throw new IllegalArgumentException("Unknown cardinality: " + cardinality);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,33 @@

import java.util.Optional;
import org.apache.graphar.info.Property;
import org.apache.graphar.info.type.DataType;
import org.apache.graphar.info.type.Cardinality;

public class PropertyYaml {
private String name;
private String data_type;
private boolean is_primary;
private Optional<Boolean> is_nullable;
private String cardinality;

public PropertyYaml() {
this.name = "";
this.data_type = "";
this.is_primary = false;
this.is_nullable = Optional.empty();
this.cardinality = "single"; // Default to single
}

public PropertyYaml(Property property) {
this.name = property.getName();
this.data_type = property.getDataType().toString();
this.is_primary = property.isPrimary();
this.is_nullable = Optional.of(property.isNullable());
this.cardinality = property.getCardinality().toString();
}

Property toProperty() {
return new Property(
name,
DataType.fromString(data_type),
is_primary,
is_nullable.orElseGet(() -> !is_primary));
return new Property(this);
}

public String getName() {
Expand Down Expand Up @@ -82,4 +81,15 @@ public boolean getIs_nullable() {
public void setIs_nullable(boolean is_nullable) {
this.is_nullable = Optional.of(is_nullable);
}

public String getCardinality() {
if (cardinality == null) {
return null;
}
return Cardinality.SINGLE.toString().equals(cardinality) ? null : cardinality;
}

public void setCardinality(String cardinality) {
this.cardinality = cardinality;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public class VertexYaml {
private String type;
private long chunk_size;
private List<PropertyGroupYaml> property_groups;
private List<String> labels;
private String prefix;
private String version;

public VertexYaml() {
this.type = "";
this.chunk_size = 0;
this.property_groups = new ArrayList<>();
this.labels = null;
this.prefix = "";
this.version = "";
}
Expand Down Expand Up @@ -80,6 +82,17 @@ public void setProperty_groups(List<PropertyGroupYaml> property_groups) {
this.property_groups = property_groups;
}

public List<String> getLabels() {
if (labels == null) {
return null;
}
return labels.isEmpty() ? null : labels;
}

public void setLabels(List<String> labels) {
this.labels = labels;
}

public String getPrefix() {
return prefix;
}
Expand Down
Loading