-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,517 additions
and
543 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
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
109 changes: 109 additions & 0 deletions
109
src/main/java/org/neo4j/importer/v1/validation/plugin/NoDuplicatedSourceHeaderColumn.java
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,109 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed 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.neo4j.importer.v1.validation.plugin; | ||
|
||
import static java.util.function.Function.identity; | ||
import static java.util.stream.Collectors.counting; | ||
import static java.util.stream.Collectors.groupingBy; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import org.neo4j.importer.v1.sources.Source; | ||
import org.neo4j.importer.v1.sources.TextSource; | ||
import org.neo4j.importer.v1.validation.SpecificationValidationResult.Builder; | ||
import org.neo4j.importer.v1.validation.SpecificationValidator; | ||
|
||
public class NoDuplicatedSourceHeaderColumn implements SpecificationValidator { | ||
private static final String ERROR_CODE = "DUPL-002"; | ||
|
||
private final Map<String, DuplicatedHeader> sourcePathToDuplicates; | ||
|
||
public NoDuplicatedSourceHeaderColumn() { | ||
sourcePathToDuplicates = new LinkedHashMap<>(); | ||
} | ||
|
||
@Override | ||
public void visitSource(int index, Source source) { | ||
if (!(source instanceof TextSource)) { | ||
return; | ||
} | ||
TextSource textSource = (TextSource) source; | ||
List<String> header = textSource.getHeader(); | ||
if (header == null) { | ||
return; | ||
} | ||
String sourcePath = String.format("$.sources[%d].header", index); | ||
getDuplicates(header).forEach(duplicate -> sourcePathToDuplicates.put(sourcePath, duplicate)); | ||
} | ||
|
||
@Override | ||
public boolean report(Builder builder) { | ||
if (sourcePathToDuplicates.isEmpty()) { | ||
return false; | ||
} | ||
sourcePathToDuplicates.forEach((sourcePath, duplicate) -> { | ||
builder.addError( | ||
sourcePath, | ||
ERROR_CODE, | ||
String.format( | ||
"%s defines column \"%s\" %d times, it must be defined at most once", | ||
sourcePath, duplicate.getName(), duplicate.getCount())); | ||
}); | ||
return true; | ||
} | ||
|
||
private static List<DuplicatedHeader> getDuplicates(List<String> header) { | ||
return header.stream().collect(groupingBy(identity(), counting())).entrySet().stream() | ||
.filter(entry -> entry.getValue() > 1) | ||
.map(entry -> new DuplicatedHeader(entry.getKey(), entry.getValue())) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
class DuplicatedHeader { | ||
private final String name; | ||
private final long count; | ||
|
||
public DuplicatedHeader(String name, long count) { | ||
this.name = name; | ||
this.count = count; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public long getCount() { | ||
return count; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
DuplicatedHeader that = (DuplicatedHeader) object; | ||
return count == that.count && Objects.equals(name, that.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, count); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...java/org/neo4j/importer/v1/validation/plugin/NoInconsistentInlineSourceDataValidator.java
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,102 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed 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.neo4j.importer.v1.validation.plugin; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import org.neo4j.importer.v1.sources.InlineTextSource; | ||
import org.neo4j.importer.v1.sources.Source; | ||
import org.neo4j.importer.v1.validation.SpecificationValidationResult.Builder; | ||
import org.neo4j.importer.v1.validation.SpecificationValidator; | ||
|
||
public class NoInconsistentInlineSourceDataValidator implements SpecificationValidator { | ||
|
||
private static final String ERROR_CODE = "MCOL-001"; | ||
|
||
private final Map<String, CountMismatch> pathToCountMismatch; | ||
|
||
public NoInconsistentInlineSourceDataValidator() { | ||
pathToCountMismatch = new LinkedHashMap<>(); | ||
} | ||
|
||
@Override | ||
public void visitSource(int sourceIndex, Source source) { | ||
if (!(source instanceof InlineTextSource)) { | ||
return; | ||
} | ||
InlineTextSource inlineSource = (InlineTextSource) source; | ||
int columnCount = inlineSource.getHeader().size(); | ||
List<List<Object>> data = inlineSource.getData(); | ||
String path = "$.sources[%d].data[%d]"; | ||
for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) { | ||
int rowColumnCount = data.get(rowIndex).size(); | ||
if (rowColumnCount < columnCount) { | ||
String rowPath = String.format(path, sourceIndex, rowIndex); | ||
pathToCountMismatch.put(rowPath, new CountMismatch(columnCount, rowColumnCount)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean report(Builder builder) { | ||
if (pathToCountMismatch.isEmpty()) { | ||
return false; | ||
} | ||
pathToCountMismatch.forEach((path, count) -> { | ||
builder.addError( | ||
path, | ||
ERROR_CODE, | ||
String.format( | ||
"row defines %d column(s), expected at least %d", | ||
count.getActualCount(), count.getExpectedCount())); | ||
}); | ||
return true; | ||
} | ||
} | ||
|
||
class CountMismatch { | ||
private final int expectedCount; | ||
private final int actualCount; | ||
|
||
public CountMismatch(int expectedCount, int actualCount) { | ||
this.expectedCount = expectedCount; | ||
this.actualCount = actualCount; | ||
} | ||
|
||
public int getExpectedCount() { | ||
return expectedCount; | ||
} | ||
|
||
public int getActualCount() { | ||
return actualCount; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
CountMismatch that = (CountMismatch) object; | ||
return expectedCount == that.expectedCount && actualCount == that.actualCount; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(expectedCount, actualCount); | ||
} | ||
} |
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
Oops, something went wrong.