1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 package org.rocksdb;
7 
8 import static org.assertj.core.api.Assertions.assertThat;
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 
13 import java.nio.file.Paths;
14 import java.util.*;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import org.junit.ClassRule;
17 import org.junit.Test;
18 
19 public class DBOptionsTest {
20 
21   @ClassRule
22   public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
23       new RocksNativeLibraryResource();
24 
25   public static final Random rand = PlatformRandomHelper.
26       getPlatformSpecificRandomFactory();
27 
28   @Test
copyConstructor()29   public void copyConstructor() {
30     DBOptions origOpts = new DBOptions();
31     origOpts.setCreateIfMissing(rand.nextBoolean());
32     origOpts.setAllow2pc(rand.nextBoolean());
33     origOpts.setMaxBackgroundJobs(rand.nextInt(10));
34     DBOptions copyOpts = new DBOptions(origOpts);
35     assertThat(origOpts.createIfMissing()).isEqualTo(copyOpts.createIfMissing());
36     assertThat(origOpts.allow2pc()).isEqualTo(copyOpts.allow2pc());
37     assertThat(origOpts.baseBackgroundCompactions()).isEqualTo(
38             copyOpts.baseBackgroundCompactions());
39   }
40 
41   @Test
getDBOptionsFromProps()42   public void getDBOptionsFromProps() {
43     // setup sample properties
44     final Properties properties = new Properties();
45     properties.put("allow_mmap_reads", "true");
46     properties.put("bytes_per_sync", "13");
47     try(final DBOptions opt = DBOptions.getDBOptionsFromProps(properties)) {
48       assertThat(opt).isNotNull();
49       assertThat(String.valueOf(opt.allowMmapReads())).
50           isEqualTo(properties.get("allow_mmap_reads"));
51       assertThat(String.valueOf(opt.bytesPerSync())).
52           isEqualTo(properties.get("bytes_per_sync"));
53     }
54   }
55 
56   @Test
failDBOptionsFromPropsWithIllegalValue()57   public void failDBOptionsFromPropsWithIllegalValue() {
58     // setup sample properties
59     final Properties properties = new Properties();
60     properties.put("tomato", "1024");
61     properties.put("burger", "2");
62     try(final DBOptions opt = DBOptions.getDBOptionsFromProps(properties)) {
63       assertThat(opt).isNull();
64     }
65   }
66 
67   @Test(expected = IllegalArgumentException.class)
failDBOptionsFromPropsWithNullValue()68   public void failDBOptionsFromPropsWithNullValue() {
69     try(final DBOptions opt = DBOptions.getDBOptionsFromProps(null)) {
70       //no-op
71     }
72   }
73 
74   @Test(expected = IllegalArgumentException.class)
failDBOptionsFromPropsWithEmptyProps()75   public void failDBOptionsFromPropsWithEmptyProps() {
76     try(final DBOptions opt = DBOptions.getDBOptionsFromProps(
77         new Properties())) {
78       //no-op
79     }
80   }
81 
82   @Test
linkageOfPrepMethods()83   public void linkageOfPrepMethods() {
84     try (final DBOptions opt = new DBOptions()) {
85       opt.optimizeForSmallDb();
86     }
87   }
88 
89   @Test
env()90   public void env() {
91     try (final DBOptions opt = new DBOptions();
92          final Env env = Env.getDefault()) {
93       opt.setEnv(env);
94       assertThat(opt.getEnv()).isSameAs(env);
95     }
96   }
97 
98   @Test
setIncreaseParallelism()99   public void setIncreaseParallelism() {
100     try(final DBOptions opt = new DBOptions()) {
101       final int threads = Runtime.getRuntime().availableProcessors() * 2;
102       opt.setIncreaseParallelism(threads);
103     }
104   }
105 
106   @Test
createIfMissing()107   public void createIfMissing() {
108     try(final DBOptions opt = new DBOptions()) {
109       final boolean boolValue = rand.nextBoolean();
110       opt.setCreateIfMissing(boolValue);
111       assertThat(opt.createIfMissing()).isEqualTo(boolValue);
112     }
113   }
114 
115   @Test
createMissingColumnFamilies()116   public void createMissingColumnFamilies() {
117     try(final DBOptions opt = new DBOptions()) {
118       final boolean boolValue = rand.nextBoolean();
119       opt.setCreateMissingColumnFamilies(boolValue);
120       assertThat(opt.createMissingColumnFamilies()).isEqualTo(boolValue);
121     }
122   }
123 
124   @Test
errorIfExists()125   public void errorIfExists() {
126     try(final DBOptions opt = new DBOptions()) {
127       final boolean boolValue = rand.nextBoolean();
128       opt.setErrorIfExists(boolValue);
129       assertThat(opt.errorIfExists()).isEqualTo(boolValue);
130     }
131   }
132 
133   @Test
paranoidChecks()134   public void paranoidChecks() {
135     try(final DBOptions opt = new DBOptions()) {
136       final boolean boolValue = rand.nextBoolean();
137       opt.setParanoidChecks(boolValue);
138       assertThat(opt.paranoidChecks()).isEqualTo(boolValue);
139     }
140   }
141 
142   @Test
maxTotalWalSize()143   public void maxTotalWalSize() {
144     try(final DBOptions opt = new DBOptions()) {
145       final long longValue = rand.nextLong();
146       opt.setMaxTotalWalSize(longValue);
147       assertThat(opt.maxTotalWalSize()).isEqualTo(longValue);
148     }
149   }
150 
151   @Test
maxOpenFiles()152   public void maxOpenFiles() {
153     try(final DBOptions opt = new DBOptions()) {
154       final int intValue = rand.nextInt();
155       opt.setMaxOpenFiles(intValue);
156       assertThat(opt.maxOpenFiles()).isEqualTo(intValue);
157     }
158   }
159 
160   @Test
maxFileOpeningThreads()161   public void maxFileOpeningThreads() {
162     try(final DBOptions opt = new DBOptions()) {
163       final int intValue = rand.nextInt();
164       opt.setMaxFileOpeningThreads(intValue);
165       assertThat(opt.maxFileOpeningThreads()).isEqualTo(intValue);
166     }
167   }
168 
169   @Test
useFsync()170   public void useFsync() {
171     try(final DBOptions opt = new DBOptions()) {
172       final boolean boolValue = rand.nextBoolean();
173       opt.setUseFsync(boolValue);
174       assertThat(opt.useFsync()).isEqualTo(boolValue);
175     }
176   }
177 
178   @Test
dbPaths()179   public void dbPaths() {
180     final List<DbPath> dbPaths = new ArrayList<>();
181     dbPaths.add(new DbPath(Paths.get("/a"), 10));
182     dbPaths.add(new DbPath(Paths.get("/b"), 100));
183     dbPaths.add(new DbPath(Paths.get("/c"), 1000));
184 
185     try(final DBOptions opt = new DBOptions()) {
186       assertThat(opt.dbPaths()).isEqualTo(Collections.emptyList());
187 
188       opt.setDbPaths(dbPaths);
189 
190       assertThat(opt.dbPaths()).isEqualTo(dbPaths);
191     }
192   }
193 
194   @Test
dbLogDir()195   public void dbLogDir() {
196     try(final DBOptions opt = new DBOptions()) {
197       final String str = "path/to/DbLogDir";
198       opt.setDbLogDir(str);
199       assertThat(opt.dbLogDir()).isEqualTo(str);
200     }
201   }
202 
203   @Test
walDir()204   public void walDir() {
205     try(final DBOptions opt = new DBOptions()) {
206       final String str = "path/to/WalDir";
207       opt.setWalDir(str);
208       assertThat(opt.walDir()).isEqualTo(str);
209     }
210   }
211 
212   @Test
deleteObsoleteFilesPeriodMicros()213   public void deleteObsoleteFilesPeriodMicros() {
214     try(final DBOptions opt = new DBOptions()) {
215       final long longValue = rand.nextLong();
216       opt.setDeleteObsoleteFilesPeriodMicros(longValue);
217       assertThat(opt.deleteObsoleteFilesPeriodMicros()).isEqualTo(longValue);
218     }
219   }
220 
221   @SuppressWarnings("deprecated")
222   @Test
baseBackgroundCompactions()223   public void baseBackgroundCompactions() {
224     try (final DBOptions opt = new DBOptions()) {
225       final int intValue = rand.nextInt();
226       opt.setBaseBackgroundCompactions(intValue);
227       assertThat(opt.baseBackgroundCompactions()).
228           isEqualTo(intValue);
229     }
230   }
231 
232   @SuppressWarnings("deprecated")
233   @Test
maxBackgroundCompactions()234   public void maxBackgroundCompactions() {
235     try(final DBOptions opt = new DBOptions()) {
236       final int intValue = rand.nextInt();
237       opt.setMaxBackgroundCompactions(intValue);
238       assertThat(opt.maxBackgroundCompactions()).isEqualTo(intValue);
239     }
240   }
241 
242   @Test
maxSubcompactions()243   public void maxSubcompactions() {
244     try (final DBOptions opt = new DBOptions()) {
245       final int intValue = rand.nextInt();
246       opt.setMaxSubcompactions(intValue);
247       assertThat(opt.maxSubcompactions()).
248           isEqualTo(intValue);
249     }
250   }
251 
252   @SuppressWarnings("deprecated")
253   @Test
maxBackgroundFlushes()254   public void maxBackgroundFlushes() {
255     try(final DBOptions opt = new DBOptions()) {
256       final int intValue = rand.nextInt();
257       opt.setMaxBackgroundFlushes(intValue);
258       assertThat(opt.maxBackgroundFlushes()).isEqualTo(intValue);
259     }
260   }
261 
262   @Test
maxBackgroundJobs()263   public void maxBackgroundJobs() {
264     try (final DBOptions opt = new DBOptions()) {
265       final int intValue = rand.nextInt();
266       opt.setMaxBackgroundJobs(intValue);
267       assertThat(opt.maxBackgroundJobs()).isEqualTo(intValue);
268     }
269   }
270 
271   @Test
maxLogFileSize()272   public void maxLogFileSize() throws RocksDBException {
273     try(final DBOptions opt = new DBOptions()) {
274       final long longValue = rand.nextLong();
275       opt.setMaxLogFileSize(longValue);
276       assertThat(opt.maxLogFileSize()).isEqualTo(longValue);
277     }
278   }
279 
280   @Test
logFileTimeToRoll()281   public void logFileTimeToRoll() throws RocksDBException {
282     try(final DBOptions opt = new DBOptions()) {
283       final long longValue = rand.nextLong();
284       opt.setLogFileTimeToRoll(longValue);
285       assertThat(opt.logFileTimeToRoll()).isEqualTo(longValue);
286     }
287   }
288 
289   @Test
keepLogFileNum()290   public void keepLogFileNum() throws RocksDBException {
291     try(final DBOptions opt = new DBOptions()) {
292       final long longValue = rand.nextLong();
293       opt.setKeepLogFileNum(longValue);
294       assertThat(opt.keepLogFileNum()).isEqualTo(longValue);
295     }
296   }
297 
298   @Test
recycleLogFileNum()299   public void recycleLogFileNum() throws RocksDBException {
300     try(final DBOptions opt = new DBOptions()) {
301       final long longValue = rand.nextLong();
302       opt.setRecycleLogFileNum(longValue);
303       assertThat(opt.recycleLogFileNum()).isEqualTo(longValue);
304     }
305   }
306 
307   @Test
maxManifestFileSize()308   public void maxManifestFileSize() {
309     try(final DBOptions opt = new DBOptions()) {
310       final long longValue = rand.nextLong();
311       opt.setMaxManifestFileSize(longValue);
312       assertThat(opt.maxManifestFileSize()).isEqualTo(longValue);
313     }
314   }
315 
316   @Test
tableCacheNumshardbits()317   public void tableCacheNumshardbits() {
318     try(final DBOptions opt = new DBOptions()) {
319       final int intValue = rand.nextInt();
320       opt.setTableCacheNumshardbits(intValue);
321       assertThat(opt.tableCacheNumshardbits()).isEqualTo(intValue);
322     }
323   }
324 
325   @Test
walSizeLimitMB()326   public void walSizeLimitMB() {
327     try(final DBOptions opt = new DBOptions()) {
328       final long longValue = rand.nextLong();
329       opt.setWalSizeLimitMB(longValue);
330       assertThat(opt.walSizeLimitMB()).isEqualTo(longValue);
331     }
332   }
333 
334   @Test
walTtlSeconds()335   public void walTtlSeconds() {
336     try(final DBOptions opt = new DBOptions()) {
337       final long longValue = rand.nextLong();
338       opt.setWalTtlSeconds(longValue);
339       assertThat(opt.walTtlSeconds()).isEqualTo(longValue);
340     }
341   }
342 
343   @Test
manifestPreallocationSize()344   public void manifestPreallocationSize() throws RocksDBException {
345     try(final DBOptions opt = new DBOptions()) {
346       final long longValue = rand.nextLong();
347       opt.setManifestPreallocationSize(longValue);
348       assertThat(opt.manifestPreallocationSize()).isEqualTo(longValue);
349     }
350   }
351 
352   @Test
useDirectReads()353   public void useDirectReads() {
354     try(final DBOptions opt = new DBOptions()) {
355       final boolean boolValue = rand.nextBoolean();
356       opt.setUseDirectReads(boolValue);
357       assertThat(opt.useDirectReads()).isEqualTo(boolValue);
358     }
359   }
360 
361   @Test
useDirectIoForFlushAndCompaction()362   public void useDirectIoForFlushAndCompaction() {
363     try(final DBOptions opt = new DBOptions()) {
364       final boolean boolValue = rand.nextBoolean();
365       opt.setUseDirectIoForFlushAndCompaction(boolValue);
366       assertThat(opt.useDirectIoForFlushAndCompaction()).isEqualTo(boolValue);
367     }
368   }
369 
370   @Test
allowFAllocate()371   public void allowFAllocate() {
372     try(final DBOptions opt = new DBOptions()) {
373       final boolean boolValue = rand.nextBoolean();
374       opt.setAllowFAllocate(boolValue);
375       assertThat(opt.allowFAllocate()).isEqualTo(boolValue);
376     }
377   }
378 
379   @Test
allowMmapReads()380   public void allowMmapReads() {
381     try(final DBOptions opt = new DBOptions()) {
382       final boolean boolValue = rand.nextBoolean();
383       opt.setAllowMmapReads(boolValue);
384       assertThat(opt.allowMmapReads()).isEqualTo(boolValue);
385     }
386   }
387 
388   @Test
allowMmapWrites()389   public void allowMmapWrites() {
390     try(final DBOptions opt = new DBOptions()) {
391       final boolean boolValue = rand.nextBoolean();
392       opt.setAllowMmapWrites(boolValue);
393       assertThat(opt.allowMmapWrites()).isEqualTo(boolValue);
394     }
395   }
396 
397   @Test
isFdCloseOnExec()398   public void isFdCloseOnExec() {
399     try(final DBOptions opt = new DBOptions()) {
400       final boolean boolValue = rand.nextBoolean();
401       opt.setIsFdCloseOnExec(boolValue);
402       assertThat(opt.isFdCloseOnExec()).isEqualTo(boolValue);
403     }
404   }
405 
406   @Test
statsDumpPeriodSec()407   public void statsDumpPeriodSec() {
408     try(final DBOptions opt = new DBOptions()) {
409       final int intValue = rand.nextInt();
410       opt.setStatsDumpPeriodSec(intValue);
411       assertThat(opt.statsDumpPeriodSec()).isEqualTo(intValue);
412     }
413   }
414 
415   @Test
statsPersistPeriodSec()416   public void statsPersistPeriodSec() {
417     try (final DBOptions opt = new DBOptions()) {
418       final int intValue = rand.nextInt();
419       opt.setStatsPersistPeriodSec(intValue);
420       assertThat(opt.statsPersistPeriodSec()).isEqualTo(intValue);
421     }
422   }
423 
424   @Test
statsHistoryBufferSize()425   public void statsHistoryBufferSize() {
426     try (final DBOptions opt = new DBOptions()) {
427       final long longValue = rand.nextLong();
428       opt.setStatsHistoryBufferSize(longValue);
429       assertThat(opt.statsHistoryBufferSize()).isEqualTo(longValue);
430     }
431   }
432 
433   @Test
adviseRandomOnOpen()434   public void adviseRandomOnOpen() {
435     try(final DBOptions opt = new DBOptions()) {
436       final boolean boolValue = rand.nextBoolean();
437       opt.setAdviseRandomOnOpen(boolValue);
438       assertThat(opt.adviseRandomOnOpen()).isEqualTo(boolValue);
439     }
440   }
441 
442   @Test
dbWriteBufferSize()443   public void dbWriteBufferSize() {
444     try(final DBOptions opt = new DBOptions()) {
445       final long longValue = rand.nextLong();
446       opt.setDbWriteBufferSize(longValue);
447       assertThat(opt.dbWriteBufferSize()).isEqualTo(longValue);
448     }
449   }
450 
451   @Test
setWriteBufferManager()452   public void setWriteBufferManager() throws RocksDBException {
453     try (final DBOptions opt = new DBOptions();
454          final Cache cache = new LRUCache(1 * 1024 * 1024);
455          final WriteBufferManager writeBufferManager = new WriteBufferManager(2000l, cache)) {
456       opt.setWriteBufferManager(writeBufferManager);
457       assertThat(opt.writeBufferManager()).isEqualTo(writeBufferManager);
458     }
459   }
460 
461   @Test
setWriteBufferManagerWithZeroBufferSize()462   public void setWriteBufferManagerWithZeroBufferSize() throws RocksDBException {
463     try (final DBOptions opt = new DBOptions();
464          final Cache cache = new LRUCache(1 * 1024 * 1024);
465          final WriteBufferManager writeBufferManager = new WriteBufferManager(0l, cache)) {
466       opt.setWriteBufferManager(writeBufferManager);
467       assertThat(opt.writeBufferManager()).isEqualTo(writeBufferManager);
468     }
469   }
470 
471   @Test
accessHintOnCompactionStart()472   public void accessHintOnCompactionStart() {
473     try(final DBOptions opt = new DBOptions()) {
474       final AccessHint accessHint = AccessHint.SEQUENTIAL;
475       opt.setAccessHintOnCompactionStart(accessHint);
476       assertThat(opt.accessHintOnCompactionStart()).isEqualTo(accessHint);
477     }
478   }
479 
480   @Test
newTableReaderForCompactionInputs()481   public void newTableReaderForCompactionInputs() {
482     try(final DBOptions opt = new DBOptions()) {
483       final boolean boolValue = rand.nextBoolean();
484       opt.setNewTableReaderForCompactionInputs(boolValue);
485       assertThat(opt.newTableReaderForCompactionInputs()).isEqualTo(boolValue);
486     }
487   }
488 
489   @Test
compactionReadaheadSize()490   public void compactionReadaheadSize() {
491     try(final DBOptions opt = new DBOptions()) {
492       final long longValue = rand.nextLong();
493       opt.setCompactionReadaheadSize(longValue);
494       assertThat(opt.compactionReadaheadSize()).isEqualTo(longValue);
495     }
496   }
497 
498   @Test
randomAccessMaxBufferSize()499   public void randomAccessMaxBufferSize() {
500     try(final DBOptions opt = new DBOptions()) {
501       final long longValue = rand.nextLong();
502       opt.setRandomAccessMaxBufferSize(longValue);
503       assertThat(opt.randomAccessMaxBufferSize()).isEqualTo(longValue);
504     }
505   }
506 
507   @Test
writableFileMaxBufferSize()508   public void writableFileMaxBufferSize() {
509     try(final DBOptions opt = new DBOptions()) {
510       final long longValue = rand.nextLong();
511       opt.setWritableFileMaxBufferSize(longValue);
512       assertThat(opt.writableFileMaxBufferSize()).isEqualTo(longValue);
513     }
514   }
515 
516   @Test
useAdaptiveMutex()517   public void useAdaptiveMutex() {
518     try(final DBOptions opt = new DBOptions()) {
519       final boolean boolValue = rand.nextBoolean();
520       opt.setUseAdaptiveMutex(boolValue);
521       assertThat(opt.useAdaptiveMutex()).isEqualTo(boolValue);
522     }
523   }
524 
525   @Test
bytesPerSync()526   public void bytesPerSync() {
527     try(final DBOptions opt = new DBOptions()) {
528       final long longValue = rand.nextLong();
529       opt.setBytesPerSync(longValue);
530       assertThat(opt.bytesPerSync()).isEqualTo(longValue);
531     }
532   }
533 
534   @Test
walBytesPerSync()535   public void walBytesPerSync() {
536     try(final DBOptions opt = new DBOptions()) {
537       final long longValue = rand.nextLong();
538       opt.setWalBytesPerSync(longValue);
539       assertThat(opt.walBytesPerSync()).isEqualTo(longValue);
540     }
541   }
542 
543   @Test
strictBytesPerSync()544   public void strictBytesPerSync() {
545     try (final DBOptions opt = new DBOptions()) {
546       assertThat(opt.strictBytesPerSync()).isFalse();
547       opt.setStrictBytesPerSync(true);
548       assertThat(opt.strictBytesPerSync()).isTrue();
549     }
550   }
551 
552   @Test
enableThreadTracking()553   public void enableThreadTracking() {
554     try (final DBOptions opt = new DBOptions()) {
555       final boolean boolValue = rand.nextBoolean();
556       opt.setEnableThreadTracking(boolValue);
557       assertThat(opt.enableThreadTracking()).isEqualTo(boolValue);
558     }
559   }
560 
561   @Test
delayedWriteRate()562   public void delayedWriteRate() {
563     try(final DBOptions opt = new DBOptions()) {
564       final long longValue = rand.nextLong();
565       opt.setDelayedWriteRate(longValue);
566       assertThat(opt.delayedWriteRate()).isEqualTo(longValue);
567     }
568   }
569 
570   @Test
enablePipelinedWrite()571   public void enablePipelinedWrite() {
572     try(final DBOptions opt = new DBOptions()) {
573       assertThat(opt.enablePipelinedWrite()).isFalse();
574       opt.setEnablePipelinedWrite(true);
575       assertThat(opt.enablePipelinedWrite()).isTrue();
576     }
577   }
578 
579   @Test
unordredWrite()580   public void unordredWrite() {
581     try(final DBOptions opt = new DBOptions()) {
582       assertThat(opt.unorderedWrite()).isFalse();
583       opt.setUnorderedWrite(true);
584       assertThat(opt.unorderedWrite()).isTrue();
585     }
586   }
587 
588   @Test
allowConcurrentMemtableWrite()589   public void allowConcurrentMemtableWrite() {
590     try (final DBOptions opt = new DBOptions()) {
591       final boolean boolValue = rand.nextBoolean();
592       opt.setAllowConcurrentMemtableWrite(boolValue);
593       assertThat(opt.allowConcurrentMemtableWrite()).isEqualTo(boolValue);
594     }
595   }
596 
597   @Test
enableWriteThreadAdaptiveYield()598   public void enableWriteThreadAdaptiveYield() {
599     try (final DBOptions opt = new DBOptions()) {
600       final boolean boolValue = rand.nextBoolean();
601       opt.setEnableWriteThreadAdaptiveYield(boolValue);
602       assertThat(opt.enableWriteThreadAdaptiveYield()).isEqualTo(boolValue);
603     }
604   }
605 
606   @Test
writeThreadMaxYieldUsec()607   public void writeThreadMaxYieldUsec() {
608     try (final DBOptions opt = new DBOptions()) {
609       final long longValue = rand.nextLong();
610       opt.setWriteThreadMaxYieldUsec(longValue);
611       assertThat(opt.writeThreadMaxYieldUsec()).isEqualTo(longValue);
612     }
613   }
614 
615   @Test
writeThreadSlowYieldUsec()616   public void writeThreadSlowYieldUsec() {
617     try (final DBOptions opt = new DBOptions()) {
618       final long longValue = rand.nextLong();
619       opt.setWriteThreadSlowYieldUsec(longValue);
620       assertThat(opt.writeThreadSlowYieldUsec()).isEqualTo(longValue);
621     }
622   }
623 
624   @Test
skipStatsUpdateOnDbOpen()625   public void skipStatsUpdateOnDbOpen() {
626     try (final DBOptions opt = new DBOptions()) {
627       final boolean boolValue = rand.nextBoolean();
628       opt.setSkipStatsUpdateOnDbOpen(boolValue);
629       assertThat(opt.skipStatsUpdateOnDbOpen()).isEqualTo(boolValue);
630     }
631   }
632 
633   @Test
walRecoveryMode()634   public void walRecoveryMode() {
635     try (final DBOptions opt = new DBOptions()) {
636       for (final WALRecoveryMode walRecoveryMode : WALRecoveryMode.values()) {
637         opt.setWalRecoveryMode(walRecoveryMode);
638         assertThat(opt.walRecoveryMode()).isEqualTo(walRecoveryMode);
639       }
640     }
641   }
642 
643   @Test
allow2pc()644   public void allow2pc() {
645     try (final DBOptions opt = new DBOptions()) {
646       final boolean boolValue = rand.nextBoolean();
647       opt.setAllow2pc(boolValue);
648       assertThat(opt.allow2pc()).isEqualTo(boolValue);
649     }
650   }
651 
652   @Test
rowCache()653   public void rowCache() {
654     try (final DBOptions opt = new DBOptions()) {
655       assertThat(opt.rowCache()).isNull();
656 
657       try(final Cache lruCache = new LRUCache(1000)) {
658         opt.setRowCache(lruCache);
659         assertThat(opt.rowCache()).isEqualTo(lruCache);
660       }
661 
662       try(final Cache clockCache = new ClockCache(1000)) {
663         opt.setRowCache(clockCache);
664         assertThat(opt.rowCache()).isEqualTo(clockCache);
665       }
666     }
667   }
668 
669   @Test
walFilter()670   public void walFilter() {
671     try (final DBOptions opt = new DBOptions()) {
672       assertThat(opt.walFilter()).isNull();
673 
674       try (final AbstractWalFilter walFilter = new AbstractWalFilter() {
675         @Override
676         public void columnFamilyLogNumberMap(
677             final Map<Integer, Long> cfLognumber,
678             final Map<String, Integer> cfNameId) {
679           // no-op
680         }
681 
682         @Override
683         public LogRecordFoundResult logRecordFound(final long logNumber,
684             final String logFileName, final WriteBatch batch,
685             final WriteBatch newBatch) {
686           return new LogRecordFoundResult(
687               WalProcessingOption.CONTINUE_PROCESSING, false);
688         }
689 
690         @Override
691         public String name() {
692           return "test-wal-filter";
693         }
694       }) {
695         opt.setWalFilter(walFilter);
696         assertThat(opt.walFilter()).isEqualTo(walFilter);
697       }
698     }
699   }
700 
701   @Test
failIfOptionsFileError()702   public void failIfOptionsFileError() {
703     try (final DBOptions opt = new DBOptions()) {
704       final boolean boolValue = rand.nextBoolean();
705       opt.setFailIfOptionsFileError(boolValue);
706       assertThat(opt.failIfOptionsFileError()).isEqualTo(boolValue);
707     }
708   }
709 
710   @Test
dumpMallocStats()711   public void dumpMallocStats() {
712     try (final DBOptions opt = new DBOptions()) {
713       final boolean boolValue = rand.nextBoolean();
714       opt.setDumpMallocStats(boolValue);
715       assertThat(opt.dumpMallocStats()).isEqualTo(boolValue);
716     }
717   }
718 
719   @Test
avoidFlushDuringRecovery()720   public void avoidFlushDuringRecovery() {
721     try (final DBOptions opt = new DBOptions()) {
722       final boolean boolValue = rand.nextBoolean();
723       opt.setAvoidFlushDuringRecovery(boolValue);
724       assertThat(opt.avoidFlushDuringRecovery()).isEqualTo(boolValue);
725     }
726   }
727 
728   @Test
avoidFlushDuringShutdown()729   public void avoidFlushDuringShutdown() {
730     try (final DBOptions opt = new DBOptions()) {
731       final boolean boolValue = rand.nextBoolean();
732       opt.setAvoidFlushDuringShutdown(boolValue);
733       assertThat(opt.avoidFlushDuringShutdown()).isEqualTo(boolValue);
734     }
735   }
736 
737   @Test
allowIngestBehind()738   public void allowIngestBehind() {
739     try (final DBOptions opt = new DBOptions()) {
740       assertThat(opt.allowIngestBehind()).isFalse();
741       opt.setAllowIngestBehind(true);
742       assertThat(opt.allowIngestBehind()).isTrue();
743     }
744   }
745 
746   @Test
preserveDeletes()747   public void preserveDeletes() {
748     try (final DBOptions opt = new DBOptions()) {
749       assertThat(opt.preserveDeletes()).isFalse();
750       opt.setPreserveDeletes(true);
751       assertThat(opt.preserveDeletes()).isTrue();
752     }
753   }
754 
755   @Test
twoWriteQueues()756   public void twoWriteQueues() {
757     try (final DBOptions opt = new DBOptions()) {
758       assertThat(opt.twoWriteQueues()).isFalse();
759       opt.setTwoWriteQueues(true);
760       assertThat(opt.twoWriteQueues()).isTrue();
761     }
762   }
763 
764   @Test
manualWalFlush()765   public void manualWalFlush() {
766     try (final DBOptions opt = new DBOptions()) {
767       assertThat(opt.manualWalFlush()).isFalse();
768       opt.setManualWalFlush(true);
769       assertThat(opt.manualWalFlush()).isTrue();
770     }
771   }
772 
773   @Test
atomicFlush()774   public void atomicFlush() {
775     try (final DBOptions opt = new DBOptions()) {
776       assertThat(opt.atomicFlush()).isFalse();
777       opt.setAtomicFlush(true);
778       assertThat(opt.atomicFlush()).isTrue();
779     }
780   }
781 
782   @Test
rateLimiter()783   public void rateLimiter() {
784     try(final DBOptions options = new DBOptions();
785         final DBOptions anotherOptions = new DBOptions();
786         final RateLimiter rateLimiter = new RateLimiter(1000, 100 * 1000, 1)) {
787       options.setRateLimiter(rateLimiter);
788       // Test with parameter initialization
789       anotherOptions.setRateLimiter(
790           new RateLimiter(1000));
791     }
792   }
793 
794   @Test
sstFileManager()795   public void sstFileManager() throws RocksDBException {
796     try (final DBOptions options = new DBOptions();
797          final SstFileManager sstFileManager =
798              new SstFileManager(Env.getDefault())) {
799       options.setSstFileManager(sstFileManager);
800     }
801   }
802 
803   @Test
statistics()804   public void statistics() {
805     try(final DBOptions options = new DBOptions()) {
806       final Statistics statistics = options.statistics();
807       assertThat(statistics).isNull();
808     }
809 
810     try(final Statistics statistics = new Statistics();
811         final DBOptions options = new DBOptions().setStatistics(statistics);
812         final Statistics stats = options.statistics()) {
813       assertThat(stats).isNotNull();
814     }
815   }
816 
817   @Test
avoidUnnecessaryBlockingIO()818   public void avoidUnnecessaryBlockingIO() {
819     try (final DBOptions options = new DBOptions()) {
820       assertThat(options.avoidUnnecessaryBlockingIO()).isEqualTo(false);
821       assertThat(options.setAvoidUnnecessaryBlockingIO(true)).isEqualTo(options);
822       assertThat(options.avoidUnnecessaryBlockingIO()).isEqualTo(true);
823     }
824   }
825 
826   @Test
persistStatsToDisk()827   public void persistStatsToDisk() {
828     try (final DBOptions options = new DBOptions()) {
829       assertThat(options.persistStatsToDisk()).isEqualTo(false);
830       assertThat(options.setPersistStatsToDisk(true)).isEqualTo(options);
831       assertThat(options.persistStatsToDisk()).isEqualTo(true);
832     }
833   }
834 
835   @Test
writeDbidToManifest()836   public void writeDbidToManifest() {
837     try (final DBOptions options = new DBOptions()) {
838       assertThat(options.writeDbidToManifest()).isEqualTo(false);
839       assertThat(options.setWriteDbidToManifest(true)).isEqualTo(options);
840       assertThat(options.writeDbidToManifest()).isEqualTo(true);
841     }
842   }
843 
844   @Test
logReadaheadSize()845   public void logReadaheadSize() {
846     try (final DBOptions options = new DBOptions()) {
847       assertThat(options.logReadaheadSize()).isEqualTo(0);
848       final int size = 1024 * 1024 * 100;
849       assertThat(options.setLogReadaheadSize(size)).isEqualTo(options);
850       assertThat(options.logReadaheadSize()).isEqualTo(size);
851     }
852   }
853 
854   @Test
bestEffortsRecovery()855   public void bestEffortsRecovery() {
856     try (final DBOptions options = new DBOptions()) {
857       assertThat(options.bestEffortsRecovery()).isEqualTo(false);
858       assertThat(options.setBestEffortsRecovery(true)).isEqualTo(options);
859       assertThat(options.bestEffortsRecovery()).isEqualTo(true);
860     }
861   }
862 
863   @Test
maxBgerrorResumeCount()864   public void maxBgerrorResumeCount() {
865     try (final DBOptions options = new DBOptions()) {
866       final int INT_MAX = 2147483647;
867       assertThat(options.maxBgerrorResumeCount()).isEqualTo(INT_MAX);
868       assertThat(options.setMaxBgErrorResumeCount(-1)).isEqualTo(options);
869       assertThat(options.maxBgerrorResumeCount()).isEqualTo(-1);
870     }
871   }
872 
873   @Test
bgerrorResumeRetryInterval()874   public void bgerrorResumeRetryInterval() {
875     try (final DBOptions options = new DBOptions()) {
876       assertThat(options.bgerrorResumeRetryInterval()).isEqualTo(1000000);
877       final long newRetryInterval = 24 * 3600 * 1000000L;
878       assertThat(options.setBgerrorResumeRetryInterval(newRetryInterval)).isEqualTo(options);
879       assertThat(options.bgerrorResumeRetryInterval()).isEqualTo(newRetryInterval);
880     }
881   }
882 
883   @Test
maxWriteBatchGroupSizeBytes()884   public void maxWriteBatchGroupSizeBytes() {
885     try (final DBOptions options = new DBOptions()) {
886       assertThat(options.maxWriteBatchGroupSizeBytes()).isEqualTo(1024 * 1024);
887       final long size = 1024 * 1024 * 1024 * 10L;
888       assertThat(options.setMaxWriteBatchGroupSizeBytes(size)).isEqualTo(options);
889       assertThat(options.maxWriteBatchGroupSizeBytes()).isEqualTo(size);
890     }
891   }
892 
893   @Test
skipCheckingSstFileSizesOnDbOpen()894   public void skipCheckingSstFileSizesOnDbOpen() {
895     try (final DBOptions options = new DBOptions()) {
896       assertThat(options.skipCheckingSstFileSizesOnDbOpen()).isEqualTo(false);
897       assertThat(options.setSkipCheckingSstFileSizesOnDbOpen(true)).isEqualTo(options);
898       assertThat(options.skipCheckingSstFileSizesOnDbOpen()).isEqualTo(true);
899     }
900   }
901 
902   @Test
eventListeners()903   public void eventListeners() {
904     final AtomicBoolean wasCalled1 = new AtomicBoolean();
905     final AtomicBoolean wasCalled2 = new AtomicBoolean();
906     try (final DBOptions options = new DBOptions();
907          final AbstractEventListener el1 =
908              new AbstractEventListener() {
909                @Override
910                public void onTableFileDeleted(final TableFileDeletionInfo tableFileDeletionInfo) {
911                  wasCalled1.set(true);
912                }
913              };
914          final AbstractEventListener el2 =
915              new AbstractEventListener() {
916                @Override
917                public void onMemTableSealed(final MemTableInfo memTableInfo) {
918                  wasCalled2.set(true);
919                }
920              }) {
921       assertThat(options.setListeners(Arrays.asList(el1, el2))).isEqualTo(options);
922       List<AbstractEventListener> listeners = options.listeners();
923       assertEquals(el1, listeners.get(0));
924       assertEquals(el2, listeners.get(1));
925       options.setListeners(Collections.<AbstractEventListener>emptyList());
926       listeners.get(0).onTableFileDeleted(null);
927       assertTrue(wasCalled1.get());
928       listeners.get(1).onMemTableSealed(null);
929       assertTrue(wasCalled2.get());
930       List<AbstractEventListener> listeners2 = options.listeners();
931       assertNotNull(listeners2);
932       assertEquals(0, listeners2.size());
933     }
934   }
935 }
936