/
RandomAccessStore.java
796 lines (727 loc) · 29.6 KB
/
RandomAccessStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
* Copyright 2004-2024 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (https://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mvstore;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* Class RandomAccessStore.
* <UL>
* <LI> 4/5/20 2:51 PM initial creation
* </UL>
*
* @author <a href="mailto:andrei.tokar@gmail.com">Andrei Tokar</a>
*/
public abstract class RandomAccessStore extends FileStore<SFChunk>
{
/**
* The free spaces between the chunks. The first block to use is block 2
* (the first two blocks are the store header).
*/
protected final FreeSpaceBitSet freeSpace = new FreeSpaceBitSet(2, BLOCK_SIZE);
/**
* Allocation mode:
* false - new chunk is always allocated at the end of file
* true - new chunk is allocated as close to the beginning of file, as possible
*/
private volatile boolean reuseSpace = true;
private long reservedLow;
private long reservedHigh;
private boolean stopIdleHousekeeping;
public RandomAccessStore(Map<String, Object> config) {
super(config);
}
@Override
protected final SFChunk createChunk(int newChunkId) {
return new SFChunk(newChunkId);
}
@Override
public SFChunk createChunk(String s) {
return new SFChunk(s);
}
@Override
protected SFChunk createChunk(Map<String, String> map) {
return new SFChunk(map);
}
/**
* Mark the space as in use.
*
* @param pos the position in bytes
* @param length the number of bytes
*/
@Override
public void markUsed(long pos, int length) {
freeSpace.markUsed(pos, length);
}
/**
* Allocate a number of blocks and mark them as used.
*
* @param length the number of bytes to allocate
* @param reservedLow start block index of the reserved area (inclusive)
* @param reservedHigh end block index of the reserved area (exclusive),
* special value -1 means beginning of the infinite free area
* @return the start position in bytes
*/
private long allocate(int length, long reservedLow, long reservedHigh) {
return freeSpace.allocate(length, reservedLow, reservedHigh);
}
/**
* Calculate starting position of the prospective allocation.
*
* @param blocks the number of blocks to allocate
* @param reservedLow start block index of the reserved area (inclusive)
* @param reservedHigh end block index of the reserved area (exclusive),
* special value -1 means beginning of the infinite free area
* @return the starting block index
*/
private long predictAllocation(int blocks, long reservedLow, long reservedHigh) {
return freeSpace.predictAllocation(blocks, reservedLow, reservedHigh);
}
@Override
public boolean shouldSaveNow(int unsavedMemory, int autoCommitMemory) {
return unsavedMemory > autoCommitMemory;
}
private boolean isFragmented() {
return freeSpace.isFragmented();
}
@Override
public boolean isSpaceReused() {
return reuseSpace;
}
@Override
public void setReuseSpace(boolean reuseSpace) {
this.reuseSpace = reuseSpace;
}
@Override
protected void freeChunkSpace(Iterable<SFChunk> chunks) {
for (SFChunk chunk : chunks) {
freeChunkSpace(chunk);
}
assert validateFileLength(String.valueOf(chunks));
}
private void freeChunkSpace(SFChunk chunk) {
long start = chunk.block * BLOCK_SIZE;
int length = chunk.len * BLOCK_SIZE;
free(start, length);
}
/**
* Mark the space as free.
*
* @param pos the position in bytes
* @param length the number of bytes
*/
protected void free(long pos, int length) {
freeSpace.free(pos, length);
}
@Override
public int getFillRate() {
saveChunkLock.lock();
try {
return freeSpace.getFillRate();
} finally {
saveChunkLock.unlock();
}
}
@Override
protected final boolean validateFileLength(String msg) {
assert saveChunkLock.isHeldByCurrentThread();
assert getFileLengthInUse() == measureFileLengthInUse() :
getFileLengthInUse() + " != " + measureFileLengthInUse() + " " + msg;
return true;
}
private long measureFileLengthInUse() {
assert saveChunkLock.isHeldByCurrentThread();
long size = 2;
for (SFChunk c : getChunks().values()) {
if (c.isAllocated()) {
size = Math.max(size, c.block + c.len);
}
}
return size * BLOCK_SIZE;
}
long getFirstFree() {
return freeSpace.getFirstFree();
}
long getFileLengthInUse() {
return freeSpace.getLastFree();
}
@Override
protected void readStoreHeader(boolean recoveryMode) {
SFChunk newest = null;
boolean assumeCleanShutdown = true;
boolean validStoreHeader = false;
// find out which chunk and version are the newest
// read the first two blocks
ByteBuffer fileHeaderBlocks = readFully((SFChunk)null, 0, 2 * FileStore.BLOCK_SIZE);
byte[] buff = new byte[FileStore.BLOCK_SIZE];
for (int i = 0; i <= FileStore.BLOCK_SIZE; i += FileStore.BLOCK_SIZE) {
fileHeaderBlocks.get(buff);
// the following can fail for various reasons
try {
HashMap<String, String> m = DataUtils.parseChecksummedMap(buff);
if (m == null) {
assumeCleanShutdown = false;
continue;
}
long version = DataUtils.readHexLong(m, FileStore.HDR_VERSION, 0);
// if both header blocks do agree on version
// we'll continue on happy path - assume that previous shutdown was clean
assumeCleanShutdown = assumeCleanShutdown && (newest == null || version == newest.version);
if (newest == null || version > newest.version) {
validStoreHeader = true;
storeHeader.putAll(m);
int chunkId = DataUtils.readHexInt(m, FileStore.HDR_CHUNK, 0);
long block = DataUtils.readHexLong(m, FileStore.HDR_BLOCK, 2);
SFChunk test = readChunkHeaderAndFooter(block, chunkId);
if (test != null) {
newest = test;
}
}
} catch (Exception ignore) {
assumeCleanShutdown = false;
}
}
if (!validStoreHeader) {
throw DataUtils.newMVStoreException(
DataUtils.ERROR_FILE_CORRUPT,
"Store header is corrupt: {0}", this);
}
processCommonHeaderAttributes();
assumeCleanShutdown = assumeCleanShutdown && newest != null && !recoveryMode;
if (assumeCleanShutdown) {
assumeCleanShutdown = DataUtils.readHexInt(storeHeader, FileStore.HDR_CLEAN, 0) != 0;
}
// assert getChunks().size() <= 1 : getChunks().size();
long fileSize = size();
long blocksInStore = fileSize / FileStore.BLOCK_SIZE;
Comparator<SFChunk> chunkComparator = (one, two) -> {
int result = Long.compare(two.version, one.version);
if (result == 0) {
// out of two copies of the same chunk we prefer the one
// close to the beginning of file (presumably later version)
result = Long.compare(one.block, two.block);
}
return result;
};
Map<Long,SFChunk> validChunksByLocation = new HashMap<>();
if (assumeCleanShutdown) {
// quickly check latest 20 chunks referenced in meta table
Queue<SFChunk> chunksToVerify = new PriorityQueue<>(20, Collections.reverseOrder(chunkComparator));
try {
setLastChunk(newest);
// load the chunk metadata: although meta's root page resides in the lastChunk,
// traversing meta map might recursively load another chunk(s)
for (SFChunk c : getChunksFromLayoutMap()) {
// might be there already, due to meta traversal
// see readPage() ... getChunkIfFound()
chunksToVerify.offer(c);
if (chunksToVerify.size() == 20) {
chunksToVerify.poll();
}
}
SFChunk c;
while (assumeCleanShutdown && (c = chunksToVerify.poll()) != null) {
SFChunk test = readChunkHeaderAndFooter(c.block, c.id);
assumeCleanShutdown = test != null;
if (assumeCleanShutdown) {
validChunksByLocation.put(test.block, test);
}
}
} catch(IllegalStateException ignored) {
assumeCleanShutdown = false;
}
} else {
SFChunk tailChunk = discoverChunk(blocksInStore);
if (tailChunk != null) {
blocksInStore = tailChunk.block; // for a possible full scan later on
validChunksByLocation.put(blocksInStore, tailChunk);
if (newest == null || tailChunk.version > newest.version) {
newest = tailChunk;
}
}
if (newest != null) {
// read the chunk header and footer,
// and follow the chain of next chunks
while (true) {
validChunksByLocation.put(newest.block, newest);
if (newest.next == 0 || newest.next >= blocksInStore) {
// no (valid) next
break;
}
SFChunk test = readChunkHeaderAndFooter(newest.next, newest.id + 1);
if (test == null || test.version <= newest.version) {
break;
}
newest = test;
}
}
}
if (!assumeCleanShutdown) {
// now we know, that previous shutdown did not go well and file
// is possibly corrupted but there is still hope for a quick
// recovery
boolean quickRecovery = !recoveryMode &&
findLastChunkWithCompleteValidChunkSet(chunkComparator, validChunksByLocation, false);
if (!quickRecovery) {
// scan whole file and try to fetch chunk header and/or footer out of every block
// matching pairs with nothing in-between are considered as valid chunk
long block = blocksInStore;
SFChunk tailChunk;
while ((tailChunk = discoverChunk(block)) != null) {
block = tailChunk.block;
validChunksByLocation.put(block, tailChunk);
}
if (!findLastChunkWithCompleteValidChunkSet(chunkComparator, validChunksByLocation, true)
&& hasPersistentData()) {
throw DataUtils.newMVStoreException(
DataUtils.ERROR_FILE_CORRUPT,
"File is corrupted - unable to recover a valid set of chunks");
}
}
}
clear();
// build the free space list
for (SFChunk c : getChunks().values()) {
if (c.isAllocated()) {
long start = c.block * FileStore.BLOCK_SIZE;
int length = c.len * FileStore.BLOCK_SIZE;
markUsed(start, length);
}
if (!c.isLive()) {
registerDeadChunk(c);
}
}
assert validateFileLength("on open");
}
@Override
protected void initializeStoreHeader(long time) {
initializeCommonHeaderAttributes(time);
writeStoreHeader();
}
@Override
protected final void allocateChunkSpace(SFChunk chunk, WriteBuffer buff) {
long reservedLow = this.reservedLow;
long reservedHigh = this.reservedHigh > 0 ? this.reservedHigh : isSpaceReused() ? 0 : getAfterLastBlock();
long filePos = allocate(buff.limit(), reservedLow, reservedHigh);
// calculate and set the likely next position
if (reservedLow > 0 || reservedHigh == reservedLow) {
chunk.next = predictAllocation(chunk.len, 0, 0);
} else {
// just after this chunk
chunk.next = 0;
}
chunk.block = filePos / BLOCK_SIZE;
}
@Override
protected final void writeChunk(SFChunk chunk, WriteBuffer buffer) {
long filePos = chunk.block * BLOCK_SIZE;
writeFully(chunk, filePos, buffer.getBuffer());
// end of the used space is not necessarily the end of the file
boolean storeAtEndOfFile = filePos + buffer.limit() >= size();
boolean shouldWriteStoreHeader = shouldWriteStoreHeader(chunk, storeAtEndOfFile);
lastChunk = chunk;
if (shouldWriteStoreHeader) {
writeStoreHeader();
}
if (!storeAtEndOfFile) {
// may only shrink after the store header was written
shrinkStoreIfPossible(1);
}
}
private boolean shouldWriteStoreHeader(SFChunk c, boolean storeAtEndOfFile) {
// whether we need to write the store header
boolean writeStoreHeader = false;
if (!storeAtEndOfFile) {
SFChunk chunk = lastChunk;
if (chunk == null) {
writeStoreHeader = true;
} else if (chunk.next != c.block) {
// the last prediction did not matched
writeStoreHeader = true;
} else {
long headerVersion = DataUtils.readHexLong(storeHeader, HDR_VERSION, 0);
if (chunk.version - headerVersion > 20) {
// we write after at least every 20 versions
writeStoreHeader = true;
} else {
for (int chunkId = DataUtils.readHexInt(storeHeader, HDR_CHUNK, 0);
!writeStoreHeader && chunkId <= chunk.id; ++chunkId) {
// one of the chunks in between
// was removed
writeStoreHeader = !getChunks().containsKey(chunkId);
}
}
}
}
if (storeHeader.remove(HDR_CLEAN) != null) {
writeStoreHeader = true;
}
return writeStoreHeader;
}
@Override
protected final void writeCleanShutdownMark() {
shrinkStoreIfPossible(0);
storeHeader.put(HDR_CLEAN, 1);
writeStoreHeader();
}
@Override
protected final void adjustStoreToLastChunk() {
storeHeader.put(HDR_CLEAN, 1);
writeStoreHeader();
readStoreHeader(false);
}
/**
* Compact store file, that is, compact blocks that have a low
* fill rate, and move chunks next to each other. This will typically
* shrink the file. Changes are flushed to the file, and old
* chunks are overwritten.
*
* @param thresholdFillRate do not compact if store fill rate above this value (0-100)
* @param maxCompactTime the maximum time in milliseconds to compact
* @param maxWriteSize the maximum amount of data to be written as part of this call
*/
@Override
protected void compactStore(int thresholdFillRate, long maxCompactTime, int maxWriteSize, MVStore mvStore) {
setRetentionTime(0);
long stopAt = System.nanoTime() + maxCompactTime * 1_000_000L;
while (compact(thresholdFillRate, maxWriteSize)) {
sync();
compactMoveChunks(thresholdFillRate, maxWriteSize, mvStore);
if (System.nanoTime() - stopAt > 0L) {
break;
}
}
}
/**
* Compact the store by moving all chunks next to each other, if there is
* free space between chunks. This might temporarily increase the file size.
* Chunks are overwritten irrespective of the current retention time. Before
* overwriting chunks and before resizing the file, syncFile() is called.
*
* @param targetFillRate do nothing if the file store fill rate is higher
* than this
* @param moveSize the number of bytes to move
* @param mvStore owner of this store
*/
public void compactMoveChunks(int targetFillRate, long moveSize, MVStore mvStore) {
if (isSpaceReused()) {
mvStore.executeFilestoreOperation(() -> {
dropUnusedChunks();
saveChunkLock.lock();
try {
if (hasPersistentData() && getFillRate() <= targetFillRate) {
compactMoveChunks(moveSize);
}
} finally {
saveChunkLock.unlock();
}
});
}
}
private void compactMoveChunks(long moveSize) {
long start = getFirstFree() / FileStore.BLOCK_SIZE;
Iterable<SFChunk> chunksToMove = findChunksToMove(start, moveSize);
if (chunksToMove != null) {
compactMoveChunks(chunksToMove);
}
}
private Iterable<SFChunk> findChunksToMove(long startBlock, long moveSize) {
long maxBlocksToMove = moveSize / FileStore.BLOCK_SIZE;
Iterable<SFChunk> result = null;
if (maxBlocksToMove > 0) {
PriorityQueue<SFChunk> queue = new PriorityQueue<>(getChunks().size() / 2 + 1,
(o1, o2) -> {
// instead of selection just closest to beginning of the file,
// pick smaller chunk(s) which sit in between bigger holes
int res = Integer.compare(o2.collectPriority, o1.collectPriority);
if (res != 0) {
return res;
}
return Long.signum(o2.block - o1.block);
});
long size = 0;
for (SFChunk chunk : getChunks().values()) {
if (chunk.isAllocated() && chunk.block > startBlock) {
chunk.collectPriority = getMovePriority(chunk);
queue.offer(chunk);
size += chunk.len;
while (size > maxBlocksToMove) {
Chunk<?> removed = queue.poll();
if (removed == null) {
break;
}
size -= removed.len;
}
}
}
if (!queue.isEmpty()) {
ArrayList<SFChunk> list = new ArrayList<>(queue);
list.sort(Chunk.PositionComparator.instance());
result = list;
}
}
return result;
}
private int getMovePriority(SFChunk chunk) {
return getMovePriority((int)chunk.block);
}
private void compactMoveChunks(Iterable<SFChunk> move) {
assert saveChunkLock.isHeldByCurrentThread();
if (move != null) {
// this will ensure better recognition of the last chunk
// in case of power failure, since we are going to move older chunks
// to the end of the file
writeStoreHeader();
sync();
Iterator<SFChunk> iterator = move.iterator();
assert iterator.hasNext();
long leftmostBlock = iterator.next().block;
long originalBlockCount = getAfterLastBlock();
// we need to ensure that chunks moved within the following loop
// do not overlap with space just released by chunks moved before them,
// hence the need to reserve this area [leftmostBlock, originalBlockCount)
for (SFChunk chunk : move) {
moveChunk(chunk, leftmostBlock, originalBlockCount);
}
// update the metadata (hopefully within the file)
store(leftmostBlock, originalBlockCount);
sync();
SFChunk chunkToMove = lastChunk;
assert chunkToMove != null;
long postEvacuationBlockCount = getAfterLastBlock();
boolean chunkToMoveIsAlreadyInside = chunkToMove.block < leftmostBlock;
boolean movedToEOF = !chunkToMoveIsAlreadyInside;
// move all chunks, which previously did not fit before reserved area
// now we can re-use previously reserved area [leftmostBlock, originalBlockCount),
// but need to reserve [originalBlockCount, postEvacuationBlockCount)
for (SFChunk c : move) {
if (c.block >= originalBlockCount &&
moveChunk(c, originalBlockCount, postEvacuationBlockCount)) {
assert c.block < originalBlockCount;
movedToEOF = true;
}
}
assert postEvacuationBlockCount >= getAfterLastBlock();
if (movedToEOF) {
boolean moved = moveChunkInside(chunkToMove, originalBlockCount);
// store a new chunk with updated metadata (hopefully within a file)
store(originalBlockCount, postEvacuationBlockCount);
sync();
// if chunkToMove did not fit within originalBlockCount (move is
// false), and since now previously reserved area
// [originalBlockCount, postEvacuationBlockCount) also can be
// used, lets try to move that chunk into this area, closer to
// the beginning of the file
long lastBoundary = moved || chunkToMoveIsAlreadyInside ?
postEvacuationBlockCount : chunkToMove.block;
moved = !moved && moveChunkInside(chunkToMove, lastBoundary);
if (moveChunkInside(lastChunk, lastBoundary) || moved) {
store(lastBoundary, -1);
}
}
shrinkStoreIfPossible(0);
sync();
}
}
private void writeStoreHeader() {
StringBuilder buff = new StringBuilder(112);
if (hasPersistentData()) {
storeHeader.put(HDR_BLOCK, lastChunk.block);
storeHeader.put(HDR_CHUNK, lastChunk.id);
storeHeader.put(HDR_VERSION, lastChunk.version);
}
DataUtils.appendMap(buff, storeHeader);
byte[] bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1);
int checksum = DataUtils.getFletcher32(bytes, 0, bytes.length);
DataUtils.appendMap(buff, HDR_FLETCHER, checksum);
buff.append('\n');
bytes = buff.toString().getBytes(StandardCharsets.ISO_8859_1);
ByteBuffer header = ByteBuffer.allocate(2 * BLOCK_SIZE);
header.put(bytes);
header.position(BLOCK_SIZE);
header.put(bytes);
header.rewind();
writeFully(null, 0, header);
}
private void store(long reservedLow, long reservedHigh) {
this.reservedLow = reservedLow;
this.reservedHigh = reservedHigh;
saveChunkLock.unlock();
try {
store();
} finally {
saveChunkLock.lock();
this.reservedLow = 0;
this.reservedHigh = 0;
}
}
private boolean moveChunkInside(SFChunk chunkToMove, long boundary) {
boolean res = chunkToMove.block >= boundary &&
predictAllocation(chunkToMove.len, boundary, -1) < boundary &&
moveChunk(chunkToMove, boundary, -1);
assert !res || chunkToMove.block + chunkToMove.len <= boundary;
return res;
}
/**
* Move specified chunk into free area of the file. "Reserved" area
* specifies file interval to be avoided, when un-allocated space will be
* chosen for a new chunk's location.
*
* @param chunk to move
* @param reservedAreaLow low boundary of reserved area, inclusive
* @param reservedAreaHigh high boundary of reserved area, exclusive
* @return true if block was moved, false otherwise
*/
private boolean moveChunk(SFChunk chunk, long reservedAreaLow, long reservedAreaHigh) {
// ignore if already removed during the previous store operations
// those are possible either as explicit commit calls
// or from meta map updates at the end of this method
if (!getChunks().containsKey(chunk.id)) {
return false;
}
long start = chunk.block * FileStore.BLOCK_SIZE;
int length = chunk.len * FileStore.BLOCK_SIZE;
long pos = allocate(length, reservedAreaLow, reservedAreaHigh);
long block = pos / FileStore.BLOCK_SIZE;
// in the absence of a reserved area,
// block should always move closer to the beginning of the file
assert reservedAreaHigh > 0 || block <= chunk.block : block + " " + chunk;
ByteBuffer readBuff = readFully(chunk, start, length);
writeFully(null, pos, readBuff);
free(start, length);
// can not set chunk's new block/len until it's fully written at new location,
// because concurrent reader can pick it up prematurely,
chunk.block = block;
chunk.next = 0;
saveChunkMetadataChanges(chunk);
return true;
}
/**
* Shrink the store if possible, and if at least a given percentage can be
* saved.
*
* @param minPercent the minimum percentage to save
*/
@Override
protected void shrinkStoreIfPossible(int minPercent) {
assert saveChunkLock.isHeldByCurrentThread();
long result = getFileLengthInUse();
assert result == measureFileLengthInUse() : result + " != " + measureFileLengthInUse();
shrinkIfPossible(minPercent);
}
private void shrinkIfPossible(int minPercent) {
if (isReadOnly()) {
return;
}
long end = getFileLengthInUse();
long fileSize = size();
if (end >= fileSize) {
return;
}
if (minPercent > 0 && fileSize - end < BLOCK_SIZE) {
return;
}
int savedPercent = (int) (100 - (end * 100 / fileSize));
if (savedPercent < minPercent) {
return;
}
sync();
truncate(end);
}
@Override
protected void doHousekeeping(MVStore mvStore) throws InterruptedException {
boolean idle = isIdle();
int rewritableChunksFillRate = getRewritableChunksFillRate();
if (idle && stopIdleHousekeeping) {
return;
}
int autoCommitMemory = mvStore.getAutoCommitMemory();
int fileFillRate = getFillRate();
long chunksTotalSize = size() * fileFillRate / 100;
if (isFragmented() && fileFillRate < getAutoCompactFillRate()) {
mvStore.tryExecuteUnderStoreLock(() -> {
int moveSize = 2 * autoCommitMemory;
if (idle) {
moveSize *= 4;
}
compactMoveChunks(101, moveSize, mvStore);
return true;
});
}
int chunksFillRate = getChunksFillRate();
int adjustedUpFillRate = 50 + rewritableChunksFillRate / 2;
int fillRateToCompare = idle ? rewritableChunksFillRate : adjustedUpFillRate;
if (fillRateToCompare < getTargetFillRate(idle)) {
int targetFillRate = idle ? adjustedUpFillRate : rewritableChunksFillRate;
mvStore.tryExecuteUnderStoreLock(() -> {
int writeLimit = autoCommitMemory;
if (!idle) {
writeLimit /= 4;
}
if (rewriteChunks(writeLimit, targetFillRate)) {
dropUnusedChunks();
}
return true;
});
}
stopIdleHousekeeping = false;
if (idle) {
int currentChunksFillRate = getChunksFillRate();
long currentTotalChunksSize = size() * getFillRate() / 100;
stopIdleHousekeeping = currentTotalChunksSize > chunksTotalSize || currentTotalChunksSize == chunksTotalSize && currentChunksFillRate <= chunksFillRate;
}
}
private int getTargetFillRate(boolean idle) {
int targetRate = getAutoCompactFillRate();
// use a lower fill rate if there were any file operations since the last time
if (!idle) {
targetRate = targetRate * targetRate / 100;
}
return targetRate;
}
protected abstract void truncate(long size);
/**
* Mark the file as empty.
*/
@Override
public void clear() {
freeSpace.clear();
}
/**
* Calculates relative "priority" for chunk to be moved.
*
* @param block where chunk starts
* @return priority, bigger number indicate that chunk need to be moved sooner
*/
public int getMovePriority(int block) {
return freeSpace.getMovePriority(block);
}
/**
* Get the index of the first block after last occupied one.
* It marks the beginning of the last (infinite) free space.
*
* @return block index
*/
private long getAfterLastBlock() {
assert saveChunkLock.isHeldByCurrentThread();
return getAfterLastBlock_();
}
protected long getAfterLastBlock_() {
return freeSpace.getAfterLastBlock();
}
@Override
public Collection<SFChunk> getRewriteCandidates() {
return isSpaceReused() ? null : Collections.emptyList();
}
}