Repository navigation
Expand file tree
/
Copy pathVolume.java
More file actions
2963 lines (2421 loc) · 94.4 KB
/
Copy pathVolume.java
File metadata and controls
2963 lines (2421 loc) · 94.4 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.*;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Arrays;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* <p>
* MapDB abstraction over raw storage (file, disk partition, memory etc...).
* </p><p>
*
* Implementations needs to be thread safe (especially
* 'ensureAvailable') operation.
* However updates do not have to be atomic, it is clients responsibility
* to ensure two threads are not writing/reading into the same location.
* </p>
*
* @author Jan Kotek
*/
public abstract class Volume implements Closeable{
static int sliceShiftFromSize(long sizeIncrement) {
//TODO optimize this method with bitcount operation
sizeIncrement = DataIO.nextPowTwo(sizeIncrement);
for(int i=0;i<32;i++){
if((1L<<i)==sizeIncrement){
return i;
}
}
throw new AssertionError("Could not find sliceShift");
}
static boolean isEmptyFile(String fileName) {
if(fileName==null || fileName.length()==0)
return true;
File f = new File(fileName);
return !f.exists() || f.length()==0;
}
public static abstract class VolumeFactory{
public abstract Volume makeVolume(String file, boolean readOnly, boolean fileLockDisabled,
int sliceShift, long initSize, boolean fixedSize);
public Volume makeVolume(String file, boolean readOnly){
return makeVolume(file,readOnly,false);
}
public Volume makeVolume(String file, boolean readOnly, boolean fileLockDisable){
return makeVolume(file,readOnly,fileLockDisable,CC.VOLUME_PAGE_SHIFT, 0, false);
}
}
private static final byte[] CLEAR = new byte[1024];
protected static final Logger LOG = Logger.getLogger(Volume.class.getName());
/**
* If {@code sun.misc.Unsafe} is available it will use Volume based on Unsafe.
* If Unsafe is not available for some reason (Android), use DirectByteBuffer instead.
*/
public static final VolumeFactory UNSAFE_VOL_FACTORY = new VolumeFactory() {
@Override
public Volume makeVolume(String file, boolean readOnly, boolean fileLockDisabled, int sliceShift, long initSize, boolean fixedSize) {
String packageName = Volume.class.getPackage().getName();
Class clazz;
try {
clazz = Class.forName(packageName+".UnsafeStuff$UnsafeVolume");
} catch (ClassNotFoundException e) {
clazz = null;
}
if(clazz!=null){
try {
return (Volume) clazz.getConstructor(long.class, int.class, long.class)
.newInstance(0L, sliceShift, initSize);
} catch (Exception e) {
LOG.log(Level.WARNING, "Could not invoke UnsafeVolume constructor. " +
"Falling back to DirectByteBuffer",e);
}
}
return MemoryVol.FACTORY.makeVolume(file, readOnly, fileLockDisabled, sliceShift, initSize, fixedSize);
}
};
protected volatile boolean closed;
public boolean isClosed(){
return closed;
}
//uncomment to get stack trace on Volume leak warning
// final private Throwable constructorStackTrace = new AssertionError();
@Override protected void finalize(){
if(CC.ASSERT){
if(!closed
&& !(this instanceof ByteArrayVol)
&& !(this instanceof SingleByteArrayVol)){
LOG.log(Level.WARNING, "Open Volume was GCed, possible file handle leak."
// ,constructorStackTrace
);
}
}
}
/**
* Check space allocated by Volume is bigger or equal to given offset.
* So it is safe to write into smaller offsets.
*
* @param offset
*/
abstract public void ensureAvailable(final long offset);
public abstract void truncate(long size);
abstract public void putLong(final long offset, final long value);
abstract public void putInt(long offset, int value);
abstract public void putByte(final long offset, final byte value);
abstract public void putData(final long offset, final byte[] src, int srcPos, int srcSize);
abstract public void putData(final long offset, final ByteBuffer buf);
public void putDataOverlap(final long offset, final byte[] src, int srcPos, int srcSize){
putData(offset,src,srcPos,srcSize);
}
abstract public long getLong(final long offset);
abstract public int getInt(long offset);
abstract public byte getByte(final long offset);
abstract public DataInput getDataInput(final long offset, final int size);
public DataInput getDataInputOverlap(final long offset, final int size){
return getDataInput(offset,size);
}
abstract public void getData(long offset, byte[] bytes, int bytesPos, int size);
abstract public void close();
abstract public void sync();
/**
*
* @return slice size or {@code -1} if not sliced
*/
abstract public int sliceSize();
public void deleteFile(){
File f = getFile();
if(f!=null && f.isFile() && !f.delete()){
LOG.warning("Could not delete file: "+f);
}
}
public abstract boolean isSliced();
public abstract long length();
public void putUnsignedShort(final long offset, final int value){
putByte(offset, (byte) (value>>8));
putByte(offset+1, (byte) (value));
}
public int getUnsignedShort(long offset) {
return (( (getByte(offset) & 0xff) << 8) |
( (getByte(offset+1) & 0xff)));
}
public int getUnsignedByte(long offset) {
return getByte(offset) & 0xff;
}
public void putUnsignedByte(long offset, int b) {
putByte(offset, (byte) (b & 0xff));
}
public int putLongPackBidi(long offset, long value) {
putUnsignedByte(offset++, (((int) value & 0x7F)) | 0x80);
value >>>= 7;
int counter = 2;
//$DELAY$
while ((value & ~0x7FL) != 0) {
putUnsignedByte(offset++, (((int) value & 0x7F)));
value >>>= 7;
//$DELAY$
counter++;
}
//$DELAY$
putUnsignedByte(offset, (byte) value | 0x80);
return counter;
}
public long getLongPackBidi(long offset){
//$DELAY$
long b = getUnsignedByte(offset++); //TODO this could be inside loop, change all implementations
if(CC.ASSERT && (b&0x80)==0)
throw new DBException.DataCorruption();
long result = (b & 0x7F) ;
int shift = 7;
do {
//$DELAY$
b = getUnsignedByte(offset++);
result |= (b & 0x7F) << shift;
if(CC.ASSERT && shift>64)
throw new DBException.DataCorruption();
shift += 7;
}while((b & 0x80) == 0);
//$DELAY$
return (((long)(shift/7))<<56) | result;
}
public long getLongPackBidiReverse(long offset){
//$DELAY$
long b = getUnsignedByte(--offset);
if(CC.ASSERT && (b&0x80)==0)
throw new DBException.DataCorruption();
long result = (b & 0x7F) ;
int counter = 1;
do {
//$DELAY$
b = getUnsignedByte(--offset);
result = (b & 0x7F) | (result<<7);
if(CC.ASSERT && counter>8)
throw new DBException.DataCorruption();
counter++;
}while((b & 0x80) == 0);
//$DELAY$
return (((long)counter)<<56) | result;
}
public long getSixLong(long pos) {
return
((long) (getByte(pos++) & 0xff) << 40) |
((long) (getByte(pos++) & 0xff) << 32) |
((long) (getByte(pos++) & 0xff) << 24) |
((long) (getByte(pos++) & 0xff) << 16) |
((long) (getByte(pos++) & 0xff) << 8) |
((long) (getByte(pos) & 0xff));
}
public void putSixLong(long pos, long value) {
if(CC.ASSERT && (value>>>48!=0))
throw new DBException.DataCorruption();
putByte(pos++, (byte) (0xff & (value >> 40)));
putByte(pos++, (byte) (0xff & (value >> 32)));
putByte(pos++, (byte) (0xff & (value >> 24)));
putByte(pos++, (byte) (0xff & (value >> 16)));
putByte(pos++, (byte) (0xff & (value >> 8)));
putByte(pos, (byte) (0xff & (value)));
}
/**
* Put packed long at given position.
*
* @param value to be written
* @return number of bytes consumed by packed value
*/
public int putPackedLong(long pos, long value){
//$DELAY$
int ret = 0;
int shift = 63-Long.numberOfLeadingZeros(value);
shift -= shift%7; // round down to nearest multiple of 7
while(shift!=0){
putByte(pos + (ret++), (byte) (((value >>> shift) & 0x7F) | 0x80));
//$DELAY$
shift-=7;
}
putByte(pos+(ret++),(byte) (value & 0x7F));
return ret;
}
/**
* Unpack long value from the Volume. Highest 4 bits reused to indicate number of bytes read from Volume.
* One can use {@code result & DataIO.PACK_LONG_RESULT_MASK} to remove size;
*
* @param position to read value from
* @return The long value, minus highest byte
*/
public long getPackedLong(long position){
long ret = 0;
long pos2 = 0;
byte v;
do{
v = getByte(position+(pos2++));
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
return (pos2<<60) | ret;
}
/** returns underlying file if it exists */
abstract public File getFile();
/** return true if this Volume holds exclusive lock over its file */
abstract public boolean getFileLocked();
/**
* Transfers data from this Volume into target volume.
* If its possible, the implementation should override this method to enable direct memory transfer.
*
* Caller must respect slice boundaries. ie it is not possible to transfer data which cross slice boundaries.
*
* @param inputOffset offset inside this Volume, ie data will be read from this offset
* @param target Volume to copy data into
* @param targetOffset position in target volume where data will be copied into
* @param size size of data to copy
*/
public void transferInto(long inputOffset, Volume target, long targetOffset, long size) {
//TODO size>Integer.MAX_VALUE
byte[] data = new byte[(int) size];
try {
getDataInput(inputOffset, (int) size).readFully(data);
}catch(IOException e){
throw new DBException.VolumeIOError(e);
}
target.putData(targetOffset,data,0, (int) size);
}
/**
* Set all bytes between {@code startOffset} and {@code endOffset} to zero.
* Area between offsets must be ready for write once clear finishes.
*/
public abstract void clear(long startOffset, long endOffset);
/**
* Copy content of this volume to another.
* Target volume might grow, but is never shrank.
* Target is also not synced
*/
public void copyEntireVolumeTo(Volume to) {
final long volSize = length();
final long bufSize = 1L<<CC.VOLUME_PAGE_SHIFT;
to.ensureAvailable(volSize);
for(long offset=0;offset<volSize;offset+=bufSize){
long size = Math.min(volSize,offset+bufSize)-offset;
if(CC.ASSERT && (size<0))
throw new AssertionError();
transferInto(offset,to,offset, size);
}
}
/**
* Abstract Volume over bunch of ByteBuffers
* It leaves ByteBufferVol details (allocation, disposal) on subclasses.
* Most methods are final for better performance (JIT compiler can inline those).
*/
abstract static public class ByteBufferVol extends Volume{
protected final boolean cleanerHackEnabled;
protected final ReentrantLock growLock = new ReentrantLock(CC.FAIR_LOCKS);
protected final int sliceShift;
protected final int sliceSizeModMask;
protected final int sliceSize;
protected volatile ByteBuffer[] slices = new ByteBuffer[0];
protected final boolean readOnly;
protected ByteBufferVol(boolean readOnly, int sliceShift, boolean cleanerHackEnabled) {
this.readOnly = readOnly;
this.sliceShift = sliceShift;
this.cleanerHackEnabled = cleanerHackEnabled;
this.sliceSize = 1<< sliceShift;
this.sliceSizeModMask = sliceSize -1;
}
protected final ByteBuffer getSlice(long offset){
ByteBuffer[] slices = this.slices;
int pos = (int)(offset >>> sliceShift);
if(pos>=slices.length)
throw new DBException.VolumeEOF("Get/Set beyong file size. Requested offset: "+offset+", volume size: "+length());
return slices[pos];
}
@Override public final void putLong(final long offset, final long value) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+8){
new IOException("VOL STACK:").printStackTrace();
}
getSlice(offset).putLong((int) (offset & sliceSizeModMask), value);
}
@Override public final void putInt(final long offset, final int value) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+4){
new IOException("VOL STACK:").printStackTrace();
}
getSlice(offset).putInt((int) (offset & sliceSizeModMask), value);
}
@Override public final void putByte(final long offset, final byte value) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+1){
new IOException("VOL STACK:").printStackTrace();
}
getSlice(offset).put((int) (offset & sliceSizeModMask), value);
}
@Override public void putData(final long offset, final byte[] src, int srcPos, int srcSize){
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+srcSize){
new IOException("VOL STACK:").printStackTrace();
}
final ByteBuffer b1 = getSlice(offset).duplicate();
final int bufPos = (int) (offset& sliceSizeModMask);
b1.position(bufPos);
b1.put(src, srcPos, srcSize);
}
@Override public final void putData(final long offset, final ByteBuffer buf) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+buf.remaining()){
new IOException("VOL STACK:").printStackTrace();
}
final ByteBuffer b1 = getSlice(offset).duplicate();
final int bufPos = (int) (offset& sliceSizeModMask);
//no overlap, so just write the value
b1.position(bufPos);
b1.put(buf);
}
@Override
public void transferInto(long inputOffset, Volume target, long targetOffset, long size) {
final ByteBuffer b1 =getSlice(inputOffset).duplicate();
final int bufPos = (int) (inputOffset& sliceSizeModMask);
b1.position(bufPos);
//TODO size>Integer.MAX_VALUE
b1.limit((int) (bufPos+size));
target.putData(targetOffset, b1);
}
@Override public void getData(final long offset, final byte[] src, int srcPos, int srcSize){
final ByteBuffer b1 = getSlice(offset).duplicate();
final int bufPos = (int) (offset& sliceSizeModMask);
b1.position(bufPos);
b1.get(src, srcPos, srcSize);
}
@Override final public long getLong(long offset) {
return getSlice(offset).getLong((int) (offset & sliceSizeModMask));
}
@Override final public int getInt(long offset) {
return getSlice(offset).getInt((int) (offset & sliceSizeModMask));
}
@Override public final byte getByte(long offset) {
return getSlice(offset).get((int) (offset & sliceSizeModMask));
}
@Override
public final DataIO.DataInputByteBuffer getDataInput(long offset, int size) {
return new DataIO.DataInputByteBuffer(getSlice(offset), (int) (offset& sliceSizeModMask));
}
@Override
public void putDataOverlap(long offset, byte[] data, int pos, int len) {
boolean overlap = (offset>>>sliceShift != (offset+len)>>>sliceShift);
if(overlap){
while(len>0){
ByteBuffer b = getSlice(offset).duplicate();
b.position((int) (offset&sliceSizeModMask));
int toPut = Math.min(len,sliceSize - b.position());
b.limit(b.position()+toPut);
b.put(data, pos, toPut);
pos+=toPut;
len-=toPut;
offset+=toPut;
}
}else{
putData(offset,data,pos,len);
}
}
@Override
public DataInput getDataInputOverlap(long offset, int size) {
boolean overlap = (offset>>>sliceShift != (offset+size)>>>sliceShift);
if(overlap){
byte[] bb = new byte[size];
final int origLen = size;
while(size>0){
ByteBuffer b = getSlice(offset).duplicate();
b.position((int) (offset&sliceSizeModMask));
int toPut = Math.min(size,sliceSize - b.position());
b.limit(b.position()+toPut);
b.get(bb,origLen-size,toPut);
size -=toPut;
offset+=toPut;
}
return new DataIO.DataInputByteArray(bb);
}else{
//return mapped buffer
return getDataInput(offset,size);
}
}
@Override
public void putUnsignedShort(long offset, int value) {
final ByteBuffer b = getSlice(offset);
int bpos = (int) (offset & sliceSizeModMask);
b.put(bpos++, (byte) (value >> 8));
b.put(bpos, (byte) (value));
}
@Override
public int getUnsignedShort(long offset) {
final ByteBuffer b = getSlice(offset);
int bpos = (int) (offset & sliceSizeModMask);
return (( (b.get(bpos++) & 0xff) << 8) |
( (b.get(bpos) & 0xff)));
}
@Override
public int getUnsignedByte(long offset) {
final ByteBuffer b = getSlice(offset);
int bpos = (int) (offset & sliceSizeModMask);
return b.get(bpos) & 0xff;
}
@Override
public void putUnsignedByte(long offset, int byt) {
final ByteBuffer b = getSlice(offset);
int bpos = (int) (offset & sliceSizeModMask);
b.put(bpos, toByte(byt));
}
protected static byte toByte(int byt) {
return (byte) (byt & 0xff);
}
protected static byte toByte(long l) {
return (byte) (l & 0xff);
}
@Override
public int putLongPackBidi(long offset, long value) {
final ByteBuffer b = getSlice(offset);
int bpos = (int) (offset & sliceSizeModMask);
b.put(bpos++, toByte((value & 0x7F) | 0x80));
value >>>= 7;
int counter = 2;
//$DELAY$
while ((value & ~0x7FL) != 0) {
b.put(bpos++, toByte(value & 0x7F));
value >>>= 7;
//$DELAY$
counter++;
}
//$DELAY$
b.put(bpos, toByte(value | 0x80));
return counter;
}
@Override
public long getLongPackBidi(long offset) {
final ByteBuffer bb = getSlice(offset);
int bpos = (int) (offset & sliceSizeModMask);
//$DELAY$
long b = bb.get(bpos++) & 0xffL; //TODO this could be inside loop, change all implementations
if(CC.ASSERT && (b&0x80)==0)
throw new DBException.DataCorruption();
long result = (b & 0x7F) ;
int shift = 7;
do {
//$DELAY$
b = bb.get(bpos++) & 0xffL;
result |= (b & 0x7F) << shift;
if(CC.ASSERT && shift>64)
throw new DBException.DataCorruption();
shift += 7;
}while((b & 0x80) == 0);
//$DELAY$
return (((long)(shift/7))<<56) | result;
}
@Override
public long getLongPackBidiReverse(long offset) {
final ByteBuffer bb = getSlice(offset);
int bpos = (int) (offset & sliceSizeModMask);
//$DELAY$
long b = bb.get(--bpos) & 0xffL;
if(CC.ASSERT && (b&0x80)==0)
throw new DBException.DataCorruption();
long result = (b & 0x7F) ;
int counter = 1;
do {
//$DELAY$
b = bb.get(--bpos) & 0xffL;
result = (b & 0x7F) | (result<<7);
if(CC.ASSERT && counter>8)
throw new DBException.DataCorruption();
counter++;
}while((b & 0x80) == 0);
//$DELAY$
return (((long)counter)<<56) | result;
}
@Override
public long getSixLong(long pos) {
final ByteBuffer bb = getSlice(pos);
int bpos = (int) (pos & sliceSizeModMask);
return
((long) (bb.get(bpos++) & 0xff) << 40) |
((long) (bb.get(bpos++) & 0xff) << 32) |
((long) (bb.get(bpos++) & 0xff) << 24) |
((long) (bb.get(bpos++) & 0xff) << 16) |
((long) (bb.get(bpos++) & 0xff) << 8) |
((long) (bb.get(bpos) & 0xff));
}
@Override
public void putSixLong(long pos, long value) {
final ByteBuffer b = getSlice(pos);
int bpos = (int) (pos & sliceSizeModMask);
if(CC.ASSERT && (value >>>48!=0))
throw new DBException.DataCorruption();
b.put(bpos++, (byte) (0xff & (value >> 40)));
b.put(bpos++, (byte) (0xff & (value >> 32)));
b.put(bpos++, (byte) (0xff & (value >> 24)));
b.put(bpos++, (byte) (0xff & (value >> 16)));
b.put(bpos++, (byte) (0xff & (value >> 8)));
b.put(bpos, (byte) (0xff & (value)));
}
@Override
public int putPackedLong(long pos, long value) {
final ByteBuffer b = getSlice(pos);
int bpos = (int) (pos & sliceSizeModMask);
//$DELAY$
int ret = 0;
int shift = 63-Long.numberOfLeadingZeros(value);
shift -= shift%7; // round down to nearest multiple of 7
while(shift!=0){
b.put(bpos + (ret++), (byte) (((value >>> shift) & 0x7F) | 0x80));
//$DELAY$
shift-=7;
}
b.put(bpos +(ret++),(byte) (value & 0x7F));
return ret;
}
@Override
public long getPackedLong(long position) {
final ByteBuffer b = getSlice(position);
int bpos = (int) (position & sliceSizeModMask);
long ret = 0;
int pos2 = 0;
byte v;
do{
v = b.get(bpos +(pos2++));
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
return (((long)pos2)<<60) | ret;
}
@Override
public void clear(long startOffset, long endOffset) {
if(CC.ASSERT && (startOffset >>> sliceShift) != ((endOffset-1) >>> sliceShift))
throw new AssertionError();
ByteBuffer buf = getSlice(startOffset);
int start = (int) (startOffset&sliceSizeModMask);
int end = (int) (endOffset&sliceSizeModMask);
int pos = start;
while(pos<end){
buf = buf.duplicate();
buf.position(pos);
buf.put(CLEAR, 0, Math.min(CLEAR.length, end-pos));
pos+=CLEAR.length;
}
}
@Override
public boolean isSliced(){
return true;
}
@Override
public int sliceSize() {
return sliceSize;
}
/**
* Hack to unmap MappedByteBuffer.
* Unmap is necessary on Windows, otherwise file is locked until JVM exits or BB is GCed.
* There is no public JVM API to unmap buffer, so this tries to use SUN proprietary API for unmap.
* Any error is silently ignored (for example SUN API does not exist on Android).
*/
protected static boolean unmap(MappedByteBuffer b){
try{
if(unmapHackSupported){
// need to dispose old direct buffer, see bug
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
Method cleanerMethod = b.getClass().getMethod("cleaner", new Class[0]);
cleanerMethod.setAccessible(true);
if(cleanerMethod!=null){
Object cleaner = cleanerMethod.invoke(b);
if(cleaner!=null){
Method clearMethod = cleaner.getClass().getMethod("clean", new Class[0]);
if(clearMethod!=null) {
clearMethod.invoke(cleaner);
return true;
}
}else{
//cleaner is null, try fallback method for readonly buffers
Method attMethod = b.getClass().getMethod("attachment", new Class[0]);
attMethod.setAccessible(true);
Object att = attMethod.invoke(b);
return att instanceof MappedByteBuffer &&
unmap((MappedByteBuffer) att);
}
}
}
}catch(Exception e){
unmapHackSupported = false;
LOG.log(Level.WARNING, "Unmap failed", e);
}
return false;
}
private static boolean unmapHackSupported = true;
static{
try{
unmapHackSupported =
SerializerPojo.classForName("sun.nio.ch.DirectBuffer")!=null;
}catch(Exception e){
unmapHackSupported = false;
}
}
// Workaround for https://github.com/jankotek/MapDB/issues/326
// File locking after .close() on Windows.
private static boolean windowsWorkaround = System.getProperty("os.name").toLowerCase().startsWith("win");
}
/**
* Abstract Volume over single ByteBuffer, maximal size is 2GB (32bit limit).
* It leaves ByteBufferVol details (allocation, disposal) on subclasses.
* Most methods are final for better performance (JIT compiler can inline those).
*/
abstract static public class ByteBufferVolSingle extends Volume{
protected final boolean cleanerHackEnabled;
protected ByteBuffer buffer;
protected final boolean readOnly;
protected final long maxSize;
protected ByteBufferVolSingle(boolean readOnly, long maxSize, boolean cleanerHackEnabled) {
//TODO assert size
this.readOnly = readOnly;
this.maxSize = maxSize;
this.cleanerHackEnabled = cleanerHackEnabled;
}
@Override
public void ensureAvailable(long offset) {
//TODO throw error if too big
}
@Override public final void putLong(final long offset, final long value) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+8){
new IOException("VOL STACK:").printStackTrace();
}
buffer.putLong((int) offset, value);
}
@Override public final void putInt(final long offset, final int value) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+4){
new IOException("VOL STACK:").printStackTrace();
}
buffer.putInt((int) (offset), value);
}
@Override public final void putByte(final long offset, final byte value) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+1){
new IOException("VOL STACK:").printStackTrace();
}
buffer.put((int) offset, value);
}
@Override public void putData(final long offset, final byte[] src, int srcPos, int srcSize){
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+srcSize){
new IOException("VOL STACK:").printStackTrace();
}
final ByteBuffer b1 = buffer.duplicate();
final int bufPos = (int) offset;
b1.position(bufPos);
b1.put(src, srcPos, srcSize);
}
@Override public final void putData(final long offset, final ByteBuffer buf) {
if(CC.VOLUME_PRINT_STACK_AT_OFFSET!=0 && CC.VOLUME_PRINT_STACK_AT_OFFSET>=offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset+buf.remaining()){
new IOException("VOL STACK:").printStackTrace();
}
final ByteBuffer b1 = buffer.duplicate();
final int bufPos = (int) offset;
//no overlap, so just write the value
b1.position(bufPos);
b1.put(buf);
}
@Override
public void transferInto(long inputOffset, Volume target, long targetOffset, long size) {
final ByteBuffer b1 = buffer.duplicate();
final int bufPos = (int) inputOffset;
b1.position(bufPos);
//TODO size>Integer.MAX_VALUE
b1.limit((int) (bufPos + size));
target.putData(targetOffset, b1);
}
@Override public void getData(final long offset, final byte[] src, int srcPos, int srcSize){
final ByteBuffer b1 = buffer.duplicate();
final int bufPos = (int) offset;
b1.position(bufPos);
b1.get(src, srcPos, srcSize);
}
@Override final public long getLong(long offset) {
return buffer.getLong((int) offset);
}
@Override final public int getInt(long offset) {
return buffer.getInt((int) offset);
}
@Override public final byte getByte(long offset) {
return buffer.get((int) offset);
}
@Override
public final DataIO.DataInputByteBuffer getDataInput(long offset, int size) {
return new DataIO.DataInputByteBuffer(buffer, (int) (offset));
}
@Override
public void putDataOverlap(long offset, byte[] data, int pos, int len) {
putData(offset,data,pos,len);
}
@Override
public DataInput getDataInputOverlap(long offset, int size) {
//return mapped buffer
return getDataInput(offset,size);
}
@Override
public void clear(long startOffset, long endOffset) {
int start = (int) (startOffset);
int end = (int) (endOffset);
ByteBuffer buf = buffer;
int pos = start;
while(pos<end){
buf = buf.duplicate();
buf.position(pos);
buf.put(CLEAR, 0, Math.min(CLEAR.length, end-pos));
pos+=CLEAR.length;
}
}
@Override
public int sliceSize() {
return -1;
}
@Override
public boolean isSliced() {
return false;
}
}
public static final class MappedFileVol extends ByteBufferVol {
public static final VolumeFactory FACTORY = new VolumeFactory() {
@Override
public Volume makeVolume(String file, boolean readOnly, boolean fileLockDisabled, int sliceShift, long initSize, boolean fixedSize) {
return factory(file, readOnly, fileLockDisabled, sliceShift, false, initSize);
}
};
public static final VolumeFactory FACTORY_WITH_CLEANER_HACK = new VolumeFactory() {