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).
7 import org
.junit
.ClassRule
;
10 import org
.junit
.rules
.TemporaryFolder
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
16 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
18 public class KeyMayExistTest
{
21 public static final RocksMemoryResource rocksMemoryResource
=
22 new RocksMemoryResource();
25 public TemporaryFolder dbFolder
= new TemporaryFolder();
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())
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
)) {
42 assertThat(columnFamilyHandleList
.size()).
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");
52 StringBuilder builder
= new StringBuilder("prefix");
53 int offset
= builder
.toString().length();
54 builder
.append("slice key 0");
55 int len
= builder
.toString().length() - offset
;
56 builder
.append("suffix");
58 byte[] sliceKey
= builder
.toString().getBytes();
59 byte[] sliceValue
= "slice value 0".getBytes();
60 db
.put(sliceKey
, offset
, len
, sliceValue
, 0, sliceValue
.length
);
62 retValue
= new StringBuilder();
63 exists
= db
.keyMayExist(sliceKey
, offset
, len
, retValue
);
64 assertThat(exists
).isTrue();
65 assertThat(retValue
.toString().getBytes()).isEqualTo(sliceValue
);
67 // Test without column family but with readOptions
68 try (final ReadOptions readOptions
= new ReadOptions()) {
69 retValue
= new StringBuilder();
70 exists
= db
.keyMayExist(readOptions
, "key".getBytes(), retValue
);
71 assertThat(exists
).isTrue();
72 assertThat(retValue
.toString()).isEqualTo("value");
74 retValue
= new StringBuilder();
75 exists
= db
.keyMayExist(readOptions
, sliceKey
, offset
, len
, retValue
);
76 assertThat(exists
).isTrue();
77 assertThat(retValue
.toString().getBytes()).isEqualTo(sliceValue
);
80 // Test with column family
81 retValue
= new StringBuilder();
82 exists
= db
.keyMayExist(columnFamilyHandleList
.get(0), "key".getBytes(),
84 assertThat(exists
).isTrue();
85 assertThat(retValue
.toString()).isEqualTo("value");
87 // Test slice sky with column family
88 retValue
= new StringBuilder();
89 exists
= db
.keyMayExist(columnFamilyHandleList
.get(0), sliceKey
, offset
, len
,
91 assertThat(exists
).isTrue();
92 assertThat(retValue
.toString().getBytes()).isEqualTo(sliceValue
);
94 // Test with column family and readOptions
95 try (final ReadOptions readOptions
= new ReadOptions()) {
96 retValue
= new StringBuilder();
97 exists
= db
.keyMayExist(readOptions
,
98 columnFamilyHandleList
.get(0), "key".getBytes(),
100 assertThat(exists
).isTrue();
101 assertThat(retValue
.toString()).isEqualTo("value");
103 // Test slice key with column family and read options
104 retValue
= new StringBuilder();
105 exists
= db
.keyMayExist(readOptions
,
106 columnFamilyHandleList
.get(0), sliceKey
, offset
, len
,
108 assertThat(exists
).isTrue();
109 assertThat(retValue
.toString().getBytes()).isEqualTo(sliceValue
);
112 // KeyMayExist in CF1 must return false
113 assertThat(db
.keyMayExist(columnFamilyHandleList
.get(1),
114 "key".getBytes(), retValue
)).isFalse();
117 assertThat(db
.keyMayExist(columnFamilyHandleList
.get(1),
118 sliceKey
, 1, 3, retValue
)).isFalse();
120 for (final ColumnFamilyHandle columnFamilyHandle
:
121 columnFamilyHandleList
) {
122 columnFamilyHandle
.close();