/
FileStore.java
2256 lines (2006 loc) · 77.9 KB
/
FileStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 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 org.h2.engine.Constants;
import static org.h2.mvstore.MVStore.INITIAL_VERSION;
import org.h2.mvstore.cache.CacheLongKeyLIRS;
import org.h2.mvstore.type.StringDataType;
import org.h2.util.MathUtils;
import org.h2.util.Utils;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.IntSupplier;
import java.util.zip.ZipOutputStream;
/**
* Class FileStore is a base class to allow for different store implementations.
* FileStore concept revolves around notion of a "chunk", which is a piece of data
* written into the store at once.
*
* @author <a href="mailto:andrei.tokar@gmail.com">Andrei Tokar</a>
*/
public abstract class FileStore<C extends Chunk<C>>
{
// The following are attribute names (keys) in store header map
static final String HDR_H = "H";
static final String HDR_BLOCK_SIZE = "blockSize";
static final String HDR_FORMAT = "format";
static final String HDR_CREATED = "created";
static final String HDR_FORMAT_READ = "formatRead";
static final String HDR_CHUNK = "chunk";
static final String HDR_BLOCK = "block";
static final String HDR_VERSION = "version";
static final String HDR_CLEAN = "clean";
static final String HDR_FLETCHER = "fletcher";
/**
* The key for the entry within "layout" map, which contains id of "meta" map.
* Entry value (hex encoded) is usually equal to 1, unless it's a legacy
* (upgraded) database and id 1 has been taken already by another map.
*/
public static final String META_ID_KEY = "meta.id";
/**
* The block size (physical sector size) of the disk. The store header is
* written twice, one copy in each block, to ensure it survives a crash.
*/
static final int BLOCK_SIZE = 4 * 1024;
private static final int FORMAT_WRITE_MIN = 3;
private static final int FORMAT_WRITE_MAX = 3;
private static final int FORMAT_READ_MIN = 3;
private static final int FORMAT_READ_MAX = 3;
MVStore mvStore;
private boolean closed;
/**
* The number of read operations.
*/
protected final AtomicLong readCount = new AtomicLong();
/**
* The number of read bytes.
*/
protected final AtomicLong readBytes = new AtomicLong();
/**
* The number of write operations.
*/
protected final AtomicLong writeCount = new AtomicLong();
/**
* The number of written bytes.
*/
protected final AtomicLong writeBytes = new AtomicLong();
/**
* The file name.
*/
private String fileName;
/**
* For how long (in milliseconds) to retain a persisted chunk after it becomes irrelevant
* (not in use, because it only contains data from some old versions).
* Non-positive value allows chunk to be discarded immediately, once it goes out of use.
*/
private int retentionTime = getDefaultRetentionTime();
private final int maxPageSize;
/**
* The file size (cached).
*/
private long size;
/**
* Whether this store is read-only.
*/
private boolean readOnly;
/**
* Lock guarding submission to serializationExecutor
*/
private final ReentrantLock serializationLock = new ReentrantLock(true);
/**
* Single-threaded executor for serialization of the store snapshot into ByteBuffer
*/
private ThreadPoolExecutor serializationExecutor;
/**
* Single-threaded executor for saving ByteBuffer as a new Chunk
*/
private ThreadPoolExecutor bufferSaveExecutor;
/**
* The page cache. The default size is 16 MB, and the average size is 2 KB.
* It is split in 16 segments. The stack move distance is 2% of the expected
* number of entries.
*/
private final CacheLongKeyLIRS<Page<?,?>> cache;
/**
* Cache for chunks "Table of Content" used to translate page's
* sequential number within containing chunk into byte position
* within chunk's image. Cache keyed by chunk id.
*/
private final CacheLongKeyLIRS<long[]> chunksToC;
private final Queue<RemovedPageInfo> removedPages = new PriorityBlockingQueue<>();
/**
* The newest chunk. If nothing was stored yet, this field is not set.
*/
protected volatile C lastChunk;
private int lastChunkId; // protected by serializationLock
protected final ReentrantLock saveChunkLock = new ReentrantLock(true);
/**
* The map of chunks.
*/
final ConcurrentHashMap<Integer, C> chunks = new ConcurrentHashMap<>();
protected final HashMap<String, Object> storeHeader = new HashMap<>();
/**
* The time the store was created, in milliseconds since 1970.
*/
private long creationTime;
private final Queue<WriteBuffer> writeBufferPool = new ArrayBlockingQueue<>(PIPE_LENGTH + 1);
/**
* The layout map. Contains chunk's metadata and root locations for all maps.
* This is relatively fast changing part of metadata
*/
private MVMap<String, String> layout;
private final Deque<C> deadChunks = new ConcurrentLinkedDeque<>();
/**
* Reference to a background thread, which is expected to be running, if any.
*/
private final AtomicReference<BackgroundWriterThread> backgroundWriterThread = new AtomicReference<>();
private final int autoCompactFillRate;
/**
* The delay in milliseconds to automatically commit and write changes.
*/
private int autoCommitDelay;
private long autoCompactLastFileOpCount;
private long lastCommitTime;
protected final boolean recoveryMode;
public static final int PIPE_LENGTH = 3;
protected FileStore(Map<String, Object> config) {
recoveryMode = config.containsKey("recoveryMode");
autoCompactFillRate = DataUtils.getConfigParam(config, "autoCompactFillRate", 90);
CacheLongKeyLIRS.Config cc = null;
int mb = DataUtils.getConfigParam(config, "cacheSize", 16);
if (mb > 0) {
cc = new CacheLongKeyLIRS.Config();
cc.maxMemory = mb * 1024L * 1024L;
Object o = config.get("cacheConcurrency");
if (o != null) {
cc.segmentCount = (Integer)o;
}
}
cache = cc == null ? null : new CacheLongKeyLIRS<>(cc);
CacheLongKeyLIRS.Config cc2 = new CacheLongKeyLIRS.Config();
cc2.maxMemory = 1024L * 1024L;
chunksToC = new CacheLongKeyLIRS<>(cc2);
int maxPageSize = Integer.MAX_VALUE;
// Make sure pages will fit into cache
if (cache != null) {
maxPageSize = 16 * 1024;
int maxCacheableSize = (int) (cache.getMaxItemSize() >> 4);
if (maxPageSize > maxCacheableSize) {
maxPageSize = maxCacheableSize;
}
}
this.maxPageSize = maxPageSize;
}
public abstract void open(String fileName, boolean readOnly, char[] encryptionKey);
public abstract FileStore<C> open(String fileName, boolean readOnly);
protected final void init(String fileName, boolean readOnly) {
this.fileName = fileName;
this.readOnly = readOnly;
}
public final void bind(MVStore mvStore) {
if(this.mvStore != mvStore) {
long pos = layout == null ? 0L : layout.getRootPage().getPos();
layout = new MVMap<>(mvStore, 0, StringDataType.INSTANCE, StringDataType.INSTANCE);
layout.setRootPos(pos, mvStore.getCurrentVersion());
this.mvStore = mvStore;
mvStore.resetLastMapId(lastChunk == null ? 0 : lastChunk.mapId);
mvStore.setCurrentVersion(lastChunkVersion());
}
}
public final void stop(long allowedCompactionTime) {
if (allowedCompactionTime > 0) {
compactStore(allowedCompactionTime);
}
writeCleanShutdown();
clearCaches();
}
public void close() {
layout.close();
closed = true;
chunks.clear();
}
public final int getMetaMapId(IntSupplier nextIdSupplier) {
String metaIdStr = layout.get(META_ID_KEY);
int metaId;
if (metaIdStr == null) {
metaId = nextIdSupplier.getAsInt();
layout.put(META_ID_KEY, Integer.toHexString(metaId));
} else {
metaId = DataUtils.parseHexInt(metaIdStr);
}
return metaId;
}
/**
* Get this store's layout map. This data is for informational purposes only. The
* data is subject to change in future versions.
* <p>
* The data in this map should not be modified (changing system data may corrupt the store).
* <p>
* The layout map contains the following entries:
* <pre>
* chunk.{chunkId} = {chunk metadata}
* root.{mapId} = {root position}
* </pre>
*
* @return the metadata map
*/
public final Map<String, String> getLayoutMap() {
return new TreeMap<>(layout);
}
@SuppressWarnings("ReferenceEquality")
public final boolean isRegularMap(MVMap<?,?> map) {
return map != layout;
}
/**
* Get "position" of the root page for the specified map
* @param mapId to get root position for
* @return opaque "position" value, that should be used to read the page
*/
public final long getRootPos(int mapId) {
String root = layout.get(MVMap.getMapRootKey(mapId));
return root == null ? 0 : DataUtils.parseHexLong(root);
}
/**
* Performs final stage of map removal - delete root location info from the layout map.
* Specified map is supposedly closed, is anonymous and has no outstanding usage by now.
*
* @param mapId to deregister
* @return true if root was removed, false if it is not there
*/
public final boolean deregisterMapRoot(int mapId) {
return layout.remove(MVMap.getMapRootKey(mapId)) != null;
}
/**
* Check whether there are any unsaved changes since specified version.
*
* @param lastStoredVersion version to take as a base for changes
* @return if there are any changes
*/
public final boolean hasChangesSince(long lastStoredVersion) {
return layout.hasChangesSince(lastStoredVersion) && lastStoredVersion > INITIAL_VERSION;
}
public final long lastChunkVersion() {
C chunk = lastChunk;
return chunk == null ? INITIAL_VERSION + 1 : chunk.version;
}
public final long getMaxPageSize() {
return maxPageSize;
}
public final int getRetentionTime() {
return retentionTime;
}
/**
* How long to retain old, persisted chunks, in milliseconds. Chunks that
* are older may be overwritten once they contain no live data.
* <p>
* The default value is 45000 (45 seconds) when using the default file
* store. It is assumed that a file system and hard disk will flush all
* write buffers within this time. Using a lower value might be dangerous,
* unless the file system and hard disk flush the buffers earlier. To
* manually flush the buffers, use
* <code>MVStore.getFile().force(true)</code>, however please note that
* according to various tests this does not always work as expected
* depending on the operating system and hardware.
* <p>
* The retention time needs to be long enough to allow reading old chunks
* while traversing over the entries of a map.
* <p>
* This setting is not persisted.
*
* @param ms how many milliseconds to retain old chunks (0 to overwrite them
* as early as possible)
*/
public final void setRetentionTime(int ms) {
retentionTime = ms;
}
/**
* Decision about autocommit is delegated to store
* @param unsavedMemory amount of unsaved memory, so far
* @param autoCommitMemory configured limit on amount of unsaved memory
* @return true if commit should happen now
*/
public abstract boolean shouldSaveNow(int unsavedMemory, int autoCommitMemory);
/**
* Get the auto-commit delay.
*
* @return the delay in milliseconds, or 0 if auto-commit is disabled.
*/
public final int getAutoCommitDelay() {
return autoCommitDelay;
}
/**
* Set the maximum delay in milliseconds to auto-commit changes.
* <p>
* To disable auto-commit, set the value to 0. In this case, changes are
* only committed when explicitly calling commit.
* <p>
* The default is 1000, meaning all changes are committed after at most one
* second.
*
* @param millis the maximum delay
*/
public final void setAutoCommitDelay(int millis) {
if (autoCommitDelay != millis) {
autoCommitDelay = millis;
if (!isReadOnly()) {
stopBackgroundThread(millis >= 0);
// start the background thread if needed
if (millis > 0 && mvStore.isOpen()) {
int sleep = Math.max(10, millis / 3);
BackgroundWriterThread t = new BackgroundWriterThread(this, sleep, toString());
if (backgroundWriterThread.compareAndSet(null, t)) {
t.start();
serializationExecutor = Utils.createSingleThreadExecutor("H2-serialization");
bufferSaveExecutor = Utils.createSingleThreadExecutor("H2-save");
}
}
}
}
}
/**
* Check whether all data can be read from this version. This requires that
* all chunks referenced by this version are still available (not
* overwritten).
*
* @param version the version
* @return true if all data can be read
*/
public final boolean isKnownVersion(long version) {
if (chunks.isEmpty()) {
// no stored data
return true;
}
// need to check if a chunk for this version exists
C c = getChunkForVersion(version);
if (c == null) {
return false;
}
try {
// also, all chunks referenced by this version
// need to be available in the file
MVMap<String, String> oldLayoutMap = getLayoutMap(version);
for (C chunk : getChunksFromLayoutMap(oldLayoutMap)) {
String chunkKey = Chunk.getMetaKey(chunk.id);
// if current layout map does not have it - verify it's existence
if (!layout.containsKey(chunkKey) && !isValidChunk(chunk)) {
return false;
}
}
} catch (MVStoreException e) {
// the chunk missing where the metadata is stored
return false;
}
return true;
}
public final void rollbackTo(long version) {
if (version == 0) {
// special case: remove all data
String metaId = layout.get(META_ID_KEY);
layout.setInitialRoot(layout.createEmptyLeaf(), INITIAL_VERSION);
layout.put(META_ID_KEY, metaId);
} else {
if (!layout.rollbackRoot(version)) {
MVMap<String, String> layoutMap = getLayoutMap(version);
layout.setInitialRoot(layoutMap.getRootPage(), version);
}
}
serializationLock.lock();
try {
C keep = getChunkForVersion(version);
if (keep != null) {
saveChunkLock.lock();
try {
deadChunks.clear();
setLastChunk(keep);
adjustStoreToLastChunk();
} finally {
saveChunkLock.unlock();
}
}
} finally {
serializationLock.unlock();
}
removedPages.clear();
clearCaches();
}
protected final void initializeCommonHeaderAttributes(long time) {
setLastChunk(null);
creationTime = time;
storeHeader.put(FileStore.HDR_H, 2);
storeHeader.put(FileStore.HDR_BLOCK_SIZE, FileStore.BLOCK_SIZE);
storeHeader.put(FileStore.HDR_FORMAT, FORMAT_WRITE_MAX);
storeHeader.put(FileStore.HDR_CREATED, creationTime);
}
protected final void processCommonHeaderAttributes() {
creationTime = DataUtils.readHexLong(storeHeader, FileStore.HDR_CREATED, 0);
long now = System.currentTimeMillis();
// calculate the year (doesn't have to be exact;
// we assume 365.25 days per year, * 4 = 1461)
int year = 1970 + (int) (now / (1000L * 60 * 60 * 6 * 1461));
if (year < 2014) {
// if the year is before 2014,
// we assume the system doesn't have a real-time clock,
// and we set the creationTime to the past, so that
// existing chunks are overwritten
creationTime = now - getRetentionTime();
} else if (now < creationTime) {
// the system time was set to the past:
// we change the creation time
creationTime = now;
storeHeader.put(FileStore.HDR_CREATED, creationTime);
}
int blockSize = DataUtils.readHexInt(storeHeader, FileStore.HDR_BLOCK_SIZE, FileStore.BLOCK_SIZE);
if (blockSize != FileStore.BLOCK_SIZE) {
throw DataUtils.newMVStoreException(
DataUtils.ERROR_UNSUPPORTED_FORMAT,
"Block size {0} is currently not supported",
blockSize);
}
long format = DataUtils.readHexLong(storeHeader, HDR_FORMAT, 1);
if (!isReadOnly()) {
if (format > FORMAT_WRITE_MAX) {
throw getUnsupportedWriteFormatException(format, FORMAT_WRITE_MAX,
"The write format {0} is larger than the supported format {1}");
} else if (format < FORMAT_WRITE_MIN) {
throw getUnsupportedWriteFormatException(format, FORMAT_WRITE_MIN,
"The write format {0} is smaller than the supported format {1}");
}
}
format = DataUtils.readHexLong(storeHeader, HDR_FORMAT_READ, format);
if (format > FORMAT_READ_MAX) {
throw DataUtils.newMVStoreException(
DataUtils.ERROR_UNSUPPORTED_FORMAT,
"The read format {0} is larger than the supported format {1}",
format, FORMAT_READ_MAX);
} else if (format < FORMAT_READ_MIN) {
throw DataUtils.newMVStoreException(
DataUtils.ERROR_UNSUPPORTED_FORMAT,
"The read format {0} is smaller than the supported format {1}",
format, FORMAT_READ_MIN);
}
}
private long getTimeSinceCreation() {
return Math.max(0, mvStore.getTimeAbsolute() - getCreationTime());
}
private MVMap<String, String> getLayoutMap(long version) {
C chunk = getChunkForVersion(version);
DataUtils.checkArgument(chunk != null, "Unknown version {0}", version);
return layout.openReadOnly(chunk.layoutRootPos, version);
}
private C getChunkForVersion(long version) {
C newest = null;
for (C c : chunks.values()) {
if (c.version <= version) {
if (newest == null || c.id > newest.id) {
newest = c;
}
}
}
return newest;
}
private void scrubLayoutMap(MVMap<String, String> meta) {
Set<String> keysToRemove = new HashSet<>();
// split meta map off layout map
for (String prefix : new String[]{ DataUtils.META_NAME, DataUtils.META_MAP }) {
for (Iterator<String> it = layout.keyIterator(prefix); it.hasNext(); ) {
String key = it.next();
if (!key.startsWith(prefix)) {
break;
}
meta.putIfAbsent(key, layout.get(key));
mvStore.markMetaChanged();
keysToRemove.add(key);
}
}
// remove roots of non-existent maps (leftover after unfinished map removal)
for (Iterator<String> it = layout.keyIterator(DataUtils.META_ROOT); it.hasNext();) {
String key = it.next();
if (!key.startsWith(DataUtils.META_ROOT)) {
break;
}
String mapIdStr = key.substring(key.lastIndexOf('.') + 1);
if(!meta.containsKey(DataUtils.META_MAP + mapIdStr) && DataUtils.parseHexInt(mapIdStr) != meta.getId()) {
keysToRemove.add(key);
}
}
for (String key : keysToRemove) {
layout.remove(key);
}
}
protected final boolean hasPersistentData() {
return lastChunk != null;
}
protected final boolean isIdle() {
return autoCompactLastFileOpCount >= getWriteCount() + getReadCount();
}
protected final void setLastChunk(C last) {
lastChunk = last;
chunks.clear();
lastChunkId = 0;
long layoutRootPos = 0;
if (last != null) { // there is a valid chunk
lastChunkId = last.id;
layoutRootPos = last.layoutRootPos;
chunks.put(last.id, last);
}
layout.setRootPos(layoutRootPos, lastChunkVersion());
}
protected final void registerDeadChunk(C chunk) {
deadChunks.offer(chunk);
}
public final void dropUnusedChunks() {
if (!deadChunks.isEmpty()) {
long oldestVersionToKeep = mvStore.getOldestVersionToKeep();
long time = getTimeSinceCreation();
List<C> toBeFreed = new ArrayList<>();
C chunk;
while ((chunk = deadChunks.poll()) != null &&
(isSeasonedChunk(chunk, time) && canOverwriteChunk(chunk, oldestVersionToKeep) ||
// if chunk is not ready yet, put it back and exit
// since this deque is unbounded, offerFirst() always return true
!deadChunks.offerFirst(chunk))) {
if (chunks.remove(chunk.id) != null) {
// purge dead pages from cache
long[] toc = cleanToCCache(chunk);
if (toc != null && cache != null) {
for (long tocElement : toc) {
long pagePos = DataUtils.composePagePos(chunk.id, tocElement);
cache.remove(pagePos);
}
}
if (layout.remove(Chunk.getMetaKey(chunk.id)) != null) {
mvStore.markMetaChanged();
}
if (chunk.isAllocated()) {
toBeFreed.add(chunk);
}
}
}
if (!toBeFreed.isEmpty()) {
saveChunkLock.lock();
try {
freeChunkSpace(toBeFreed);
} finally {
saveChunkLock.unlock();
}
}
}
}
private static <C extends Chunk<C>> boolean canOverwriteChunk(C c, long oldestVersionToKeep) {
return !c.isLive() && c.unusedAtVersion < oldestVersionToKeep;
}
private boolean isSeasonedChunk(C chunk, long time) {
int retentionTime = getRetentionTime();
return retentionTime < 0 || chunk.time + retentionTime <= time;
}
private boolean isRewritable(C chunk, long time) {
return chunk.isRewritable() && isSeasonedChunk(chunk, time);
}
/**
* Write to the file.
* @param chunk to write
* @param pos the write position
* @param src the source buffer
*/
protected abstract void writeFully(C chunk, long pos, ByteBuffer src);
/**
* Read data from the store.
*
* @param chunk that owns data to be read
* @param pos the read "position"
* @param len the number of bytes to read
* @return the byte buffer with data requested
*/
public abstract ByteBuffer readFully(C chunk, long pos, int len);
protected final ByteBuffer readFully(FileChannel file, long pos, int len) {
ByteBuffer dst = ByteBuffer.allocate(len);
DataUtils.readFully(file, pos, dst);
readCount.incrementAndGet();
readBytes.addAndGet(len);
return dst;
}
/**
* Allocate logical space and assign position of the buffer within the store.
*
* @param chunk to allocate space for
* @param buff to allocate space for
*/
protected abstract void allocateChunkSpace(C chunk, WriteBuffer buff);
/**
* Write buffer associated with chunk into store at chunk's allocated position
* @param chunk chunk to write
* @param buffer to write
*/
protected abstract void writeChunk(C chunk, WriteBuffer buffer);
/**
* Performs final preparation before store is closed normally
*/
protected abstract void writeCleanShutdownMark();
/**
* Make persistent changes after lastChunk was reset
*/
protected abstract void adjustStoreToLastChunk();
/**
* Get the store header. This data is for informational purposes only. The
* data is subject to change in future versions. The data should not be
* modified (doing so may corrupt the store).
*
* @return the store header
*/
public Map<String, Object> getStoreHeader() {
return storeHeader;
}
private C createChunk(long time, long version) {
int newChunkId = findNewChunkId();
C c = createChunk(newChunkId);
c.time = time;
c.version = version;
c.occupancy = new BitSet();
return c;
}
protected abstract C createChunk(int id);
/**
* Build a Chunk from the given string.
*
* @param s the string
* @return the Chunk created
*/
public abstract C createChunk(String s);
protected abstract C createChunk(Map<String, String> map);
private int findNewChunkId() {
int newChunkId;
while (true) {
newChunkId = ++lastChunkId & Chunk.MAX_ID;
if (newChunkId == lastChunkId) {
break;
}
C old = chunks.get(newChunkId);
if (old == null) {
break;
}
if (!old.isSaved()) {
throw DataUtils.newMVStoreException(
DataUtils.ERROR_INTERNAL,
"Last block {0} not stored, possibly due to out-of-memory", old);
}
}
return newChunkId;
}
protected void writeCleanShutdown() {
if (!isReadOnly()) {
saveChunkLock.lock();
try {
writeCleanShutdownMark();
sync();
assert validateFileLength("on close");
} finally {
saveChunkLock.unlock();
}
}
}
/**
* Store chunk's serialized metadata as an entry in a layout map.
* Key for this entry would be "chunk.<id>"
*
* @param chunk to save
*/
public void saveChunkMetadataChanges(C chunk) {
assert serializationLock.isHeldByCurrentThread();
// chunk's location has to be determined before
// it's metadata can be is serialized
while (!chunk.isAllocated()) {
saveChunkLock.lock();
try {
if (chunk.isAllocated()) {
break;
}
} finally {
saveChunkLock.unlock();
}
// just let chunks saving thread to deal with it
Thread.yield();
}
layout.put(Chunk.getMetaKey(chunk.id), chunk.asString());
}
/**
* Mark the space occupied by specified chunks as free.
*
* @param chunks chunks to be processed
*/
protected abstract void freeChunkSpace(Iterable<C> chunks);
protected abstract boolean validateFileLength(String msg);
/**
* Try to increase the fill rate by re-writing partially full chunks. Chunks
* with a low number of live items are re-written.
* <p>
* If the current fill rate is higher than the target fill rate, nothing is
* done.
* <p>
* Please note this method will not necessarily reduce the file size, as
* empty chunks are not overwritten.
* <p>
* Only data of open maps can be moved. For maps that are not open, the old
* chunk is still referenced. Therefore, it is recommended to open all maps
* before calling this method.
*
* @param targetFillRate the minimum percentage of live entries
* @param write the minimum number of bytes to write
* @return if any chunk was re-written
*/
public boolean compact(int targetFillRate, int write) {
if (hasPersistentData()) {
if (targetFillRate > 0 && getChunksFillRate() < targetFillRate) {
// We can't wait forever for the lock here,
// because if called from the background thread,
// it might go into deadlock with concurrent database closure
// and attempt to stop this thread.
try {
Boolean result = mvStore.tryExecuteUnderStoreLock(() -> rewriteChunks(write, 100));
return result != null && result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
return false;
}
public void compactStore(long maxCompactTime) {
compactStore(autoCompactFillRate, maxCompactTime, 16 * 1024 * 1024, mvStore);
}
/**
* 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
* @param mvStore that owns this FileStore
*/
protected abstract void compactStore(int thresholdFillRate, long maxCompactTime, int maxWriteSize, //
MVStore mvStore);
protected abstract void doHousekeeping(MVStore mvStore) throws InterruptedException;
public MVMap<String, String> start() {
if (size() == 0) {
initializeCommonHeaderAttributes(mvStore.getTimeAbsolute());
initializeStoreHeader(mvStore.getTimeAbsolute());
} else {
saveChunkLock.lock();
try {
readStoreHeader(recoveryMode);
} finally {
saveChunkLock.unlock();
}
}
lastCommitTime = getTimeSinceCreation();
mvStore.resetLastMapId(lastMapId());
mvStore.setCurrentVersion(lastChunkVersion());
MVMap<String, String> metaMap = mvStore.openMetaMap();
scrubLayoutMap(metaMap);
return metaMap;
}
protected abstract void initializeStoreHeader(long time);
protected abstract void readStoreHeader(boolean recoveryMode);
private int lastMapId() {
C chunk = lastChunk;
return chunk == null ? 0 : chunk.mapId;
}
private MVStoreException getUnsupportedWriteFormatException(long format, int expectedFormat, String s) {
format = DataUtils.readHexLong(storeHeader, HDR_FORMAT_READ, format);
if (format >= FORMAT_READ_MIN && format <= FORMAT_READ_MAX) {
s += ", and the file was not opened in read-only mode";
}
return DataUtils.newMVStoreException(DataUtils.ERROR_UNSUPPORTED_FORMAT, s, format, expectedFormat);
}
/**
* Discover a valid chunk, searching file backwards from the given block
*
* @param block to start search from (found chunk footer should be no
* further than block-1)
* @return valid chunk or null if none found
*/
protected final C discoverChunk(long block) {
long candidateLocation = Long.MAX_VALUE;
C candidate = null;
while (true) {
if (block == candidateLocation) {
return candidate;
}
if (block == 2) { // number of blocks occupied by headers
return null;
}
C test = readChunkFooter(block);
if (test != null) {
// if we encounter chunk footer (with or without corresponding header)
// in the middle of prospective chunk, stop considering it
candidateLocation = Long.MAX_VALUE;
test = readChunkHeaderOptionally(test.block, test.id);
if (test != null) {
// if that footer has a corresponding header,
// consider them as a new candidate for a valid chunk
candidate = test;
candidateLocation = test.block;
}
}
// if we encounter chunk header without corresponding footer
// (due to incomplete write?) in the middle of prospective
// chunk, stop considering it
if (--block > candidateLocation && readChunkHeaderOptionally(block) != null) {
candidateLocation = Long.MAX_VALUE;
}
}
}
protected final boolean findLastChunkWithCompleteValidChunkSet(Comparator<C> chunkComparator,
Map<Long, C> validChunksByLocation, boolean afterFullScan) {
// this collection will hold potential candidates for lastChunk to fall back to,
// in order from the most to the least likely
C[] array = createChunksArray(validChunksByLocation.size());
C[] lastChunkCandidates = validChunksByLocation.values().toArray(array);
Arrays.sort(lastChunkCandidates, chunkComparator);
Map<Integer, C> validChunksById = new HashMap<>();
for (C chunk : lastChunkCandidates) {
validChunksById.put(chunk.id, chunk);
}
// Try candidates for "last chunk" in order from newest to oldest
// until suitable is found. Suitable one should have meta map
// where all chunk references point to valid locations.
for (C chunk : lastChunkCandidates) {