/
PropertyStore.java
686 lines (623 loc) · 23.2 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/**
* Copyright (c) 2002-2015 "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.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.neo4j.helpers.Pair;
import org.neo4j.helpers.UTF8;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.helpers.collection.Visitor;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.IdGeneratorFactory;
import org.neo4j.kernel.IdType;
import org.neo4j.kernel.api.index.NodePropertyUpdate;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.api.index.PropertyPhysicalToLogicalConverter;
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.transaction.state.PropertyRecordChange;
import org.neo4j.kernel.impl.util.StringLogger;
import org.neo4j.kernel.monitoring.Monitors;
import static org.neo4j.helpers.collection.IteratorUtil.first;
import static org.neo4j.io.pagecache.PagedFile.PF_EXCLUSIVE_LOCK;
import static org.neo4j.io.pagecache.PagedFile.PF_SHARED_LOCK;
import static org.neo4j.kernel.impl.store.DynamicArrayStore.getRightArray;
/**
* 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 AbstractRecordStore<PropertyRecord> implements Store
{
public static abstract class Configuration extends AbstractStore.Configuration
{
}
public static final int DEFAULT_DATA_BLOCK_SIZE = 120;
public static final int DEFAULT_PAYLOAD_SIZE = 32;
public static final String TYPE_DESCRIPTOR = "PropertyStore";
public static final int RECORD_SIZE = 1/*next and prev high bits*/
+ 4/*next*/
+ 4/*prev*/
+ DEFAULT_PAYLOAD_SIZE /*property blocks*/;
// = 41
private DynamicStringStore stringPropertyStore;
private PropertyKeyTokenStore propertyKeyTokenStore;
private DynamicArrayStore arrayPropertyStore;
private final PropertyPhysicalToLogicalConverter physicalToLogicalConverter;
public PropertyStore(
File fileName,
Config configuration,
IdGeneratorFactory idGeneratorFactory,
PageCache pageCache,
FileSystemAbstraction fileSystemAbstraction,
StringLogger stringLogger,
DynamicStringStore stringPropertyStore,
PropertyKeyTokenStore propertyKeyTokenStore,
DynamicArrayStore arrayPropertyStore,
StoreVersionMismatchHandler versionMismatchHandler,
Monitors monitors )
{
super( fileName, configuration, IdType.PROPERTY, idGeneratorFactory, pageCache,
fileSystemAbstraction, stringLogger, versionMismatchHandler, monitors );
this.stringPropertyStore = stringPropertyStore;
this.propertyKeyTokenStore = propertyKeyTokenStore;
this.arrayPropertyStore = arrayPropertyStore;
this.physicalToLogicalConverter = new PropertyPhysicalToLogicalConverter( this );
}
@Override
public <FAILURE extends Exception> void accept( RecordStore.Processor<FAILURE> processor, PropertyRecord record )
throws FAILURE
{
processor.processProperty( this, record );
}
public DynamicStringStore getStringStore()
{
return stringPropertyStore;
}
public DynamicArrayStore getArrayStore()
{
return arrayPropertyStore;
}
@Override
protected void closeStorage()
{
if ( stringPropertyStore != null )
{
stringPropertyStore.close();
stringPropertyStore = null;
}
if ( propertyKeyTokenStore != null )
{
propertyKeyTokenStore.close();
propertyKeyTokenStore = null;
}
if ( arrayPropertyStore != null )
{
arrayPropertyStore.close();
arrayPropertyStore = null;
}
}
@Override
public String getTypeDescriptor()
{
return TYPE_DESCRIPTOR;
}
@Override
public int getRecordSize()
{
return RECORD_SIZE;
}
@Override
public int getRecordHeaderSize()
{
return RECORD_SIZE - DEFAULT_PAYLOAD_SIZE;
}
public PropertyKeyTokenStore getPropertyKeyTokenStore()
{
return propertyKeyTokenStore;
}
@Override
public void updateRecord( PropertyRecord record )
{
long pageId = pageIdForRecord( record.getId() );
updatePropertyBlocks( record );
try ( PageCursor cursor = storeFile.io( pageId, PF_EXCLUSIVE_LOCK ) )
{
if ( cursor.next() ) // should always be true
{
do
{
updateRecord( record, cursor );
} while ( cursor.shouldRetry() );
}
else
{
throw new UnderlyingStorageException(
"Could not pin page[" + pageId +
" exclusively for updateRecord: " + record );
}
}
catch ( IOException e )
{
throw new UnderlyingStorageException( e );
}
}
@Override
public void forceUpdateRecord( PropertyRecord record )
{
updateRecord( record ); // TODO: should we do something special for property records?
}
private void updateRecord( PropertyRecord record, PageCursor cursor )
{
long id = record.getId();
cursor.setOffset( (int) (id * RECORD_SIZE % storeFile.pageSize()) );
if ( record.inUse() )
{
// Set up the record header
short prevModifier = record.getPrevProp() == Record.NO_NEXT_RELATIONSHIP.intValue() ? 0
: (short) ( ( record.getPrevProp() & 0xF00000000L ) >> 28 );
short nextModifier = record.getNextProp() == Record.NO_NEXT_RELATIONSHIP.intValue() ? 0
: (short) ( ( record.getNextProp() & 0xF00000000L ) >> 32 );
byte modifiers = (byte) ( prevModifier | nextModifier );
/*
* [pppp,nnnn] previous, next high bits
*/
cursor.putByte( modifiers );
cursor.putInt( (int) record.getPrevProp() );
cursor.putInt( (int) record.getNextProp() );
// Then go through the blocks
int longsAppended = 0; // For marking the end of blocks
for ( PropertyBlock block : record )
{
long[] propBlockValues = block.getValueBlocks();
for ( long propBlockValue : propBlockValues )
{
cursor.putLong( propBlockValue );
}
longsAppended += propBlockValues.length;
}
if ( longsAppended < PropertyType.getPayloadSizeLongs() )
{
cursor.putLong( 0 );
}
}
else
{
freeId( id );
// skip over the record header, nothing useful there
cursor.setOffset( cursor.getOffset() + 9 );
cursor.putLong( 0 );
}
}
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 )
{
if ( valueRecord.getType() == PropertyType.STRING.intValue() )
{
stringPropertyStore.updateRecord( valueRecord );
}
else if ( valueRecord.getType() == PropertyType.ARRAY.intValue() )
{
arrayPropertyStore.updateRecord( valueRecord );
}
else
{
throw new InvalidRecordException( "Unknown dynamic record"
+ valueRecord );
}
}
}
public PropertyRecord getLightRecord( long id )
{
return getRecord( id );
}
public void ensureHeavy( PropertyBlock block )
{
if ( block.getType() == PropertyType.STRING )
{
if ( block.isLight() )
{
Collection<DynamicRecord> stringRecords = stringPropertyStore.getLightRecords( block.getSingleValueLong() );
for ( DynamicRecord stringRecord : stringRecords )
{
stringRecord.setType( PropertyType.STRING.intValue() );
block.addValueRecord( stringRecord );
}
}
for ( DynamicRecord stringRecord : block.getValueRecords() )
{
stringPropertyStore.ensureHeavy( stringRecord );
}
}
else if ( block.getType() == PropertyType.ARRAY )
{
if ( block.isLight() )
{
Collection<DynamicRecord> arrayRecords = arrayPropertyStore.getLightRecords( block.getSingleValueLong() );
for ( DynamicRecord arrayRecord : arrayRecords )
{
arrayRecord.setType( PropertyType.ARRAY.intValue() );
block.addValueRecord( arrayRecord );
}
}
for ( DynamicRecord arrayRecord : block.getValueRecords() )
{
arrayPropertyStore.ensureHeavy( arrayRecord );
}
}
}
@Override
public PropertyRecord getRecord( long id )
{
try ( PageCursor cursor = storeFile.io( pageIdForRecord( id ), PF_SHARED_LOCK ) )
{
PropertyRecord record = null;
if ( cursor.next() )
{
do
{
record = getRecord( id, cursor );
} while ( cursor.shouldRetry() );
}
if ( record == null || !record.inUse() )
{
throw new InvalidRecordException( "PropertyRecord[" + id + "] not in use" );
}
return record;
}
catch ( IOException e )
{
throw new UnderlyingStorageException( e );
}
}
@Override
public PropertyRecord forceGetRecord( long id )
{
try ( PageCursor cursor = storeFile.io( pageIdForRecord( id ), PF_SHARED_LOCK ) )
{
PropertyRecord record = null;
if ( cursor.next() )
{
do
{
record = getRecord( id, cursor );
} while ( cursor.shouldRetry() );
}
return record == null? new PropertyRecord( id ) : record;
}
catch ( IOException e )
{
throw new UnderlyingStorageException( e );
}
}
@Override
public PropertyRecord forceGetRaw( PropertyRecord record )
{
return record;
}
@Override
public PropertyRecord forceGetRaw( long id )
{
return forceGetRecord( id );
}
private PropertyRecord getRecordFromBuffer( long id, PageCursor cursor )
{
int offsetAtBeginning = cursor.getOffset();
PropertyRecord record = new PropertyRecord( id );
/*
* [pppp,nnnn] previous, next high bits
*/
byte modifiers = cursor.getByte();
long prevMod = ( modifiers & 0xF0L ) << 28;
long nextMod = ( modifiers & 0x0FL ) << 32;
long prevProp = cursor.getUnsignedInt();
long nextProp = cursor.getUnsignedInt();
record.setPrevProp( longFromIntAndMod( prevProp, prevMod ) );
record.setNextProp( longFromIntAndMod( nextProp, nextMod ) );
while ( cursor.getOffset() - offsetAtBeginning < RECORD_SIZE )
{
PropertyBlock newBlock = getPropertyBlock( cursor );
if ( newBlock != null )
{
record.addPropertyBlock( newBlock );
record.setInUse( true );
}
else
{
// We assume that storage is defragged
break;
}
}
return record;
}
private PropertyRecord getRecord( long id, PageCursor cursor )
{
cursor.setOffset( (int) (id * RECORD_SIZE % storeFile.pageSize()) );
return getRecordFromBuffer( id, cursor );
}
/*
* It is assumed that the argument does hold a property block - all zeros is
* a valid (not in use) block, so even if the Bits object has been exhausted a
* result is returned, that has inUse() return false. Also, the argument is not
* touched.
*/
private PropertyBlock getPropertyBlock( PageCursor cursor )
{
long header = cursor.getLong();
PropertyType type = PropertyType.getPropertyType( header, true );
if ( type == null )
{
return null;
}
PropertyBlock toReturn = new PropertyBlock();
// toReturn.setInUse( true );
int numBlocks = type.calculateNumberOfBlocksUsed( header );
long[] blockData = new long[numBlocks];
blockData[0] = header; // we already have that
for ( int i = 1; i < numBlocks; i++ )
{
blockData[i] = cursor.getLong();
}
toReturn.setValueBlocks( blockData );
return toReturn;
}
public Object getValue( PropertyBlock propertyBlock )
{
return propertyBlock.getType().getValue( propertyBlock, this );
}
@Override
public void makeStoreOk()
{
propertyKeyTokenStore.makeStoreOk();
stringPropertyStore.makeStoreOk();
arrayPropertyStore.makeStoreOk();
super.makeStoreOk();
}
@Override
public void visitStore( Visitor<CommonAbstractStore, RuntimeException> visitor )
{
propertyKeyTokenStore.visitStore( visitor );
stringPropertyStore.visitStore( visitor );
arrayPropertyStore.visitStore( visitor );
visitor.visit( this );
}
public static void allocateStringRecords( Collection<DynamicRecord> target, byte[] chars,
DynamicRecordAllocator allocator )
{
AbstractDynamicStore.allocateRecordsFromBytes( target, chars,
IteratorUtil.<DynamicRecord>emptyIterator(), allocator );
}
public static void allocateArrayRecords( Collection<DynamicRecord> target, Object array,
DynamicRecordAllocator allocator )
{
DynamicArrayStore.allocateRecords( target, array, IteratorUtil.<DynamicRecord>emptyIterator(), allocator );
}
public void encodeValue( PropertyBlock block, int keyId, Object value )
{
encodeValue( block, keyId, value, stringPropertyStore, arrayPropertyStore );
}
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, 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()) << 24);
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()) << 24),
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, 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() );
}
}
private 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()) << 24) | (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 = stringPropertyStore.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( arrayPropertyStore.readFullByteArray( records, PropertyType.ARRAY ) );
}
public int getStringBlockSize()
{
return stringPropertyStore.getBlockSize();
}
public int getArrayBlockSize()
{
return arrayPropertyStore.getBlockSize();
}
@Override
public void logVersions(StringLogger.LineLogger logger )
{
super.logVersions( logger );
propertyKeyTokenStore.logVersions( logger );
stringPropertyStore.logVersions( logger );
arrayPropertyStore.logVersions(logger );
}
@Override
public void logIdUsage(StringLogger.LineLogger logger )
{
super.logIdUsage(logger);
propertyKeyTokenStore.logIdUsage( logger );
stringPropertyStore.logIdUsage( logger );
arrayPropertyStore.logIdUsage( logger );
}
@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 = getLightRecord( nextProp );
toReturn.add(propRecord);
nextProp = propRecord.getNextProp();
}
return toReturn;
}
public void toLogicalUpdates( Collection<NodePropertyUpdate> target,
Iterable<PropertyRecordChange> changes,
long[] nodeLabelsBefore,
long[] nodeLabelsAfter )
{
physicalToLogicalConverter.apply( target, changes, nodeLabelsBefore, nodeLabelsAfter );
}
/**
* For property records there's no "inUse" byte and we need to read the whole record to
* see if there are any PropertyBlocks in use in it.
*/
@Override
protected boolean isRecordInUse( PageCursor cursor )
{
return getRecordFromBuffer( 0 /*id doesn't matter here*/, cursor ).inUse();
}
}