-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CsvInputBatchImportIT.java
506 lines (465 loc) · 20.9 KB
/
CsvInputBatchImportIT.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* 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 org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.api.ReadOperations;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.TokenStore;
import org.neo4j.kernel.impl.util.AutoCreatingHashMap;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.storageengine.api.Token;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import org.neo4j.unsafe.impl.batchimport.BatchImporter;
import org.neo4j.unsafe.impl.batchimport.Configuration;
import org.neo4j.unsafe.impl.batchimport.ParallelBatchImporter;
import org.neo4j.unsafe.impl.batchimport.input.Collector;
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 static java.lang.Runtime.getRuntime;
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.nio.charset.Charset.defaultCharset;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.kernel.impl.util.AutoCreatingHashMap.nested;
import static org.neo4j.kernel.impl.util.AutoCreatingHashMap.values;
import static org.neo4j.register.Registers.newDoubleLongRegister;
import static org.neo4j.unsafe.impl.batchimport.input.Collectors.silentBadCollector;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntity.NO_PROPERTIES;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.NO_NODE_DECORATOR;
import static org.neo4j.unsafe.impl.batchimport.input.InputEntityDecorators.NO_RELATIONSHIP_DECORATOR;
import static org.neo4j.unsafe.impl.batchimport.input.csv.Configuration.COMMAS;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.data;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.defaultFormatNodeFileHeader;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.defaultFormatRelationshipFileHeader;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.nodeData;
import static org.neo4j.unsafe.impl.batchimport.input.csv.DataFactories.relationshipData;
import static org.neo4j.unsafe.impl.batchimport.staging.ExecutionMonitors.invisible;
public class CsvInputBatchImportIT
{
/** Don't support these counts at the moment so don't compute them */
private static final boolean COMPUTE_DOUBLE_SIDED_RELATIONSHIP_COUNTS = false;
private String nameOf( InputNode node )
{
return (String) node.properties()[1];
}
@Rule
public final TestDirectory directory = TestDirectory.testDirectory();
@Rule
public final DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();
private final long seed = currentTimeMillis();
private final Random random = new Random( seed );
@Test
public void shouldImportDataComingFromCsvFiles() throws Exception
{
// GIVEN
BatchImporter importer = new ParallelBatchImporter( directory.graphDbDir(), fileSystemRule.get(),
smallBatchSizeConfig(), NullLogService.getInstance(), invisible(), Config.empty() );
List<InputNode> nodeData = randomNodeData();
List<InputRelationship> relationshipData = randomRelationshipData( nodeData );
// WHEN
boolean success = false;
try
{
importer.doImport( csv( nodeDataAsFile( nodeData ), relationshipDataAsFile( relationshipData ),
IdType.STRING, lowBufferSize( COMMAS ), silentBadCollector( 0 ),
getRuntime().availableProcessors() ) );
// THEN
verifyImportedData( nodeData, relationshipData );
success = true;
}
finally
{
if ( !success )
{
System.err.println( "Seed " + seed );
}
}
}
public static Input csv( File nodes, File relationships, IdType idType,
org.neo4j.unsafe.impl.batchimport.input.csv.Configuration configuration, Collector badCollector, int maxProcessors )
{
return new CsvInput(
nodeData( data( NO_NODE_DECORATOR, defaultCharset(), nodes ) ), defaultFormatNodeFileHeader(),
relationshipData( data( NO_RELATIONSHIP_DECORATOR, defaultCharset(), relationships ) ),
defaultFormatRelationshipFileHeader(), idType, configuration,
badCollector, maxProcessors, true );
}
private static org.neo4j.unsafe.impl.batchimport.input.csv.Configuration lowBufferSize(
org.neo4j.unsafe.impl.batchimport.input.csv.Configuration actual )
{
return new org.neo4j.unsafe.impl.batchimport.input.csv.Configuration.Overridden( actual )
{
@Override
public int bufferSize()
{
return 10_000;
}
};
}
// ======================================================
// Below is code for generating import data
// ======================================================
private List<InputNode> randomNodeData()
{
List<InputNode> nodes = new ArrayList<>();
for ( int i = 0; i < 300; i++ )
{
Object[] properties = new Object[] { "name", "Node " + i };
String id = UUID.randomUUID().toString();
nodes.add( new InputNode( "source", i, i, id, properties, null,
randomLabels( random ), null ) );
}
return nodes;
}
private String[] randomLabels( Random random )
{
String[] labels = new String[random.nextInt( 3 )];
for ( int i = 0; i < labels.length; i++ )
{
labels[i] = "Label" + random.nextInt( 4 );
}
return labels;
}
private Configuration smallBatchSizeConfig()
{
return new Configuration()
{
@Override
public int batchSize()
{
return 100;
}
@Override
public int denseNodeThreshold()
{
return 5;
}
};
}
private File relationshipDataAsFile( List<InputRelationship> relationshipData ) throws IOException
{
File file = directory.file( "relationships.csv" );
try ( Writer writer = fileSystemRule.get().openAsWriter( file, StandardCharsets.UTF_8, false ) )
{
// Header
println( writer, ":start_id,:end_id,:type" );
// Data
for ( InputRelationship relationship : relationshipData )
{
println( writer, relationship.startNode() + "," + relationship.endNode() + "," + relationship.type() );
}
}
return file;
}
private File nodeDataAsFile( List<InputNode> nodeData ) throws IOException
{
File file = directory.file( "nodes.csv" );
try ( Writer writer = fileSystemRule.get().openAsWriter( file, StandardCharsets.UTF_8, false ) )
{
// Header
println( writer, "id:ID,name,some-labels:LABEL" );
// Data
for ( InputNode node : nodeData )
{
String csvLabels = csvLabels( node.labels() );
println( writer, node.id() + "," + node.properties()[1] +
(csvLabels != null && csvLabels.length() > 0 ? "," + csvLabels : "") );
}
}
return file;
}
private String csvLabels( String[] labels )
{
if ( labels == null || labels.length == 0 )
{
return null;
}
StringBuilder builder = new StringBuilder();
for ( String label : labels )
{
builder.append( builder.length() > 0 ? ";" : "" ).append( label );
}
return builder.toString();
}
private void println( Writer writer, String string ) throws IOException
{
writer.write( string + "\n" );
}
private List<InputRelationship> randomRelationshipData( List<InputNode> nodeData )
{
List<InputRelationship> relationships = new ArrayList<>();
for ( int i = 0; i < 1000; i++ )
{
relationships.add( new InputRelationship(
"source", i, i,
NO_PROPERTIES, null,
nodeData.get( random.nextInt( nodeData.size() ) ).id(),
nodeData.get( random.nextInt( nodeData.size() ) ).id(),
"TYPE_" + random.nextInt( 3 ), null ) );
}
return relationships;
}
// ======================================================
// Below is code for verifying the imported data
// ======================================================
private void verifyImportedData( List<InputNode> nodeData, List<InputRelationship> relationshipData )
{
// Build up expected data for the verification below
Map<String/*id*/, InputNode> expectedNodes = new HashMap<>();
Map<String,String[]> expectedNodeNames = new HashMap<>();
Map<String/*start node name*/, Map<String/*end node name*/, Map<String, AtomicInteger>>> expectedRelationships =
new AutoCreatingHashMap<>( nested( String.class, nested( String.class, values( AtomicInteger.class ) ) ) );
Map<String, AtomicLong> expectedNodeCounts = new AutoCreatingHashMap<>( values( AtomicLong.class ) );
Map<String, Map<String, Map<String, AtomicLong>>> expectedRelationshipCounts =
new AutoCreatingHashMap<>( nested( String.class, nested( String.class, values( AtomicLong.class ) ) ) );
buildUpExpectedData( nodeData, relationshipData, expectedNodes, expectedNodeNames, expectedRelationships,
expectedNodeCounts, expectedRelationshipCounts );
// Do the verification
GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase( directory.graphDbDir() );
try ( Transaction tx = db.beginTx() )
{
// Verify nodes
for ( Node node : db.getAllNodes() )
{
String name = (String) node.getProperty( "name" );
String[] labels = expectedNodeNames.remove( name );
assertEquals( asSet( labels ), names( node.getLabels() ) );
}
assertEquals( 0, expectedNodeNames.size() );
// Verify relationships
for ( Relationship relationship : db.getAllRelationships() )
{
String startNodeName = (String) relationship.getStartNode().getProperty( "name" );
Map<String, Map<String, AtomicInteger>> inner = expectedRelationships.get( startNodeName );
String endNodeName = (String) relationship.getEndNode().getProperty( "name" );
Map<String, AtomicInteger> innerInner = inner.get( endNodeName );
String type = relationship.getType().name();
int countAfterwards = innerInner.get( type ).decrementAndGet();
assertThat( countAfterwards, greaterThanOrEqualTo( 0 ) );
if ( countAfterwards == 0 )
{
innerInner.remove( type );
if ( innerInner.isEmpty() )
{
inner.remove( endNodeName );
if ( inner.isEmpty() )
{
expectedRelationships.remove( startNodeName );
}
}
}
}
assertEquals( 0, expectedRelationships.size() );
// Verify counts, TODO how to get counts store other than this way?
NeoStores neoStores = ((GraphDatabaseAPI)db).getDependencyResolver().resolveDependency(
RecordStorageEngine.class ).testAccessNeoStores();
Function<String, Integer> labelTranslationTable =
translationTable( neoStores.getLabelTokenStore(), ReadOperations.ANY_LABEL );
for ( Pair<Integer,Long> count : allNodeCounts( labelTranslationTable, expectedNodeCounts ) )
{
assertEquals( "Label count mismatch for label " + count.first(),
count.other().longValue(),
neoStores.getCounts()
.nodeCount( count.first().intValue(), newDoubleLongRegister() )
.readSecond() );
}
Function<String, Integer> relationshipTypeTranslationTable =
translationTable( neoStores.getRelationshipTypeTokenStore(), ReadOperations.ANY_RELATIONSHIP_TYPE );
for ( Pair<RelationshipCountKey,Long> count : allRelationshipCounts( labelTranslationTable,
relationshipTypeTranslationTable, expectedRelationshipCounts ) )
{
RelationshipCountKey key = count.first();
assertEquals( "Label count mismatch for label " + key,
count.other().longValue(),
neoStores.getCounts()
.relationshipCount( key.startLabel, key.type, key.endLabel, newDoubleLongRegister() )
.readSecond() );
}
tx.success();
}
finally
{
db.shutdown();
}
}
private static class RelationshipCountKey
{
private final int startLabel;
private final int type;
private final int endLabel;
RelationshipCountKey( int startLabel, int type, int endLabel )
{
this.startLabel = startLabel;
this.type = type;
this.endLabel = endLabel;
}
@Override
public String toString()
{
return format( "[start:%d, type:%d, end:%d]", startLabel, type, endLabel );
}
}
private Iterable<Pair<RelationshipCountKey,Long>> allRelationshipCounts(
Function<String, Integer> labelTranslationTable,
Function<String, Integer> relationshipTypeTranslationTable,
Map<String, Map<String, Map<String, AtomicLong>>> counts )
{
Collection<Pair<RelationshipCountKey,Long>> result = new ArrayList<>();
for ( Map.Entry<String, Map<String, Map<String, AtomicLong>>> startLabel : counts.entrySet() )
{
for ( Map.Entry<String, Map<String, AtomicLong>> type : startLabel.getValue().entrySet() )
{
for ( Map.Entry<String, AtomicLong> endLabel : type.getValue().entrySet() )
{
RelationshipCountKey key = new RelationshipCountKey(
labelTranslationTable.apply( startLabel.getKey() ),
relationshipTypeTranslationTable.apply( type.getKey() ),
labelTranslationTable.apply( endLabel.getKey() ) );
result.add( Pair.of( key, endLabel.getValue().longValue() ) );
}
}
}
return result;
}
private Iterable<Pair<Integer,Long>> allNodeCounts( Function<String, Integer> labelTranslationTable,
Map<String, AtomicLong> counts )
{
Collection<Pair<Integer,Long>> result = new ArrayList<>();
for ( Map.Entry<String, AtomicLong> count : counts.entrySet() )
{
result.add( Pair.of( labelTranslationTable.apply( count.getKey() ), count.getValue().get() ) );
}
counts.put( null, new AtomicLong( counts.size() ) );
return result;
}
private Function<String, Integer> translationTable( TokenStore<?, ?> tokenStore, final int anyValue )
{
final Map<String, Integer> translationTable = new HashMap<>();
for ( Token token : tokenStore.getTokens( Integer.MAX_VALUE ) )
{
translationTable.put( token.name(), token.id() );
}
return from -> from == null ? anyValue : translationTable.get( from );
}
private Set<String> names( Iterable<Label> labels )
{
Set<String> names = new HashSet<>();
for ( Label label : labels )
{
names.add( label.name() );
}
return names;
}
private void buildUpExpectedData(
List<InputNode> nodeData,
List<InputRelationship> relationshipData,
Map<String, InputNode> expectedNodes,
Map<String, String[]> expectedNodeNames,
Map<String, Map<String, Map<String, AtomicInteger>>> expectedRelationships,
Map<String, AtomicLong> nodeCounts,
Map<String, Map<String, Map<String, AtomicLong>>> relationshipCounts )
{
for ( InputNode node : nodeData )
{
expectedNodes.put( (String) node.id(), node );
expectedNodeNames.put( nameOf( node ), node.labels() );
countNodeLabels( nodeCounts, node.labels() );
}
for ( InputRelationship relationship : relationshipData )
{
// Expected relationship counts per node, type and direction
InputNode startNode = expectedNodes.get( relationship.startNode() );
InputNode endNode = expectedNodes.get( relationship.endNode() );
{
expectedRelationships.get( nameOf( startNode ) )
.get( nameOf( endNode ) )
.get( relationship.type() )
.incrementAndGet();
}
// Expected counts per start/end node label ids
// Let's do what CountsState#addRelationship does, roughly
relationshipCounts.get( null ).get( null ).get( null ).incrementAndGet();
relationshipCounts.get( null ).get( relationship.type() ).get( null ).incrementAndGet();
for ( String startNodeLabelName : asSet( startNode.labels() ) )
{
Map<String, Map<String, AtomicLong>> startLabelCounts = relationshipCounts.get( startNodeLabelName );
startLabelCounts.get( null ).get( null ).incrementAndGet();
Map<String, AtomicLong> typeCounts = startLabelCounts.get( relationship.type() );
typeCounts.get( null ).incrementAndGet();
if ( COMPUTE_DOUBLE_SIDED_RELATIONSHIP_COUNTS )
{
for ( String endNodeLabelName : asSet( endNode.labels() ) )
{
startLabelCounts.get( null ).get( endNodeLabelName ).incrementAndGet();
typeCounts.get( endNodeLabelName ).incrementAndGet();
}
}
}
for ( String endNodeLabelName : asSet( endNode.labels() ) )
{
relationshipCounts.get( null ).get( null ).get( endNodeLabelName ).incrementAndGet();
relationshipCounts.get( null ).get( relationship.type() ).get( endNodeLabelName ).incrementAndGet();
}
}
}
private void countNodeLabels( Map<String, AtomicLong> nodeCounts, String[] labels )
{
Set<String> seen = new HashSet<>();
for ( String labelName : labels )
{
if ( seen.add( labelName ) )
{
nodeCounts.get( labelName ).incrementAndGet();
}
}
}
}