]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/KeyMayExistTest.java
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / KeyMayExistTest.java
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 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 // Slice key
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");
57
58 byte[] sliceKey = builder.toString().getBytes();
59 byte[] sliceValue = "slice value 0".getBytes();
60 db.put(sliceKey, offset, len, sliceValue, 0, sliceValue.length);
61
62 retValue = new StringBuilder();
63 exists = db.keyMayExist(sliceKey, offset, len, retValue);
64 assertThat(exists).isTrue();
65 assertThat(retValue.toString().getBytes()).isEqualTo(sliceValue);
66
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");
73
74 retValue = new StringBuilder();
75 exists = db.keyMayExist(readOptions, sliceKey, offset, len, retValue);
76 assertThat(exists).isTrue();
77 assertThat(retValue.toString().getBytes()).isEqualTo(sliceValue);
78 }
79
80 // Test with column family
81 retValue = new StringBuilder();
82 exists = db.keyMayExist(columnFamilyHandleList.get(0), "key".getBytes(),
83 retValue);
84 assertThat(exists).isTrue();
85 assertThat(retValue.toString()).isEqualTo("value");
86
87 // Test slice sky with column family
88 retValue = new StringBuilder();
89 exists = db.keyMayExist(columnFamilyHandleList.get(0), sliceKey, offset, len,
90 retValue);
91 assertThat(exists).isTrue();
92 assertThat(retValue.toString().getBytes()).isEqualTo(sliceValue);
93
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(),
99 retValue);
100 assertThat(exists).isTrue();
101 assertThat(retValue.toString()).isEqualTo("value");
102
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,
107 retValue);
108 assertThat(exists).isTrue();
109 assertThat(retValue.toString().getBytes()).isEqualTo(sliceValue);
110 }
111
112 // KeyMayExist in CF1 must return false
113 assertThat(db.keyMayExist(columnFamilyHandleList.get(1),
114 "key".getBytes(), retValue)).isFalse();
115
116 // slice key
117 assertThat(db.keyMayExist(columnFamilyHandleList.get(1),
118 sliceKey, 1, 3, retValue)).isFalse();
119 } finally {
120 for (final ColumnFamilyHandle columnFamilyHandle :
121 columnFamilyHandleList) {
122 columnFamilyHandle.close();
123 }
124 }
125 }
126 }
127 }