Repository navigation
Expand file tree
/
Copy pathStoreDirect.java
More file actions
1808 lines (1481 loc) · 62.3 KB
/
Copy pathStoreDirect.java
File metadata and controls
1808 lines (1481 loc) · 62.3 KB
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
package org.mapdb;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.logging.Level;
import static org.mapdb.DataIO.*;
public class StoreDirect extends Store {
/** 2 byte store version*/
protected static final int STORE_VERSION = 100;
/** 4 byte file header */
protected static final int HEADER = (0xA9DB<<16) | STORE_VERSION;
protected static final long PAGE_SIZE = 1<< CC.VOLUME_PAGE_SHIFT;
protected static final long PAGE_MASK = PAGE_SIZE-1;
protected static final long PAGE_MASK_INVERSE = 0xFFFFFFFFFFFFFFFFL<<CC.VOLUME_PAGE_SHIFT;
protected static final long MOFFSET = 0x0000FFFFFFFFFFF0L;
protected static final long MLINKED = 0x8L;
protected static final long MUNUSED = 0x4L;
protected static final long MARCHIVE = 0x2L;
protected static final long MPARITY = 0x1L;
protected static final long STORE_SIZE = 8*2;
/** physical offset of maximal allocated recid. Parity1.
* It is value of last allocated RECID multiplied by recid size.
* Use {@code val/indexValSize} to get actual RECID*/
protected static final long MAX_RECID_OFFSET = 8*3;
protected static final long LAST_PHYS_ALLOCATED_DATA_OFFSET = 8*4; //TODO update doc
protected static final long FREE_RECID_STACK = 8*5;
/*following slots might be used in future */
protected static final long UNUSED1 = 8*6;
protected static final long UNUSED2 = 8*7;
protected static final long UNUSED3 = 8*8;
protected static final long UNUSED4 = 8*9;
protected static final long UNUSED5 = 8*10;
protected static final int MAX_REC_SIZE = 0xFFFF;
/** number of free physical slots */
protected static final int SLOTS_COUNT = 2+(MAX_REC_SIZE)/16; //it rounds down, plus extra slot for zeros (not really used)
protected static final long HEAD_END = UNUSED5 + SLOTS_COUNT * 8;
// 8*RECID_LAST_RESERVED;// also include reserved recids into mix;
protected static final long INITCRC_INDEX_PAGE = 4329042389490239043L;
protected static final long[] EMPTY_LONGS = new long[0];
//TODO this refs are swapped during compaction. Investigate performance implications
protected volatile Volume vol;
protected volatile Volume headVol;
//TODO this only grows under structural lock, but reads are outside structural lock, does it have to be volatile?
protected volatile long[] indexPages;
protected volatile long lastAllocatedData=0; //TODO this is under structural lock, does it have to be volatile?
protected final ScheduledExecutorService executor;
protected final List<Snapshot> snapshots;
protected final long indexValSize;
protected final long startSize;
protected final long sizeIncrement;
protected final boolean recidReuseDisable;
protected final int sliceShift;
protected final AtomicLong freeSize = new AtomicLong(-1);
public StoreDirect(String fileName,
Volume.VolumeFactory volumeFactory,
Cache cache,
int lockScale,
int lockingStrategy,
boolean checksum,
boolean compress,
byte[] password,
boolean readonly,
boolean snapshotEnable,
boolean fileLockDisable,
DataIO.HeartbeatFileLock fileLockHeartbeat,
ScheduledExecutorService executor,
long startSize,
long sizeIncrement,
boolean recidReuseDisable
) {
super(fileName, volumeFactory, cache, lockScale, lockingStrategy, checksum, compress, password, readonly,
snapshotEnable, fileLockDisable, fileLockHeartbeat);
this.executor = executor;
this.snapshots = snapshotEnable?
new CopyOnWriteArrayList<Snapshot>():
null;
this.indexValSize = checksum ? 10 : 8;
this.sizeIncrement = Math.max(1L<<CC.VOLUME_PAGE_SHIFT, DataIO.nextPowTwo(sizeIncrement));
this.startSize = Fun.roundUp(Math.max(1L<<CC.VOLUME_PAGE_SHIFT,startSize), this.sizeIncrement);
this.recidReuseDisable = recidReuseDisable;
this.sliceShift = Volume.sliceShiftFromSize(this.sizeIncrement);
if(CC.LOG_STORE && LOG.isLoggable(Level.FINE)){
LOG.log(Level.FINE, "StoreDirect constructed: executor={0}, snapshots={1}, indexValSize={2}, " +
"startSize={3}, sizeIncrement={4}",
new Object[]{executor,snapshots,indexValSize,startSize,sizeIncrement});
}
}
@Override
public void init() {
commitLock.lock();
try {
structuralLock.lock();
try {
boolean empty = Volume.isEmptyFile(fileName);
this.vol = volumeFactory.makeVolume(fileName, readonly, fileLockDisable, sliceShift, startSize, false);
if (empty) {
initCreate();
} else {
initOpen();
}
} finally {
structuralLock.unlock();
}
}catch(RuntimeException e){
initFailedCloseFiles();
if(vol!=null && !vol.isClosed()) {
vol.close();
}
vol = null;
throw e;
}finally {
commitLock.unlock();
}
}
protected void initFailedCloseFiles() {
}
protected void initOpen() {
if(CC.ASSERT && !commitLock.isHeldByCurrentThread())
throw new AssertionError();
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
int header = vol.getInt(0);
if(header!=header){
throw new DBException.WrongConfig("This is not MapDB file");
}
if (CC.LOG_STORE && LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "initOpen: file={0}, volLength={1}, vol={2}", new Object[]{fileName, vol.length(), vol});
}
if(vol.getInt(0)!=HEADER){
//TODO handle version numbers
throw new DBException.DataCorruption("wrong header in file: "+fileName);
}
//check header config
checkFeaturesBitmap(vol.getLong(HEAD_FEATURES));
initHeadVol();
//check head checksum
int expectedChecksum = vol.getInt(HEAD_CHECKSUM);
int actualChecksum = headChecksum(vol);
if (actualChecksum != expectedChecksum) {
throw new DBException.HeadChecksumBroken();
}
//load index pages
long[] ip = new long[]{0};
long indexPage = parity16Get(vol.getLong(HEAD_END));
int i=1;
for(;indexPage!=0;i++){
if(CC.ASSERT && indexPage%PAGE_SIZE!=0)
throw new DBException.DataCorruption();
if(ip.length==i){
ip = Arrays.copyOf(ip, ip.length * 4);
}
ip[i] = indexPage;
//move to next page
indexPage = parity16Get(vol.getLong(indexPage));
}
indexPages = Arrays.copyOf(ip,i);
lastAllocatedData = parity3Get(vol.getLong(LAST_PHYS_ALLOCATED_DATA_OFFSET));
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "indexPages: {0}", Arrays.toString(indexPages));
LOG.log(Level.FINEST, "lastAllocatedData: {0}", lastAllocatedData);
}
}
protected void initCreate() {
if(CC.ASSERT && !commitLock.isHeldByCurrentThread())
throw new AssertionError();
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
//create initial structure
//set features bitmap
final long features = makeFeaturesBitmap();
if (CC.LOG_STORE && LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "initCreate: file={0}, volLength={1}, vol={2}, features={3}",
new Object[]{fileName, vol.length(), vol, Long.toBinaryString(features)});
}
//create new store
indexPages = new long[]{0};
vol.ensureAvailable(PAGE_SIZE);
vol.clear(0, PAGE_SIZE);
//set sizes
vol.putLong(STORE_SIZE, parity16Set(PAGE_SIZE));
vol.putLong(MAX_RECID_OFFSET, parity1Set(RECID_LAST_RESERVED * indexValSize));
//pointer to next index page (zero)
vol.putLong(HEAD_END, parity16Set(0));
lastAllocatedData = 0L;
vol.putLong(LAST_PHYS_ALLOCATED_DATA_OFFSET, parity3Set(lastAllocatedData));
//put reserved recids
for(long recid=1;recid<RECID_FIRST;recid++){
long indexVal = parity1Set(MLINKED | MARCHIVE);
long indexOffset = recidToOffset(recid);
vol.putLong(indexOffset, indexVal);
if(checksum) {
vol.putUnsignedShort(indexOffset + 8, DataIO.longHash(indexVal)&0xFFFF);
}
}
//put long stack master links
for(long masterLinkOffset = FREE_RECID_STACK;masterLinkOffset<HEAD_END;masterLinkOffset+=8){
vol.putLong(masterLinkOffset,parity4Set(0));
}
//write header
vol.putInt(0,HEADER);
vol.putLong(HEAD_FEATURES, features);
//and set header checksum
vol.putInt(HEAD_CHECKSUM, headChecksum(vol));
vol.sync();
initHeadVol();
}
protected void initHeadVol() {
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
this.headVol = vol;
}
public StoreDirect(String fileName) {
this(fileName,
fileName==null? CC.DEFAULT_MEMORY_VOLUME_FACTORY : CC.DEFAULT_FILE_VOLUME_FACTORY,
null,
CC.DEFAULT_LOCK_SCALE,
0,
false,false,null,false,false,false,null,
null, 0L, 0L, false);
}
protected int headChecksum(Volume vol2) {
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
int ret = 0;
for(int offset = 8;
offset< HEAD_END;
offset+=8){
long val = vol2.getLong(offset);
ret += DataIO.longHash(offset + val);
}
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "headChecksum={0}", Integer.toHexString(ret));
}
return ret;
}
@Override
protected <A> A get2(long recid, Serializer<A> serializer) {
if (CC.ASSERT)
assertReadLocked(recid);
long[] offsets = offsetsGet(lockPos(recid),indexValGet(recid));
return getFromOffset(serializer, offsets);
}
protected <A> A getFromOffset(Serializer<A> serializer, long[] offsets) {
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "serializer={0}, offsets={1}",new Object[]{serializer, Arrays.toString(offsets)});
}
if (offsets == null) {
return null; //zero size
}else if (offsets.length==0){
return deserialize(serializer,0,new DataInputByteArray(new byte[0]));
}else if (offsets.length == 1) {
//not linked
int size = (int) (offsets[0] >>> 48);
long offset = offsets[0] & MOFFSET;
DataInput in = vol.getDataInput(offset, size);
return deserialize(serializer, size, in);
} else {
//calculate total size
int totalSize = offsetsTotalSize(offsets);
byte[] b = getLoadLinkedRecord(offsets, totalSize);
DataInput in = new DataInputByteArray(b);
return deserialize(serializer, totalSize, in);
}
}
private byte[] getLoadLinkedRecord(long[] offsets, int totalSize) {
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "totalSize={0}, offsets={1}", new Object[]{totalSize, Arrays.toString(offsets)});
}
//load data
byte[] b = new byte[totalSize];
int bpos = 0;
for (int i = 0; i < offsets.length; i++) {
int plus = (i == offsets.length - 1)?0:8;
long size = (offsets[i] >>> 48) - plus;
if(CC.ASSERT && (size&0xFFFF)!=size)
throw new DBException.DataCorruption("size mismatch");
long offset = offsets[i] & MOFFSET;
//System.out.println("GET "+(offset + plus)+ " - "+size+" - "+bpos);
vol.getData(offset + plus, b, bpos, (int) size);
bpos += size;
}
if (CC.ASSERT && bpos != totalSize)
throw new DBException.DataCorruption("size does not match");
return b;
}
protected int offsetsTotalSize(long[] offsets) {
if(offsets==null || offsets.length==0)
return 0;
int totalSize = 8;
for (long l : offsets) {
totalSize += (l >>> 48) - 8;
}
return totalSize;
}
@Override
protected void update2(long recid, DataOutputByteArray out) {
int pos = lockPos(recid);
if(CC.ASSERT)
assertWriteLocked(pos);
long oldIndexVal = indexValGet(recid);
boolean releaseOld = true;
if(snapshotEnable){
for(Snapshot snap:snapshots){
snap.oldRecids[pos].putIfAbsent(recid,oldIndexVal);
releaseOld = false;
}
}
long[] oldOffsets = offsetsGet(pos,oldIndexVal);
int oldSize = offsetsTotalSize(oldOffsets);
int newSize = out==null?0:out.pos;
long[] newOffsets;
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "recid={0}, oldIndexVal={1}, oldSize={2}, newSize={3}, oldOffsets={4}",
new Object[]{recid, oldIndexVal, oldSize, newSize, Arrays.toString(oldOffsets)});
}
//if new version fits into old one, reuse space
if(releaseOld && oldSize==newSize){
//TODO more precise check of linked records
//TODO check rounUp 16 for non-linked records
newOffsets = oldOffsets;
}else {
structuralLock.lock();
try {
if(releaseOld && oldOffsets!=null)
freeDataPut(oldOffsets);
newOffsets = newSize==0?null:freeDataTake(out.pos);
} finally {
structuralLock.unlock();
}
}
if(CC.ASSERT)
offsetsVerify(newOffsets);
putData(recid, newOffsets, out==null?null:out.buf, out==null?0:out.pos);
}
protected void offsetsVerify(long[] ret) {
//TODO check non tail records are mod 16
//TODO check linkage
if(ret==null)
return;
for(int i=0;i<ret.length;i++) {
boolean last = (i==ret.length-1);
boolean linked = (ret[i]&MLINKED)!=0;
if(!last && !linked)
throw new DBException.DataCorruption("body not linked");
if(last && linked)
throw new DBException.DataCorruption("tail is linked");
long offset = ret[i]&MOFFSET;
if(offset<PAGE_SIZE)
throw new DBException.DataCorruption("offset is too small");
if(((offset&MOFFSET)%16)!=0)
throw new DBException.DataCorruption("offset not mod 16");
int size = (int) (ret[i] >>>48);
if(size<=0)
throw new DBException.DataCorruption("size too small");
}
}
/** return positions of (possibly) linked record */
protected long[] offsetsGet(int segment, long indexVal) {;
if(indexVal>>>48==0){
return ((indexVal&MLINKED)!=0) ? null : EMPTY_LONGS;
}
long[] ret = new long[]{indexVal};
while((ret[ret.length-1]&MLINKED)!=0){
ret = Arrays.copyOf(ret,ret.length+1);
ret[ret.length-1] = parity3Get(vol.getLong(ret[ret.length-2]&MOFFSET));
}
if(CC.ASSERT){
offsetsVerify(ret);
}
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "indexVal={0}, ret={1}",
new Object[]{Long.toHexString(indexVal), Arrays.toString(ret)});
}
return ret;
}
protected void indexValPut(long recid, int size, long offset, boolean linked, boolean unused) {
if(CC.ASSERT)
assertWriteLocked(lockPos(recid));
long indexOffset = recidToOffset(recid);
long newval = composeIndexVal(size, offset, linked, unused, true);
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "recid={0}, indexOffset={1}, newval={2}",
new Object[]{recid, indexOffset, Long.toHexString(newval)});
}
vol.putLong(indexOffset, newval);
if(checksum){
vol.putUnsignedShort(indexOffset + 8, DataIO.longHash(newval) & 0xFFFF);
}
}
@Override
protected <A> void delete2(long recid, Serializer<A> serializer) {
if(CC.ASSERT)
assertWriteLocked(lockPos(recid));
final int pos = lockPos(recid);
long oldIndexVal = indexValGet(recid);
long[] offsets = offsetsGet(pos,oldIndexVal);
boolean releaseOld = true;
if(snapshotEnable){
for(Snapshot snap:snapshots){
snap.oldRecids[pos].putIfAbsent(recid,oldIndexVal);
releaseOld = false;
}
}
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "recid={0}, oldIndexVal={1}, releaseOld={2}, offsets={3}",
new Object[]{recid, Long.toHexString(oldIndexVal), releaseOld, Arrays.toString(offsets)});
}
if(offsets!=null && releaseOld) {
structuralLock.lock();
try {
freeDataPut(offsets);
} finally {
structuralLock.unlock();
}
}
indexValPut(recid, 0, 0, true, true);
if(!recidReuseDisable){
structuralLock.lock();
try {
longStackPut(FREE_RECID_STACK, recid, false);
}finally {
structuralLock.unlock();
}
}
}
@Override
public long getCurrSize() {
return vol.length() - lastAllocatedData % CHUNKSIZE;
}
@Override
public long getFreeSize() {
long ret = freeSize.get();
if(ret!=-1)
return ret;
structuralLock.lock();
try{
//try one more time under lock
ret = freeSize.get();
if(ret!=-1)
return ret;
//traverse list of recids,
ret=
8* longStackCount(FREE_RECID_STACK);
for(long stackNum = 1;stackNum<=SLOTS_COUNT;stackNum++){
long indexOffset = FREE_RECID_STACK+stackNum*8;
long size = stackNum*16;
ret += size * longStackCount(indexOffset);
}
freeSize.set(ret);
return ret;
}finally {
structuralLock.unlock();
}
}
protected void freeSizeIncrement(int increment){
for(;;) {
long val = freeSize.get();
if (val == -1 || freeSize.compareAndSet(val, val + increment))
return;
}
}
@Override
public long preallocate() {
long recid;
structuralLock.lock();
try {
//TODO possible race condition here? Can this modify existing data?
recid = freeRecidTake();
}finally {
structuralLock.unlock();
}
Lock lock = locks[lockPos(recid)].writeLock();
lock.lock();
try {
indexValPut(recid, 0, 0L, true, true);
}finally {
lock.unlock();
}
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "recid={0}",recid);
}
return recid;
}
@Override
public <A> long put(A value, Serializer<A> serializer) {
long recid;
long[] offsets;
DataOutputByteArray out = serialize(value, serializer);
boolean notalloc = out==null || out.pos==0;
commitLock.lock();
try {
structuralLock.lock();
try {
recid = freeRecidTake();
} finally {
structuralLock.unlock();
}
int pos = lockPos(recid);
Lock lock = locks[pos].writeLock();
lock.lock();
//TODO possible deadlock, should not lock segment under different segment lock
//TODO investigate if this lock is necessary, recid has not been yet published, perhaps cache does not have to be updated
try {
if(CC.ASSERT && recidReuseDisable && vol.getLong(recidToOffset(recid))!=0){
throw new AssertionError("Recid not empty: "+recid);
}
if (caches != null) {
caches[pos].put(recid, value);
}
if (snapshotEnable) {
for (Snapshot snap : snapshots) {
snap.oldRecids[pos].putIfAbsent(recid, 0);
}
}
structuralLock.lock();
try {
offsets = notalloc ? null : freeDataTake(out.pos);
} finally {
structuralLock.unlock();
}
if (CC.ASSERT && offsets != null && (offsets[0] & MOFFSET) < PAGE_SIZE)
throw new DBException.DataCorruption();
putData(recid, offsets, out == null ? null : out.buf, out == null ? 0 : out.pos);
} finally {
lock.unlock();
}
}finally {
commitLock.unlock();
}
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "recid={0}, serSize={1}, serializer={2}",
new Object[]{recid, notalloc?0:out.pos, serializer});
}
return recid;
}
protected void putData(long recid, long[] offsets, byte[] src, int srcLen) {
if(CC.ASSERT)
assertWriteLocked(lockPos(recid));
if(CC.ASSERT && offsetsTotalSize(offsets)!=(src==null?0:srcLen))
throw new DBException.DataCorruption("size mismatch");
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "recid={0}, srcLen={1}, offsets={2}",
new Object[]{recid, srcLen, Arrays.toString(offsets)});
}
if(offsets!=null) {
int outPos = 0;
for (int i = 0; i < offsets.length; i++) {
final boolean last = (i == offsets.length - 1);
if (CC.ASSERT && ((offsets[i] & MLINKED) == 0) != last)
throw new DBException.DataCorruption("linked bit set wrong way");
long offset = (offsets[i] & MOFFSET);
if(CC.ASSERT && offset%16!=0)
throw new DBException.DataCorruption("not aligned to 16");
int plus = (last?0:8);
int size = (int) ((offsets[i]>>>48) - plus);
if(CC.ASSERT && ((size&0xFFFF)!=size || size==0))
throw new DBException.DataCorruption("size mismatch");
int segment = lockPos(recid);
//write offset to next page
if (!last) {
putDataSingleWithLink(segment, offset,parity3Set(offsets[i + 1]), src,outPos,size);
}else{
putDataSingleWithoutLink(segment, offset, src, outPos, size);
}
outPos += size;
}
if(CC.ASSERT && outPos!=srcLen)
throw new DBException.DataCorruption("size mismatch");
}
//update index val
boolean firstLinked =
(offsets!=null && offsets.length>1) || //too large record
(src==null); //null records
boolean empty = offsets==null || offsets.length==0;
int firstSize = (int) (empty ? 0L : offsets[0]>>>48);
long firstOffset = empty? 0L : offsets[0]&MOFFSET;
indexValPut(recid, firstSize, firstOffset, firstLinked, false);
}
protected void putDataSingleWithoutLink(int segment, long offset, byte[] buf, int bufPos, int size) {
vol.putData(offset, buf, bufPos, size);
}
protected void putDataSingleWithLink(int segment, long offset, long link, byte[] buf, int bufPos, int size) {
vol.putLong(offset, link);
vol.putData(offset + 8, buf, bufPos, size);
}
protected void freeDataPut(long[] linkedOffsets) {
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
for(long v:linkedOffsets){
int size = round16Up((int) (v >>> 48));
v &= MOFFSET;
freeDataPut(v,size);
}
}
protected void freeDataPut(long offset, int size) {
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
if(CC.ASSERT && size%16!=0 )
throw new DBException.DataCorruption("unalligned size");
if(CC.ASSERT && (offset%16!=0 || offset<PAGE_SIZE))
throw new DBException.DataCorruption("wrong offset");
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "offset={0}, size={1}",
new Object[]{offset, size});
}
if(!(this instanceof StoreWAL)) //TODO WAL needs to handle record clear, perhaps WAL instruction?
vol.clear(offset,offset+size);
//shrink store if this is last record
if(offset+size==lastAllocatedData){
lastAllocatedData-=size;
return;
}
freeSizeIncrement(size);
long masterPointerOffset = size/2 + FREE_RECID_STACK; // really is size*8/16
longStackPut(
masterPointerOffset,
offset >>> 4, //offset is multiple of 16, save some space
false);
}
protected long[] freeDataTake(int size) {
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
if(CC.ASSERT && size<=0)
throw new DBException.DataCorruption("size too small");
//compose of multiple single records
long[] ret = EMPTY_LONGS;
while(size>MAX_REC_SIZE){
ret = Arrays.copyOf(ret,ret.length+1);
ret[ret.length-1] = (((long)MAX_REC_SIZE)<<48) | freeDataTakeSingle(round16Up(MAX_REC_SIZE)) | MLINKED;
size = size-MAX_REC_SIZE+8;
}
//allocate last section
ret = Arrays.copyOf(ret,ret.length+1);
ret[ret.length-1] = (((long)size)<<48) | freeDataTakeSingle(round16Up(size)) ;
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "size={0}, ret={1}",
new Object[]{size, Arrays.toString(ret)});
}
return ret;
}
protected long freeDataTakeSingle(int size) {
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
if(CC.ASSERT && size%16!=0)
throw new DBException.DataCorruption("unalligned size");
if(CC.ASSERT && size>round16Up(MAX_REC_SIZE))
throw new DBException.DataCorruption("size too big");
long masterPointerOffset = size/2 + FREE_RECID_STACK; // really is size*8/16
long ret = longStackTake(masterPointerOffset,false) <<4; //offset is multiple of 16, save some space
if(ret!=0) {
if(CC.ASSERT && ret<PAGE_SIZE)
throw new DBException.DataCorruption("wrong ret");
if(CC.ASSERT && ret%16!=0)
throw new DBException.DataCorruption("unalligned ret");
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "size={0}, ret={1}",
new Object[]{size, Long.toHexString(ret)});
}
freeSizeIncrement(-size);
return ret;
}
if(lastAllocatedData==0){
//allocate new data page
long page = pageAllocate();
lastAllocatedData = page+size;
if(CC.ASSERT && page<PAGE_SIZE)
throw new DBException.DataCorruption("wrong page");
if(CC.ASSERT && page%16!=0)
throw new DBException.DataCorruption("unalligned page");
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "size={0}, ret={1}",
new Object[]{size, Long.toHexString(ret)});
}
return page;
}
//does record fit into rest of the page?
if((lastAllocatedData%PAGE_SIZE + size)/PAGE_SIZE !=0){
//throw away rest of the page and allocate new
lastAllocatedData=0;
freeDataTakeSingle(size);
//TODO i thing return! should be here, but not sure.
//TODO it could be possible to recycle data here.
// save pointers and put them into free list after new page was allocated.
}
//yes it fits here, increase pointer
ret = lastAllocatedData;
lastAllocatedData+=size;
if(CC.ASSERT && ret%16!=0)
throw new DBException.DataCorruption();
if(CC.ASSERT && lastAllocatedData%16!=0)
throw new DBException.DataCorruption();
if(CC.ASSERT && ret<PAGE_SIZE)
throw new DBException.DataCorruption();
if (CC.LOG_STORE && LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "size={0}, ret={1}",
new Object[]{size, Long.toHexString(ret)});
}
return ret;
}
//TODO use var size
protected final static long CHUNKSIZE = 100*16;
protected void longStackPut(final long masterLinkOffset, final long value, boolean recursive){
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
if(CC.ASSERT && (masterLinkOffset<=0 || masterLinkOffset>PAGE_SIZE || masterLinkOffset % 8!=0)) //TODO perhaps remove the last check
throw new DBException.DataCorruption("wrong master link");
long masterLinkVal = parity4Get(headVol.getLong(masterLinkOffset));
long pageOffset = masterLinkVal&MOFFSET;
if(masterLinkVal==0L){
longStackNewPage(masterLinkOffset, 0L, value);
return;
}
long currSize = masterLinkVal>>>48;
long prevLinkVal = parity4Get(vol.getLong(pageOffset));
long pageSize = prevLinkVal>>>48;
//is there enough space in current page?
if(currSize+8>=pageSize){ // +8 is just to make sure and is worse case scenario, perhaps make better check based on actual packed size
//no there is not enough space
//first zero out rest of the page
vol.clear(pageOffset+currSize, pageOffset+pageSize);
//allocate new page
longStackNewPage(masterLinkOffset,pageOffset,value);
return;
}
//there is enough space, so just write new value
currSize += vol.putLongPackBidi(pageOffset+currSize, longParitySet(value));
//and update master pointer
headVol.putLong(masterLinkOffset, parity4Set(currSize<<48 | pageOffset));
}
protected void longStackNewPage(long masterLinkOffset, long prevPageOffset, long value) {
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
long newPageOffset = freeDataTakeSingle((int) CHUNKSIZE);
//write size of current chunk with link to prev page
vol.putLong(newPageOffset, parity4Set((CHUNKSIZE<<48) | prevPageOffset));
//put value
long currSize = 8 + vol.putLongPackBidi(newPageOffset+8, longParitySet(value));
//update master pointer
headVol.putLong(masterLinkOffset, parity4Set((currSize<<48)|newPageOffset));
}
protected long longStackTake(long masterLinkOffset, boolean recursive){
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
if(CC.ASSERT && (masterLinkOffset<FREE_RECID_STACK ||
masterLinkOffset>FREE_RECID_STACK+round16Up(MAX_REC_SIZE)/2 ||
masterLinkOffset % 8!=0))
throw new DBException.DataCorruption("wrong master link");
long masterLinkVal = parity4Get(headVol.getLong(masterLinkOffset));
if(masterLinkVal==0 ){
return 0;
}
long currSize = masterLinkVal>>>48;
final long pageOffset = masterLinkVal&MOFFSET;
//read packed link from stack
long ret = vol.getLongPackBidiReverse(pageOffset+currSize);
//extract number of read bytes
long oldCurrSize = currSize;
currSize-= ret >>>56;
//clear bytes occupied by prev value
vol.clear(pageOffset+currSize, pageOffset+oldCurrSize);
//and finally set return value
ret = longParityGet(ret & DataIO.PACK_LONG_RESULT_MASK);
if(CC.ASSERT && currSize<8)
throw new DBException.DataCorruption();
//is there space left on current page?
if(currSize>8){
//yes, just update master link
headVol.putLong(masterLinkOffset, parity4Set(currSize << 48 | pageOffset));
return ret;
}
//there is no space at current page, so delete current page and update master pointer
long prevPageOffset = parity4Get(vol.getLong(pageOffset));
final int currPageSize = (int) (prevPageOffset>>>48);
prevPageOffset &= MOFFSET;
//does previous page exists?
if(prevPageOffset!=0) {
//yes previous page exists
//find pointer to end of previous page
// (data are packed with var size, traverse from end of page, until zeros
//first read size of current page
currSize = parity4Get(vol.getLong(prevPageOffset)) >>> 48;
//now read bytes from end of page, until they are zeros
while (vol.getUnsignedByte(prevPageOffset + currSize-1) == 0) {
currSize--;
}
if (CC.ASSERT && currSize < 10)
throw new DBException.DataCorruption();
}else{
//no prev page does not exist
currSize=0;
}
//update master link with curr page size and offset
headVol.putLong(masterLinkOffset, parity4Set(currSize<<48 | prevPageOffset));
//release old page, size is stored as part of prev page value
freeDataPut(pageOffset, currPageSize);
return ret;
}
protected long longStackCount(final long masterLinkOffset){
if(CC.ASSERT && !structuralLock.isHeldByCurrentThread())
throw new AssertionError();
if (CC.ASSERT && (masterLinkOffset <= 0 || masterLinkOffset > PAGE_SIZE || masterLinkOffset % 8 != 0))