-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RecordFormatSelector.java
360 lines (332 loc) · 14.4 KB
/
RecordFormatSelector.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.store.format;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Service;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.layout.DatabaseLayout;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.store.format.standard.MetaDataRecordFormat;
import org.neo4j.kernel.impl.store.format.standard.Standard;
import org.neo4j.kernel.impl.store.format.standard.StandardV2_3;
import org.neo4j.kernel.impl.store.format.standard.StandardV3_0;
import org.neo4j.kernel.impl.store.format.standard.StandardV3_2;
import org.neo4j.kernel.impl.store.format.standard.StandardV3_4;
import org.neo4j.logging.LogProvider;
import static java.util.Arrays.asList;
import static org.neo4j.helpers.collection.Iterables.concat;
import static org.neo4j.helpers.collection.Iterables.map;
import static org.neo4j.kernel.impl.store.MetaDataStore.Position.STORE_VERSION;
/**
* Selects record format that will be used in a database.
* Supports selection based on the existing store and given configuration.
* <p>
* Automatic selection is used by various tools and tests that should pretend being format independent (for
* example backup).
*/
public class RecordFormatSelector
{
private static final RecordFormats DEFAULT_FORMAT = Standard.LATEST_RECORD_FORMATS;
private static final List<RecordFormats> KNOWN_FORMATS = asList(
StandardV2_3.RECORD_FORMATS,
StandardV3_0.RECORD_FORMATS,
StandardV3_2.RECORD_FORMATS,
StandardV3_4.RECORD_FORMATS
);
private RecordFormatSelector()
{
throw new AssertionError( "Not for instantiation!" );
}
/**
* Select {@link #DEFAULT_FORMAT} record format.
*
* @return default record format.
*/
@Nonnull
public static RecordFormats defaultFormat()
{
return DEFAULT_FORMAT;
}
/**
* Select record formats for provided store version.
*
* @param storeVersion store version to find format for
* @return record formats
* @throws IllegalArgumentException if format for specified store version not found
*/
@Nonnull
public static RecordFormats selectForVersion( String storeVersion )
{
for ( RecordFormats format : allFormats() )
{
if ( format.storeVersion().equals( storeVersion ) )
{
return format;
}
}
throw new IllegalArgumentException( "Unknown store version '" + storeVersion + "'" );
}
/**
* Select configured record format based on available services in class path.
* Specific format can be specified by {@link GraphDatabaseSettings#record_format} property.
* <p>
* If format is not specified {@link #DEFAULT_FORMAT} will be used.
*
* @param config configuration parameters
* @param logProvider logging provider
* @return selected record format
* @throws IllegalArgumentException if requested format not found
*/
@Nonnull
public static RecordFormats selectForConfig( Config config, LogProvider logProvider )
{
String recordFormat = configuredRecordFormat( config );
if ( StringUtils.isEmpty( recordFormat ) )
{
info( logProvider, "Record format not configured, selected default: " + defaultFormat() );
return defaultFormat();
}
RecordFormats format = selectSpecificFormat( recordFormat );
info( logProvider, "Selected record format based on config: " + format );
return format;
}
/**
* Select record format for the given store directory.
* <p>
* <b>Note:</b> package private only for testing.
*
* @param databaseLayout directory with the store
* @param fs file system used to access store files
* @param pageCache page cache to read store files
* @return record format of the given store or <code>null</code> if {@link DatabaseLayout#metadataStore()} file not
* found or can't be read
*/
@Nullable
static RecordFormats selectForStore( DatabaseLayout databaseLayout, FileSystemAbstraction fs, PageCache pageCache, LogProvider logProvider )
{
File neoStoreFile = databaseLayout.metadataStore();
if ( fs.fileExists( neoStoreFile ) )
{
try
{
long value = MetaDataStore.getRecord( pageCache, neoStoreFile, STORE_VERSION );
if ( value != MetaDataRecordFormat.FIELD_NOT_PRESENT )
{
String storeVersion = MetaDataStore.versionLongToString( value );
for ( RecordFormats format : allFormats() )
{
if ( format.storeVersion().equals( storeVersion ) )
{
info( logProvider, "Selected " + format + " record format from store " + databaseLayout.databaseDirectory() );
return format;
}
}
}
}
catch ( IOException e )
{
info( logProvider, "Unable to read store format: " + e.getMessage() );
}
}
return null;
}
/**
* Select record format for the given store (if exists) or from the given configuration. If there is no store and
* record format is not configured than {@link #DEFAULT_FORMAT} is selected.
*
* @param config configuration parameters
* @param databaseLayout database directory structure
* @param fs file system used to access store files
* @param pageCache page cache to read store files
* @return record format from the store (if it can be read) or configured record format or {@link #DEFAULT_FORMAT}
* @throws IllegalArgumentException when configured format is different from the format present in the store
*/
@Nonnull
public static RecordFormats selectForStoreOrConfig(
Config config, DatabaseLayout databaseLayout, FileSystemAbstraction fs, PageCache pageCache, LogProvider logProvider )
{
RecordFormats configuredFormat = loadRecordFormat( configuredRecordFormat( config ) );
boolean formatConfigured = configuredFormat != null;
RecordFormats currentFormat = selectForStore( databaseLayout, fs, pageCache, logProvider );
boolean storeWithFormatExists = currentFormat != null;
if ( formatConfigured && storeWithFormatExists )
{
if ( currentFormat.getFormatFamily().equals( configuredFormat.getFormatFamily() ) &&
(currentFormat.generation() == configuredFormat.generation()) )
{
info( logProvider, "Configured format matches format in the store. Selected: " + currentFormat );
return currentFormat;
}
throw new IllegalArgumentException( String.format(
"Configured format '%s' is different from the actual format in the store '%s'",
configuredFormat, currentFormat ) );
}
if ( !formatConfigured && storeWithFormatExists )
{
info( logProvider, "Format not configured. Selected format from the store: " + currentFormat );
return currentFormat;
}
if ( formatConfigured )
{
info( logProvider, "Selected configured format: " + configuredFormat );
return configuredFormat;
}
return DEFAULT_FORMAT;
}
/**
* Check if store and configured formats are compatible. In case if format is not configured or store does not
* exist yet - we consider formats as compatible.
* @param config configuration parameters
* @param databaseLayout database directory structure
* @param fs file system used to access store files
* @param pageCache page cache to read store files
* @param logProvider log provider
* @return true if configured and actual format is compatible, false otherwise.
*/
public static boolean isStoreAndConfigFormatsCompatible(
Config config, DatabaseLayout databaseLayout, FileSystemAbstraction fs, PageCache pageCache, LogProvider logProvider )
{
RecordFormats configuredFormat = loadRecordFormat( configuredRecordFormat( config ) );
RecordFormats currentFormat = selectForStore( databaseLayout, fs, pageCache, logProvider );
return (configuredFormat == null) || (currentFormat == null) ||
(currentFormat.getFormatFamily().equals( configuredFormat.getFormatFamily() ) &&
(currentFormat.generation() == configuredFormat.generation()));
}
/**
* Select explicitly configured record format (via given {@code config}) or format from the store. If store does
* not exist or has old format ({@link RecordFormats#generation()}) than this method returns
* {@link #DEFAULT_FORMAT}.
*
* @param config configuration parameters
* @param databaseLayout database directory structure
* @param fs file system used to access store files
* @param pageCache page cache to read store files
* @return record format from the store (if it can be read) or configured record format or {@link #DEFAULT_FORMAT}
* @see RecordFormats#generation()
*/
@Nonnull
public static RecordFormats selectNewestFormat(
Config config, DatabaseLayout databaseLayout, FileSystemAbstraction fs, PageCache pageCache, LogProvider logProvider )
{
boolean formatConfigured = StringUtils.isNotEmpty( configuredRecordFormat( config ) );
if ( formatConfigured )
{
// format was explicitly configured so select it
return selectForConfig( config, logProvider );
}
else
{
RecordFormats result = selectForStore( databaseLayout, fs, pageCache, logProvider );
if ( result == null )
{
// format was not explicitly configured and store does not exist, select default format
info( logProvider, "Selected format '" + DEFAULT_FORMAT + "' for the new store" );
result = DEFAULT_FORMAT;
}
else if ( FormatFamily.isHigherFamilyFormat( DEFAULT_FORMAT, result ) ||
(FormatFamily.isSameFamily( result, DEFAULT_FORMAT ) && (result.generation() < DEFAULT_FORMAT.generation())) )
{
// format was not explicitly configured and store has lower format
// select default format, upgrade is intended
info( logProvider,
"Selected format '" + DEFAULT_FORMAT + "' for existing store with format '" + result + "'" );
result = DEFAULT_FORMAT;
}
return result;
}
}
/**
* Finds which format, if any, succeeded the specified format. Only formats in the same family are considered.
*
* @param format to find successor to.
* @return the format with the lowest generation > format.generation, or None if no such format is known.
*/
@Nonnull
public static Optional<RecordFormats> findSuccessor( @Nonnull final RecordFormats format )
{
return StreamSupport.stream( RecordFormatSelector.allFormats().spliterator(), false )
.filter( candidate -> FormatFamily.isSameFamily( format, candidate ) )
.filter( candidate -> candidate.generation() > format.generation() )
.reduce( ( a, b ) -> a.generation() < b.generation() ? a : b );
}
/**
* Gets all {@link RecordFormats} that the selector is aware of.
* @return An iterable over all known record formats.
*/
public static Iterable<RecordFormats> allFormats()
{
Iterable<RecordFormats.Factory> loadableFormatFactories = Service.load( RecordFormats.Factory.class );
Iterable<RecordFormats> loadableFormats = map( RecordFormats.Factory::newInstance, loadableFormatFactories );
return concat( KNOWN_FORMATS, loadableFormats );
}
@Nonnull
private static RecordFormats selectSpecificFormat( String recordFormat )
{
RecordFormats formats = loadRecordFormat( recordFormat );
if ( formats == null )
{
throw new IllegalArgumentException( "No record format found with the name '" + recordFormat + "'." );
}
return formats;
}
@Nullable
private static RecordFormats loadRecordFormat( String recordFormat )
{
if ( StringUtils.isNotEmpty( recordFormat ) )
{
if ( Standard.LATEST_NAME.equals( recordFormat ) )
{
return Standard.LATEST_RECORD_FORMATS;
}
for ( RecordFormats knownFormat : KNOWN_FORMATS )
{
if ( recordFormat.equals( knownFormat.name() ) )
{
return knownFormat;
}
}
RecordFormats.Factory formatFactory = Service.loadSilently( RecordFormats.Factory.class, recordFormat );
if ( formatFactory != null )
{
return formatFactory.newInstance();
}
}
return null;
}
private static void info( LogProvider logProvider, String message )
{
logProvider.getLog( RecordFormatSelector.class ).info( message );
}
@Nonnull
private static String configuredRecordFormat( Config config )
{
return config.get( GraphDatabaseSettings.record_format );
}
}