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.
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");
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");
59 // Test with column family
60 retValue
= new StringBuilder();
61 exists
= db
.keyMayExist(columnFamilyHandleList
.get(0), "key".getBytes(),
63 assertThat(exists
).isTrue();
64 assertThat(retValue
.toString()).isEqualTo("value");
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(),
72 assertThat(exists
).isTrue();
73 assertThat(retValue
.toString()).isEqualTo("value");
76 // KeyMayExist in CF1 must return false
77 assertThat(db
.keyMayExist(columnFamilyHandleList
.get(1),
78 "key".getBytes(), retValue
)).isFalse();
80 for (final ColumnFamilyHandle columnFamilyHandle
:
81 columnFamilyHandleList
) {
82 columnFamilyHandle
.close();