-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RelationshipTypeCheckerStep.java
126 lines (114 loc) · 5.71 KB
/
RelationshipTypeCheckerStep.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
/*
* 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;
import org.apache.commons.lang3.mutable.MutableLong;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Stream;
import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.unsafe.impl.batchimport.input.InputRelationship;
import org.neo4j.unsafe.impl.batchimport.staging.BatchSender;
import org.neo4j.unsafe.impl.batchimport.staging.ProcessorStep;
import org.neo4j.unsafe.impl.batchimport.staging.StageControl;
import org.neo4j.unsafe.impl.batchimport.store.BatchingTokenRepository.BatchingRelationshipTypeTokenRepository;
import static java.lang.Thread.currentThread;
/**
* Counts relationships per type to later be able to provide all types, even sorted in descending order
* of number of relationships per type.
*/
public class RelationshipTypeCheckerStep extends ProcessorStep<Batch<InputRelationship,RelationshipRecord>>
{
private static final Function<Object,MutableLong> NEW_MUTABLE_LONG = type -> new MutableLong();
private static final Comparator<Map.Entry<Object,MutableLong>> SORT_BY_COUNT_DESC =
( e1, e2 ) -> Long.compare( e2.getValue().longValue(), e1.getValue().longValue() );
private static final Comparator<Map.Entry<Object,MutableLong>> SORT_BY_ID_DESC =
( e1, e2 ) -> Integer.compare( (Integer) e2.getKey(), (Integer) e1.getKey() );
private final Map<Thread,Map<Object,MutableLong>> typeCheckers = new ConcurrentHashMap<>();
private final BatchingRelationshipTypeTokenRepository typeTokenRepository;
private final long nodeCount;
private RelationshipTypeDistribution distribution;
public RelationshipTypeCheckerStep( StageControl control, Configuration config,
BatchingRelationshipTypeTokenRepository typeTokenRepository, long nodeCount )
{
super( control, "TYPE", config, 0 );
this.typeTokenRepository = typeTokenRepository;
this.nodeCount = nodeCount;
}
@Override
protected void process( Batch<InputRelationship,RelationshipRecord> batch, BatchSender sender ) throws Throwable
{
Map<Object,MutableLong> typeMap = typeCheckers.computeIfAbsent( currentThread(), t -> new HashMap<>() );
Stream.of( batch.input )
.map( InputRelationship::typeAsObject )
.filter( type -> type != null )
.forEach( type -> typeMap.computeIfAbsent( type, NEW_MUTABLE_LONG ).increment() );
sender.send( batch );
}
@SuppressWarnings( "unchecked" )
@Override
protected void done()
{
Map<Object,MutableLong> mergedTypes = new HashMap<>();
typeCheckers.forEach( ( thread, localTypes ) ->
localTypes.forEach( ( type, localCount ) ->
mergedTypes.computeIfAbsent( type, t -> new MutableLong() ).add( localCount.longValue() ) ) );
Map.Entry<Object,MutableLong>[] sortedTypes = mergedTypes.entrySet().toArray( new Map.Entry[mergedTypes.size()] );
if ( sortedTypes.length > 0 )
{
Comparator<Map.Entry<Object,MutableLong>> comparator = sortedTypes[0].getKey() instanceof Integer ?
SORT_BY_ID_DESC : SORT_BY_COUNT_DESC;
Arrays.sort( sortedTypes, comparator );
}
// Create the types in the reverse order of which is returned in getAllTypes()
// Why do we do that? Well, it's so that the relationship groups can be created iteratively
// and still keeping order of (ascending) type in its chains. Relationship groups have next pointers
// and creating these groups while still adhering to principal of sequential I/O doesn't allow us
// to go back and update a previous group to point to a next relationship group. This is why we
// create the groups in ascending id order whereas next pointers will always point backwards to
// lower ids (and therefore relationship type ids). This fulfills the constraint of having
// relationship group record chains be in order of ascending relationship type.
for ( int i = sortedTypes.length - 1; i >= 0; i-- )
{
typeTokenRepository.getOrCreateId( sortedTypes[i].getKey() );
}
distribution = new RelationshipTypeDistribution( nodeCount, convert( sortedTypes ) );
super.done();
}
private static Pair<Object,Long>[] convert( Entry<Object,MutableLong>[] sortedTypes )
{
@SuppressWarnings( "unchecked" )
Pair<Object,Long>[] result = new Pair[sortedTypes.length];
for ( int i = 0; i < sortedTypes.length; i++ )
{
result[i] = Pair.of( sortedTypes[i].getKey(), sortedTypes[i].getValue().longValue() );
}
return result;
}
public RelationshipTypeDistribution getDistribution()
{
return distribution;
}
}