-
Notifications
You must be signed in to change notification settings - Fork 39
/
MPFastKV.java
944 lines (867 loc) · 30.3 KB
/
MPFastKV.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
package io.fastkv;
import android.os.FileObserver;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import io.fastkv.Container.BaseContainer;
import io.fastkv.Container.VarContainer;
import io.fastkv.interfaces.FastCipher;
import io.fastkv.interfaces.FastEncoder;
/**
* Multi-process FastKV <br>
* <p>
* This class support cross process storage and update notify.
* It implements API of SharePreferences, so it could be used as SharePreferences.<br>
* <p>
* Note 1: <br>
* Remember to call 'commit' or 'apply' after editing !!!
* <p>
* Note 2: <br>
* To support cross process storage, MPFastKV needs to do many state checking, it's slower than {@link FastKV}.
* So if you don't need to access the data in multi-process, just use FastKV.
*/
@SuppressWarnings("rawtypes")
public final class MPFastKV extends AbsFastKV {
private static final int MSG_REFRESH = 1;
private static final int MSG_APPLY = 2;
private static final int MSG_DATA_CHANGE = 3;
private static final int MSG_CLEAR = 4;
private static final int LOCK_TIMEOUT = 3000;
private static final Random random = new Random();
private final boolean needWatchFileChange;
private final File aFile;
private final File bFile;
private RandomAccessFile aAccessFile;
private RandomAccessFile bAccessFile;
private FileChannel aChannel;
private FileChannel bChannel;
private MappedByteBuffer aBuffer;
private FileLock bFileLock;
private int[] updateStartAndSize = new int[16];
private int updateCount = 0;
private long updateHash;
private boolean needFullWrite = false;
private final Executor refreshExecutor = new LimitExecutor();
// We need to keep reference to the observer in case of gc recycle it.
private volatile KVFileObserver fileObserver;
private final Set<String> changedKey = new HashSet<>();
MPFastKV(final String path,
final String name,
FastEncoder[] encoders,
FastCipher cipher,
boolean needWatchFileChange) {
super(path, name, encoders, cipher);
aFile = new File(path, name + A_SUFFIX);
bFile = new File(path, name + B_SUFFIX);
this.needWatchFileChange = needWatchFileChange;
synchronized (data) {
FastKVConfig.getExecutor().execute(this::loadData);
while (!startLoading) {
try {
// wait util loadData() get the object lock
data.wait();
} catch (InterruptedException ignore) {
}
}
}
}
private synchronized void loadData() {
// we got the object lock, notify the waiter to continue the constructor
synchronized (data) {
startLoading = true;
data.notify();
}
long start = System.nanoTime();
if (!loadFromCFile()) {
loadFromABFile();
}
if (fastBuffer == null) {
fastBuffer = new FastBuffer(PAGE_SIZE);
}
if (dataEnd == 0) {
dataEnd = DATA_START;
}
if (needRewrite) {
rewrite();
info("rewrite data");
}
if (logger != null) {
long t = (System.nanoTime() - start) / 1000000;
info("loading finish, data len:" + dataEnd + ", get keys:" + data.size() + ", use time:" + t + " ms");
}
trySettingObserver();
}
protected void copyToMainFile(FastKV tempKV) {
writeToABFile(tempKV.fastBuffer);
}
private void trySettingObserver() {
if (needWatchFileChange && fileObserver == null && bFile != null && bFile.exists()) {
fileObserver = new KVFileObserver(bFile.getPath());
fileObserver.startWatching();
}
}
private void loadFromABFile() {
try {
int count = 0;
while ((!Utils.makeFileIfNotExist(aFile) || !Utils.makeFileIfNotExist(bFile)) && count < 3) {
//noinspection BusyWait
Thread.sleep(20L);
count++;
}
if (!aFile.exists() || !bFile.exists()) {
error(new Exception(OPEN_FILE_FAILED));
return;
}
aAccessFile = new RandomAccessFile(aFile, "rw");
bAccessFile = new RandomAccessFile(bFile, "rw");
long aFileLen = aAccessFile.length();
long bFileLen = bAccessFile.length();
aChannel = aAccessFile.getChannel();
bChannel = bAccessFile.getChannel();
FileLock lock = bChannel.lock();
try {
try {
aBuffer = aChannel.map(FileChannel.MapMode.READ_WRITE, 0, aFileLen > 0 ? aFileLen : PAGE_SIZE);
aBuffer.order(ByteOrder.LITTLE_ENDIAN);
} catch (IOException e) {
error(e);
tryBlockingIO(aFile, bFile);
return;
}
if (aFileLen == 0 && bFileLen == 0) {
dataEnd = DATA_START;
bAccessFile.setLength(PAGE_SIZE);
bChannel.truncate(PAGE_SIZE);
} else {
if (loadWithBlockingIO(bFile)) {
boolean isAEqualB = false;
if (aFileLen == bFileLen && fastBuffer.hb.length == aBuffer.capacity()) {
byte[] b = fastBuffer.hb;
byte[] a = new byte[dataEnd];
aBuffer.get(a, 0, dataEnd);
int i = 0;
for (; i < dataEnd; i++) {
if (a[i] != b[i]) break;
}
isAEqualB = i == dataEnd;
}
if (!isAEqualB) {
warning(new Exception("A file error"));
fullWriteBufferToA();
}
} else {
updateCount = 0;
resetData();
if (fastBuffer == null || fastBuffer.hb.length != aBuffer.capacity()) {
fastBuffer = new FastBuffer(aBuffer.capacity());
}
int aSize = aBuffer.getInt();
int aDataSize = unpackSize(aSize);
boolean aHadEncrypted = isCipher(aSize);
boolean isAValid = false;
if (aDataSize >= 0 && (aDataSize <= aFileLen - DATA_START)) {
dataEnd = DATA_START + aDataSize;
long aCheckSum = aBuffer.getLong(4);
aBuffer.rewind();
aBuffer.get(fastBuffer.hb, 0, dataEnd);
if (aCheckSum == fastBuffer.getChecksum(DATA_START, aDataSize) && parseData(aHadEncrypted)) {
checksum = aCheckSum;
isAValid = true;
}
}
if (isAValid) {
warning(new Exception("B file error"));
fullWriteAToB();
} else {
error(BOTH_FILES_ERROR);
clearData();
}
}
}
getUpdateHash();
} finally {
lock.release();
}
} catch (Exception e) {
error(e);
resetMemory();
}
}
private void getUpdateHash() {
if (aBuffer != null && dataEnd + 8 < aBuffer.capacity()) {
updateHash = aBuffer.getLong(dataEnd);
}
}
private void fullWriteAToB() {
try {
if (!Utils.makeFileIfNotExist(bFile)) {
return;
}
setBFileSize(aBuffer.capacity());
syncAToB(0, dataEnd);
} catch (Exception e) {
error(e);
}
}
private void fullWriteBufferToA() {
try {
if (alignAToBuffer()) {
aBuffer.position(0);
aBuffer.put(fastBuffer.hb, 0, dataEnd);
}
} catch (Exception e) {
error(e);
}
}
private boolean alignAToBuffer() {
int bufferSize = fastBuffer.hb.length;
try {
if (aAccessFile == null) {
if (!Utils.makeFileIfNotExist(aFile)) {
return false;
}
aAccessFile = new RandomAccessFile(aFile, "rw");
}
if (aAccessFile.length() != bufferSize) {
aAccessFile.setLength(bufferSize);
}
if (aChannel == null) {
aChannel = aAccessFile.getChannel();
} else if (aChannel.size() != bufferSize) {
aChannel.truncate(bufferSize);
}
if (aBuffer == null || aBuffer.capacity() != bufferSize) {
aBuffer = aChannel.map(FileChannel.MapMode.READ_WRITE, 0, bufferSize);
aBuffer.order(ByteOrder.LITTLE_ENDIAN);
}
} catch (Exception e) {
error(e);
return false;
}
return true;
}
// MPFastKV do not write data to CFile,
// just in case of user write data with FastKV at first and then change to MPFastKV
private boolean loadFromCFile() {
boolean hadWriteToABFile = false;
File cFile = new File(path, name + C_SUFFIX);
File tmpFile = new File(path, name + TEMP_SUFFIX);
try {
File srcFile = null;
if (cFile.exists()) {
srcFile = cFile;
} else if (tmpFile.exists()) {
srcFile = tmpFile;
}
if (srcFile != null) {
if (loadWithBlockingIO(srcFile)) {
if (writeToABFile(fastBuffer)) {
info("recover from c file");
hadWriteToABFile = true;
}
} else {
resetMemory();
}
deleteCFiles();
}
} catch (Exception e) {
error(e);
}
return hadWriteToABFile;
}
private boolean writeToABFile(FastBuffer buffer) {
int bufferLen = buffer.hb.length;
try {
if (!Utils.makeFileIfNotExist(aFile) || !Utils.makeFileIfNotExist(bFile)) {
throw new Exception(OPEN_FILE_FAILED);
}
if (bAccessFile == null) {
bAccessFile = new RandomAccessFile(bFile, "rw");
}
if (bChannel == null) {
bChannel = bAccessFile.getChannel();
}
FileLock lock = bFileLock == null ? bChannel.lock() : null;
try {
alignAToBuffer();
aBuffer.put(buffer.hb, 0, dataEnd);
getUpdateHash();
if (bAccessFile.length() != bufferLen) {
bAccessFile.setLength(bufferLen);
}
bChannel.truncate(bufferLen);
syncAToB(0, dataEnd);
bChannel.force(false);
} finally {
if (lock != null) {
lock.release();
}
}
trySettingObserver();
return true;
} catch (Exception e) {
error(e);
}
return false;
}
private void syncAToB(int offset, int length) throws IOException {
MappedByteBuffer buffer = aBuffer;
buffer.position(offset);
buffer.limit(offset + length);
if (bChannel.size() != buffer.capacity()) {
bChannel.truncate(buffer.capacity());
}
bChannel.position(offset);
while (buffer.hasRemaining()) {
bChannel.write(buffer);
}
buffer.limit(buffer.capacity());
}
private void syncBufferToA(int offset, int length) {
byte[] bytes = fastBuffer.hb;
aBuffer.position(offset);
aBuffer.put(bytes, offset, length);
}
public synchronized Editor remove(String key) {
lockAndCheckUpdate();
handleChange(key);
BaseContainer container = data.get(key);
if (container != null) {
String oldFileName = null;
data.remove(key);
bigValueCache.remove(key);
externalCache.remove(key);
byte type = container.getType();
if (type <= DataType.DOUBLE) {
int keySize = FastBuffer.getStringSize(key);
int start = container.offset - (2 + keySize);
remove(type, start, container.offset + TYPE_SIZE[type]);
} else {
VarContainer c = (VarContainer) container;
remove(type, c.start, c.offset + c.valueSize);
oldFileName = c.external ? (String) c.value : null;
}
if (oldFileName != null) {
deletedFiles.add(oldFileName);
}
checkGC();
}
return this;
}
@Override
protected void handleChange(String key) {
if (!listeners.isEmpty()) {
changedKey.add(key);
}
}
private synchronized void notifyChangedKeys() {
if (!changedKey.isEmpty()) {
for (String key : changedKey) {
notifyListeners(key);
}
changedKey.clear();
}
}
public synchronized void force() {
try {
if (aBuffer != null) {
aBuffer.force();
}
if (bChannel != null) {
bChannel.force(true);
}
} catch (Exception e) {
error(e);
}
}
@Override
public boolean commit() {
return updateFile();
}
@Override
public void apply() {
applyExecutor.execute(this::updateFile);
}
private boolean fullWrite() {
fastBuffer.position = 0;
int dataSize = unpackSize(fastBuffer.getInt());
checksum = fastBuffer.getLong();
dataEnd = DATA_START + dataSize;
if (checksum == fastBuffer.getChecksum(DATA_START, dataSize)) {
return writeToABFile(fastBuffer);
} else {
clearData();
return true;
}
}
private synchronized boolean updateFile() {
if (bFileLock == null) {
return false;
}
if (fastBuffer == null || (updateCount == 0 && !needFullWrite)) {
releaseLock();
return false;
}
try {
int dataSize = dataEnd - DATA_START;
int packedSize = packSize(dataSize);
fastBuffer.putInt(0, packedSize);
fastBuffer.putLong(4, checksum);
if (needFullWrite) {
boolean result = fullWrite();
if (result) {
needFullWrite = false;
}
return result;
}
if (!alignAToBuffer()) {
if (aBuffer != null) {
// If aBuffer can't align to dataBuffer (Not enough memory?),
// drop the update
reloadFromABuffer();
} else {
needFullWrite = true;
}
return false;
}
setBFileSize(aBuffer.capacity());
// update A Aile
// syncBufferToA(0, DATA_START);
aBuffer.putInt(0, packedSize);
aBuffer.putLong(4, checksum);
for (int i = 0; i < updateCount; i += 2) {
int start = updateStartAndSize[i];
int size = updateStartAndSize[i + 1];
syncBufferToA(start, size);
}
if (dataEnd + 8 < aBuffer.capacity()) {
updateHash = random.nextLong() ^ System.nanoTime();
aBuffer.putLong(dataEnd, updateHash);
}
// update B File
syncAToB(0, DATA_START);
for (int i = 0; i < updateCount; i += 2) {
int start = updateStartAndSize[i];
int size = updateStartAndSize[i + 1];
syncAToB(start, size);
}
if (!deletedFiles.isEmpty()) {
for (String oldFileName : deletedFiles) {
deleteExternalFile(oldFileName);
}
}
if (fastBuffer.hb.length - dataEnd > TRUNCATE_THRESHOLD) {
truncate();
}
return true;
} catch (Exception e) {
error(e);
needFullWrite = true;
} finally {
updateCount = 0;
if (!deletedFiles.isEmpty()) {
deletedFiles.clear();
}
waitExternalWriting();
releaseLock();
kvHandler.sendEmptyMessage(MSG_DATA_CHANGE);
}
return false;
}
private void waitExternalWriting() {
while (externalExecutor.isNotEmpty()) {
try {
//noinspection BusyWait
Thread.sleep(10L);
} catch (Exception ignore) {
}
}
}
protected void lockAndCheckUpdate() {
if (bFileLock != null) {
return;
}
if (bChannel == null) {
loadFromABFile();
trySettingObserver();
}
if (bChannel != null) {
try {
bFileLock = bChannel.lock();
try {
checkUpdate();
} finally {
// In case of user forget to release lock,
// set a timeout to apply data (will release lock as well).
kvHandler.sendEmptyMessageDelayed(MSG_APPLY, LOCK_TIMEOUT);
}
} catch (Exception e) {
error(e);
}
}
}
private synchronized void releaseLock() {
if (bFileLock != null) {
try {
bFileLock.release();
} catch (Exception e) {
error(e);
}
bFileLock = null;
kvHandler.removeMessages(MSG_APPLY);
}
}
private void reloadFromABuffer() {
if (aBuffer == null) {
return;
}
reloadData();
getUpdateHash();
fastBuffer.position = 0;
int size = fastBuffer.getInt();
int dataSize = unpackSize(size);
boolean hadEncrypted = isCipher(size);
checksum = fastBuffer.getLong();
dataEnd = DATA_START + dataSize;
if (checksum != fastBuffer.getChecksum(DATA_START, dataSize) || !parseData(hadEncrypted)) {
clearData();
}
}
private void reloadData() {
data.clear();
clearInvalid();
int capacity = aBuffer.capacity();
if (fastBuffer == null) {
fastBuffer = new FastBuffer(capacity);
} else if (fastBuffer.hb.length != capacity) {
fastBuffer.hb = new byte[capacity];
}
aBuffer.rewind();
aBuffer.get(fastBuffer.hb, 0, dataEnd);
}
private void checkUpdate() throws IOException {
MappedByteBuffer buffer = aBuffer;
if (buffer == null || aFile == null) {
return;
}
int fileLen = (int) aFile.length();
if (fileLen <= 0) {
error("invalid file length");
return;
}
if (aBuffer.capacity() != fileLen) {
aChannel.truncate(fileLen);
buffer = aChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileLen);
if (buffer == null) {
return;
}
aBuffer = buffer;
aBuffer.order(ByteOrder.LITTLE_ENDIAN);
}
if (bChannel.size() != fileLen) {
bChannel.truncate(fileLen);
}
int capacity = buffer.capacity();
int size = buffer.getInt(0);
int dataSize = unpackSize(size);
boolean hadEncrypted = isCipher(size);
if (dataSize < 0 || dataSize > capacity) {
throw new IllegalStateException("Invalid file, dataSize:" + dataSize + ", capacity:" + capacity);
}
long sum = buffer.getLong(4);
int end = DATA_START + dataSize;
long hash = updateHash;
if (end < buffer.capacity() - 8) {
hash = buffer.getLong(end);
}
if (end != dataEnd || sum != checksum || hash != updateHash) {
dataEnd = end;
checksum = sum;
updateHash = hash;
HashMap<String, BaseContainer> oldData = listeners.isEmpty() ? null : new HashMap<>(data);
reloadData();
if (sum == fastBuffer.getChecksum(DATA_START, dataSize) && parseData(hadEncrypted)) {
if (oldData != null) {
checkDiff(oldData);
}
} else {
clearData();
}
}
}
private void checkDiff(HashMap<String, BaseContainer> oldData) {
Set<String> newSet = new HashSet<>(data.keySet());
Set<String> oldSet = new HashSet<>(oldData.keySet());
Set<String> common = new HashSet<>(newSet);
common.retainAll(oldSet);
newSet.removeAll(common);
oldSet.removeAll(common);
changedKey.addAll(newSet);
changedKey.addAll(oldSet);
for (String key : common) {
BaseContainer oldValue = oldData.get(key);
BaseContainer newValue = data.get(key);
if (oldValue != null && !oldValue.equalTo(newValue)) {
changedKey.add(key);
}
}
if (!changedKey.isEmpty()) {
kvHandler.sendEmptyMessage(MSG_DATA_CHANGE);
}
}
/**
* Clear all data, take effect immediately (no need to call commit/apply).
*/
public synchronized Editor clear() {
lockAndCheckUpdate();
clearData();
releaseLock();
return this;
}
private void clearData() {
resetMemory();
try {
alignAToBuffer();
aBuffer.putInt(0, packSize(0));
aBuffer.putLong(4, 0L);
getUpdateHash();
if (Utils.makeFileIfNotExist(bFile)) {
setBFileSize(PAGE_SIZE);
syncAToB(0, DATA_START);
trySettingObserver();
}
} catch (Exception e) {
error(e);
needFullWrite = true;
}
Utils.deleteFile(new File(path + name));
kvHandler.sendEmptyMessage(MSG_CLEAR);
}
private void setBFileSize(int size) throws IOException {
if (bAccessFile == null) {
bAccessFile = new RandomAccessFile(bFile, "rw");
}
if (bChannel == null) {
bChannel = bAccessFile.getChannel();
}
if (bChannel.size() != size) {
bAccessFile.setLength(size);
bChannel.truncate(size);
}
}
protected void resetData() {
super.resetData();
updateHash = 0;
}
private void addUpdate(int start, int size) {
int count = updateCount;
int capacity = updateStartAndSize.length;
if ((count << 1) >= capacity) {
int[] newArray = new int[capacity << 1];
System.arraycopy(updateStartAndSize, 0, newArray, 0, capacity);
updateStartAndSize = newArray;
}
updateStartAndSize[count] = start;
updateStartAndSize[count + 1] = size;
updateCount = count + 2;
}
protected void updateChange() {
checksum ^= fastBuffer.getChecksum(updateStart, updateSize);
if (updateSize != 0) {
addUpdate(updateStart, updateSize);
updateSize = 0;
}
}
protected void ensureSize(int allocate) {
int capacity = fastBuffer.hb.length;
int expected = dataEnd + allocate + 8;
if (expected >= capacity) {
int newCapacity = getNewCapacity(capacity, expected);
byte[] bytes = new byte[newCapacity];
System.arraycopy(fastBuffer.hb, 0, bytes, 0, dataEnd);
fastBuffer.hb = bytes;
}
}
protected void updateBoolean(byte value, int offset) {
checksum ^= shiftCheckSum(1L, offset);
fastBuffer.hb[offset] = value;
addUpdate(offset, 1);
}
protected void updateInt32(int value, long sum, int offset) {
checksum ^= shiftCheckSum(sum, offset);
fastBuffer.putInt(offset, value);
addUpdate(offset, 4);
}
protected void updateInt64(long value, long sum, int offset) {
checksum ^= shiftCheckSum(sum, offset);
fastBuffer.putLong(offset, value);
addUpdate(offset, 8);
}
protected void updateBytes(int offset, byte[] bytes) {
super.updateBytes(offset, bytes);
addUpdate(offset, bytes.length);
}
protected void removeOldFile(String oldFileName) {
deletedFiles.add(oldFileName);
}
protected void remove(byte type, int start, int end) {
super.remove(type, start, end);
addUpdate(start, 1);
}
protected void checkGC() {
if (invalidBytes >= bytesThreshold() || invalids.size() >= BASE_GC_KEYS_THRESHOLD) {
gc(0);
}
}
protected void updateBuffer(int gcStart, int allocate, int gcUpdateSize) {
int minUpdateStart = gcStart;
for (int i = 0; i < updateCount; i += 2) {
int s = updateStartAndSize[i];
if (s < minUpdateStart) {
minUpdateStart = s;
}
}
updateStartAndSize[0] = minUpdateStart;
updateStartAndSize[1] = dataEnd - minUpdateStart;
updateCount = 2;
}
private void truncate() {
// reserve at least one page space
int newCapacity = getNewCapacity(PAGE_SIZE, dataEnd + PAGE_SIZE);
if (newCapacity >= fastBuffer.hb.length) {
return;
}
byte[] bytes = new byte[newCapacity];
System.arraycopy(fastBuffer.hb, 0, bytes, 0, dataEnd);
fastBuffer.hb = bytes;
try {
aChannel.truncate(newCapacity);
aBuffer = aChannel.map(FileChannel.MapMode.READ_WRITE, 0, newCapacity);
aBuffer.order(ByteOrder.LITTLE_ENDIAN);
bAccessFile.setLength(newCapacity);
bChannel.truncate(newCapacity);
} catch (Exception e) {
error(new Exception(MAP_FAILED, e));
needFullWrite = true;
}
info(TRUNCATE_FINISH);
}
private synchronized void refresh() {
lockAndCheckUpdate();
releaseLock();
}
private final Handler kvHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REFRESH:
refreshExecutor.execute(MPFastKV.this::refresh);
break;
case MSG_APPLY:
apply();
break;
case MSG_DATA_CHANGE:
notifyChangedKeys();
break;
case MSG_CLEAR:
notifyListeners(null);
break;
default:
break;
}
}
};
private class KVFileObserver extends FileObserver {
public KVFileObserver(String path) {
super(path, FileObserver.MODIFY);
}
@Override
public void onEvent(int event, String path) {
// Delay a few time to avoid frequency refresh.
if (!kvHandler.hasMessages(MSG_REFRESH)) {
kvHandler.sendEmptyMessageDelayed(MSG_REFRESH, 30L);
}
}
}
public static class Builder {
private static final Map<String, MPFastKV> INSTANCE_MAP = new ConcurrentHashMap<>();
private final String path;
private final String name;
private FastEncoder[] encoders;
private FastCipher cipher;
private boolean needWatchFileChange = true;
public Builder(String path, String name) {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("path is empty");
}
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name is empty");
}
this.path = path.endsWith("/") ? path : (path + '/');
this.name = name;
}
/**
* Set obj Encoders
*
* @param encoders The encoder array to decode the bytes to obj.
* @return the builder
*/
public Builder encoder(FastEncoder[] encoders) {
this.encoders = encoders;
return this;
}
/**
* Set encryption cipher.
*/
public Builder cipher(FastCipher cipher) {
this.cipher = cipher;
return this;
}
/**
* If there are multi processes access one file, we need to watch the file's changes generally.
* But if only one process will update the file,
* that process is unnecessary to watch changes (other processes won't change the file).
* In that case, call this method will benefit to efficiency.
*
* @return the builder
*/
public Builder disableWatchFileChange() {
needWatchFileChange = false;
return this;
}
public MPFastKV build() {
String key = path + name;
MPFastKV kv = INSTANCE_MAP.get(key);
if (kv == null) {
synchronized (Builder.class) {
kv = INSTANCE_MAP.get(key);
if (kv == null) {
kv = new MPFastKV(path, name, encoders, cipher, needWatchFileChange);
INSTANCE_MAP.put(key, kv);
}
}
}
return kv;
}
}
@NonNull
@Override
public String toString() {
return "MPFastKV: path:" + path + " name:" + name;
}
}