-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
GenericKeyState.java
1971 lines (1819 loc) · 64.8 KB
/
GenericKeyState.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.index.schema;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.impl.index.schema.GenericLayout.Type;
import org.neo4j.kernel.impl.store.TemporalValueWriterAdapter;
import org.neo4j.string.UTF8;
import org.neo4j.values.storable.BooleanValue;
import org.neo4j.values.storable.DateTimeValue;
import org.neo4j.values.storable.DateValue;
import org.neo4j.values.storable.DurationValue;
import org.neo4j.values.storable.LocalDateTimeValue;
import org.neo4j.values.storable.LocalTimeValue;
import org.neo4j.values.storable.NumberValue;
import org.neo4j.values.storable.PrimitiveArrayWriting;
import org.neo4j.values.storable.TimeValue;
import org.neo4j.values.storable.TimeZones;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.Values;
import static java.lang.Integer.min;
import static org.neo4j.io.pagecache.PageCache.PAGE_SIZE;
import static org.neo4j.kernel.impl.index.schema.DurationIndexKey.AVG_DAY_SECONDS;
import static org.neo4j.kernel.impl.index.schema.DurationIndexKey.AVG_MONTH_SECONDS;
import static org.neo4j.kernel.impl.index.schema.GenericLayout.HIGHEST_TYPE_BY_VALUE_GROUP;
import static org.neo4j.kernel.impl.index.schema.GenericLayout.LOWEST_TYPE_BY_VALUE_GROUP;
import static org.neo4j.kernel.impl.index.schema.NativeIndexKey.Inclusion.HIGH;
import static org.neo4j.kernel.impl.index.schema.NativeIndexKey.Inclusion.LOW;
import static org.neo4j.kernel.impl.index.schema.NativeIndexKey.Inclusion.NEUTRAL;
import static org.neo4j.kernel.impl.index.schema.StringIndexKey.lexicographicalUnsignedByteArrayCompare;
import static org.neo4j.kernel.impl.index.schema.ZonedDateTimeLayout.ZONE_ID_FLAG;
import static org.neo4j.kernel.impl.index.schema.ZonedDateTimeLayout.ZONE_ID_MASK;
import static org.neo4j.kernel.impl.index.schema.ZonedDateTimeLayout.asZoneId;
import static org.neo4j.kernel.impl.index.schema.ZonedDateTimeLayout.asZoneOffset;
import static org.neo4j.kernel.impl.index.schema.ZonedDateTimeLayout.isZoneId;
import static org.neo4j.values.storable.Values.NO_VALUE;
class GenericKeyState extends TemporalValueWriterAdapter<RuntimeException>
{
/**
* This is the biggest size a static (as in non-dynamic, like string), non-array value can have.
*/
static final int BIGGEST_STATIC_SIZE = Long.BYTES * 4; // long0, long1, long2, long3
// TODO copy-pasted from individual keys
// TODO also put this in Type enum
private static final int SIZE_ZONED_DATE_TIME = Long.BYTES + /* epochSecond */
Integer.BYTES + /* nanoOfSecond */
Integer.BYTES; /* timeZone */
private static final int SIZE_LOCAL_DATE_TIME = Long.BYTES + /* epochSecond */
Integer.BYTES; /* nanoOfSecond */
private static final int SIZE_DATE = Long.BYTES; /* epochDay */
private static final int SIZE_ZONED_TIME = Long.BYTES + /* nanosOfDayUTC */
Integer.BYTES; /* zoneOffsetSeconds */
private static final int SIZE_LOCAL_TIME = Long.BYTES; /* nanoOfDay */
private static final int SIZE_DURATION = Long.BYTES + /* totalAvgSeconds */
Integer.BYTES + /* nanosOfSecond */
Long.BYTES + /* months */
Long.BYTES; /* days */
private static final int SIZE_BOOLEAN = Byte.BYTES; /* byte for this boolean value */
private static final int SIZE_NUMBER_TYPE = Byte.BYTES; /* type of value */
private static final int SIZE_NUMBER = Long.BYTES; /* raw value bits */
private static final int SIZE_ARRAY_LENGTH = Integer.BYTES;
private static final int BIGGEST_REASONABLE_ARRAY_LENGTH = PAGE_SIZE / 2 / Integer.SIZE;
private static final long TRUE = 1;
private static final long FALSE = 0;
private static final int TYPE_ID_SIZE = Byte.BYTES;
Type type;
NativeIndexKey.Inclusion inclusion;
private boolean isArray;
private int arrayLength;
private int currentArrayOffset;
// zoned date time: long0 (epochSecondUTC), long1 (nanoOfSecond), long2 (zoneId), long3 (zoneOffsetSeconds)
// local date time: long0 (nanoOfSecond), long1 (epochSecond)
// date: long0 (epochDay)
// zoned time: long0 (nanosOfDayUTC), long1 (zoneOffsetSeconds)
// local time: long0 (nanoOfDay)
// duration: long0 (totalAvgSeconds), long1 (nanosOfSecond), long2 (months), long3 (days)
// text: long0 (length), long1 (bytesDereferenced), long2 (ignoreLength), long3 (isHighest), byteArray
// boolean: long0
// number: long0 (value), long1 (number type)
// TODO spatial
// for non-array values
private long long0;
private long long1;
private long long2;
private long long3;
private byte[] byteArray;
// for array values
private long[] long0Array;
private long[] long1Array;
private long[] long2Array;
private long[] long3Array;
private byte[][] byteArrayArray;
/* <initializers> */
void clear()
{
type = null;
long0 = 0;
long1 = 0;
long2 = 0;
long3 = 0;
inclusion = NEUTRAL;
isArray = false;
arrayLength = 0;
currentArrayOffset = 0;
}
void initializeToDummyValue()
{
clear();
writeInteger( 0 );
inclusion = NEUTRAL;
}
// todo is this simple lowest approach viable? Probably not when including arrays
// todo not for arrays most likely
void initValueAsLowest( ValueGroup valueGroup )
{
type = valueGroup == ValueGroup.UNKNOWN ? LOWEST_TYPE_BY_VALUE_GROUP : GenericLayout.TYPE_BY_GROUP[valueGroup.ordinal()];
long0 = Long.MIN_VALUE;
long1 = Long.MIN_VALUE;
long2 = Long.MIN_VALUE;
long3 = Long.MIN_VALUE;
byteArray = null;
if ( type == Type.TEXT )
{
long3 = FALSE;
}
inclusion = LOW;
}
// todo is this simple highest approach viable? Probably not when including arrays
// todo not for arrays most likely
void initValueAsHighest( ValueGroup valueGroup )
{
type = valueGroup == ValueGroup.UNKNOWN ? HIGHEST_TYPE_BY_VALUE_GROUP : GenericLayout.TYPE_BY_GROUP[valueGroup.ordinal()];
long0 = Long.MAX_VALUE;
long1 = Long.MAX_VALUE;
long2 = Long.MAX_VALUE;
long3 = Long.MAX_VALUE;
byteArray = null;
if ( type == Type.TEXT )
{
long3 = TRUE;
}
inclusion = HIGH;
}
void initAsPrefixLow( String prefix )
{
writeString( prefix );
long2 = FALSE;
inclusion = LOW;
// Don't set ignoreLength = true here since the "low" a.k.a. left side of the range should care about length.
// This will make the prefix lower than those that matches the prefix (their length is >= that of the prefix)
}
void initAsPrefixHigh( String prefix )
{
writeString( prefix );
long2 = TRUE; // ignoreLength
inclusion = HIGH;
}
/* </initializers> */
/* <copyFrom> */
void copyFrom( GenericKeyState key )
{
copyMetaFrom( key );
if ( !key.isArray )
{
this.long0 = key.long0;
this.long1 = key.long1;
this.long2 = key.long2;
this.long3 = key.long3;
this.copyByteArrayFromIfExists( key, (int) key.long0 );
}
else
{
this.arrayLength = key.arrayLength;
this.currentArrayOffset = key.currentArrayOffset;
switch ( key.type )
{
case ZONED_DATE_TIME_ARRAY:
copyZonedDateTimeArrayFrom( key, key.arrayLength );
break;
case LOCAL_DATE_TIME_ARRAY:
copyLocalDateTimeArrayFrom( key, key.arrayLength );
break;
case DATE_ARRAY:
copyDateArrayFrom( key, key.arrayLength );
break;
case ZONED_TIME_ARRAY:
copyZonedTimeArrayFrom( key, key.arrayLength );
break;
case LOCAL_TIME_ARRAY:
copyLocalTimeArrayFrom( key, key.arrayLength );
break;
case DURATION_ARRAY:
copyDurationArrayFrom( key, key.arrayLength );
break;
case TEXT_ARRAY:
copyTextArrayFrom( key, key.arrayLength );
break;
case BOOLEAN_ARRAY:
copyBooleanArrayFrom( key, key.arrayLength );
break;
case NUMBER_ARRAY:
copyNumberArrayFrom( key, key.arrayLength );
break;
default:
throw new IllegalStateException( "Expected an array type but was " + type );
}
}
}
private void copyMetaFrom( GenericKeyState key )
{
this.type = key.type;
this.inclusion = key.inclusion;
this.isArray = key.isArray;
}
private void copyByteArrayFromIfExists( GenericKeyState key, int targetLength )
{
if ( key.type == Type.TEXT )
{
setBytesLength( targetLength );
System.arraycopy( key.byteArray, 0, byteArray, 0, targetLength );
}
else
{
byteArray = null;
}
}
/* </copyFrom> */
static Value assertCorrectType( Value value )
{
if ( Values.isGeometryValue( value ) )
{
throw new IllegalArgumentException( "Unsupported value " + value );
}
return value;
}
void writeValue( Value value, NativeIndexKey.Inclusion inclusion )
{
value.writeTo( this );
this.inclusion = inclusion;
}
int compareValueTo( GenericKeyState other )
{
if ( type == null )
{
return -1;
}
else if ( other.type == null )
{
return 1;
}
int typeComparison = GenericLayout.TYPE_COMPARATOR.compare( type, other.type );
if ( typeComparison != 0 )
{
return typeComparison;
}
int valueComparison = internalCompareValueTo( other );
if ( valueComparison != 0 )
{
return valueComparison;
}
return inclusion.compareTo( other.inclusion );
}
/* <minimalSplitter> */
public static void minimalSplitter( GenericKeyState left, GenericKeyState right, GenericKeyState into )
{
into.clear();
into.copyMetaFrom( right );
switch ( right.type )
{
case TEXT:
minimalSplitterText( left, right, into );
break;
case ZONED_DATE_TIME_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareZonedDateTime(
o1.long0Array[i], o1.long1Array[i], o1.long2Array[i], o1.long3Array[i],
o2.long0Array[i], o2.long1Array[i], o2.long2Array[i], o2.long3Array[i] ),
GenericKeyState::copyZonedDateTimeArrayFrom );
break;
case LOCAL_DATE_TIME_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareLocalDateTime(
o1.long0Array[i], o1.long1Array[i],
o2.long0Array[i], o2.long1Array[i] ),
GenericKeyState::copyLocalDateTimeArrayFrom );
break;
case DATE_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareDate(
o1.long0Array[i],
o2.long0Array[i] ),
GenericKeyState::copyDateArrayFrom );
break;
case ZONED_TIME_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareZonedTime(
o1.long0Array[i], o1.long1Array[i],
o2.long0Array[i], o2.long1Array[i] ),
GenericKeyState::copyZonedTimeArrayFrom );
break;
case LOCAL_TIME_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareLocalTime(
o1.long0Array[i],
o2.long0Array[i] ),
GenericKeyState::copyLocalTimeArrayFrom );
break;
case DURATION_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareDuration(
o1.long0Array[i], o1.long1Array[i], o1.long2Array[i], o1.long3Array[i],
o2.long0Array[i], o2.long1Array[i], o2.long2Array[i], o2.long3Array[i] ),
GenericKeyState::copyDurationArrayFrom );
break;
case TEXT_ARRAY:
// todo continue here! Nested minimal splitter for array of text
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareText(
o1.byteArrayArray[i], o1.long0Array[i], o1.long2, o1.long3,
o2.byteArrayArray[i], o2.long0Array[i], o2.long2, o2.long3 ),
GenericKeyState::copyTextArrayFrom );
break;
case BOOLEAN_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareBoolean(
o1.long0Array[i],
o2.long0Array[i] ),
GenericKeyState::copyBooleanArrayFrom );
break;
case NUMBER_ARRAY:
minimalSplitterArray( left, right, into, ( o1, o2, i ) -> compareNumber(
o1.long0Array[i], o1.long1,
o2.long0Array[i], o2.long1 ),
GenericKeyState::copyNumberArrayFrom );
break;
default:
// if not text or array, just copy from 'right'
into.copyFrom( right );
}
}
private static void minimalSplitterArray( GenericKeyState left, GenericKeyState right, GenericKeyState into,
ArrayElementComparator comparator, ArrayCopier arrayCopier )
{
int index = 0;
if ( left.type == right.type )
{
int compare = 0;
int length = min( left.arrayLength, right.arrayLength );
for ( ; compare == 0 && index < length; index++ )
{
compare = comparator.compare( left, right, index );
}
}
arrayCopier.copyArray( into, right, index );
}
private static void minimalSplitterText( GenericKeyState left, GenericKeyState right, GenericKeyState into )
{
int length = 0;
if ( left.type == Type.TEXT )
{
length = StringLayout.firstPosToDiffer( left.byteArray, (int) left.long0, right.byteArray, (int) right.long0 );
}
into.writeUTF8( right.byteArray, 0, length );
}
/* </minimalSplitter> */
/* <size> */
int size()
{
return valueSize() + TYPE_ID_SIZE;
}
private int valueSize()
{
switch ( type )
{
case ZONED_DATE_TIME:
return SIZE_ZONED_DATE_TIME;
case LOCAL_DATE_TIME:
return SIZE_LOCAL_DATE_TIME;
case DATE:
return SIZE_DATE;
case ZONED_TIME:
return SIZE_ZONED_TIME;
case LOCAL_TIME:
return SIZE_LOCAL_TIME;
case DURATION:
return SIZE_DURATION;
case TEXT:
return Short.BYTES + /* short field with bytesLength value */
(int) long0; /* bytesLength */
case BOOLEAN:
return SIZE_BOOLEAN;
case NUMBER:
return SIZE_NUMBER + SIZE_NUMBER_TYPE;
case ZONED_DATE_TIME_ARRAY:
return arrayKeySize( SIZE_ZONED_DATE_TIME );
case LOCAL_DATE_TIME_ARRAY:
return arrayKeySize( SIZE_LOCAL_DATE_TIME );
case DATE_ARRAY:
return arrayKeySize( SIZE_DATE );
case ZONED_TIME_ARRAY:
return arrayKeySize( SIZE_ZONED_TIME );
case LOCAL_TIME_ARRAY:
return arrayKeySize( SIZE_LOCAL_TIME );
case DURATION_ARRAY:
return arrayKeySize( SIZE_DURATION );
case TEXT_ARRAY:
int stringArraySize = 0;
for ( int i = 0; i < arrayLength; i++ )
{
stringArraySize += Short.BYTES + /* short field with bytesLength value */
(int) long0Array[i]; /* bytesLength */
}
return SIZE_ARRAY_LENGTH + stringArraySize;
case BOOLEAN_ARRAY:
return arrayKeySize( SIZE_BOOLEAN );
case NUMBER_ARRAY:
return arrayKeySize( SIZE_NUMBER ) + SIZE_NUMBER_TYPE;
default:
throw new IllegalArgumentException( "Unknown type " + type );
}
}
private int arrayKeySize( int elementSize )
{
return SIZE_ARRAY_LENGTH + arrayLength * elementSize;
}
/* </size> */
/* <asValue> */
Value asValue()
{
switch ( type )
{
case ZONED_DATE_TIME:
return zonedDateTimeAsValue( long0, long1, long2, long3 );
case LOCAL_DATE_TIME:
return localDateTimeAsValue( long0, long1 );
case DATE:
return dateAsValue( long0 );
case ZONED_TIME:
return zonedTimeAsValue( long0, long1 );
case LOCAL_TIME:
return localTimeAsValue( long0 );
case DURATION:
return durationAsValue( long0, long1, long2, long3 );
case TEXT:
long1 = TRUE; // bytes dereferenced
return textAsValue( byteArray, long0 );
case BOOLEAN:
return booleanAsValue( long0 );
case NUMBER:
return numberAsValue( long0, long1 );
case ZONED_DATE_TIME_ARRAY:
return Values.of( populateValueArray( new ZonedDateTime[arrayLength],
i -> zonedDateTimeAsValueRaw( long0Array[i], long1Array[i], long2Array[i], long3Array[i] ) ) );
case LOCAL_DATE_TIME_ARRAY:
return Values.of( populateValueArray( new LocalDateTime[arrayLength], i -> localDateTimeAsValueRaw( long0Array[i], long1Array[i] ) ) );
case DATE_ARRAY:
return Values.of( populateValueArray( new LocalDate[arrayLength], i -> dateAsValueRaw( long0Array[i] ) ) );
case ZONED_TIME_ARRAY:
return Values.of( populateValueArray( new OffsetTime[arrayLength], i -> zonedTimeAsValueRaw( long0Array[i], long1Array[i] ) ) );
case LOCAL_TIME_ARRAY:
return Values.of( populateValueArray( new LocalTime[arrayLength], i -> localTimeAsValueRaw( long0Array[i] ) ) );
case DURATION_ARRAY:
return Values.of( populateValueArray( new DurationValue[arrayLength],
i -> durationAsValue( long0Array[i], long1Array[i], long2Array[i], long3Array[i] ) ) );
case TEXT_ARRAY:
// no need to set bytes dereferenced because byte[][] owned by this class will deserialized into String objects.
return Values.of( populateValueArray( new String[arrayLength], i -> textAsValueRaw( byteArrayArray[i], long0Array[i] ) ) );
case BOOLEAN_ARRAY:
return booleanArrayAsValue();
case NUMBER_ARRAY:
return numberArrayAsValue();
default:
throw new IllegalArgumentException( "Unknown type " + type );
}
}
private Value numberArrayAsValue()
{
byte numberType = (byte) this.long1;
switch ( numberType )
{
case RawBits.BYTE:
byte[] byteArray = new byte[arrayLength];
for ( int i = 0; i < arrayLength; i++ )
{
byteArray[i] = (byte) long0Array[i];
}
return Values.byteArray( byteArray );
case RawBits.SHORT:
short[] shortArray = new short[arrayLength];
for ( int i = 0; i < arrayLength; i++ )
{
shortArray[i] = (short) long0Array[i];
}
return Values.shortArray( shortArray );
case RawBits.INT:
int[] intArray = new int[arrayLength];
for ( int i = 0; i < arrayLength; i++ )
{
intArray[i] = (int) long0Array[i];
}
return Values.intArray( intArray );
case RawBits.LONG:
return Values.longArray( Arrays.copyOf( long0Array, arrayLength ) );
case RawBits.FLOAT:
float[] floatArray = new float[arrayLength];
for ( int i = 0; i < arrayLength; i++ )
{
floatArray[i] = Float.intBitsToFloat( (int) long0Array[i] );
}
return Values.floatArray( floatArray );
case RawBits.DOUBLE:
double[] doubleArray = new double[arrayLength];
for ( int i = 0; i < arrayLength; i++ )
{
doubleArray[i] = Double.longBitsToDouble( long0Array[i] );
}
return Values.doubleArray( doubleArray );
default:
throw new IllegalArgumentException( "Unknown number type " + numberType );
}
}
private Value booleanArrayAsValue()
{
boolean[] array = new boolean[arrayLength];
for ( int i = 0; i < arrayLength; i++ )
{
array[i] = booleanAsValueRaw( long0Array[i] );
}
return Values.of( array );
}
private <T> T[] populateValueArray( T[] array, ArrayElementValueFactory<T> valueFactory )
{
for ( int i = 0; i < arrayLength; i++ )
{
array[i] = valueFactory.from( i );
}
return array;
}
/* </asValue> */
/* <compare> */
private int internalCompareValueTo( GenericKeyState that )
{
switch ( type )
{
case ZONED_DATE_TIME:
return compareZonedDateTime(
this.long0, this.long1, this.long2, this.long3,
that.long0, that.long1, that.long2, that.long3 );
case LOCAL_DATE_TIME:
return compareLocalDateTime(
this.long0, this.long1,
that.long0, that.long1 );
case DATE:
return compareDate(
this.long0,
that.long0 );
case ZONED_TIME:
return compareZonedTime(
this.long0, this.long1,
that.long0, that.long1 );
case LOCAL_TIME:
return compareLocalTime(
this.long0,
that.long0 );
case DURATION:
return compareDuration(
this.long0, this.long1, this.long2, this.long3,
that.long0, that.long1, that.long2, that.long3 );
case TEXT:
return compareText(
this.byteArray, this.long0, this.long2, this.long3,
that.byteArray, that.long0, that.long2, that.long3 );
case BOOLEAN:
return compareBoolean(
this.long0,
that.long0 );
case NUMBER:
return compareNumber(
this.long0, this.long1,
that.long0, that.long1 );
case ZONED_DATE_TIME_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareZonedDateTime(
o1.long0Array[i], o1.long1Array[i], o1.long2Array[i], o1.long3Array[i],
o2.long0Array[i], o2.long1Array[i], o2.long2Array[i], o2.long3Array[i] ) );
case LOCAL_DATE_TIME_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareLocalDateTime(
o1.long0Array[i], o1.long1Array[i],
o2.long0Array[i], o2.long1Array[i] ) );
case DATE_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareDate(
o1.long0Array[i],
o2.long0Array[i] ) );
case ZONED_TIME_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareZonedTime(
o1.long0Array[i], o1.long1Array[i],
o2.long0Array[i], o2.long1Array[i] ) );
case LOCAL_TIME_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareLocalTime(
o1.long0Array[i],
o2.long0Array[i] ) );
case DURATION_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareDuration(
o1.long0Array[i], o1.long1Array[i], o1.long2Array[i], o1.long3Array[i],
o2.long0Array[i], o2.long1Array[i], o2.long2Array[i], o2.long3Array[i] ) );
case TEXT_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareText(
o1.byteArrayArray[i], o1.long0Array[i], o1.long2, o1.long3,
o2.byteArrayArray[i], o2.long0Array[i], o2.long2, o2.long3 ) );
case BOOLEAN_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareBoolean(
o1.long0Array[i],
o2.long0Array[i] ) );
case NUMBER_ARRAY:
return compareArrays( that, ( o1, o2, i ) -> compareNumber(
o1.long0Array[i], o1.long1,
o2.long0Array[i], o2.long1 ) );
default:
throw new IllegalArgumentException( "Unknown type " + type );
}
}
private int compareArrays( GenericKeyState that, ArrayElementComparator comparator )
{
int index = 0;
int compare = 0;
int length = min( this.arrayLength, that.arrayLength );
for ( ; compare == 0 && index < length; index++ )
{
compare = comparator.compare( this, that, index );
}
return compare == 0 ? Integer.compare( this.arrayLength, that.arrayLength ) : compare;
}
private static int compareNumber(
long this_long0, long this_long1,
long that_long0, long that_long1 )
{
return RawBits.compare( this_long0, (byte) this_long1, that_long0, (byte) that_long1 );
}
private static int compareBoolean(
long this_long0,
long that_long0 )
{
return Long.compare( this_long0, that_long0 );
}
private static int compareText(
byte[] this_byteArray, long this_long0, long this_long2, long this_long3,
byte[] that_byteArray, long that_long0, long that_long2, long that_long3 )
{
if ( this_byteArray != that_byteArray )
{
if ( this_byteArray == null )
{
return isHighestText( this_long3 ) ? 1 : -1;
}
if ( that_byteArray == null )
{
return isHighestText( that_long3 ) ? -1 : 1;
}
}
else
{
return 0;
}
return lexicographicalUnsignedByteArrayCompare( this_byteArray, (int) this_long0, that_byteArray, (int) that_long0,
booleanOf( this_long2 ) | booleanOf( that_long2 ) );
}
private static int compareZonedDateTime(
long this_long0, long this_long1, long this_long2, long this_long3,
long that_long0, long that_long1, long that_long2, long that_long3 )
{
int compare = Long.compare( this_long0, that_long0 );
if ( compare == 0 )
{
compare = Integer.compare( (int) this_long1, (int) that_long1 );
if ( compare == 0 &&
// We need to check validity upfront without throwing exceptions, because the PageCursor might give garbage bytes
TimeZones.validZoneOffset( (int) this_long3 ) &&
TimeZones.validZoneOffset( (int) that_long3 ) )
{
// In the rare case of comparing the same instant in different time zones, we settle for
// mapping to values and comparing using the general values comparator.
compare = Values.COMPARATOR.compare(
zonedDateTimeAsValue( this_long0, this_long1, this_long2, this_long3 ),
zonedDateTimeAsValue( that_long0, that_long1, that_long2, that_long3 ) );
}
}
return compare;
}
private static int compareLocalDateTime(
long this_long0, long this_long1,
long that_long0, long that_long1 )
{
int compare = Long.compare( this_long1, that_long1 );
if ( compare == 0 )
{
compare = Integer.compare( (int) this_long0, (int) that_long0 );
}
return compare;
}
private static int compareDate(
long this_long0,
long that_long0 )
{
return Long.compare( this_long0, that_long0 );
}
private static int compareZonedTime(
long this_long0, long this_long1,
long that_long0, long that_long1 )
{
int compare = Long.compare( this_long0, that_long0 );
if ( compare == 0 )
{
compare = Integer.compare( (int) this_long1, (int) that_long1 );
}
return compare;
}
private static int compareLocalTime(
long this_long0,
long that_long0 )
{
return Long.compare( this_long0, that_long0 );
}
private static int compareDuration(
long this_long0, long this_long1, long this_long2, long this_long3,
long that_long0, long that_long1, long that_long2, long that_long3 )
{
int comparison = Long.compare( this_long0, that_long0 );
if ( comparison == 0 )
{
comparison = Integer.compare( (int) this_long1, (int) that_long1 );
if ( comparison == 0 )
{
comparison = Long.compare( this_long2, that_long2 );
if ( comparison == 0 )
{
comparison = Long.compare( this_long3, that_long3 );
}
}
}
return comparison;
}
/* </compare> */
/* <put> */
void put( PageCursor cursor )
{
cursor.putByte( type.typeId );
switch ( type )
{
case ZONED_DATE_TIME:
putZonedDateTime( cursor, long0, long1, long2, long3 );
break;
case LOCAL_DATE_TIME:
putLocalDateTime( cursor, long0, long1 );
break;
case DATE:
putDate( cursor, long0 );
break;
case ZONED_TIME:
putZonedTime( cursor, long0, long1 );
break;
case LOCAL_TIME:
putLocalTime( cursor, long0 );
break;
case DURATION:
putDuration( cursor, long0, long1, long2, long3 );
break;
case TEXT:
putText( cursor, byteArray, long0 );
break;
case BOOLEAN:
putBoolean( cursor, long0 );
break;
case NUMBER:
putNumberIncludingType( cursor, long0, long1 );
break;
case ZONED_DATE_TIME_ARRAY:
putArray( cursor, ( c, i ) -> putZonedDateTime( c, long0Array[i], long1Array[i], long2Array[i], long3Array[i] ) );
break;
case LOCAL_DATE_TIME_ARRAY:
putArray( cursor, ( c, i ) -> putLocalDateTime( c, long0Array[i], long1Array[i] ) );
break;
case DATE_ARRAY:
putArray( cursor, ( c, i ) -> putDate( c, long0Array[i] ) );
break;
case ZONED_TIME_ARRAY:
putArray( cursor, ( c, i ) -> putZonedTime( c, long0Array[i], long1Array[i] ) );
break;
case LOCAL_TIME_ARRAY:
putArray( cursor, ( c, i ) -> putLocalTime( c, long0Array[i] ) );
break;
case DURATION_ARRAY:
putArray( cursor, ( c, i ) -> putDuration( c, long0Array[i], long1Array[i], long2Array[i], long3Array[i] ) );
break;
case TEXT_ARRAY:
putArray( cursor, ( c, i ) -> putText( c, byteArrayArray[i], long0Array[i] ) );
break;
case BOOLEAN_ARRAY:
putArray( cursor, ( c, i ) -> putBoolean( c, long0Array[i] ) );
break;
case NUMBER_ARRAY:
cursor.putByte( (byte) long1 );
putArray( cursor, numberArrayElementWriter( long1 ) );
break;
default:
throw new IllegalArgumentException( "Unknown type " + type );
}
}
private ArrayElementWriter numberArrayElementWriter( long long1 )
{
switch ( (int) long1 )
{
case RawBits.BYTE:
return ( c, i ) -> c.putByte( (byte) long0Array[i] );
case RawBits.SHORT:
return ( c, i ) -> c.putShort( (short) long0Array[i] );
case RawBits.INT:
case RawBits.FLOAT:
return ( c, i ) -> c.putInt( (int) long0Array[i] );
case RawBits.LONG:
case RawBits.DOUBLE:
return ( c, i ) -> c.putLong( long0Array[i] );
default:
throw new IllegalArgumentException( "Unknown number type " + long1 );
}
}
interface ArrayElementWriter
{
void write( PageCursor cursor, int i );
}
private void putArray( PageCursor cursor, ArrayElementWriter writer )
{
cursor.putInt( arrayLength );
for ( int i = 0; i < arrayLength; i++ )
{
writer.write( cursor, i );
}
}
private static void putNumberIncludingType( PageCursor cursor, long long0, long long1 )
{
cursor.putByte( (byte) long1 );
switch ( (int) long1 )
{
case RawBits.BYTE:
cursor.putByte( (byte) long0 );
break;
case RawBits.SHORT:
cursor.putShort( (short) long0 );
break;
case RawBits.INT:
case RawBits.FLOAT:
cursor.putInt( (int) long0 );
break;
case RawBits.LONG:
case RawBits.DOUBLE:
cursor.putLong( long0 );
break;
default:
throw new IllegalArgumentException( "Unknown number type " + long1 );
}
}
private static void putBoolean( PageCursor cursor, long long0 )
{
cursor.putByte( (byte) long0 );
}
private static void putText( PageCursor cursor, byte[] byteArray, long long0 )
{
short length = (short) long0;
cursor.putShort( length );
cursor.putBytes( byteArray, 0, length );
}
private static void putDuration( PageCursor cursor, long long0, long long1, long long2, long long3 )
{
cursor.putLong( long0 );
cursor.putInt( (int) long1 );
cursor.putLong( long2 );
cursor.putLong( long3 );
}
private static void putLocalTime( PageCursor cursor, long long0 )
{
cursor.putLong( long0 );
}
private static void putZonedTime( PageCursor cursor, long long0, long long1 )
{
cursor.putLong( long0 );
cursor.putInt( (int) long1 );
}
private static void putDate( PageCursor cursor, long long0 )
{
cursor.putLong( long0 );
}
private static void putLocalDateTime( PageCursor cursor, long long0, long long1 )
{
cursor.putLong( long1 );
cursor.putInt( (int) long0 );
}
private static void putZonedDateTime( PageCursor cursor, long long0, long long1, long long2, long long3 )
{
cursor.putLong( long0 );
cursor.putInt( (int) long1 );
if ( long2 >= 0 )
{
cursor.putInt( (int) long2 | ZONE_ID_FLAG );
}
else
{
cursor.putInt( (int) long3 & ZONE_ID_MASK );
}
}
/* </put> */
/* <read> */
boolean read( PageCursor cursor, int size )
{
if ( size <= TYPE_ID_SIZE )
{
return false;
}
byte typeId = cursor.getByte();
if ( typeId < 0 || typeId >= GenericLayout.TYPES.length )
{
return false;
}