]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | // This source code is licensed under the BSD-style license found in the | |
3 | // LICENSE file in the root directory of this source tree. An additional grant | |
4 | // of patent rights can be found in the PATENTS file in the same directory. | |
5 | package org.rocksdb; | |
6 | ||
7 | import org.junit.ClassRule; | |
8 | import org.junit.Rule; | |
9 | import org.junit.Test; | |
10 | import org.junit.rules.TemporaryFolder; | |
11 | ||
12 | import java.util.ArrayList; | |
13 | import java.util.Arrays; | |
14 | import java.util.List; | |
15 | ||
16 | import static org.assertj.core.api.Assertions.assertThat; | |
17 | ||
18 | public class KeyMayExistTest { | |
19 | ||
20 | @ClassRule | |
21 | public static final RocksMemoryResource rocksMemoryResource = | |
22 | new RocksMemoryResource(); | |
23 | ||
24 | @Rule | |
25 | public TemporaryFolder dbFolder = new TemporaryFolder(); | |
26 | ||
27 | @Test | |
28 | public void keyMayExist() throws RocksDBException { | |
29 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
30 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
31 | new ColumnFamilyDescriptor("new_cf".getBytes()) | |
32 | ); | |
33 | ||
34 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
35 | try (final DBOptions options = new DBOptions() | |
36 | .setCreateIfMissing(true) | |
37 | .setCreateMissingColumnFamilies(true); | |
38 | final RocksDB db = RocksDB.open(options, | |
39 | dbFolder.getRoot().getAbsolutePath(), | |
40 | cfDescriptors, columnFamilyHandleList)) { | |
41 | try { | |
42 | assertThat(columnFamilyHandleList.size()). | |
43 | isEqualTo(2); | |
44 | db.put("key".getBytes(), "value".getBytes()); | |
45 | // Test without column family | |
46 | StringBuilder retValue = new StringBuilder(); | |
47 | boolean exists = db.keyMayExist("key".getBytes(), retValue); | |
48 | assertThat(exists).isTrue(); | |
49 | assertThat(retValue.toString()).isEqualTo("value"); | |
50 | ||
51 | // Test without column family but with readOptions | |
52 | try (final ReadOptions readOptions = new ReadOptions()) { | |
53 | retValue = new StringBuilder(); | |
54 | exists = db.keyMayExist(readOptions, "key".getBytes(), retValue); | |
55 | assertThat(exists).isTrue(); | |
56 | assertThat(retValue.toString()).isEqualTo("value"); | |
57 | } | |
58 | ||
59 | // Test with column family | |
60 | retValue = new StringBuilder(); | |
61 | exists = db.keyMayExist(columnFamilyHandleList.get(0), "key".getBytes(), | |
62 | retValue); | |
63 | assertThat(exists).isTrue(); | |
64 | assertThat(retValue.toString()).isEqualTo("value"); | |
65 | ||
66 | // Test with column family and readOptions | |
67 | try (final ReadOptions readOptions = new ReadOptions()) { | |
68 | retValue = new StringBuilder(); | |
69 | exists = db.keyMayExist(readOptions, | |
70 | columnFamilyHandleList.get(0), "key".getBytes(), | |
71 | retValue); | |
72 | assertThat(exists).isTrue(); | |
73 | assertThat(retValue.toString()).isEqualTo("value"); | |
74 | } | |
75 | ||
76 | // KeyMayExist in CF1 must return false | |
77 | assertThat(db.keyMayExist(columnFamilyHandleList.get(1), | |
78 | "key".getBytes(), retValue)).isFalse(); | |
79 | } finally { | |
80 | for (final ColumnFamilyHandle columnFamilyHandle : | |
81 | columnFamilyHandleList) { | |
82 | columnFamilyHandle.close(); | |
83 | } | |
84 | } | |
85 | } | |
86 | } | |
87 | } |