-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PhysicalLogCommandReaderV3_0_2.java
790 lines (731 loc) · 30.9 KB
/
PhysicalLogCommandReaderV3_0_2.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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*
* 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.transaction.command;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMap;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException;
import org.neo4j.kernel.impl.index.IndexCommand;
import org.neo4j.kernel.impl.index.IndexCommand.AddNodeCommand;
import org.neo4j.kernel.impl.index.IndexCommand.AddRelationshipCommand;
import org.neo4j.kernel.impl.index.IndexCommand.CreateCommand;
import org.neo4j.kernel.impl.index.IndexCommand.DeleteCommand;
import org.neo4j.kernel.impl.index.IndexCommand.RemoveCommand;
import org.neo4j.kernel.impl.index.IndexDefineCommand;
import org.neo4j.kernel.impl.store.AbstractDynamicStore;
import org.neo4j.kernel.impl.store.PropertyType;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.LabelTokenRecord;
import org.neo4j.kernel.impl.store.record.NeoStoreRecord;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord;
import org.neo4j.kernel.impl.store.record.SchemaRuleSerialization;
import org.neo4j.kernel.impl.transaction.command.CommandReading.DynamicRecordAdder;
import org.neo4j.storageengine.api.ReadableChannel;
import org.neo4j.storageengine.api.schema.SchemaRule;
import static org.neo4j.helpers.Numbers.unsignedShortToInt;
import static org.neo4j.kernel.impl.transaction.command.CommandReading.COLLECTION_DYNAMIC_RECORD_ADDER;
import static org.neo4j.kernel.impl.transaction.command.CommandReading.PROPERTY_BLOCK_DYNAMIC_RECORD_ADDER;
import static org.neo4j.kernel.impl.transaction.command.CommandReading.PROPERTY_DELETED_DYNAMIC_RECORD_ADDER;
import static org.neo4j.kernel.impl.transaction.command.CommandReading.PROPERTY_INDEX_DYNAMIC_RECORD_ADDER;
import static org.neo4j.kernel.impl.util.Bits.bitFlag;
import static org.neo4j.kernel.impl.util.IoPrimitiveUtils.read2bLengthAndString;
import static org.neo4j.kernel.impl.util.IoPrimitiveUtils.read2bMap;
import static org.neo4j.kernel.impl.util.IoPrimitiveUtils.read3bLengthAndString;
public class PhysicalLogCommandReaderV3_0_2 extends BaseCommandReader
{
@Override
protected Command read( byte commandType, ReadableChannel channel ) throws IOException
{
switch ( commandType )
{
case NeoCommandType.NODE_COMMAND:
return visitNodeCommand( channel );
case NeoCommandType.PROP_COMMAND:
return visitPropertyCommand( channel );
case NeoCommandType.PROP_INDEX_COMMAND:
return visitPropertyKeyTokenCommand( channel );
case NeoCommandType.REL_COMMAND:
return visitRelationshipCommand( channel );
case NeoCommandType.REL_TYPE_COMMAND:
return visitRelationshipTypeTokenCommand( channel );
case NeoCommandType.LABEL_KEY_COMMAND:
return visitLabelTokenCommand( channel );
case NeoCommandType.NEOSTORE_COMMAND:
return visitNeoStoreCommand( channel );
case NeoCommandType.SCHEMA_RULE_COMMAND:
return visitSchemaRuleCommand( channel );
case NeoCommandType.REL_GROUP_COMMAND:
return visitRelationshipGroupCommand( channel );
case NeoCommandType.INDEX_DEFINE_COMMAND:
return visitIndexDefineCommand( channel );
case NeoCommandType.INDEX_ADD_COMMAND:
return visitIndexAddNodeCommand( channel );
case NeoCommandType.INDEX_ADD_RELATIONSHIP_COMMAND:
return visitIndexAddRelationshipCommand( channel );
case NeoCommandType.INDEX_REMOVE_COMMAND:
return visitIndexRemoveCommand( channel );
case NeoCommandType.INDEX_DELETE_COMMAND:
return visitIndexDeleteCommand( channel );
case NeoCommandType.INDEX_CREATE_COMMAND:
return visitIndexCreateCommand( channel );
case NeoCommandType.UPDATE_RELATIONSHIP_COUNTS_COMMAND:
return visitRelationshipCountsCommand( channel );
case NeoCommandType.UPDATE_NODE_COUNTS_COMMAND:
return visitNodeCountsCommand( channel );
default:
throw unknownCommandType( commandType, channel );
}
}
private static final class IndexCommandHeader
{
byte valueType;
byte entityType;
boolean entityIdNeedsLong;
int indexNameId;
boolean startNodeNeedsLong;
boolean endNodeNeedsLong;
int keyId;
IndexCommandHeader( byte valueType, byte entityType, boolean entityIdNeedsLong, int indexNameId,
boolean startNodeNeedsLong, boolean endNodeNeedsLong, int keyId )
{
this.valueType = valueType;
this.entityType = entityType;
this.entityIdNeedsLong = entityIdNeedsLong;
this.indexNameId = indexNameId;
this.startNodeNeedsLong = startNodeNeedsLong;
this.endNodeNeedsLong = endNodeNeedsLong;
this.keyId = keyId;
}
}
private Command visitNodeCommand( ReadableChannel channel ) throws IOException
{
long id = channel.getLong();
NodeRecord before = readNodeRecord( id, channel );
if ( before == null )
{
return null;
}
NodeRecord after = readNodeRecord( id, channel );
if ( after == null )
{
return null;
}
if ( !before.inUse() && after.inUse() )
{
after.setCreated();
}
return new Command.NodeCommand( before, after );
}
private Command visitRelationshipCommand( ReadableChannel channel ) throws IOException
{
long id = channel.getLong();
RelationshipRecord before = readRelationshipRecord( id, channel );
if ( before == null )
{
return null;
}
RelationshipRecord after = readRelationshipRecord( id, channel );
if ( after == null )
{
return null;
}
if ( !before.inUse() && after.inUse() )
{
after.setCreated();
}
return new Command.RelationshipCommand( before, after );
}
private Command visitPropertyCommand( ReadableChannel channel ) throws IOException
{
// ID
long id = channel.getLong(); // 8
// BEFORE
PropertyRecord before = readPropertyRecord( id, channel );
if ( before == null )
{
return null;
}
// AFTER
PropertyRecord after = readPropertyRecord( id, channel );
if ( after == null )
{
return null;
}
return new Command.PropertyCommand( before, after );
}
private Command visitRelationshipGroupCommand( ReadableChannel channel ) throws IOException
{
long id = channel.getLong();
RelationshipGroupRecord before = readRelationshipGroupRecord( id, channel );
RelationshipGroupRecord after = readRelationshipGroupRecord( id, channel );
return new Command.RelationshipGroupCommand( before, after );
}
private RelationshipGroupRecord readRelationshipGroupRecord( long id, ReadableChannel channel )
throws IOException
{
byte flags = channel.get();
boolean inUse = bitFlag( flags, Record.IN_USE.byteValue() );
boolean requireSecondaryUnit = bitFlag( flags, Record.REQUIRE_SECONDARY_UNIT );
boolean hasSecondaryUnit = bitFlag( flags, Record.HAS_SECONDARY_UNIT );
boolean usesFixedReferenceFormat = bitFlag( flags, Record.USES_FIXED_REFERENCE_FORMAT );
int type = unsignedShortToInt( channel.getShort() );
RelationshipGroupRecord record = new RelationshipGroupRecord( id, type );
record.setInUse( inUse );
record.setNext( channel.getLong() );
record.setFirstOut( channel.getLong() );
record.setFirstIn( channel.getLong() );
record.setFirstLoop( channel.getLong() );
record.setOwningNode( channel.getLong() );
record.setRequiresSecondaryUnit( requireSecondaryUnit );
if ( hasSecondaryUnit )
{
record.setSecondaryUnitId( channel.getLong() );
}
record.setUseFixedReferences( usesFixedReferenceFormat );
return record;
}
private Command visitRelationshipTypeTokenCommand( ReadableChannel channel ) throws IOException
{
int id = channel.getInt();
RelationshipTypeTokenRecord before = readRelationshipTypeTokenRecord( id, channel );
if ( before == null )
{
return null;
}
RelationshipTypeTokenRecord after = readRelationshipTypeTokenRecord( id, channel );
if ( after == null )
{
return null;
}
return new Command.RelationshipTypeTokenCommand( before, after );
}
private RelationshipTypeTokenRecord readRelationshipTypeTokenRecord( int id, ReadableChannel channel )
throws IOException
{
// in_use(byte)+type_blockId(int)+nr_type_records(int)
byte inUseFlag = channel.get();
boolean inUse = false;
if ( (inUseFlag & Record.IN_USE.byteValue()) == Record.IN_USE.byteValue() )
{
inUse = true;
}
else if ( inUseFlag != Record.NOT_IN_USE.byteValue() )
{
throw new IOException( "Illegal in use flag: " + inUseFlag );
}
RelationshipTypeTokenRecord record = new RelationshipTypeTokenRecord( id );
record.setInUse( inUse );
record.setNameId( channel.getInt() );
int nrTypeRecords = channel.getInt();
for ( int i = 0; i < nrTypeRecords; i++ )
{
DynamicRecord dr = readDynamicRecord( channel );
if ( dr == null )
{
return null;
}
record.addNameRecord( dr );
}
return record;
}
private Command visitLabelTokenCommand( ReadableChannel channel ) throws IOException
{
int id = channel.getInt();
LabelTokenRecord before = readLabelTokenRecord( id, channel );
if ( before == null )
{
return null;
}
LabelTokenRecord after = readLabelTokenRecord( id, channel );
if ( after == null )
{
return null;
}
return new Command.LabelTokenCommand( before, after );
}
private LabelTokenRecord readLabelTokenRecord( int id, ReadableChannel channel ) throws IOException
{
// in_use(byte)+type_blockId(int)+nr_type_records(int)
byte inUseFlag = channel.get();
boolean inUse = false;
if ( (inUseFlag & Record.IN_USE.byteValue()) == Record.IN_USE.byteValue() )
{
inUse = true;
}
else if ( inUseFlag != Record.NOT_IN_USE.byteValue() )
{
throw new IOException( "Illegal in use flag: " + inUseFlag );
}
LabelTokenRecord record = new LabelTokenRecord( id );
record.setInUse( inUse );
record.setNameId( channel.getInt() );
int nrTypeRecords = channel.getInt();
for ( int i = 0; i < nrTypeRecords; i++ )
{
DynamicRecord dr = readDynamicRecord( channel );
if ( dr == null )
{
return null;
}
record.addNameRecord( dr );
}
return record;
}
private Command visitPropertyKeyTokenCommand( ReadableChannel channel ) throws IOException
{
int id = channel.getInt();
PropertyKeyTokenRecord before = readPropertyKeyTokenRecord( id, channel );
if ( before == null )
{
return null;
}
PropertyKeyTokenRecord after = readPropertyKeyTokenRecord( id, channel );
if ( after == null )
{
return null;
}
return new Command.PropertyKeyTokenCommand( before, after );
}
private PropertyKeyTokenRecord readPropertyKeyTokenRecord( int id, ReadableChannel channel ) throws IOException
{
// in_use(byte)+count(int)+key_blockId(int)
byte inUseFlag = channel.get();
boolean inUse = false;
if ( (inUseFlag & Record.IN_USE.byteValue()) == Record.IN_USE.byteValue() )
{
inUse = true;
}
else if ( inUseFlag != Record.NOT_IN_USE.byteValue() )
{
throw new IOException( "Illegal in use flag: " + inUseFlag );
}
PropertyKeyTokenRecord record = new PropertyKeyTokenRecord( id );
record.setInUse( inUse );
record.setPropertyCount( channel.getInt() );
record.setNameId( channel.getInt() );
if ( readDynamicRecords( channel, record, PROPERTY_INDEX_DYNAMIC_RECORD_ADDER ) == -1 )
{
return null;
}
return record;
}
private Command visitSchemaRuleCommand( ReadableChannel channel ) throws IOException
{
Collection<DynamicRecord> recordsBefore = new ArrayList<>();
readDynamicRecords( channel, recordsBefore, COLLECTION_DYNAMIC_RECORD_ADDER );
Collection<DynamicRecord> recordsAfter = new ArrayList<>();
readDynamicRecords( channel, recordsAfter, COLLECTION_DYNAMIC_RECORD_ADDER );
byte isCreated = channel.get();
if ( 1 == isCreated )
{
for ( DynamicRecord record : recordsAfter )
{
record.setCreated();
}
}
SchemaRule rule = Iterables.first( recordsAfter ).inUse()
? readSchemaRule( recordsAfter )
: readSchemaRule( recordsBefore );
return new Command.SchemaRuleCommand( recordsBefore, recordsAfter, rule );
}
private Command visitNeoStoreCommand( ReadableChannel channel ) throws IOException
{
NeoStoreRecord before = readNeoStoreRecord( channel );
NeoStoreRecord after = readNeoStoreRecord( channel );
return new Command.NeoStoreCommand( before, after );
}
private NeoStoreRecord readNeoStoreRecord( ReadableChannel channel ) throws IOException
{
long nextProp = channel.getLong();
NeoStoreRecord record = new NeoStoreRecord();
record.setNextProp( nextProp );
return record;
}
private NodeRecord readNodeRecord( long id, ReadableChannel channel ) throws IOException
{
byte flags = channel.get();
boolean inUse = bitFlag( flags, Record.IN_USE.byteValue() );
boolean requiresSecondaryUnit = bitFlag( flags, Record.REQUIRE_SECONDARY_UNIT );
boolean hasSecondaryUnit = bitFlag( flags, Record.HAS_SECONDARY_UNIT );
boolean usesFixedReferenceFormat = bitFlag( flags, Record.USES_FIXED_REFERENCE_FORMAT );
NodeRecord record;
Collection<DynamicRecord> dynamicLabelRecords = new ArrayList<>();
long labelField = Record.NO_LABELS_FIELD.intValue();
if ( inUse )
{
boolean dense = channel.get() == 1;
record = new NodeRecord( id, dense, channel.getLong(), channel.getLong() );
// labels
labelField = channel.getLong();
record.setRequiresSecondaryUnit( requiresSecondaryUnit );
if ( hasSecondaryUnit )
{
record.setSecondaryUnitId( channel.getLong() );
}
record.setUseFixedReferences( usesFixedReferenceFormat );
}
else
{
record = new NodeRecord( id );
}
readDynamicRecords( channel, dynamicLabelRecords, COLLECTION_DYNAMIC_RECORD_ADDER );
record.setLabelField( labelField, dynamicLabelRecords );
record.setInUse( inUse );
return record;
}
private RelationshipRecord readRelationshipRecord( long id, ReadableChannel channel ) throws IOException
{
byte flags = channel.get();
boolean inUse = bitFlag( flags, Record.IN_USE.byteValue() );
boolean requiresSecondaryUnit = bitFlag( flags, Record.REQUIRE_SECONDARY_UNIT );
boolean hasSecondaryUnit = bitFlag( flags, Record.HAS_SECONDARY_UNIT );
boolean usesFixedReferenceFormat = bitFlag( flags, Record.USES_FIXED_REFERENCE_FORMAT );
RelationshipRecord record;
if ( inUse )
{
record = new RelationshipRecord( id, channel.getLong(), channel.getLong(), channel.getInt() );
record.setInUse( true );
record.setRequiresSecondaryUnit( requiresSecondaryUnit );
record.setFirstPrevRel( channel.getLong() );
record.setFirstNextRel( channel.getLong() );
record.setSecondPrevRel( channel.getLong() );
record.setSecondNextRel( channel.getLong() );
record.setNextProp( channel.getLong() );
byte extraByte = channel.get();
record.setFirstInFirstChain( (extraByte & 0x1) > 0 );
record.setFirstInSecondChain( (extraByte & 0x2) > 0 );
if ( hasSecondaryUnit )
{
record.setSecondaryUnitId( channel.getLong() );
}
record.setUseFixedReferences( usesFixedReferenceFormat );
}
else
{
record = new RelationshipRecord( id, -1, -1, channel.getInt() );
record.setInUse( false );
}
if ( bitFlag( flags, Record.CREATED_IN_TX ) )
{
record.setCreated();
}
return record;
}
private DynamicRecord readDynamicRecord( ReadableChannel channel ) throws IOException
{
// id+type+in_use(byte)+nr_of_bytes(int)+next_block(long)
long id = channel.getLong();
assert id >= 0 && id <= (1L << 36) - 1 : id + " is not a valid dynamic record id";
int type = channel.getInt();
byte inUseFlag = channel.get();
boolean inUse = (inUseFlag & Record.IN_USE.byteValue()) != 0;
DynamicRecord record = new DynamicRecord( id );
record.setInUse( inUse, type );
if ( inUse )
{
record.setStartRecord( (inUseFlag & Record.FIRST_IN_CHAIN.byteValue()) != 0 );
int nrOfBytes = channel.getInt();
assert nrOfBytes >= 0 && nrOfBytes < ((1 << 24) - 1) : nrOfBytes
+ " is not valid for a number of bytes field of " + "a dynamic record";
long nextBlock = channel.getLong();
assert (nextBlock >= 0 && nextBlock <= (1L << 36 - 1))
|| (nextBlock == Record.NO_NEXT_BLOCK.intValue()) : nextBlock
+ " is not valid for a next record field of " + "a dynamic record";
record.setNextBlock( nextBlock );
byte[] data = new byte[nrOfBytes];
channel.get( data, nrOfBytes );
record.setData( data );
}
return record;
}
private <T> int readDynamicRecords( ReadableChannel channel, T target, DynamicRecordAdder<T> adder )
throws IOException
{
int numberOfRecords = channel.getInt();
assert numberOfRecords >= 0;
while ( numberOfRecords > 0 )
{
DynamicRecord read = readDynamicRecord( channel );
if ( read == null )
{
return -1;
}
adder.add( target, read );
numberOfRecords--;
}
return numberOfRecords;
}
private PropertyRecord readPropertyRecord( long id, ReadableChannel channel ) throws IOException
{
// in_use(byte)+type(int)+key_indexId(int)+prop_blockId(long)+
// prev_prop_id(long)+next_prop_id(long)
PropertyRecord record = new PropertyRecord( id );
byte flags = channel.get(); // 1
boolean inUse = bitFlag( flags, Record.IN_USE.byteValue() );
boolean nodeProperty = !bitFlag( flags, Record.REL_PROPERTY.byteValue() );
boolean requireSecondaryUnit = bitFlag( flags, Record.REQUIRE_SECONDARY_UNIT );
boolean hasSecondaryUnit = bitFlag( flags, Record.HAS_SECONDARY_UNIT );
boolean usesFixedReferenceFormat = bitFlag( flags, Record.USES_FIXED_REFERENCE_FORMAT );
record.setRequiresSecondaryUnit( requireSecondaryUnit );
record.setUseFixedReferences( usesFixedReferenceFormat );
long nextProp = channel.getLong(); // 8
long prevProp = channel.getLong(); // 8
record.setNextProp( nextProp );
record.setPrevProp( prevProp );
long primitiveId = channel.getLong(); // 8
if ( primitiveId != -1 && nodeProperty )
{
record.setNodeId( primitiveId );
}
else if ( primitiveId != -1 )
{
record.setRelId( primitiveId );
}
if ( hasSecondaryUnit )
{
record.setSecondaryUnitId( channel.getLong() );
}
int nrPropBlocks = channel.get();
assert nrPropBlocks >= 0;
if ( nrPropBlocks > 0 )
{
record.setInUse( true );
}
while ( nrPropBlocks-- > 0 )
{
PropertyBlock block = readPropertyBlock( channel );
if ( block == null )
{
return null;
}
record.addPropertyBlock( block );
}
int deletedRecords = readDynamicRecords( channel, record, PROPERTY_DELETED_DYNAMIC_RECORD_ADDER );
if ( deletedRecords == -1 )
{
return null;
}
assert deletedRecords >= 0;
while ( deletedRecords-- > 0 )
{
DynamicRecord read = readDynamicRecord( channel );
if ( read == null )
{
return null;
}
record.addDeletedRecord( read );
}
if ( (inUse && !record.inUse()) || (!inUse && record.inUse()) )
{
throw new IllegalStateException( "Weird, inUse was read in as " + inUse + " but the record is " + record );
}
return record;
}
private PropertyBlock readPropertyBlock( ReadableChannel channel ) throws IOException
{
PropertyBlock toReturn = new PropertyBlock();
byte blockSize = channel.get(); // the size is stored in bytes // 1
assert blockSize > 0 && blockSize % 8 == 0 : blockSize + " is not a valid block size value";
// Read in blocks
long[] blocks = readLongs( channel, blockSize / 8 );
assert blocks.length == blockSize / 8 : blocks.length
+ " longs were read in while i asked for what corresponds to " + blockSize;
assert PropertyType.getPropertyTypeOrThrow( blocks[0] ).calculateNumberOfBlocksUsed(
blocks[0] ) == blocks.length : blocks.length + " is not a valid number of blocks for type "
+ PropertyType.getPropertyTypeOrThrow( blocks[0] );
/*
* Ok, now we may be ready to return, if there are no DynamicRecords. So
* we start building the Object
*/
toReturn.setValueBlocks( blocks );
/*
* Read in existence of DynamicRecords. Remember, this has already been
* read in the buffer with the blocks, above.
*/
if ( readDynamicRecords( channel, toReturn, PROPERTY_BLOCK_DYNAMIC_RECORD_ADDER ) == -1 )
{
return null;
}
return toReturn;
}
private long[] readLongs( ReadableChannel channel, int count ) throws IOException
{
long[] result = new long[count];
for ( int i = 0; i < count; i++ )
{
result[i] = channel.getLong();
}
return result;
}
private SchemaRule readSchemaRule( Collection<DynamicRecord> recordsBefore )
{
// TODO: Why was this assertion here?
// assert first(recordsBefore).inUse() : "Asked to deserialize schema records that were not in
// use.";
SchemaRule rule;
ByteBuffer deserialized = AbstractDynamicStore.concatData( recordsBefore, new byte[100] );
try
{
rule = SchemaRuleSerialization.deserialize( Iterables.first( recordsBefore ).getId(), deserialized );
}
catch ( MalformedSchemaRuleException e )
{
return null;
}
return rule;
}
private Command visitIndexAddNodeCommand( ReadableChannel channel ) throws IOException
{
IndexCommandHeader header = readIndexCommandHeader( channel );
Number entityId = header.entityIdNeedsLong ? channel.getLong() : channel.getInt();
Object value = readIndexValue( header.valueType, channel );
AddNodeCommand command = new AddNodeCommand();
command.init( header.indexNameId, entityId.longValue(), header.keyId, value );
return command;
}
private Command visitIndexAddRelationshipCommand( ReadableChannel channel ) throws IOException
{
IndexCommandHeader header = readIndexCommandHeader( channel );
Number entityId = header.entityIdNeedsLong ? channel.getLong() : channel.getInt();
Object value = readIndexValue( header.valueType, channel );
Number startNode = header.startNodeNeedsLong ? channel.getLong() : channel.getInt();
Number endNode = header.endNodeNeedsLong ? channel.getLong() : channel.getInt();
AddRelationshipCommand command = new AddRelationshipCommand();
command.init( header.indexNameId, entityId.longValue(), header.keyId, value, startNode.longValue(),
endNode.longValue() );
return command;
}
private Command visitIndexRemoveCommand( ReadableChannel channel ) throws IOException
{
IndexCommandHeader header = readIndexCommandHeader( channel );
Number entityId = header.entityIdNeedsLong ? channel.getLong() : channel.getInt();
Object value = readIndexValue( header.valueType, channel );
RemoveCommand command = new RemoveCommand();
command.init( header.indexNameId, header.entityType, entityId.longValue(), header.keyId, value );
return command;
}
private Command visitIndexDeleteCommand( ReadableChannel channel ) throws IOException
{
IndexCommandHeader header = readIndexCommandHeader( channel );
DeleteCommand command = new DeleteCommand();
command.init( header.indexNameId, header.entityType );
return command;
}
private Command visitIndexCreateCommand( ReadableChannel channel ) throws IOException
{
IndexCommandHeader header = readIndexCommandHeader( channel );
Map<String,String> config = read2bMap( channel );
CreateCommand command = new CreateCommand();
command.init( header.indexNameId, header.entityType, config );
return command;
}
private Command visitIndexDefineCommand( ReadableChannel channel ) throws IOException
{
readIndexCommandHeader( channel );
MutableObjectIntMap<String> indexNames = readMap( channel );
MutableObjectIntMap<String> keys = readMap( channel );
IndexDefineCommand command = new IndexDefineCommand();
command.init( indexNames, keys );
return command;
}
private Command visitNodeCountsCommand( ReadableChannel channel ) throws IOException
{
int labelId = channel.getInt();
long delta = channel.getLong();
return new Command.NodeCountsCommand( labelId, delta );
}
private Command visitRelationshipCountsCommand( ReadableChannel channel ) throws IOException
{
int startLabelId = channel.getInt();
int typeId = channel.getInt();
int endLabelId = channel.getInt();
long delta = channel.getLong();
return new Command.RelationshipCountsCommand( startLabelId, typeId, endLabelId, delta );
}
private MutableObjectIntMap<String> readMap( ReadableChannel channel ) throws IOException
{
int size = getUnsignedShort( channel );
MutableObjectIntMap<String> result = new ObjectIntHashMap<>( size );
for ( int i = 0; i < size; i++ )
{
String key = read2bLengthAndString( channel );
int id = getUnsignedShort( channel );
if ( key == null )
{
return null;
}
result.put( key, id );
}
return result;
}
private int getUnsignedShort( ReadableChannel channel ) throws IOException
{
int result = channel.getShort() & 0xFFFF;
return result == 0xFFFF ? -1 : result;
}
private IndexCommandHeader readIndexCommandHeader( ReadableChannel channel ) throws IOException
{
byte firstHeaderByte = channel.get();
byte valueType = (byte) ((firstHeaderByte & 0x1C) >> 2);
byte entityType = (byte) ((firstHeaderByte & 0x2) >> 1);
boolean entityIdNeedsLong = (firstHeaderByte & 0x1) > 0;
byte secondHeaderByte = channel.get();
boolean startNodeNeedsLong = (secondHeaderByte & 0x80) > 0;
boolean endNodeNeedsLong = (secondHeaderByte & 0x40) > 0;
int indexNameId = getUnsignedShort( channel );
int keyId = getUnsignedShort( channel );
return new IndexCommandHeader( valueType, entityType, entityIdNeedsLong, indexNameId, startNodeNeedsLong,
endNodeNeedsLong, keyId );
}
private Object readIndexValue( byte valueType, ReadableChannel channel ) throws IOException
{
switch ( valueType )
{
case IndexCommand.VALUE_TYPE_NULL:
return null;
case IndexCommand.VALUE_TYPE_SHORT:
return channel.getShort();
case IndexCommand.VALUE_TYPE_INT:
return channel.getInt();
case IndexCommand.VALUE_TYPE_LONG:
return channel.getLong();
case IndexCommand.VALUE_TYPE_FLOAT:
return channel.getFloat();
case IndexCommand.VALUE_TYPE_DOUBLE:
return channel.getDouble();
case IndexCommand.VALUE_TYPE_STRING:
return read3bLengthAndString( channel );
default:
throw new RuntimeException( "Unknown value type " + valueType );
}
}
}