-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StoreFile.java
336 lines (299 loc) · 11.4 KB
/
StoreFile.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
/*
* 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.kernel.impl.storemigration;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.function.Predicate;
import org.neo4j.helpers.UTF8;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.impl.store.DynamicArrayStore;
import org.neo4j.kernel.impl.store.DynamicStringStore;
import org.neo4j.kernel.impl.store.LabelTokenStore;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.PropertyKeyTokenStore;
import org.neo4j.kernel.impl.store.PropertyStore;
import org.neo4j.kernel.impl.store.RelationshipGroupStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.RelationshipTypeTokenStore;
import org.neo4j.kernel.impl.store.SchemaStore;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.counts.CountsTracker;
import org.neo4j.kernel.impl.store.format.lowlimit.LowLimitV2_0;
import org.neo4j.kernel.impl.store.format.lowlimit.LowLimitV2_1;
import org.neo4j.kernel.impl.store.format.lowlimit.LowLimitV2_2;
import org.neo4j.kernel.impl.store.format.lowlimit.LowLimitV3_0;
import static org.neo4j.helpers.collection.Iterables.iterable;
public enum StoreFile
{
// all store files in Neo4j
NODE_STORE(
NodeStore.TYPE_DESCRIPTOR,
StoreFactory.NODE_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
NODE_LABEL_STORE(
DynamicArrayStore.TYPE_DESCRIPTOR,
StoreFactory.NODE_LABELS_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
PROPERTY_STORE(
PropertyStore.TYPE_DESCRIPTOR,
StoreFactory.PROPERTY_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
PROPERTY_ARRAY_STORE(
DynamicArrayStore.TYPE_DESCRIPTOR,
StoreFactory.PROPERTY_ARRAYS_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
PROPERTY_STRING_STORE(
DynamicStringStore.TYPE_DESCRIPTOR,
StoreFactory.PROPERTY_STRINGS_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
PROPERTY_KEY_TOKEN_STORE(
PropertyKeyTokenStore.TYPE_DESCRIPTOR,
StoreFactory.PROPERTY_KEY_TOKEN_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
PROPERTY_KEY_TOKEN_NAMES_STORE(
DynamicStringStore.TYPE_DESCRIPTOR,
StoreFactory.PROPERTY_KEY_TOKEN_NAMES_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
RELATIONSHIP_STORE(
RelationshipStore.TYPE_DESCRIPTOR,
StoreFactory.RELATIONSHIP_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
RELATIONSHIP_GROUP_STORE(
RelationshipGroupStore.TYPE_DESCRIPTOR,
StoreFactory.RELATIONSHIP_GROUP_STORE_NAME,
LowLimitV2_1.STORE_VERSION
),
RELATIONSHIP_TYPE_TOKEN_STORE(
RelationshipTypeTokenStore.TYPE_DESCRIPTOR,
StoreFactory.RELATIONSHIP_TYPE_TOKEN_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
RELATIONSHIP_TYPE_TOKEN_NAMES_STORE(
DynamicStringStore.TYPE_DESCRIPTOR,
StoreFactory.RELATIONSHIP_TYPE_TOKEN_NAMES_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
LABEL_TOKEN_STORE(
LabelTokenStore.TYPE_DESCRIPTOR,
StoreFactory.LABEL_TOKEN_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
LABEL_TOKEN_NAMES_STORE(
DynamicStringStore.TYPE_DESCRIPTOR,
StoreFactory.LABEL_TOKEN_NAMES_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
SCHEMA_STORE(
SchemaStore.TYPE_DESCRIPTOR,
StoreFactory.SCHEMA_STORE_NAME,
LowLimitV2_0.STORE_VERSION
),
COUNTS_STORE_LEFT(
CountsTracker.TYPE_DESCRIPTOR,
StoreFactory.COUNTS_STORE + CountsTracker.LEFT,
LowLimitV2_2.STORE_VERSION,
false
)
{
@Override
boolean isOptional()
{
return true;
}
},
COUNTS_STORE_RIGHT(
CountsTracker.TYPE_DESCRIPTOR,
StoreFactory.COUNTS_STORE + CountsTracker.RIGHT,
LowLimitV2_2.STORE_VERSION,
false
)
{
@Override
boolean isOptional()
{
return true;
}
},
NEO_STORE(
MetaDataStore.TYPE_DESCRIPTOR,
"",
LowLimitV2_0.STORE_VERSION
);
private final String typeDescriptor;
private final String storeFileNamePart;
private final String sinceVersion;
private final boolean recordStore;
StoreFile( String typeDescriptor, String storeFileNamePart, String sinceVersion )
{
this( typeDescriptor, storeFileNamePart, sinceVersion, true );
}
private StoreFile( String typeDescriptor, String storeFileNamePart, String sinceVersion, boolean recordStore )
{
this.typeDescriptor = typeDescriptor;
this.storeFileNamePart = storeFileNamePart;
this.sinceVersion = sinceVersion;
this.recordStore = recordStore;
}
public String forVersion( String version )
{
return typeDescriptor + " " + version;
}
public String fileName( StoreFileType type )
{
return type.augment( MetaDataStore.DEFAULT_NAME + storeFileNamePart );
}
public String storeFileName()
{
return fileName( StoreFileType.STORE );
}
public boolean isRecordStore()
{
return recordStore;
}
public static Iterable<StoreFile> legacyStoreFilesForVersion( final String version )
{
Predicate<StoreFile> predicate = item -> version.compareTo( item.sinceVersion ) >= 0;
Iterable<StoreFile> storeFiles = currentStoreFiles();
Iterable<StoreFile> filter = Iterables.filter( predicate, storeFiles );
return filter;
}
public static Iterable<StoreFile> currentStoreFiles()
{
return Iterables.iterable( values() );
}
public static void fileOperation( FileOperation operation, FileSystemAbstraction fs, File fromDirectory,
File toDirectory, StoreFile... files ) throws IOException
{
fileOperation( operation, fs, fromDirectory, toDirectory, storeFiles( files ), false,
ExistingTargetStrategy.FAIL );
}
public static void fileOperation( FileOperation operation, FileSystemAbstraction fs, File fromDirectory,
File toDirectory, Iterable<StoreFile> files,
boolean allowSkipNonExistentFiles, ExistingTargetStrategy existingTargetStrategy ) throws IOException
{
fileOperation( operation, fs, fromDirectory, toDirectory, files, allowSkipNonExistentFiles,
existingTargetStrategy, StoreFileType.values() );
}
/**
* Performs a file operation on a database's store files from one directory
* to another. Remember that in the case of {@link FileOperation#MOVE moving files}, the way that's done is to
* just rename files (the standard way of moving with JDK6) from and to must be on the same disk partition.
*
* @param fromDirectory directory that hosts the database files.
* @param toDirectory directory to receive the database files.
* @throws IOException if any of the operations fail for any reason.
*/
public static void fileOperation( FileOperation operation, FileSystemAbstraction fs, File fromDirectory,
File toDirectory, Iterable<StoreFile> files,
boolean allowSkipNonExistentFiles, ExistingTargetStrategy existingTargetStrategy,
StoreFileType... types ) throws IOException
{
// TODO: change the order of files to handle failure conditions properly
for ( StoreFile storeFile : files )
{
for ( StoreFileType type : types )
{
String fileName = storeFile.fileName( type );
operation.perform( fs, fileName,
fromDirectory, allowSkipNonExistentFiles, toDirectory, existingTargetStrategy );
}
}
}
public static void removeTrailers( String version, FileSystemAbstraction fs, File storeDir, int pageSize )
throws IOException
{
for ( StoreFile storeFile : legacyStoreFilesForVersion( LowLimitV3_0.STORE_VERSION ) )
{
String trailer = storeFile.forVersion( version );
byte[] encodedTrailer = UTF8.encode( trailer );
File file = new File( storeDir, storeFile.storeFileName() );
long fileSize = fs.getFileSize( file );
long truncationPosition = containsTrailer( fs, file, fileSize, pageSize, encodedTrailer );
if ( truncationPosition != -1 )
{
fs.truncate( file, truncationPosition );
}
}
}
private static long containsTrailer( FileSystemAbstraction fs, File file, long fileSize, int pageSize,
byte[] encodedTrailer ) throws IOException
{
if ( !fs.fileExists( file ) )
{
return -1l;
}
try ( StoreChannel channel = fs.open( file, "rw" ) )
{
ByteBuffer buffer = ByteBuffer.allocate( encodedTrailer.length );
long newPosition = Math.max( 0, fileSize - encodedTrailer.length );
long stopPosition = Math.max( 0, fileSize - encodedTrailer.length - pageSize );
while ( newPosition >= stopPosition )
{
channel.position( newPosition );
int totalRead = 0;
do
{
int read = channel.read( buffer );
if ( read == -1 )
{
return -1l;
}
totalRead += read;
}
while ( totalRead < encodedTrailer.length );
if ( Arrays.equals( buffer.array(), encodedTrailer ) )
{
return newPosition;
}
else
{
newPosition -= 1;
buffer.clear();
}
}
return -1;
}
}
boolean isOptional()
{
return false;
}
/**
* Merely here as convenience since java generics is acting up in many cases, so this is nicer for
* inlining such a call into {@link #fileOperation(FileOperation, FileSystemAbstraction, File, File, Iterable, boolean, boolean, StoreFileType...)}
*/
public static Iterable<StoreFile> storeFiles( StoreFile... files )
{
return iterable( files );
}
}