-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CsvInput.java
240 lines (220 loc) · 10.3 KB
/
CsvInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.unsafe.impl.batchimport.input.csv;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
import org.neo4j.csv.reader.CharSeeker;
import org.neo4j.kernel.impl.util.Validators;
import org.neo4j.unsafe.impl.batchimport.InputIterable;
import org.neo4j.unsafe.impl.batchimport.InputIterator;
import org.neo4j.unsafe.impl.batchimport.cache.NumberArrayFactory;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdGenerator;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper;
import org.neo4j.unsafe.impl.batchimport.input.Collector;
import org.neo4j.unsafe.impl.batchimport.input.Group;
import org.neo4j.unsafe.impl.batchimport.input.Groups;
import org.neo4j.unsafe.impl.batchimport.input.HeaderException;
import org.neo4j.unsafe.impl.batchimport.input.Input;
import org.neo4j.unsafe.impl.batchimport.input.InputNode;
import org.neo4j.unsafe.impl.batchimport.input.InputRelationship;
import org.neo4j.unsafe.impl.batchimport.input.MissingRelationshipDataException;
import org.neo4j.unsafe.impl.batchimport.input.csv.InputGroupsDeserializer.DeserializerFactory;
import static java.lang.String.format;
import static org.neo4j.csv.reader.CharSeekers.charSeeker;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DeserializerFactories.defaultNodeDeserializer;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DeserializerFactories.defaultRelationshipDeserializer;
/**
* Provides {@link Input} from data contained in tabular/csv form. Expects factories for instantiating
* the {@link CharSeeker} objects seeking values in the csv data and header factories for how to
* extract meta data about the values.
*/
public class CsvInput implements Input
{
private final Iterable<DataFactory<InputNode>> nodeDataFactory;
private final Header.Factory nodeHeaderFactory;
private final Iterable<DataFactory<InputRelationship>> relationshipDataFactory;
private final Header.Factory relationshipHeaderFactory;
private final IdType idType;
private final Configuration config;
private final Groups groups = new Groups();
private final Collector badCollector;
private final int maxProcessors;
private final boolean validateRelationshipData;
/**
* @param nodeDataFactory multiple {@link DataFactory} instances providing data, each {@link DataFactory}
* specifies an input group with its own header, extracted by the {@code nodeHeaderFactory}. From the outside
* it looks like one stream of nodes.
* @param nodeHeaderFactory factory for reading node headers.
* @param relationshipDataFactory multiple {@link DataFactory} instances providing data, each {@link DataFactory}
* specifies an input group with its own header, extracted by the {@code relationshipHeaderFactory}.
* From the outside it looks like one stream of relationships.
* @param relationshipHeaderFactory factory for reading relationship headers.
* @param idType {@link IdType} to expect in id fields of node and relationship input.
* @param config CSV configuration.
* @param badCollector Collector getting calls about bad input data.
* @param maxProcessors maximum number of processors in scenarios where multiple threads may parse CSV data.
* @param validateRelationshipData whether or not to validate relationship data strictly. If {@code true} then
* {@link MissingRelationshipDataException} will be thrown if some mandatory relationship field is missing, such as
* START_ID, END_ID or TYPE, otherwise if {@code false} such relationships will be collected by the {@code badCollector}.
*/
public CsvInput(
Iterable<DataFactory<InputNode>> nodeDataFactory, Header.Factory nodeHeaderFactory,
Iterable<DataFactory<InputRelationship>> relationshipDataFactory, Header.Factory relationshipHeaderFactory,
IdType idType, Configuration config, Collector badCollector, int maxProcessors, boolean validateRelationshipData )
{
this.maxProcessors = maxProcessors;
assertSaneConfiguration( config );
this.nodeDataFactory = nodeDataFactory;
this.nodeHeaderFactory = nodeHeaderFactory;
this.relationshipDataFactory = relationshipDataFactory;
this.relationshipHeaderFactory = relationshipHeaderFactory;
this.idType = idType;
this.config = config;
this.badCollector = badCollector;
this.validateRelationshipData = validateRelationshipData;
verifyHeaders();
}
/**
* Verifies so that all headers in input files looks sane:
* <ul>
* <li>node/relationship headers can be parsed correctly</li>
* <li>relationship headers uses ID spaces previously defined in node headers</li>
* </ul>
*/
private void verifyHeaders()
{
try
{
// parse all node headers and remember all ID spaces
for ( DataFactory<InputNode> dataFactory : nodeDataFactory )
{
try ( CharSeeker dataStream = charSeeker( dataFactory.create( config ).stream(), config, true ) )
{
Header header = nodeHeaderFactory.create( dataStream, config, idType );
Header.Entry idHeader = header.entry( Type.ID );
if ( idHeader != null )
{
// will create this group inside groups, so no need to do something with the result of it right now
groups.getOrCreate( idHeader.groupName() );
}
}
}
// parse all relationship headers and verify all ID spaces
for ( DataFactory<InputRelationship> dataFactory : relationshipDataFactory )
{
try ( CharSeeker dataStream = charSeeker( dataFactory.create( config ).stream(), config, true ) )
{
Header header = relationshipHeaderFactory.create( dataStream, config, idType );
verifyRelationshipHeader( header, Type.START_ID, dataStream.sourceDescription() );
verifyRelationshipHeader( header, Type.END_ID, dataStream.sourceDescription() );
}
}
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}
private void verifyRelationshipHeader( Header header, Type type, String source )
{
Header.Entry entry = header.entry( type );
String groupName = entry.groupName();
if ( groups.get( groupName ) == null )
{
throw new HeaderException(
format( "Relationship header %s in %s refers to ID space %s which no node header specifies",
header, source, groupName != null ? groupName : Group.GLOBAL.name() ) );
}
}
private static void assertSaneConfiguration( Configuration config )
{
Map<Character,String> delimiters = new HashMap<>();
delimiters.put( config.delimiter(), "delimiter" );
checkUniqueCharacter( delimiters, config.arrayDelimiter(), "array delimiter" );
checkUniqueCharacter( delimiters, config.quotationCharacter(), "quotation character" );
}
private static void checkUniqueCharacter( Map<Character,String> characters, char character, String characterDescription )
{
String conflict = characters.put( character, characterDescription );
if ( conflict != null )
{
throw new IllegalArgumentException( "Character '" + character + "' specified by " + characterDescription +
" is the same as specified by " + conflict );
}
}
@Override
public InputIterable<InputNode> nodes()
{
return new InputIterable<InputNode>()
{
@Override
public InputIterator<InputNode> iterator()
{
DeserializerFactory<InputNode> factory = defaultNodeDeserializer( groups, config, idType, badCollector );
return new InputGroupsDeserializer<>( nodeDataFactory.iterator(), nodeHeaderFactory, config,
idType, maxProcessors, 1, factory, Validators.emptyValidator(), InputNode.class );
}
@Override
public boolean supportsMultiplePasses()
{
return true;
}
};
}
@Override
public InputIterable<InputRelationship> relationships()
{
return new InputIterable<InputRelationship>()
{
@Override
public InputIterator<InputRelationship> iterator()
{
DeserializerFactory<InputRelationship> factory =
defaultRelationshipDeserializer( groups, config, idType, badCollector );
return new InputGroupsDeserializer<>( relationshipDataFactory.iterator(), relationshipHeaderFactory,
config, idType, maxProcessors, 1, factory,
validateRelationshipData ? new InputRelationshipValidator() : Validators.emptyValidator(),
InputRelationship.class );
}
@Override
public boolean supportsMultiplePasses()
{
return true;
}
};
}
@Override
public IdMapper idMapper( NumberArrayFactory numberArrayFactory )
{
return idType.idMapper( numberArrayFactory );
}
@Override
public IdGenerator idGenerator()
{
return idType.idGenerator();
}
@Override
public Collector badCollector()
{
return badCollector;
}
}