-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
InputCache.java
261 lines (241 loc) · 9.08 KB
/
InputCache.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
/*
* Copyright (c) 2002-2016 "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;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.neo4j.function.ThrowingSupplier;
import org.neo4j.io.ByteUnit;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.impl.store.format.RecordFormats;
import org.neo4j.unsafe.impl.batchimport.Configuration;
import org.neo4j.unsafe.impl.batchimport.InputIterable;
import org.neo4j.unsafe.impl.batchimport.InputIterator;
import org.neo4j.unsafe.impl.batchimport.ParallelBatchImporter;
/**
* Cache of streams of {@link InputNode} or {@link InputRelationship} from an {@link Input} instance.
* Useful since {@link ParallelBatchImporter} may require multiple passes over the input data and so
* consecutive passes will be served by this thing instead.
*
* <pre>
* Properties format:
* - 2B property count, or {@link #HAS_FIRST_PROPERTY_ID} or {@link #END_OF_ENTITIES}
* - property...:
* - 4B token id (token string->id mapping is in header file)
* - ?B value, see {@link ValueType}
* </pre>
*
* <pre>
* Group format:
* - 1B, {@link #SAME_GROUP} or {@link #NEW_GROUP}
* IF {@link #NEW_GROUP}
* - 4B group if (if {@link #NEW_GROUP}
* - 4B token id
* </pre>
*
* <pre>
* Node format:
* - properties (see "Properties format")
* - group (see "Group format")
* - id
* - ?B id value, see {@link ValueType}
* - labels
* - 1B label mode, {@link #HAS_LABEL_FIELD} or {@link #LABEL_ADDITION} or {@link #LABEL_REMOVAL} or
* {@link #END_OF_LABEL_CHANGES}
* WHILE NOT {@link #END_OF_LABEL_CHANGES}
* - 4B token id, to add or remove
* - 1B label mode, next mode
* </pre>
*
* <pre>
* Relationship format:
* - properties (see "Properties format")
* - start node group (see "Group format")
* - end node group (see "Group format")
* - start node id
* - ?B id value, see {@link ValueType}
* - end node id
* - ?B id value, see {@link ValueType}
* - type
* - 1B mode, {@link #SAME_TYPE} or {@link #NEW_TYPE} or {@link #HAS_TYPE_ID}
* IF {@link #HAS_TYPE_ID}
* 4B type id
* ELSE IF {@link #NEW_TYPE}
* 4B token id
* </pre>
*
* The format stores entities in batches, each batch having a small header containing number of bytes
* and number of entities.
*/
public class InputCache implements Closeable
{
public static final String MAIN = "main";
private static final String HEADER = "-header";
private static final String NODES = "nodes";
private static final String RELATIONSHIPS = "relationships";
private static final String NODES_HEADER = NODES + HEADER;
private static final String RELATIONSHIPS_HEADER = RELATIONSHIPS + HEADER;
static final byte SAME_GROUP = 0;
static final byte NEW_GROUP = 1;
static final byte PROPERTY_KEY_TOKEN = 0;
static final byte LABEL_TOKEN = 1;
static final byte RELATIONSHIP_TYPE_TOKEN = 2;
static final byte GROUP_TOKEN = 3;
static final byte HIGH_TOKEN_TYPE = 4;
static final short HAS_FIRST_PROPERTY_ID = -1;
static final byte HAS_LABEL_FIELD = 3;
static final byte LABEL_REMOVAL = 1;
static final byte LABEL_ADDITION = 2;
static final byte END_OF_LABEL_CHANGES = 0;
static final byte HAS_TYPE_ID = 2;
static final byte SAME_TYPE = 0;
static final byte NEW_TYPE = 1;
static final byte END_OF_HEADER = -2;
static final short END_OF_ENTITIES = -3;
static final int NO_ENTITIES = 0;
static final long END_OF_CACHE = 0L;
private final FileSystemAbstraction fs;
private final File cacheDirectory;
private final RecordFormats recordFormats;
private final Configuration config;
private final int bufferSize;
private final Set<String> subTypes = new HashSet<>();
private final int batchSize;
public InputCache( FileSystemAbstraction fs, File cacheDirectory, RecordFormats recordFormats,
Configuration config )
{
this( fs, cacheDirectory, recordFormats, config, (int) ByteUnit.kibiBytes( 512 ), 10_000 );
}
/**
* @param fs {@link FileSystemAbstraction} to use
* @param cacheDirectory directory for placing the cached files
* @param config import configuration
* @param bufferSize buffer size for writing/reading cache files
* @param batchSize number of entities in each batch
*/
public InputCache( FileSystemAbstraction fs, File cacheDirectory, RecordFormats recordFormats,
Configuration config, int bufferSize, int batchSize )
{
this.fs = fs;
this.cacheDirectory = cacheDirectory;
this.recordFormats = recordFormats;
this.config = config;
this.bufferSize = bufferSize;
this.batchSize = batchSize;
}
public Receiver<InputNode[],IOException> cacheNodes( String subType ) throws IOException
{
return new InputNodeCacher( channel( NODES, subType, "rw" ), channel( NODES_HEADER, subType, "rw" ),
recordFormats, bufferSize, batchSize );
}
public Receiver<InputRelationship[],IOException> cacheRelationships( String subType ) throws
IOException
{
return new InputRelationshipCacher( channel( RELATIONSHIPS, subType, "rw" ),
channel( RELATIONSHIPS_HEADER, subType, "rw" ), recordFormats, bufferSize, batchSize );
}
private StoreChannel channel( String type, String subType, String mode ) throws IOException
{
return fs.open( file( type, subType ), mode );
}
private File file( String type, String subType )
{
subTypes.add( subType );
return new File( cacheDirectory, "input-" + type + "-" + subType );
}
public InputIterable<InputNode> nodes( String subType, boolean deleteAfterUse )
{
return entities( new ThrowingSupplier<InputIterator<InputNode>, IOException>()
{
@Override
public InputIterator<InputNode> get() throws IOException
{
return new InputNodeReader( channel( NODES, subType, "r" ), channel( NODES_HEADER, subType, "r" ),
bufferSize, deleteAction( deleteAfterUse, NODES, NODES_HEADER, subType ),
config.maxNumberOfProcessors() );
}
} );
}
public InputIterable<InputRelationship> relationships( String subType, boolean deleteAfterUse )
{
return entities( new ThrowingSupplier<InputIterator<InputRelationship>, IOException>()
{
@Override
public InputIterator<InputRelationship> get() throws IOException
{
return new InputRelationshipReader( channel( RELATIONSHIPS, subType, "r" ),
channel( RELATIONSHIPS_HEADER, subType, "r" ), bufferSize,
deleteAction( deleteAfterUse, RELATIONSHIPS, RELATIONSHIPS_HEADER, subType ),
config.maxNumberOfProcessors() );
}
} );
}
protected Runnable deleteAction( boolean deleteAfterUse, String type, String header, String subType )
{
if ( !deleteAfterUse )
{
return () -> {};
}
return () ->
{
fs.deleteFile( file( type, subType ) );
fs.deleteFile( file( header, subType ) );
subTypes.remove( subType );
};
}
private <T extends InputEntity> InputIterable<T> entities(
final ThrowingSupplier<InputIterator<T>, IOException> factory )
{
return new InputIterable<T>()
{
@Override
public InputIterator<T> iterator()
{
try
{
return factory.get();
}
catch ( IOException e )
{
throw new InputException( "Unable to read cached relationship", e );
}
}
@Override
public boolean supportsMultiplePasses()
{
return true;
}
};
}
@Override
public void close() throws IOException
{
for ( String subType : subTypes )
{
fs.deleteFile( file( NODES, subType ) );
fs.deleteFile( file( RELATIONSHIPS, subType ) );
fs.deleteFile( file( NODES_HEADER, subType ) );
fs.deleteFile( file( RELATIONSHIPS_HEADER, subType ) );
}
}
}