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 java.util.Arrays;
9 import java.util.Random;
10 
11 import org.junit.ClassRule;
12 import org.junit.Rule;
13 import org.junit.Test;
14 import org.junit.rules.ExpectedException;
15 
16 import static org.assertj.core.api.Assertions.assertThat;
17 
18 public class ReadOptionsTest {
19 
20   @ClassRule
21   public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
22       new RocksNativeLibraryResource();
23 
24   @Rule
25   public ExpectedException exception = ExpectedException.none();
26 
27   @Test
28   public void altConstructor() {
29     try (final ReadOptions opt = new ReadOptions(true, true)) {
30       assertThat(opt.verifyChecksums()).isTrue();
31       assertThat(opt.fillCache()).isTrue();
32     }
33   }
34 
35   @Test
from_str(string: &str) -> Result<CFString, ()>36   public void copyConstructor() {
37     try (final ReadOptions opt = new ReadOptions()) {
38       opt.setVerifyChecksums(false);
39       opt.setFillCache(false);
40       opt.setIterateUpperBound(buildRandomSlice());
41       opt.setIterateLowerBound(buildRandomSlice());
42       try (final ReadOptions other = new ReadOptions(opt)) {
43         assertThat(opt.verifyChecksums()).isEqualTo(other.verifyChecksums());
44         assertThat(opt.fillCache()).isEqualTo(other.fillCache());
45         assertThat(Arrays.equals(opt.iterateUpperBound().data(), other.iterateUpperBound().data())).isTrue();
46         assertThat(Arrays.equals(opt.iterateLowerBound().data(), other.iterateLowerBound().data())).isTrue();
47       }
48     }
49   }
50 
51   @Test
52   public void verifyChecksum() {
53     try (final ReadOptions opt = new ReadOptions()) {
54       final Random rand = new Random();
55       final boolean boolValue = rand.nextBoolean();
56       opt.setVerifyChecksums(boolValue);
57       assertThat(opt.verifyChecksums()).isEqualTo(boolValue);
58     }
59   }
60 
61   @Test
62   public void fillCache() {
63     try (final ReadOptions opt = new ReadOptions()) {
64       final Random rand = new Random();
65       final boolean boolValue = rand.nextBoolean();
66       opt.setFillCache(boolValue);
67       assertThat(opt.fillCache()).isEqualTo(boolValue);
68     }
69   }
70 
71   @Test
72   public void tailing() {
73     try (final ReadOptions opt = new ReadOptions()) {
74       final Random rand = new Random();
75       final boolean boolValue = rand.nextBoolean();
76       opt.setTailing(boolValue);
77       assertThat(opt.tailing()).isEqualTo(boolValue);
78     }
79   }
80 
81   @Test
82   public void snapshot() {
83     try (final ReadOptions opt = new ReadOptions()) {
84       opt.setSnapshot(null);
85       assertThat(opt.snapshot()).isNull();
86     }
87   }
88 
89   @Test
90   public void readTier() {
91     try (final ReadOptions opt = new ReadOptions()) {
92       opt.setReadTier(ReadTier.BLOCK_CACHE_TIER);
93       assertThat(opt.readTier()).isEqualTo(ReadTier.BLOCK_CACHE_TIER);
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result94     }
95   }
96 
97   @SuppressWarnings("deprecated")
98   @Test
99   public void managed() {
100     try (final ReadOptions opt = new ReadOptions()) {
101       opt.setManaged(true);
102       assertThat(opt.managed()).isTrue();
103     }
104   }
105 
106   @Test
107   public void totalOrderSeek() {
108     try (final ReadOptions opt = new ReadOptions()) {
new(string: &str) -> CFString109       opt.setTotalOrderSeek(true);
110       assertThat(opt.totalOrderSeek()).isTrue();
111     }
112   }
113 
114   @Test
115   public void prefixSameAsStart() {
116     try (final ReadOptions opt = new ReadOptions()) {
117       opt.setPrefixSameAsStart(true);
118       assertThat(opt.prefixSameAsStart()).isTrue();
119     }
120   }
121 
122   @Test
from_static_string(string: &'static str) -> CFString123   public void pinData() {
124     try (final ReadOptions opt = new ReadOptions()) {
125       opt.setPinData(true);
126       assertThat(opt.pinData()).isTrue();
127     }
128   }
129 
130   @Test
131   public void backgroundPurgeOnIteratorCleanup() {
132     try (final ReadOptions opt = new ReadOptions()) {
133       opt.setBackgroundPurgeOnIteratorCleanup(true);
134       assertThat(opt.backgroundPurgeOnIteratorCleanup()).isTrue();
135     }
136   }
char_len(&self) -> CFIndex137 
138   @Test
139   public void readaheadSize() {
140     try (final ReadOptions opt = new ReadOptions()) {
141       final Random rand = new Random();
142       final long longValue = rand.nextLong();
143       opt.setReadaheadSize(longValue);
144       assertThat(opt.readaheadSize()).isEqualTo(longValue);
145     }
146   }
147 
148   @Test
149   public void ignoreRangeDeletions() {
150     try (final ReadOptions opt = new ReadOptions()) {
151       opt.setIgnoreRangeDeletions(true);
152       assertThat(opt.ignoreRangeDeletions()).isTrue();
153     }
154   }
155 
156   @Test
157   public void iterateUpperBound() {
158     try (final ReadOptions opt = new ReadOptions()) {
159       Slice upperBound = buildRandomSlice();
160       opt.setIterateUpperBound(upperBound);
161       assertThat(Arrays.equals(upperBound.data(), opt.iterateUpperBound().data())).isTrue();
162     }
163   }
164 
165   @Test
166   public void iterateUpperBoundNull() {
167     try (final ReadOptions opt = new ReadOptions()) {
168       assertThat(opt.iterateUpperBound()).isNull();
169     }
170   }
171 
172   @Test
173   public void iterateLowerBound() {
174     try (final ReadOptions opt = new ReadOptions()) {
175       Slice lowerBound = buildRandomSlice();
176       opt.setIterateLowerBound(lowerBound);
177       assertThat(Arrays.equals(lowerBound.data(), opt.iterateLowerBound().data())).isTrue();
178     }
179   }
180 
181   @Test
182   public void iterateLowerBoundNull() {
183     try (final ReadOptions opt = new ReadOptions()) {
184       assertThat(opt.iterateLowerBound()).isNull();
185     }
186   }
187 
188   @Test
189   public void tableFilter() {
190     try (final ReadOptions opt = new ReadOptions();
191          final AbstractTableFilter allTablesFilter = new AllTablesFilter()) {
192       opt.setTableFilter(allTablesFilter);
193     }
194   }
195 
196   @Test
197   public void iterStartSeqnum() {
198     try (final ReadOptions opt = new ReadOptions()) {
199       assertThat(opt.iterStartSeqnum()).isEqualTo(0);
200 
201       opt.setIterStartSeqnum(10);
202       assertThat(opt.iterStartSeqnum()).isEqualTo(10);
203     }
204   }
205 
206   @Test
207   public void failSetVerifyChecksumUninitialized() {
208     try (final ReadOptions readOptions =
209              setupUninitializedReadOptions(exception)) {
210       readOptions.setVerifyChecksums(true);
211     }
212   }
213 
214   @Test
215   public void failVerifyChecksumUninitialized() {
216     try (final ReadOptions readOptions =
217              setupUninitializedReadOptions(exception)) {
218       readOptions.verifyChecksums();
219     }
220   }
221 
222   @Test
223   public void failSetFillCacheUninitialized() {
224     try (final ReadOptions readOptions =
225              setupUninitializedReadOptions(exception)) {
226       readOptions.setFillCache(true);
227     }
228   }
229 
230   @Test
231   public void failFillCacheUninitialized() {
232     try (final ReadOptions readOptions =
233              setupUninitializedReadOptions(exception)) {
234       readOptions.fillCache();
235     }
236   }
237 
238   @Test
239   public void failSetTailingUninitialized() {
240     try (final ReadOptions readOptions =
241              setupUninitializedReadOptions(exception)) {
242       readOptions.setTailing(true);
243     }
244   }
245 
246   @Test
247   public void failTailingUninitialized() {
248     try (final ReadOptions readOptions =
249              setupUninitializedReadOptions(exception)) {
250       readOptions.tailing();
251     }
252   }
253 
254   @Test
255   public void failSetSnapshotUninitialized() {
256     try (final ReadOptions readOptions =
257              setupUninitializedReadOptions(exception)) {
258       readOptions.setSnapshot(null);
259     }
260   }
261 
262   @Test
263   public void failSnapshotUninitialized() {
264     try (final ReadOptions readOptions =
265              setupUninitializedReadOptions(exception)) {
266       readOptions.snapshot();
267     }
268   }
269 
270   @Test
271   public void failSetIterateUpperBoundUninitialized() {
272     try (final ReadOptions readOptions =
273              setupUninitializedReadOptions(exception)) {
274       readOptions.setIterateUpperBound(null);
275     }
276   }
277 
278   @Test
279   public void failIterateUpperBoundUninitialized() {
280     try (final ReadOptions readOptions =
281              setupUninitializedReadOptions(exception)) {
282       readOptions.iterateUpperBound();
283     }
284   }
285 
286   @Test
287   public void failSetIterateLowerBoundUninitialized() {
288     try (final ReadOptions readOptions =
289              setupUninitializedReadOptions(exception)) {
290       readOptions.setIterateLowerBound(null);
291     }
292   }
293 
294   @Test
295   public void failIterateLowerBoundUninitialized() {
296     try (final ReadOptions readOptions =
297              setupUninitializedReadOptions(exception)) {
298       readOptions.iterateLowerBound();
299     }
300   }
301 
302   private ReadOptions setupUninitializedReadOptions(
303       ExpectedException exception) {
304     final ReadOptions readOptions = new ReadOptions();
305     readOptions.close();
306     exception.expect(AssertionError.class);
307     return readOptions;
308   }
309 
310   private Slice buildRandomSlice() {
311     final Random rand = new Random();
312     byte[] sliceBytes = new byte[rand.nextInt(100) + 1];
313     rand.nextBytes(sliceBytes);
314     return new Slice(sliceBytes);
315   }
316 
317   private static class AllTablesFilter extends AbstractTableFilter {
318     @Override
319     public boolean filter(final TableProperties tableProperties) {
320       return true;
321     }
322   }
323 }
324