Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check non-nullable keys before insert #190

Merged
merged 1 commit into from
Dec 10, 2020
Merged
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 @@ -99,6 +99,7 @@ public LoadContext context() {

public boolean load() {
try {
// Switch to loading mode
this.context.setLoadingMode();
// Clear schema if needed
this.clearAllDataIfNeeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.baidu.hugegraph.loader.builder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -42,6 +43,7 @@ public class EdgeBuilder extends ElementBuilder<Edge> {
private final EdgeLabel edgeLabel;
private final VertexLabel sourceLabel;
private final VertexLabel targetLabel;
private final Collection<String> nonNullKeys;

public EdgeBuilder(LoadContext context, InputStruct struct,
EdgeMapping mapping) {
Expand All @@ -50,6 +52,7 @@ public EdgeBuilder(LoadContext context, InputStruct struct,
this.edgeLabel = this.getEdgeLabel(this.mapping.label());
this.sourceLabel = this.getVertexLabel(this.edgeLabel.sourceLabel());
this.targetLabel = this.getVertexLabel(this.edgeLabel.targetLabel());
this.nonNullKeys = this.nonNullableKeys(this.edgeLabel);
// Ensure that the source/target id fileds are matched with id strategy
this.checkIdFields(this.sourceLabel, this.mapping.sourceFields());
this.checkIdFields(this.targetLabel, this.mapping.targetFields());
Expand Down Expand Up @@ -86,6 +89,7 @@ public List<Edge> build(Map<String, Object> keyValues) {
edge.target(target);
// Add properties
this.addProperties(edge, kvPairs.properties);
this.checkNonNullableKeys(edge);
edges.add(edge);
}
return edges;
Expand All @@ -105,6 +109,11 @@ protected SchemaLabel schemaLabel() {
return this.edgeLabel;
}

@Override
protected Collection<String> nonNullableKeys() {
return this.nonNullKeys;
}

@Override
protected boolean isIdField(String fieldName) {
return this.mapping.sourceFields().contains(fieldName) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.CoderResult;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand All @@ -33,6 +34,7 @@
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -75,8 +77,16 @@ public ElementBuilder(LoadContext context, InputStruct struct) {

protected abstract SchemaLabel schemaLabel();

protected abstract Collection<String> nonNullableKeys();

protected abstract boolean isIdField(String fieldName);

@SuppressWarnings("unchecked")
protected Collection<String> nonNullableKeys(SchemaLabel schemaLabel) {
return CollectionUtils.subtract(schemaLabel.properties(),
schemaLabel.nullableKeys());
}

protected VertexKVPairs newKVPairs(VertexLabel vertexLabel, boolean unfold) {
IdStrategy idStrategy = vertexLabel.idStrategy();
if (idStrategy.isCustomize()) {
Expand Down Expand Up @@ -143,6 +153,20 @@ protected void addProperties(GraphElement element,
}
}

protected void checkNonNullableKeys(GraphElement element) {
Set<String> keys = element.properties().keySet();
// Check whether passed all non-null property
Collection<String> requiredKeys = this.nonNullableKeys();
if (!keys.containsAll(requiredKeys)) {
@SuppressWarnings("unchecked")
Collection<String> missed = CollectionUtils.subtract(requiredKeys,
keys);
E.checkArgument(false, "All non-null property keys %s of '%s' " +
"must be setted, but missed keys %s",
requiredKeys, this.schemaLabel().name(), missed);
}
}

protected PropertyKey getPropertyKey(String name) {
return this.schema.getPropertyKey(name);
}
Expand Down Expand Up @@ -351,6 +375,7 @@ public List<Vertex> buildVertices(boolean withProperty) {
addProperty(vertex, key, this.idValue);
}
addProperties(vertex, this.properties);
checkNonNullableKeys(vertex);
}
return ImmutableList.of(vertex);
}
Expand Down Expand Up @@ -423,6 +448,7 @@ public List<Vertex> buildVertices(boolean withProperty) {
addProperty(vertex, key, idValue);
}
addProperties(vertex, this.properties);
checkNonNullableKeys(vertex);
}
vertices.add(vertex);
}
Expand Down Expand Up @@ -515,6 +541,7 @@ public List<Vertex> buildVertices(boolean withProperty) {
this.pkValues[i], false);
}
addProperties(vertex, this.properties);
checkNonNullableKeys(vertex);
} else {
vertex.id(id);
}
Expand Down Expand Up @@ -610,6 +637,7 @@ public List<Vertex> buildVertices(boolean withProperty) {
if (withProperty) {
addProperty(vertex, this.pkName, pkValue, false);
addProperties(vertex, this.properties);
checkNonNullableKeys(vertex);
} else {
vertex.id(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.loader.builder;

import java.util.Collection;
import java.util.List;
import java.util.Map;

Expand All @@ -34,12 +35,14 @@ public class VertexBuilder extends ElementBuilder<Vertex> {

private final VertexMapping mapping;
private final VertexLabel vertexLabel;
private final Collection<String> nonNullKeys;

public VertexBuilder(LoadContext context, InputStruct struct,
VertexMapping mapping) {
super(context, struct);
this.mapping = mapping;
this.vertexLabel = this.getVertexLabel(this.mapping.label());
this.nonNullKeys = this.nonNullableKeys(this.vertexLabel);
// Ensure the id field is matched with id strategy
this.checkIdField();
}
Expand All @@ -62,6 +65,11 @@ protected SchemaLabel schemaLabel() {
return this.vertexLabel;
}

@Override
protected Collection<String> nonNullableKeys() {
return this.nonNullKeys;
}

@Override
protected boolean isIdField(String fieldName) {
return fieldName.equals(this.mapping.idField());
Expand Down