-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
InputCache.java
239 lines (219 loc) · 8.19 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
/*
* Copyright (c) 2002-2018 "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.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Set;
import org.neo4j.concurrent.Runnables;
import org.neo4j.function.ThrowingSupplier;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.OpenMode;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.impl.store.format.RecordFormats;
import org.neo4j.unsafe.impl.batchimport.InputIterable;
import org.neo4j.unsafe.impl.batchimport.InputIterator;
import org.neo4j.unsafe.impl.batchimport.ParallelBatchImporter;
import static org.neo4j.io.fs.OpenMode.READ;
import static org.neo4j.io.fs.OpenMode.READ_WRITE;
/**
* Cache of streams of nodes and relationships 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 cache instead, for {@link InputIterable} that does not
* {@link InputIterable#supportsMultiplePasses() support multiple passes}.
*
* <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;
private final FileSystemAbstraction fs;
private final File cacheDirectory;
private final RecordFormats recordFormats;
private final Set<String> subTypes = new HashSet<>();
private final int chunkSize;
/**
* @param fs {@link FileSystemAbstraction} to use
* @param cacheDirectory directory for placing the cached files
* @param recordFormats which {@link RecordFormats format} records are in
* @param chunkSize rough size of chunks written to the cache
*/
public InputCache( FileSystemAbstraction fs, File cacheDirectory, RecordFormats recordFormats, int chunkSize )
{
this.fs = fs;
this.cacheDirectory = cacheDirectory;
this.recordFormats = recordFormats;
this.chunkSize = chunkSize;
}
public InputCacher cacheNodes( String subType ) throws IOException
{
return new InputNodeCacheWriter( channel( NODES, subType, READ_WRITE ), channel( NODES_HEADER, subType, READ_WRITE ),
recordFormats, chunkSize );
}
public InputCacher cacheRelationships( String subType ) throws IOException
{
return new InputRelationshipCacheWriter( channel( RELATIONSHIPS, subType, READ_WRITE ),
channel( RELATIONSHIPS_HEADER, subType, READ_WRITE ), recordFormats, chunkSize );
}
private StoreChannel channel( String type, String subType, OpenMode openMode ) throws IOException
{
return fs.open( file( type, subType ), openMode );
}
private File file( String type, String subType )
{
subTypes.add( subType );
return new File( cacheDirectory, "input-" + type + "-" + subType );
}
public InputIterable nodes( String subType, boolean deleteAfterUse )
{
return entities( () -> new InputNodeCacheReader( channel( NODES, subType, READ ),
channel( NODES_HEADER, subType, READ ),
deleteAction( deleteAfterUse, NODES, NODES_HEADER, subType ) ) );
}
public InputIterable relationships( String subType, boolean deleteAfterUse )
{
return entities( () -> new InputRelationshipCacheReader( channel( RELATIONSHIPS, subType, READ ),
channel( RELATIONSHIPS_HEADER, subType, READ ),
deleteAction( deleteAfterUse, RELATIONSHIPS, RELATIONSHIPS_HEADER, subType ) ) );
}
protected Runnable deleteAction( boolean deleteAfterUse, String type, String header, String subType )
{
if ( !deleteAfterUse )
{
return Runnables.EMPTY_RUNNABLE;
}
return () ->
{
fs.deleteFile( file( type, subType ) );
fs.deleteFile( file( header, subType ) );
subTypes.remove( subType );
};
}
private InputIterable entities( final ThrowingSupplier<InputIterator, IOException> factory )
{
return new InputIterable()
{
@Override
public InputIterator iterator()
{
try
{
return factory.get();
}
catch ( IOException e )
{
throw new InputException( "Unable to open reader for cached entities", 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 ) );
}
}
static ByteBuffer newChunkHeaderBuffer()
{
return ByteBuffer.allocate( Integer.BYTES );
}
}