Repository navigation
Expand file tree
/
Copy pathStoreDirect.java
More file actions
1275 lines (1080 loc) · 44.8 KB
/
Copy pathStoreDirect.java
File metadata and controls
1275 lines (1080 loc) · 44.8 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
/*
* Copyright (c) 2012 Jan Kotek
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapdb;
import java.io.DataInput;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.locks.Lock;
import java.util.logging.Level;
/**
* Storage Engine which saves record directly into file.
* It has zero protection from data corruption and must be closed properly after modifications.
* It is used when Write-Ahead-Log transactions are disabled.
*
*
* Storage format
* ----------------
* `StoreDirect` is composed of two files: Index file is sequence of 8-byte longs, it translates
* `recid` (offset in index file) to record size and offset in physical file. Records position
* may change, but it requires stable ID, so the index file is used for translation.
* This store uses data structure called `Long Stack` to manage (and reuse) free space, it is
* is linked LIFO queue of 8-byte longs.
*
* Index file
* --------------
* Index file is translation table between permanent record ID (recid) and mutable location in physical file.
* Index file is sequence of 8-byte longs, one for each record. It also has some extra longs to manage
* free space and other metainfo. Index table and physical data could be stored in single file, but
* keeping index table separate simplifies compaction.
*
* Basic **structure of index file** is bellow. Each slot is 8-bytes long so `offset=slot*8`
*
* slot | in code | description
* --- | --- | ---
* 0 | {@link StoreDirect#HEADER} | File header, format version and flags
* 1 | {@link StoreDirect#IO_INDEX_SIZE} | Allocated file size of index file in bytes.
* 2 | {@link StoreDirect#IO_PHYS_SIZE} | Allocated file size of physical file in bytes.
* 3 | {@link StoreDirect#IO_FREE_SIZE} | Space occupied by free records in physical file in bytes.
* 4 | {@link StoreDirect#IO_INDEX_SUM} | Checksum of all Index file headers. Checks if store was closed correctly
* 5..9 | | Reserved for future use
* 10..14 | | For usage by user
* 15 | {@link StoreDirect#IO_FREE_RECID} |Long Stack of deleted recids, those will be reused and returned by {@link Engine#put(Object, Serializer)}
* 16..4111 | |Long Stack of free physical records. This contains free space released by record update or delete. Each slots corresponds to free record size. TODO check 4111 is right
* 4112 | {@link StoreDirect#IO_USER_START} |Record size and offset in physical file for recid=1
* 4113 | |Record size and offset in physical file for recid=2
* ... | ... |... snip ...
* N+4111 | |Record size and offset in physical file for recid=N
*
*
* Long Stack
* ------------
* Long Stack is data structure used to store free records. It is LIFO queue which uses linked records to store 8-byte longs.
* Long Stack is identified by slot in Index File, which stores pointer to Long Stack head. The structure of
* of index pointer is following:
*
* byte | description
* --- |---
* 0..1 | relative offset in head Long Stack Record to take value from. This value decreases by 8 each take
* 2..7 | physical file offset of head Long Stack Record, zero if Long Stack is empty
*
* Each Long Stack Record is sequence of 8-byte longs, first slot is header. Long Stack Record structure is following:
*
* byte | description
* --- |---
* 0..1 | length of current Long Stack Record in bytes
* 2..7 | physical file offset of next Long Stack Record, zero of this record is last
* 8-15 | Long Stack value
* 16-23 | Long Stack value
* ... | and so on until end of Long Stack Record
*
* Physical pointer
* ----------------
* Index slot value typically contains physical pointer (information about record location and size in physical file). First 2 bytes
* are record size (max 65536). Then there is 6 byte offset in physical file (max store size is 281 TB).
* Physical file offset must always be multiple of 16, so last 4 bites are used to flag extra record information.
* Structure of **physical pointer**:
*
* bite | in code | description
* --- | --- | ---
* 0-15 |`val>>>48` | record size
* 16-59 |`val&{@link StoreDirect#MASK_OFFSET}` | physical offset
* 60 |`val&{@link StoreDirect#MASK_LINKED}!=0` | linked record flag
* 61 |`val&{@link StoreDirect#MASK_DISCARD}!=0` | to be discarded while storage is offline flag
* 62 |`val&{@link StoreDirect#MASK_ARCHIVE}!=0` | record modified since last backup flag
* 63 | | not used yet
*
* Records in Physical File
* ---------------------------
* Records are stored in physical file. Maximal record size size is 64KB, so larger records must
* be stored in form of the linked list. Each record starts by Physical Pointer from Index File.
* There is flag in Physical Pointer indicating if record is linked. If record is not linked you may
* just read ByteBuffer from given size and offset.
*
* If record is linked, each record starts with Physical Pointer to next record. So actual data payload is record size-8.
* The last linked record does not have the Physical Pointer header to next record, there is MASK_LINKED flag which
* indicates if next record is the last one.
*
*
* @author Jan Kotek
*/
public class StoreDirect extends Store{
protected static final long MASK_OFFSET = 0x0000FFFFFFFFFFF0L;
protected static final long MASK_LINKED = 0x8L;
protected static final long MASK_DISCARD = 0x4L;
protected static final long MASK_ARCHIVE = 0x2L;
/** 4 byte file header */
protected static final int HEADER = 234243482;
/** 2 byte store version*/
protected static final short STORE_VERSION = 10000;
/** maximal non linked record size */
protected static final int MAX_REC_SIZE = 65536-1;
/** number of free physical slots */
protected static final int PHYS_FREE_SLOTS_COUNT = 2048*2;
/** index file offset where current size of index file is stored*/
protected static final int IO_INDEX_SIZE = 1*8;
/** index file offset where current size of phys file is stored */
protected static final int IO_PHYS_SIZE = 2*8;
/** index file offset where space occupied by free phys records is stored */
protected static final int IO_FREE_SIZE = 3*8;
/** checksum of all index file headers. Used to verify store was closed correctly */
protected static final int IO_INDEX_SUM = 4*8;
/** index file offset where reference to longstack of free recid is stored*/
protected static final int IO_FREE_RECID = 15*8;
/** index file offset where first recid available to user is stored */
protected static final int IO_USER_START = IO_FREE_RECID+PHYS_FREE_SLOTS_COUNT*8+8;
public static final String DATA_FILE_EXT = ".p";
protected final static int LONG_STACK_PREF_COUNT = 204;
protected final static long LONG_STACK_PREF_SIZE = 8+LONG_STACK_PREF_COUNT*6;
protected final static int LONG_STACK_PREF_COUNT_ALTER = 212;
protected final static long LONG_STACK_PREF_SIZE_ALTER = 8+LONG_STACK_PREF_COUNT_ALTER*6;
protected Volume index;
protected Volume phys;
protected long physSize;
protected long indexSize;
protected long freeSize;
protected final boolean deleteFilesAfterClose;
protected final boolean readOnly;
protected final boolean syncOnCommitDisabled;
protected final boolean spaceReclaimReuse;
protected final boolean spaceReclaimSplit;
protected final boolean spaceReclaimTrack;
protected final long sizeLimit;
/** maximal non zero slot in free phys record, access requires `structuralLock`*/
protected long maxUsedIoList = 0;
public StoreDirect(Volume.Factory volFac, boolean readOnly, boolean deleteFilesAfterClose,
int spaceReclaimMode, boolean syncOnCommitDisabled, long sizeLimit,
boolean checksum, boolean compress, byte[] password, boolean disableLocks,int sizeIncrement) {
super(checksum, compress, password, disableLocks);
this.readOnly = readOnly;
this.deleteFilesAfterClose = deleteFilesAfterClose;
this.syncOnCommitDisabled = syncOnCommitDisabled;
this.sizeLimit = sizeLimit;
this.spaceReclaimSplit = spaceReclaimMode>4;
this.spaceReclaimReuse = spaceReclaimMode>2;
this.spaceReclaimTrack = spaceReclaimMode>0;
boolean allGood = false;
try{
index = volFac.createIndexVolume();
phys = volFac.createPhysVolume();
if(index.isEmpty()){
createStructure();
}else{
checkHeaders();
indexSize = index.getLong(IO_INDEX_SIZE);
physSize = index.getLong(IO_PHYS_SIZE);
freeSize = index.getLong(IO_FREE_SIZE);
maxUsedIoList=IO_USER_START-8;
while(index.getLong(maxUsedIoList)!=0 && maxUsedIoList>IO_FREE_RECID)
maxUsedIoList-=8;
}
allGood = true;
}finally{
if(!allGood){
//exception was thrown, try to unlock files
if(index!=null){
index.sync();
index.close();
index = null;
}
if(phys!=null){
phys.sync();
phys.close();
phys = null;
}
}
}
}
public StoreDirect(Volume.Factory volFac) {
this(volFac, false,false,5,false,0L, false,false,null,false,0);
}
protected void checkHeaders() {
if(index.getInt(0)!=HEADER||phys.getInt(0)!=HEADER)
throw new IOError(new IOException("storage has invalid header"));
if(index.getUnsignedShort(4)>StoreDirect.STORE_VERSION || phys.getUnsignedShort(4)>StoreDirect.STORE_VERSION )
throw new IOError(new IOException("New store format version, please use newer MapDB version"));
final int masks = index.getUnsignedShort(6);
if(masks!=phys.getUnsignedShort(6))
throw new IllegalArgumentException("Index and Phys file have different feature masks");
if(masks!=expectedMasks())
throw new IllegalArgumentException("File created with different features. Please check compression, checksum or encryption");
long checksum = index.getLong(IO_INDEX_SUM);
if(checksum!=indexHeaderChecksum())
throw new IOError(new IOException("Wrong index checksum, store was not closed properly and could be corrupted."));
}
protected void createStructure() {
indexSize = IO_USER_START+LAST_RESERVED_RECID*8+8;
assert(indexSize>IO_USER_START);
index.ensureAvailable(indexSize);
for(int i=0;i<indexSize;i+=8) index.putLong(i,0L);
index.putInt(0, HEADER);
index.putUnsignedShort(4,STORE_VERSION);
index.putUnsignedShort(6,expectedMasks());
index.putLong(IO_INDEX_SIZE,indexSize);
physSize =16;
index.putLong(IO_PHYS_SIZE,physSize);
phys.ensureAvailable(physSize);
phys.putInt(0, HEADER);
phys.putUnsignedShort(4,STORE_VERSION);
phys.putUnsignedShort(6,expectedMasks());
freeSize = 0;
index.putLong(IO_FREE_SIZE,freeSize);
index.putLong(IO_INDEX_SUM,indexHeaderChecksum());
}
protected long indexHeaderChecksum(){
long ret = 0;
for(long offset = 0;offset<IO_USER_START;offset+=8){
if(offset == IO_INDEX_SUM) continue;
long indexVal = index.getLong(offset);
ret |= indexVal | LongHashMap.longHash(indexVal|offset) ;
}
return ret;
}
@Override
public long preallocate() {
if(!disableLocks) {
newRecidLock.readLock().lock();
}
try{
if(!disableLocks) {
structuralLock.lock();
}
final long ioRecid;
try{
ioRecid = freeIoRecidTake(true) ;
}finally {
if(!disableLocks) {
structuralLock.unlock();
}
}
final Lock lock;
if(disableLocks) {
lock = null;
}else {
lock = locks[Store.lockPos(ioRecid)].writeLock();
lock.lock();
}
try{
index.putLong(ioRecid,MASK_DISCARD);
}finally {
if(!disableLocks) {
lock.unlock();
}
}
long recid = (ioRecid-IO_USER_START)/8;
assert(recid>0);
if(CC.LOG_STORE && LOG.isLoggable(Level.FINEST))
LOG.finest("Preallocate recid=" + recid);
return recid;
}finally {
if(!disableLocks) {
newRecidLock.readLock().unlock();
}
}
}
@Override
public void preallocate(long[] recids) {
if(!disableLocks) {
newRecidLock.readLock().lock();
}
try{
if(!disableLocks) {
structuralLock.lock();
}
try{
for(int i=0;i<recids.length;i++)
recids[i] = freeIoRecidTake(true) ;
}finally {
if(!disableLocks) {
structuralLock.unlock();
}
}
for(int i=0;i<recids.length;i++){
final long ioRecid = recids[i];
final Lock lock;
if(disableLocks) {
lock = null;
}else {
lock = locks[Store.lockPos(ioRecid)].writeLock();
lock.lock();
}
try{
index.putLong(ioRecid,MASK_DISCARD);
}finally {
if(!disableLocks) {
lock.unlock();
}
}
recids[i] = (ioRecid-IO_USER_START)/8;
assert(recids[i]>0);
}
if(CC.LOG_STORE && LOG.isLoggable(Level.FINEST))
LOG.finest("Preallocate recids="+Arrays.toString(recids));
}finally {
if(!disableLocks) {
newRecidLock.readLock().unlock();
}
}
}
@Override
public <A> long put(A value, Serializer<A> serializer) {
assert(value!=null);
DataOutput2 out = serialize(value, serializer);
final long ioRecid;
if(!disableLocks) {
newRecidLock.readLock().lock();
}
try{
if(!disableLocks) {
structuralLock.lock();
}
final long[] indexVals;
try{
ioRecid = freeIoRecidTake(true) ;
indexVals = physAllocate(out.pos,true,false);
}finally {
if(!disableLocks) {
structuralLock.unlock();
}
}
final Lock lock;
if(disableLocks) {
lock = null;
}else {
lock = locks[Store.lockPos(ioRecid)].writeLock();
lock.lock();
}
try{
put2(out, ioRecid, indexVals);
}finally {
if(!disableLocks) {
lock.unlock();
}
}
}finally {
if(!disableLocks) {
newRecidLock.readLock().unlock();
}
}
long recid = (ioRecid-IO_USER_START)/8;
assert(recid>0);
if(CC.LOG_STORE && LOG.isLoggable(Level.FINEST))
LOG.finest("Put recid="+recid+", "+" size="+out.pos+", "+" val="+value+" ser="+serializer );
recycledDataOuts.offer(out);
return recid;
}
protected void put2(DataOutput2 out, long ioRecid, long[] indexVals) {
assert(disableLocks || locks[Store.lockPos(ioRecid)].writeLock().isHeldByCurrentThread());
index.putLong(ioRecid, indexVals[0]|MASK_ARCHIVE);
//write stuff
if(indexVals.length==1||indexVals[1]==0){ //is more then one? ie linked
//write single
phys.putData(indexVals[0]&MASK_OFFSET, out.buf, 0, out.pos);
}else{
int outPos = 0;
//write linked
for(int i=0;i<indexVals.length;i++){
final int c = i==indexVals.length-1 ? 0: 8;
final long indexVal = indexVals[i];
final boolean isLast = (indexVal & MASK_LINKED) ==0;
assert(isLast==(i==indexVals.length-1));
final int size = (int) (indexVal>>>48);
final long offset = indexVal&MASK_OFFSET;
//write data
phys.putData(offset+c,out.buf,outPos, size-c);
outPos+=size-c;
if(c>0){
//write position of next linked record
phys.putLong(offset, indexVals[i + 1]);
}
}
if(outPos!=out.pos) throw new AssertionError();
}
}
@Override
public <A> A get(long recid, Serializer<A> serializer) {
assert(recid>0);
final long ioRecid = IO_USER_START + recid*8;
final Lock lock;
if(disableLocks) {
lock = null;
}else {
lock = locks[Store.lockPos(ioRecid)].readLock();
lock.lock();
}
try{
final A ret = get2(ioRecid,serializer);
if(CC.LOG_STORE && LOG.isLoggable(Level.FINEST))
LOG.finest("GET recid="+recid+", "+" ret="+ret+", "+" ser="+serializer );
return ret;
}catch(IOException e){
throw new IOError(e);
}finally{
if(!disableLocks) {
lock.unlock();
}
}
}
protected <A> A get2(long ioRecid,Serializer<A> serializer) throws IOException {
assert(disableLocks || locks[Store.lockPos(ioRecid)].getWriteHoldCount()==0||
locks[Store.lockPos(ioRecid)].writeLock().isHeldByCurrentThread());
long indexVal = index.getLong(ioRecid);
if(indexVal == MASK_DISCARD) return null; //preallocated record
int size = (int) (indexVal>>>48);
DataInput di;
long offset = indexVal&MASK_OFFSET;
if((indexVal& MASK_LINKED)==0){
//read single record
di = phys.getDataInput(offset, size);
}else{
//is linked, first construct buffer we will read data to
int pos = 0;
int c = 8;
//TODO use mapped bb and direct copying?
byte[] buf = new byte[64];
//read parts into segment
for(;;){
DataInput in = phys.getDataInput(offset + c, size-c);
if(buf.length<pos+size-c)
buf = Arrays.copyOf(buf,Math.max(pos+size-c,buf.length*2)); //buf to small, grow
in.readFully(buf,pos,size-c);
pos+=size-c;
if(c==0) break;
//read next part
long next = phys.getLong(offset);
offset = next&MASK_OFFSET;
size = (int) (next>>>48);
//is the next part last?
c = ((next& MASK_LINKED)==0)? 0 : 8;
}
di = new DataIO.DataInputByteArray(buf);
size = pos;
}
return deserialize(serializer, size, di);
}
@Override
public <A> void update(long recid, A value, Serializer<A> serializer) {
assert(value!=null);
assert(recid>0);
DataOutput2 out = serialize(value, serializer);
final long ioRecid = IO_USER_START + recid*8;
final Lock lock;
if(disableLocks) {
lock = null;
}else {
lock = locks[Store.lockPos(ioRecid)].writeLock();
lock.lock();
}
try{
update2(out, ioRecid);
}finally{
if(!disableLocks) {
lock.unlock();
}
}
if(CC.LOG_STORE && LOG.isLoggable(Level.FINEST))
LOG.finest("Update recid="+recid+", "+" size="+out.pos+", "+" val="+value+" ser="+serializer );
recycledDataOuts.offer(out);
}
protected void update2(DataOutput2 out, long ioRecid) {
final long indexVal = index.getLong(ioRecid);
final int size = (int) (indexVal>>>48);
final boolean linked = (indexVal&MASK_LINKED)!=0;
assert(disableLocks || locks[Store.lockPos(ioRecid)].writeLock().isHeldByCurrentThread());
if(!linked && out.pos>0 && size>0 && size2ListIoRecid(size) == size2ListIoRecid(out.pos)){
//size did change, but still fits into this location
final long offset = indexVal & MASK_OFFSET;
//note: if size would not change, we still have to write MASK_ARCHIVE bit
index.putLong(ioRecid, (((long)out.pos)<<48)|offset|MASK_ARCHIVE);
phys.putData(offset, out.buf, 0, out.pos);
}else{
long[] indexVals = spaceReclaimTrack ? getLinkedRecordsIndexVals(indexVal) : null;
if(!disableLocks) {
structuralLock.lock();
}
try{
if(spaceReclaimTrack){
//free first record pointed from indexVal
if(size>0)
freePhysPut(indexVal,false);
//if there are more linked records, free those as well
if(indexVals!=null){
for(int i=0;i<indexVals.length && indexVals[i]!=0;i++){
freePhysPut(indexVals[i],false);
}
}
}
indexVals = physAllocate(out.pos,true,false);
}finally {
if(!disableLocks) {
structuralLock.unlock();
}
}
put2(out, ioRecid, indexVals);
}
assert(disableLocks || locks[Store.lockPos(ioRecid)].writeLock().isHeldByCurrentThread());
}
@Override
public <A> boolean compareAndSwap(long recid, A expectedOldValue, A newValue, Serializer<A> serializer) {
assert(expectedOldValue!=null && newValue!=null);
assert(recid>0);
final long ioRecid = IO_USER_START + recid*8;
final Lock lock;
if(disableLocks) {
lock = null;
}else {
lock = locks[Store.lockPos(ioRecid)].writeLock();
lock.lock();
}
DataOutput2 out;
try{
/*
* deserialize old value
*/
A oldVal = get2(ioRecid,serializer);
/*
* compare oldValue and expected
*/
if((oldVal == null && expectedOldValue!=null) || (oldVal!=null && !oldVal.equals(expectedOldValue)))
return false;
/*
* write new value
*/
out = serialize(newValue, serializer);
update2(out, ioRecid);
}catch(IOException e){
throw new IOError(e);
}finally{
if(!disableLocks) {
lock.unlock();
}
}
recycledDataOuts.offer(out);
return true;
}
@Override
public <A> void delete(long recid, Serializer<A> serializer) {
assert(recid>0);
final long ioRecid = IO_USER_START + recid*8;
final Lock lock;
if(disableLocks) {
lock = null;
}else {
lock = locks[Store.lockPos(ioRecid)].writeLock();
lock.lock();
}
try{
//get index val and zero it out
final long indexVal = index.getLong(ioRecid);
index.putLong(ioRecid,0L|MASK_ARCHIVE);
if(!spaceReclaimTrack) return; //free space is not tracked, so do not mark stuff as free
long[] linkedRecords = getLinkedRecordsIndexVals(indexVal);
//now lock everything and mark free space
if(!disableLocks) {
structuralLock.lock();
}
try{
//free recid
freeIoRecidPut(ioRecid);
//free first record pointed from indexVal\
if((indexVal>>>48)>0)
freePhysPut(indexVal,false);
//if there are more linked records, free those as well
if(linkedRecords!=null){
for(int i=0; i<linkedRecords.length &&linkedRecords[i]!=0;i++){
freePhysPut(linkedRecords[i],false);
}
}
}finally {
if(!disableLocks) {
structuralLock.unlock();
}
}
}finally{
if(!disableLocks) {
lock.unlock();
}
}
}
protected long[] getLinkedRecordsIndexVals(long indexVal) {
long[] linkedRecords = null;
int linkedPos = 0;
if((indexVal& MASK_LINKED)!=0){
//record is composed of multiple linked records, so collect all of them
linkedRecords = new long[2];
//traverse linked records
long linkedVal = phys.getLong(indexVal&MASK_OFFSET);
for(;;){
if(linkedPos==linkedRecords.length) //grow if necessary
linkedRecords = Arrays.copyOf(linkedRecords, linkedRecords.length * 2);
//store last linkedVal
linkedRecords[linkedPos] = linkedVal;
if((linkedVal& MASK_LINKED)==0){
break; //this is last linked record, so break
}
//move and read to next
linkedPos++;
linkedVal = phys.getLong(linkedVal&MASK_OFFSET);
}
}
return linkedRecords;
}
protected long[] physAllocate(int size, boolean ensureAvail,boolean recursive) {
assert(disableLocks || structuralLock.isHeldByCurrentThread());
if(size==0L) return new long[]{0L};
//append to end of file
if(size<MAX_REC_SIZE){
long indexVal = freePhysTake(size,ensureAvail,recursive);
indexVal |= ((long)size)<<48;
return new long[]{indexVal};
}else{
long[] ret = new long[2];
int retPos = 0;
int c = 8;
while(size>0){
if(retPos == ret.length) ret = Arrays.copyOf(ret, ret.length*2);
int allocSize = Math.min(size, MAX_REC_SIZE);
size -= allocSize - c;
//append to end of file
long indexVal = freePhysTake(allocSize, ensureAvail,recursive);
indexVal |= (((long)allocSize)<<48);
if(c!=0) indexVal|= MASK_LINKED;
ret[retPos++] = indexVal;
c = size<=MAX_REC_SIZE ? 0 : 8;
}
if(size!=0) throw new AssertionError();
return Arrays.copyOf(ret, retPos);
}
}
protected static long roundTo16(long offset){
long rem = offset&15; // modulo 16
if(rem!=0) offset +=16-rem;
return offset;
}
@Override
public void close() {
for(Runnable closeListener:closeListeners)
closeListener.run();
lockAllWrite();
try{
try {
if(!readOnly){
if(serializerPojo!=null && serializerPojo.hasUnsavedChanges()){
serializerPojo.save(this);
}
index.putLong(IO_PHYS_SIZE,physSize);
index.putLong(IO_INDEX_SIZE,indexSize);
index.putLong(IO_FREE_SIZE,freeSize);
index.putLong(IO_INDEX_SUM,indexHeaderChecksum());
}
// Syncs are expensive -- don't sync if the files are going to
// get deleted anyway.
if (!deleteFilesAfterClose) {
index.sync();
phys.sync();
}
} finally {
try {
index.close();
} finally {
try {
phys.close();
} finally {
if(deleteFilesAfterClose){
index.deleteFile();
phys.deleteFile();
}
index = null;
phys = null;
}
}
}
}finally{
unlockAllWrite();
}
}
@Override
public boolean isClosed() {
return index==null;
}
@Override
public void commit() {
if(!readOnly){
if(serializerPojo!=null && serializerPojo.hasUnsavedChanges()){
serializerPojo.save(this);
}
index.putLong(IO_PHYS_SIZE,physSize);
index.putLong(IO_INDEX_SIZE,indexSize);
index.putLong(IO_FREE_SIZE,freeSize);
index.putLong(IO_INDEX_SUM, indexHeaderChecksum());
}
if(!syncOnCommitDisabled){
index.sync();
phys.sync();
}
}
@Override
public void rollback() throws UnsupportedOperationException {
throw new UnsupportedOperationException("rollback not supported with journal disabled");
}
@Override
public boolean isReadOnly() {
return readOnly;
}
@Override
public boolean canRollback(){
return false;
}
@Override
public void clearCache() {
}
@Override
public void compact() {
if(readOnly) throw new IllegalAccessError();
final File indexFile = index.getFile();
final File physFile = phys.getFile();
final int rafMode;
if(index instanceof Volume.FileChannelVol){
rafMode=2;
}else if(index instanceof Volume.MappedFileVol && phys instanceof Volume.FileChannelVol){
rafMode = 1;
}else{
rafMode = 0;
}
lockAllWrite();
try{
final File compactedFile = new File((indexFile!=null?indexFile:File.createTempFile("mapdb","compact"))+".compact");
Volume.Factory fab = Volume.fileFactory(compactedFile,rafMode,false,sizeLimit, CC.VOLUME_SLICE_SHIFT,0);
StoreDirect store2 = new StoreDirect(fab,false,false,5,false,0L, checksum,compress,password,false,0);
compactPreUnderLock();
index.putLong(IO_PHYS_SIZE,physSize);
index.putLong(IO_INDEX_SIZE,indexSize);
index.putLong(IO_FREE_SIZE,freeSize);
//create secondary files for compaction
store2.lockAllWrite();
//transfer stack of free recids
//TODO long stack take modifies the original store
for(long recid =longStackTake(IO_FREE_RECID,false);
recid!=0; recid=longStackTake(IO_FREE_RECID,false)){
store2.longStackPut(IO_FREE_RECID,recid, false);
}
//iterate over recids and transfer physical records
store2.index.putLong(IO_INDEX_SIZE, indexSize);
for(long ioRecid = IO_USER_START; ioRecid<indexSize;ioRecid+=8){
byte[] bb = get2(ioRecid,Serializer.BYTE_ARRAY_NOSIZE);
store2.index.ensureAvailable(ioRecid+8);
if(bb==null||bb.length==0){
store2.index.putLong(ioRecid,0);
}else{
DataOutput2 out = serialize(bb,Serializer.BYTE_ARRAY_NOSIZE);
long[] indexVals = store2.physAllocate(out.pos,true,false);
store2.put2(out, ioRecid,indexVals);
}
}
File indexFile2 = store2.index.getFile();
File physFile2 = store2.phys.getFile();
store2.unlockAllWrite();
final boolean useDirectBuffer = index instanceof Volume.MemoryVol &&
((Volume.MemoryVol)index).useDirectBuffer;
index.sync(); //TODO is sync needed here?
index.close();
index = null;
phys.sync(); //TODO is sync needed here?
phys.close();
phys = null;
if(indexFile != null){
final long time = System.currentTimeMillis();
final File indexFile_ = indexFile!=null? new File(indexFile.getPath()+"_"+time+"_orig"): null;
final File physFile_ = physFile!=null? new File(physFile.getPath()+"_"+time+"_orig") : null;
store2.close();
//not in memory, so just rename files
if(!indexFile.renameTo(indexFile_))
throw new AssertionError("could not rename file");
if(!physFile.renameTo(physFile_))
throw new AssertionError("could not rename file");
if(!indexFile2.renameTo(indexFile))
throw new AssertionError("could not rename file");
//TODO process may fail in middle of rename, analyze sequence and add recovery
if(!physFile2.renameTo(physFile))
throw new AssertionError("could not rename file");
final Volume.Factory fac2 = Volume.fileFactory(indexFile,rafMode, false, sizeLimit, CC.VOLUME_SLICE_SHIFT,0);
index = fac2.createIndexVolume();
phys = fac2.createPhysVolume();
indexFile_.delete();
physFile_.delete();
}else{
//in memory, so copy files into memory
Volume indexVol2 = new Volume.MemoryVol(useDirectBuffer,sizeLimit, CC.VOLUME_SLICE_SHIFT);
Volume.volumeTransfer(indexSize, store2.index, indexVol2);
Volume physVol2 = new Volume.MemoryVol(useDirectBuffer,sizeLimit, CC.VOLUME_SLICE_SHIFT);
Volume.volumeTransfer(store2.physSize, store2.phys, physVol2);
store2.close();
index = indexVol2;
phys = physVol2;
}
physSize = store2.physSize;
freeSize = store2.freeSize;
index.putLong(IO_PHYS_SIZE, physSize);
index.putLong(IO_INDEX_SIZE, indexSize);
index.putLong(IO_FREE_SIZE, freeSize);
index.putLong(IO_INDEX_SUM,indexHeaderChecksum());
maxUsedIoList=IO_USER_START-8;
while(index.getLong(maxUsedIoList)!=0 && maxUsedIoList>IO_FREE_RECID)
maxUsedIoList-=8;
compactPostUnderLock();
}catch(IOException e){
throw new IOError(e);
}finally {
unlockAllWrite();
}
}
/** subclasses put additional checks before compaction starts here */
protected void compactPreUnderLock() {
}
/** subclasses put additional cleanup after compaction finishes here */