Repository navigation
Expand file tree
/
Copy pathDBMaker.java
More file actions
1016 lines (845 loc) · 33.6 KB
/
Copy pathDBMaker.java
File metadata and controls
1016 lines (845 loc) · 33.6 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 org.mapdb.EngineWrapper.ReadOnlyEngine;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
/**
* A builder class for creating and opening a database.
*
* @author Jan Kotek
*/
public class DBMaker<DBMakerT extends DBMaker<DBMakerT>> {
protected final String TRUE = "true";
protected Fun.RecordCondition cacheCondition;
protected interface Keys{
String cache = "cache";
String cacheSize = "cacheSize";
String cache_disable = "disable";
String cache_hashTable = "hashTable";
String cache_hardRef = "hardRef";
String cache_softRef = "softRef";
String cache_weakRef = "weakRef";
String cache_lru = "lru";
String file = "file";
String volume = "volume";
String volume_raf = "raf";
String volume_mmapfPartial = "mmapfPartial";
String volume_mmapfIfSupported = "mmapfIfSupported";
String volume_mmapf = "mmapf";
String volume_byteBuffer = "byteBuffer";
String volume_directByteBuffer = "directByteBuffer";
String store = "store";
String store_direct = "direct";
String store_wal = "wal";
String store_append = "append";
String store_heap = "heap";
String transactionDisable = "transactionDisable";
String asyncWrite = "asyncWrite";
String asyncWriteFlushDelay = "asyncWriteFlushDelay";
String asyncWriteQueueSize = "asyncWriteQueueSize";
String deleteFilesAfterClose = "deleteFilesAfterClose";
String closeOnJvmShutdown = "closeOnJvmShutdown";
String readOnly = "readOnly";
String compression = "compression";
String compression_lzf = "lzf";
String encryptionKey = "encryptionKey";
String encryption = "encryption";
String encryption_xtea = "xtea";
String checksum = "checksum";
String concurrencyDisable = "concurrencyDisable";
String freeSpaceReclaimQ = "freeSpaceReclaimQ";
String commitFileSyncDisable = "commitFileSyncDisable";
String snapshots = "snapshots";
String strictDBGet = "strictDBGet";
String sizeLimit = "sizeLimit";
String fullTx = "fullTx";
}
protected Properties props = new Properties();
/** use static factory methods, or make subclass */
protected DBMaker(){}
protected DBMaker(File file) {
props.setProperty(Keys.file, file.getPath());
}
/**
* Creates new in-memory database which stores all data on heap without serialization.
* This mode should be very fast, but data will affect Garbage Collector the same way as traditional Java Collections.
*/
public static DBMaker newHeapDB(){
return new DBMaker()._newHeapDB();
}
public DBMakerT _newHeapDB(){
props.setProperty(Keys.store,Keys.store_heap);
return getThis();
}
/** Creates new in-memory database. Changes are lost after JVM exits.
* <p/>
* This will use HEAP memory so Garbage Collector is affected.
*/
public static DBMaker newMemoryDB(){
return new DBMaker()._newMemoryDB();
}
public DBMakerT _newMemoryDB(){
props.setProperty(Keys.volume,Keys.volume_byteBuffer);
return getThis();
}
/** Creates new in-memory database. Changes are lost after JVM exits.
* <p/>
* This will use DirectByteBuffer outside of HEAP, so Garbage Collector is not affected
*/
public static DBMaker newMemoryDirectDB(){
return new DBMaker()._newMemoryDirectDB();
}
public DBMakerT _newMemoryDirectDB() {
props.setProperty(Keys.volume,Keys.volume_directByteBuffer);
return getThis();
}
/**
* Creates or open append-only database stored in file.
* This database uses format other than usual file db
*
* @param file
* @return maker
*/
public static DBMaker newAppendFileDB(File file) {
return new DBMaker()._newAppendFileDB(file);
}
public DBMakerT _newAppendFileDB(File file) {
props.setProperty(Keys.file, file.getPath());
props.setProperty(Keys.store, Keys.store_append);
return getThis();
}
/**
* Create new BTreeMap backed by temporary file storage.
* This is quick way to create 'throw away' collection.
*
* <p>Storage is created in temp folder and deleted on JVM shutdown
*/
public static <K,V> BTreeMap<K,V> newTempTreeMap(){
return newTempFileDB()
.deleteFilesAfterClose()
.closeOnJvmShutdown()
.transactionDisable()
.make()
.getTreeMap("temp");
}
/**
* Create new HTreeMap backed by temporary file storage.
* This is quick way to create 'throw away' collection.
*
* <p>Storage is created in temp folder and deleted on JVM shutdown
*/
public static <K,V> HTreeMap<K,V> newTempHashMap(){
return newTempFileDB()
.deleteFilesAfterClose()
.closeOnJvmShutdown()
.transactionDisable()
.make()
.getHashMap("temp");
}
/**
* Create new TreeSet backed by temporary file storage.
* This is quick way to create 'throw away' collection.
*
* <p>Storage is created in temp folder and deleted on JVM shutdown
*/
public static <K> NavigableSet<K> newTempTreeSet(){
return newTempFileDB()
.deleteFilesAfterClose()
.closeOnJvmShutdown()
.transactionDisable()
.make()
.getTreeSet("temp");
}
/**
* Create new HashSet backed by temporary file storage.
* This is quick way to create 'throw away' collection.
* <p>
* Storage is created in temp folder and deleted on JVM shutdown
*/
public static <K> Set<K> newTempHashSet(){
return newTempFileDB()
.deleteFilesAfterClose()
.closeOnJvmShutdown()
.transactionDisable()
.make()
.getHashSet("temp");
}
/**
* Creates new database in temporary folder.
*/
public static DBMaker newTempFileDB() {
try {
return newFileDB(File.createTempFile("mapdb-temp","db"));
} catch (IOException e) {
throw new IOError(e);
}
}
/**
* Creates new off-heap cache with maximal size in GBs.
* Entries are removed from cache in most-recently-used fashion
* if store becomes too big.
*
* This method uses off-heap direct ByteBuffers. See {@link java.nio.ByteBuffer#allocateDirect(int)}
*
* @param size maximal size of off-heap store in gigabytes.
* @return map
*/
public static <K,V> HTreeMap<K,V> newCacheDirect(double size){
return DBMaker
.newMemoryDirectDB()
.transactionDisable()
.make()
.createHashMap("cache")
.expireStoreSize(size)
.counterEnable()
.make();
}
/**
* Creates new cache with maximal size in GBs.
* Entries are removed from cache in most-recently-used fashion
* if store becomes too big.
*
* This cache uses on-heap `byte[]`, but does not affect GC since objects are serialized into binary form.
* This method uses ByteBuffers backed by on-heap byte[]. See {@link java.nio.ByteBuffer#allocate(int)}
*
* @param size maximal size of off-heap store in gigabytes.
* @return map
*/
public static <K,V> HTreeMap<K,V> newCache(double size){
return DBMaker
.newMemoryDB()
.transactionDisable()
.make()
.createHashMap("cache")
.expireStoreSize(size)
.counterEnable()
.make();
}
/** Creates or open database stored in file. */
public static DBMaker newFileDB(File file){
return new DBMaker(file);
}
public DBMakerT _newFileDB(File file){
props.setProperty(Keys.file, file.getPath());
return getThis();
}
protected DBMakerT getThis(){
return (DBMakerT)this;
}
/**
* Transaction journal is enabled by default
* You must call <b>DB.commit()</b> to save your changes.
* It is possible to disable transaction journal for better write performance
* In this case all integrity checks are sacrificed for faster speed.
* <p/>
* If transaction journal is disabled, all changes are written DIRECTLY into store.
* You must call DB.close() method before exit,
* otherwise your store <b>WILL BE CORRUPTED</b>
*
*
* @return this builder
*/
public DBMakerT transactionDisable(){
props.put(Keys.transactionDisable,TRUE);
return getThis();
}
/**
* Install callback condition, which decides if some record is to be included in cache.
* Condition should return `true` for every record which should be included
*
* This could be for example useful to include only BTree Directory Nodes and leave values and Leaf nodes outside of cache.
*
* !!! Warning:!!!
*
* Cache requires **consistent** true or false. Failing to do so will result in inconsitent cache and possible data corruption.
* Condition is also executed several times, so it must be very fast
*
* You should only use very simple logic such as `value instanceof SomeClass`.
*
* @return this builder
*/
public DBMakerT cacheCondition(Fun.RecordCondition cacheCondition){
this.cacheCondition = cacheCondition;
return getThis();
}
/**
/**
* Instance cache is enabled by default.
* This greatly decreases serialization overhead and improves performance.
* Call this method to disable instance cache, so an object will always be deserialized.
* <p/>
* This may workaround some problems
*
* @return this builder
*/
public DBMakerT cacheDisable(){
props.put(Keys.cache,Keys.cache_disable);
return getThis();
}
/**
* Enables unbounded hard reference cache.
* This cache is good if you have lot of available memory.
* <p/>
* All fetched records are added to HashMap and stored with hard reference.
* To prevent OutOfMemoryExceptions MapDB monitors free memory,
* if it is bellow 25% cache is cleared.
*
* @return this builder
*/
public DBMakerT cacheHardRefEnable(){
props.put(Keys.cache,Keys.cache_hardRef);
return getThis();
}
/**
* Enables unbounded cache which uses <code>WeakReference</code>.
* Items are removed from cache by Garbage Collector
*
* @return this builder
*/
public DBMakerT cacheWeakRefEnable(){
props.put(Keys.cache,Keys.cache_weakRef);
return getThis();
}
/**
* Enables unbounded cache which uses <code>SoftReference</code>.
* Items are removed from cache by Garbage Collector
*
* @return this builder
*/
public DBMakerT cacheSoftRefEnable(){
props.put(Keys.cache,Keys.cache_softRef);
return getThis();
}
/**
* Enables Least Recently Used cache. It is fixed size cache and it removes less used items to make space.
*
* @return this builder
*/
public DBMakerT cacheLRUEnable(){
props.put(Keys.cache,Keys.cache_lru);
return getThis();
}
/**
* Enables Memory Mapped Files, much faster storage option. However on 32bit JVM this mode could corrupt
* your DB thanks to 4GB memory addressing limit.
*
* You may experience `java.lang.OutOfMemoryError: Map failed` exception on 32bit JVM, if you enable this
* mode.
*/
public DBMakerT mmapFileEnable() {
assertNotInMemoryVolume();
props.setProperty(Keys.volume,Keys.volume_mmapf);
return getThis();
}
/**
* Keeps small-frequently-used part of storage files memory mapped, but main area is accessed using Random Access File.
*
* This mode is good performance compromise between Memory Mapped Files and old slow Random Access Files.
*
* Index file is typically 5% of storage. It contains small frequently read values,
* which is where memory mapped file excel.
*
* With this mode you will experience `java.lang.OutOfMemoryError: Map failed` exceptions on 32bit JVMs
* eventually. But storage size limit is pushed to somewhere around 40GB.
*
*/
public DBMakerT mmapFileEnablePartial() {
assertNotInMemoryVolume();
props.setProperty(Keys.volume,Keys.volume_mmapfPartial);
return getThis();
}
private void assertNotInMemoryVolume() {
if(Keys.volume_byteBuffer.equals(props.getProperty(Keys.volume)) ||
Keys.volume_directByteBuffer.equals(props.getProperty(Keys.volume)))
throw new IllegalArgumentException("Can not enable mmap file for in-memory store");
}
/**
* Enable Memory Mapped Files only if current JVM supports it (is 64bit).
*/
public DBMakerT mmapFileEnableIfSupported() {
assertNotInMemoryVolume();
props.setProperty(Keys.volume,Keys.volume_mmapfIfSupported);
return getThis();
}
/**
* Set cache size. Interpretations depends on cache type.
* For fixed size caches (such as FixedHashTable cache) it is maximal number of items in cache.
* <p/>
* For unbounded caches (such as HardRef cache) it is initial capacity of underlying table (HashMap).
* <p/>
* Default cache size is 32768.
*
* @param cacheSize new cache size
* @return this builder
*/
public DBMakerT cacheSize(int cacheSize){
props.setProperty(Keys.cacheSize,""+cacheSize);
return getThis();
}
/**
* MapDB supports snapshots. `TxEngine` requires additional locking which has small overhead when not used.
* Snapshots are disabled by default. This option switches the snapshots on.
*
* @return this builder
*/
public DBMakerT snapshotEnable(){
props.setProperty(Keys.snapshots,TRUE);
return getThis();
}
/**
* Enables mode where all modifications are queued and written into disk on Background Writer Thread.
* So all modifications are performed in asynchronous mode and do not block.
*
* <p/>
* Enabling this mode might increase performance for single threaded apps.
*
* @return this builder
*/
public DBMakerT asyncWriteEnable(){
props.setProperty(Keys.asyncWrite,TRUE);
return getThis();
}
/**
* Set flush interval for write cache, by default is 0
* <p/>
* When BTreeMap is constructed from ordered set, tree node size is increasing linearly with each
* item added. Each time new key is added to tree node, its size changes and
* storage needs to find new place. So constructing BTreeMap from ordered set leads to large
* store fragmentation.
* <p/>
* Setting flush interval is workaround as BTreeMap node is always updated in memory (write cache)
* and only final version of node is stored on disk.
*
*
* @param delay flush write cache every N miliseconds
* @return this builder
*/
public DBMakerT asyncWriteFlushDelay(int delay){
props.setProperty(Keys.asyncWriteFlushDelay,""+delay);
return getThis();
}
/**
* Set size of async Write Queue. Default size is 32 000
* <p/>
* Using too large queue size can lead to out of memory exception.
*
* @param queueSize of queue
* @return this builder
*/
public DBMakerT asyncWriteQueueSize(int queueSize){
props.setProperty(Keys.asyncWriteQueueSize,""+queueSize);
return getThis();
}
/**
* MapDB is thread-safe by default.
* Concurrency locking brings some overhead which is unnecessary for single-threaded applications.
* This switch will disable concurrency locks and make MapDB thread-unsafe,
* in exchange for small performance bonus in single-threaded use.
*
* Usafull for data imports etc.
*
* @return this builder
*/
public DBMakerT concurrencyDisable(){
props.setProperty(Keys.concurrencyDisable,TRUE);
return getThis();
}
/**
* Try to delete files after DB is closed.
* File deletion may silently fail, especially on Windows where buffer needs to be unmapped file delete.
*
* @return this builder
*/
public DBMakerT deleteFilesAfterClose(){
props.setProperty(Keys.deleteFilesAfterClose,TRUE);
return getThis();
}
/**
* Adds JVM shutdown hook and closes DB just before JVM;
*
* @return this builder
*/
public DBMakerT closeOnJvmShutdown(){
props.setProperty(Keys.closeOnJvmShutdown,TRUE);
return getThis();
}
/**
* Enables record compression.
* <p/>
* Make sure you enable this every time you reopen store, otherwise record de-serialization fails unpredictably.
*
* @return this builder
*/
public DBMakerT compressionEnable(){
props.setProperty(Keys.compression,Keys.compression_lzf);
return getThis();
}
/**
* Encrypt storage using XTEA algorithm.
* <p/>
* XTEA is sound encryption algorithm. However implementation in MapDB was not peer-reviewed.
* MapDB only encrypts records data, so attacker may see number of records and their sizes.
* <p/>
* Make sure you enable this every time you reopen store, otherwise record de-serialization fails unpredictably.
*
* @param password for encryption
* @return this builder
*/
public DBMakerT encryptionEnable(String password){
return encryptionEnable(password.getBytes(Charset.forName("UTF8")));
}
/**
* Encrypt storage using XTEA algorithm.
* <p/>
* XTEA is sound encryption algorithm. However implementation in MapDB was not peer-reviewed.
* MapDB only encrypts records data, so attacker may see number of records and their sizes.
* <p/>
* Make sure you enable this every time you reopen store, otherwise record de-serialization fails unpredictably.
*
* @param password for encryption
* @return this builder
*/
public DBMakerT encryptionEnable(byte[] password){
props.setProperty(Keys.encryption, Keys.encryption_xtea);
props.setProperty(Keys.encryptionKey, toHexa(password));
return getThis();
}
/**
* Adds CRC32 checksum at end of each record to check data integrity.
* It throws 'IOException("Checksum does not match, data broken")' on de-serialization if data are corrupted
* <p/>
* Make sure you enable this every time you reopen store, otherwise record de-serialization fails.
*
* @return this builder
*/
public DBMakerT checksumEnable(){
props.setProperty(Keys.checksum,TRUE);
return getThis();
}
/**
* DB Get methods such as {@link DB#getTreeMap(String)} or {@link DB#getAtomicLong(String)} auto create
* new record with default values, if record with given name does not exist. This could be problem if you would like to enforce
* stricter database schema. So this parameter disables record auto creation.
*
* If this set, `DB.getXX()` will throw an exception if given name does not exist, instead of creating new record (or collection)
*
* @return this builder
*/
public DBMakerT strictDBGet(){
props.setProperty(Keys.strictDBGet,TRUE);
return getThis();
}
/**
* Open store in read-only mode. Any modification attempt will throw
* <code>UnsupportedOperationException("Read-only")</code>
*
* @return this builder
*/
public DBMakerT readOnly(){
props.setProperty(Keys.readOnly,TRUE);
return getThis();
}
/**
* Set free space reclaim Q. It is value from 0 to 10, indicating how eagerly MapDB
* searchs for free space inside store to reuse, before expanding store file.
* 0 means that no free space will be reused and store file will just grow (effectively append only).
* 10 means that MapDB tries really hard to reuse free space, even if it may hurt performance.
* Default value is 5;
*
*
* @return this builder
*/
public DBMakerT freeSpaceReclaimQ(int q){
if(q<0||q>10) throw new IllegalArgumentException("wrong Q");
props.setProperty(Keys.freeSpaceReclaimQ,""+q);
return getThis();
}
/**
* Disables file sync on commit. This way transactions are preserved (rollback works),
* but commits are not 'durable' and data may be lost if store is not properly closed.
* File store will get properly synced when closed.
* Disabling this will make commits faster.
*
* @return this builder
*/
public DBMakerT commitFileSyncDisable(){
props.setProperty(Keys.commitFileSyncDisable,TRUE);
return getThis();
}
/**
* Sets store size limit. Disk or memory space consumed be storage should not grow over this space.
* Limit is not strict and does not apply to some parts such as index table. Actual store size might
* be 10% or more bigger.
*
*
* @param maxSize maximal store size in GB
* @return this builder
*/
public DBMakerT sizeLimit(double maxSize){
long size = (long) (maxSize * 1024D*1024D*1024D);
props.setProperty(Keys.sizeLimit,""+size);
return getThis();
}
/** constructs DB using current settings */
public DB make(){
boolean strictGet = propsGetBool(Keys.strictDBGet);
Engine engine = makeEngine();
boolean dbCreated = false;
try{
DB db = new DB(engine, strictGet,false);
dbCreated = true;
return db;
}finally {
//did db creation fail? in that case close engine to unlock files
if(!dbCreated)
engine.close();
}
}
public TxMaker makeTxMaker(){
props.setProperty(Keys.fullTx,TRUE);
snapshotEnable();
Engine e = makeEngine();
//init catalog if needed
DB db = new DB(e);
db.commit();
return new TxMaker(e, propsGetBool(Keys.strictDBGet), propsGetBool(Keys.snapshots));
}
/** constructs Engine using current settings */
public Engine makeEngine(){
final boolean readOnly = propsGetBool(Keys.readOnly);
final File file = props.containsKey(Keys.file)? new File(props.getProperty(Keys.file)):null;
final String volume = props.getProperty(Keys.volume);
final String store = props.getProperty(Keys.store);
if(readOnly && file==null)
throw new UnsupportedOperationException("Can not open in-memory DB in read-only mode.");
if(readOnly && !file.exists() && !Keys.store_append.equals(store)){
throw new UnsupportedOperationException("Can not open non-existing file in read-only mode.");
}
if(propsGetLong(Keys.sizeLimit,0)>0 && Keys.store_append.equals(store))
throw new UnsupportedOperationException("Append-Only store does not support Size Limit");
if(propsGetBool(Keys.concurrencyDisable) && propsGetBool(Keys.asyncWrite)){
throw new UnsupportedOperationException("Can not use `concurrencyDisable` together with Async Writer");
}
extendArgumentCheck();
Engine engine;
if(Keys.store_heap.equals(store)){
engine = extendHeapStore();
}else if(Keys.store_append.equals(store)){
if(Keys.volume_byteBuffer.equals(volume)||Keys.volume_directByteBuffer.equals(volume))
throw new UnsupportedOperationException("Append Storage format is not supported with in-memory dbs");
engine = extendStoreAppend();
}else{
Volume.Factory folFac = extendStoreVolumeFactory();
engine = propsGetBool(Keys.transactionDisable) ?
extendStoreDirect(folFac):
extendStoreWAL(folFac);
}
engine = extendWrapStore(engine);
if(propsGetBool(Keys.asyncWrite) && !readOnly){
engine = extendAsyncWriteEngine(engine);
}
final String cache = props.getProperty(Keys.cache, CC.DEFAULT_CACHE);
if(Keys.cache_disable.equals(cache)){
//do not wrap engine in cache
}else if(Keys.cache_hashTable.equals(cache)){
engine = extendCacheHashTable(engine);
}else if (Keys.cache_hardRef.equals(cache)){
engine = extendCacheHardRef(engine);
}else if (Keys.cache_weakRef.equals(cache)){
engine = extendCacheWeakRef(engine);
}else if (Keys.cache_softRef.equals(cache)){
engine = extendCacheSoftRef(engine);
}else if (Keys.cache_lru.equals(cache)){
engine = extendCacheLRU(engine);
}else{
throw new IllegalArgumentException("unknown cache type: "+cache);
}
engine = extendWrapCache(engine);
if(propsGetBool(Keys.snapshots))
engine = extendSnapshotEngine(engine);
engine = extendWrapSnapshotEngine(engine);
if(readOnly)
engine = new ReadOnlyEngine(engine);
if(propsGetBool(Keys.closeOnJvmShutdown)){
engine = new EngineWrapper.CloseOnJVMShutdown(engine);
}
//try to read one record from DB, to make sure encryption and compression are correctly set.
Fun.Tuple2<Integer,byte[]> check = null;
try{
check = (Fun.Tuple2<Integer, byte[]>) engine.get(Engine.CHECK_RECORD, Serializer.BASIC);
if(check!=null){
if(check.a != Arrays.hashCode(check.b))
throw new RuntimeException("invalid checksum");
}
}catch(Throwable e){
throw new IllegalArgumentException("Error while opening store. Make sure you have right password, compression or encryption is well configured.",e);
}
if(check == null && !engine.isReadOnly()){
//new db, so insert testing record
byte[] b = new byte[127];
new Random().nextBytes(b);
check = Fun.t2(Arrays.hashCode(b), b);
engine.update(Engine.CHECK_RECORD, check, Serializer.BASIC);
engine.commit();
}
return engine;
}
protected int propsGetInt(String key, int defValue){
String ret = props.getProperty(key);
if(ret==null) return defValue;
return Integer.valueOf(ret);
}
protected long propsGetLong(String key, long defValue){
String ret = props.getProperty(key);
if(ret==null) return defValue;
return Long.valueOf(ret);
}
protected boolean propsGetBool(String key){
String ret = props.getProperty(key);
return ret!=null && ret.equals(TRUE);
}
protected byte[] propsGetXteaEncKey(){
if(!Keys.encryption_xtea.equals(props.getProperty(Keys.encryption)))
return null;
return fromHexa(props.getProperty(Keys.encryptionKey));
}
/**
* Check if large files can be mapped into memory.
* For example 32bit JVM can only address 2GB and large files can not be mapped,
* so for 32bit JVM this function returns false.
*
*/
protected static boolean JVMSupportsLargeMappedFiles() {
String prop = System.getProperty("os.arch");
if(prop!=null && prop.contains("64")) return true;
//TODO better check for 32bit JVM
return false;
}
protected int propsGetRafMode(){
String volume = props.getProperty(Keys.volume);
if(volume==null||Keys.volume_raf.equals(volume)){
return 2;
}else if(Keys.volume_mmapfIfSupported.equals(volume)){
return JVMSupportsLargeMappedFiles()?0:2;
}else if(Keys.volume_mmapfPartial.equals(volume)){
return 1;
}else if(Keys.volume_mmapf.equals(volume)){
return 0;
}
return 2; //default option is RAF
}
protected Engine extendSnapshotEngine(Engine engine) {
return new TxEngine(engine,propsGetBool(Keys.fullTx));
}
protected Engine extendCacheLRU(Engine engine) {
int cacheSize = propsGetInt(Keys.cacheSize, CC.DEFAULT_CACHE_SIZE);
return new Caches.LRU(engine, cacheSize,propsGetBool(Keys.concurrencyDisable), cacheCondition);
}
protected Engine extendCacheWeakRef(Engine engine) {
return new Caches.WeakSoftRef(engine,true,propsGetBool(Keys.concurrencyDisable), cacheCondition);
}
protected Engine extendCacheSoftRef(Engine engine) {
return new Caches.WeakSoftRef(engine,false,propsGetBool(Keys.concurrencyDisable), cacheCondition);
}
protected Engine extendCacheHardRef(Engine engine) {
int cacheSize = propsGetInt(Keys.cacheSize, CC.DEFAULT_CACHE_SIZE);
return new Caches.HardRef(engine,cacheSize,propsGetBool(Keys.concurrencyDisable), cacheCondition);
}
protected Engine extendCacheHashTable(Engine engine) {
int cacheSize = propsGetInt(Keys.cacheSize, CC.DEFAULT_CACHE_SIZE);
return new Caches.HashTable(engine, cacheSize,propsGetBool(Keys.concurrencyDisable), cacheCondition);
}
protected Engine extendAsyncWriteEngine(Engine engine) {
return new AsyncWriteEngine(engine,
propsGetInt(Keys.asyncWriteFlushDelay,CC.ASYNC_WRITE_FLUSH_DELAY),
propsGetInt(Keys.asyncWriteQueueSize,CC.ASYNC_WRITE_QUEUE_SIZE),
null);
}
protected void extendArgumentCheck() {
}
protected Engine extendWrapStore(Engine engine) {
return engine;
}
protected Engine extendWrapCache(Engine engine) {
return engine;
}
protected Engine extendWrapSnapshotEngine(Engine engine) {
return engine;
}
protected Engine extendHeapStore() {
return new StoreHeap(propsGetBool(Keys.concurrencyDisable));
}
protected Engine extendStoreAppend() {
final File file = props.containsKey(Keys.file)? new File(props.getProperty(Keys.file)):null;
boolean compressionEnabled = Keys.compression_lzf.equals(props.getProperty(Keys.compression));
return new StoreAppend(file, propsGetRafMode()>0, propsGetBool(Keys.readOnly),
propsGetBool(Keys.transactionDisable),
propsGetBool(Keys.deleteFilesAfterClose),
propsGetBool(Keys.commitFileSyncDisable),
propsGetBool(Keys.checksum),compressionEnabled,propsGetXteaEncKey(),
propsGetBool(Keys.concurrencyDisable));
}
protected Engine extendStoreDirect(Volume.Factory folFac) {
boolean compressionEnabled = Keys.compression_lzf.equals(props.getProperty(Keys.compression));
return new StoreDirect(folFac, propsGetBool(Keys.readOnly),
propsGetBool(Keys.deleteFilesAfterClose),
propsGetInt(Keys.freeSpaceReclaimQ,CC.DEFAULT_FREE_SPACE_RECLAIM_Q),
propsGetBool(Keys.commitFileSyncDisable),propsGetLong(Keys.sizeLimit,0),
propsGetBool(Keys.checksum),compressionEnabled,propsGetXteaEncKey(),
propsGetBool(Keys.concurrencyDisable),0);
}
protected Engine extendStoreWAL(Volume.Factory folFac) {
boolean compressionEnabled = Keys.compression_lzf.equals(props.getProperty(Keys.compression));
return new StoreWAL(folFac, propsGetBool(Keys.readOnly),propsGetBool(Keys.deleteFilesAfterClose),
propsGetInt(Keys.freeSpaceReclaimQ,CC.DEFAULT_FREE_SPACE_RECLAIM_Q),
propsGetBool(Keys.commitFileSyncDisable),propsGetLong(Keys.sizeLimit,-1),
propsGetBool(Keys.checksum),compressionEnabled,propsGetXteaEncKey(),
propsGetBool(Keys.concurrencyDisable),0);
}
protected Volume.Factory extendStoreVolumeFactory() {
long sizeLimit = propsGetLong(Keys.sizeLimit,0);
String volume = props.getProperty(Keys.volume);
if(Keys.volume_byteBuffer.equals(volume))
return Volume.memoryFactory(false,sizeLimit,CC.VOLUME_SLICE_SHIFT);
else if(Keys.volume_directByteBuffer.equals(volume))
return Volume.memoryFactory(true,sizeLimit,CC.VOLUME_SLICE_SHIFT);
File file = new File(props.getProperty(Keys.file));
return Volume.fileFactory(
file,
propsGetRafMode(), propsGetBool(Keys.readOnly),
sizeLimit,CC.VOLUME_SLICE_SHIFT,0);
}
protected static String toHexa( byte [] bb ) {
char[] HEXA_CHARS = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};