-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PropertyStore.java
406 lines (367 loc) · 14.5 KB
/
PropertyStore.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* 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.kernel.impl.store;
import java.io.File;
import java.nio.file.OpenOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.neo4j.collection.primitive.PrimitiveLongObjectMap;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.helpers.collection.Pair;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.format.RecordFormats;
import org.neo4j.kernel.impl.store.format.standard.StandardFormatSettings;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.store.id.IdType;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.store.record.RecordLoad;
import org.neo4j.logging.LogProvider;
import org.neo4j.string.UTF8;
import static org.neo4j.kernel.impl.store.DynamicArrayStore.getRightArray;
import static org.neo4j.kernel.impl.store.NoStoreHeaderFormat.NO_STORE_HEADER_FORMAT;
import static org.neo4j.kernel.impl.store.record.Record.NULL_REFERENCE;
import static org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL;
/**
* Implementation of the property store. This implementation has two dynamic
* stores. One used to store keys and another for string property values.
*/
public class PropertyStore extends CommonAbstractStore<PropertyRecord,NoStoreHeader>
{
public abstract static class Configuration extends CommonAbstractStore.Configuration
{
}
public static final String TYPE_DESCRIPTOR = "PropertyStore";
private final DynamicStringStore stringStore;
private final PropertyKeyTokenStore propertyKeyTokenStore;
private final DynamicArrayStore arrayStore;
public PropertyStore(
File fileName,
Config configuration,
IdGeneratorFactory idGeneratorFactory,
PageCache pageCache,
LogProvider logProvider,
DynamicStringStore stringPropertyStore,
PropertyKeyTokenStore propertyKeyTokenStore,
DynamicArrayStore arrayPropertyStore,
RecordFormats recordFormats,
OpenOption... openOptions )
{
super( fileName, configuration, IdType.PROPERTY, idGeneratorFactory, pageCache, logProvider, TYPE_DESCRIPTOR,
recordFormats.property(), NO_STORE_HEADER_FORMAT, recordFormats.storeVersion(), openOptions );
this.stringStore = stringPropertyStore;
this.propertyKeyTokenStore = propertyKeyTokenStore;
this.arrayStore = arrayPropertyStore;
}
@Override
public <FAILURE extends Exception> void accept( RecordStore.Processor<FAILURE> processor, PropertyRecord record )
throws FAILURE
{
processor.processProperty( this, record );
}
public DynamicStringStore getStringStore()
{
return stringStore;
}
public DynamicArrayStore getArrayStore()
{
return arrayStore;
}
public PropertyKeyTokenStore getPropertyKeyTokenStore()
{
return propertyKeyTokenStore;
}
@Override
public void updateRecord( PropertyRecord record )
{
updatePropertyBlocks( record );
super.updateRecord( record );
}
private void updatePropertyBlocks( PropertyRecord record )
{
if ( record.inUse() )
{
// Go through the blocks
for ( PropertyBlock block : record )
{
/*
* For each block we need to update its dynamic record chain if
* it is just created. Deleted dynamic records are in the property
* record and dynamic records are never modified. Also, they are
* assigned as a whole, so just checking the first should be enough.
*/
if ( !block.isLight()
&& block.getValueRecords().get( 0 ).isCreated() )
{
updateDynamicRecords( block.getValueRecords() );
}
}
}
updateDynamicRecords( record.getDeletedRecords() );
}
private void updateDynamicRecords( List<DynamicRecord> records )
{
for ( DynamicRecord valueRecord : records )
{
PropertyType recordType = valueRecord.getType();
if ( recordType == PropertyType.STRING )
{
stringStore.updateRecord( valueRecord );
}
else if ( recordType == PropertyType.ARRAY )
{
arrayStore.updateRecord( valueRecord );
}
else
{
throw new InvalidRecordException( "Unknown dynamic record"
+ valueRecord );
}
}
}
@Override
public void ensureHeavy( PropertyRecord record )
{
for ( PropertyBlock block : record )
{
ensureHeavy( block );
}
}
public void ensureHeavy( PropertyBlock block )
{
if ( !block.isLight() )
{
return;
}
PropertyType type = block.getType();
RecordStore<DynamicRecord> dynamicStore = dynamicStoreForValueType( type );
if ( dynamicStore == null )
{
return;
}
DynamicRecord dynamicRecord = dynamicStore.newRecord();
long id = block.getSingleValueLong();
try ( PageCursor cursor = dynamicStore.newPageCursor() )
{
while ( !NULL_REFERENCE.is( id ) )
{
dynamicStore.readRecord( id, dynamicRecord, NORMAL, cursor );
dynamicRecord.setType( type.intValue() );
block.addValueRecord( dynamicRecord.clone() );
id = dynamicStore.getNextRecordReference( dynamicRecord );
}
}
}
private RecordStore<DynamicRecord> dynamicStoreForValueType( PropertyType type )
{
switch ( type )
{
case ARRAY: return arrayStore;
case STRING: return stringStore;
default: return null;
}
}
public Object getValue( PropertyBlock propertyBlock )
{
return propertyBlock.getType().getValue( propertyBlock, this );
}
public static void allocateStringRecords( Collection<DynamicRecord> target, byte[] chars,
DynamicRecordAllocator allocator )
{
AbstractDynamicStore.allocateRecordsFromBytes( target, chars, allocator );
}
public static void allocateArrayRecords( Collection<DynamicRecord> target, Object array,
DynamicRecordAllocator allocator )
{
DynamicArrayStore.allocateRecords( target, array, allocator );
}
public void encodeValue( PropertyBlock block, int keyId, Object value )
{
encodeValue( block, keyId, value, stringStore, arrayStore );
}
public static void encodeValue( PropertyBlock block, int keyId, Object value,
DynamicRecordAllocator stringAllocator, DynamicRecordAllocator arrayAllocator )
{
if ( value instanceof String )
{ // Try short string first, i.e. inlined in the property block
String string = (String) value;
if ( LongerShortString.encode( keyId, string, block, PropertyType.getPayloadSize() ) )
{
return;
}
// Fall back to dynamic string store
byte[] encodedString = encodeString( string );
List<DynamicRecord> valueRecords = new ArrayList<>();
allocateStringRecords( valueRecords, encodedString, stringAllocator );
setSingleBlockValue( block, keyId, PropertyType.STRING, Iterables.first( valueRecords ).getId() );
for ( DynamicRecord valueRecord : valueRecords )
{
valueRecord.setType( PropertyType.STRING.intValue() );
}
block.setValueRecords( valueRecords );
}
else if ( value instanceof Integer )
{
setSingleBlockValue( block, keyId, PropertyType.INT, ((Integer) value).longValue() );
}
else if ( value instanceof Boolean )
{
setSingleBlockValue( block, keyId, PropertyType.BOOL, (Boolean) value ? 1L : 0L );
}
else if ( value instanceof Float )
{
setSingleBlockValue( block, keyId, PropertyType.FLOAT, Float.floatToRawIntBits( (Float) value ) );
}
else if ( value instanceof Long )
{
long keyAndType = keyId | (((long) PropertyType.LONG.intValue()) <<
StandardFormatSettings.PROPERTY_TOKEN_MAXIMUM_ID_BITS);
if ( ShortArray.LONG.getRequiredBits( (Long) value ) <= 35 )
{ // We only need one block for this value, special layout compared to, say, an integer
block.setSingleBlock( keyAndType | (1L << 28) | ((Long) value << 29) );
}
else
{ // We need two blocks for this value
block.setValueBlocks( new long[]{keyAndType, (Long) value} );
}
}
else if ( value instanceof Double )
{
block.setValueBlocks( new long[]{ keyId |
(((long) PropertyType.DOUBLE.intValue()) << StandardFormatSettings.PROPERTY_TOKEN_MAXIMUM_ID_BITS),
Double.doubleToRawLongBits( (Double) value )} );
}
else if ( value instanceof Byte )
{
setSingleBlockValue( block, keyId, PropertyType.BYTE, ((Byte) value).longValue() );
}
else if ( value instanceof Character )
{
setSingleBlockValue( block, keyId, PropertyType.CHAR, (Character) value );
}
else if ( value instanceof Short )
{
setSingleBlockValue( block, keyId, PropertyType.SHORT, ((Short) value).longValue() );
}
else if ( value.getClass().isArray() )
{ // Try short array first, i.e. inlined in the property block
if ( ShortArray.encode( keyId, value, block, PropertyType.getPayloadSize() ) )
{
return;
}
// Fall back to dynamic array store
List<DynamicRecord> arrayRecords = new ArrayList<>();
allocateArrayRecords( arrayRecords, value, arrayAllocator );
setSingleBlockValue( block, keyId, PropertyType.ARRAY, Iterables.first( arrayRecords ).getId() );
for ( DynamicRecord valueRecord : arrayRecords )
{
valueRecord.setType( PropertyType.ARRAY.intValue() );
}
block.setValueRecords( arrayRecords );
}
else
{
throw new IllegalArgumentException( "Unknown property type on: " + value + ", " + value.getClass() );
}
}
public static void setSingleBlockValue( PropertyBlock block, int keyId, PropertyType type, long longValue )
{
block.setSingleBlock( singleBlockLongValue( keyId, type, longValue ) );
}
public static long singleBlockLongValue( int keyId, PropertyType type, long longValue )
{
return keyId | (((long) type.intValue()) << StandardFormatSettings.PROPERTY_TOKEN_MAXIMUM_ID_BITS) |
(longValue << 28);
}
public static byte[] encodeString( String string )
{
return UTF8.encode( string );
}
public static String decodeString( byte[] byteArray )
{
return UTF8.decode( byteArray );
}
public String getStringFor( PropertyBlock propertyBlock )
{
ensureHeavy( propertyBlock );
return getStringFor( propertyBlock.getValueRecords() );
}
public String getStringFor( Collection<DynamicRecord> dynamicRecords )
{
Pair<byte[], byte[]> source = stringStore.readFullByteArray( dynamicRecords, PropertyType.STRING );
// A string doesn't have a header in the data array
return decodeString( source.other() );
}
public Object getArrayFor( PropertyBlock propertyBlock )
{
ensureHeavy( propertyBlock );
return getArrayFor( propertyBlock.getValueRecords() );
}
public Object getArrayFor( Iterable<DynamicRecord> records )
{
return getRightArray( arrayStore.readFullByteArray( records, PropertyType.ARRAY ) );
}
@Override
public String toString()
{
return super.toString() + "[blocksPerRecord:" + PropertyType.getPayloadSizeLongs() + "]";
}
public Collection<PropertyRecord> getPropertyRecordChain( long firstRecordId )
{
long nextProp = firstRecordId;
List<PropertyRecord> toReturn = new LinkedList<>();
while ( nextProp != Record.NO_NEXT_PROPERTY.intValue() )
{
PropertyRecord propRecord = new PropertyRecord( nextProp );
getRecord( nextProp, propRecord, RecordLoad.NORMAL );
toReturn.add( propRecord );
nextProp = propRecord.getNextProp();
}
return toReturn;
}
public Collection<PropertyRecord> getPropertyRecordChain( long firstRecordId,
PrimitiveLongObjectMap<PropertyRecord> propertyLookup )
{
long nextProp = firstRecordId;
List<PropertyRecord> toReturn = new ArrayList<>();
while ( nextProp != Record.NO_NEXT_PROPERTY.intValue() )
{
PropertyRecord propRecord = propertyLookup.get( nextProp );
if ( propRecord == null )
{
getRecord( nextProp, propRecord = newRecord(), RecordLoad.NORMAL );
}
toReturn.add( propRecord );
nextProp = propRecord.getNextProp();
}
return toReturn;
}
@Override
public PropertyRecord newRecord()
{
return new PropertyRecord( -1 );
}
}