]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/MergeTest.java
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / MergeTest.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
6 package org.rocksdb;
7
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.ArrayList;
11
12 import org.junit.ClassRule;
13 import org.junit.Rule;
14 import org.junit.Test;
15 import org.junit.rules.TemporaryFolder;
16
17 import static org.assertj.core.api.Assertions.assertThat;
18
19 public class MergeTest {
20
21 @ClassRule
22 public static final RocksMemoryResource rocksMemoryResource =
23 new RocksMemoryResource();
24
25 @Rule
26 public TemporaryFolder dbFolder = new TemporaryFolder();
27
28 @Test
29 public void stringOption()
30 throws InterruptedException, RocksDBException {
31 try (final Options opt = new Options()
32 .setCreateIfMissing(true)
33 .setMergeOperatorName("stringappend");
34 final RocksDB db = RocksDB.open(opt,
35 dbFolder.getRoot().getAbsolutePath())) {
36 // writing aa under key
37 db.put("key".getBytes(), "aa".getBytes());
38 // merge bb under key
39 db.merge("key".getBytes(), "bb".getBytes());
40
41 final byte[] value = db.get("key".getBytes());
42 final String strValue = new String(value);
43 assertThat(strValue).isEqualTo("aa,bb");
44 }
45 }
46
47 @Test
48 public void cFStringOption()
49 throws InterruptedException, RocksDBException {
50
51 try (final ColumnFamilyOptions cfOpt1 = new ColumnFamilyOptions()
52 .setMergeOperatorName("stringappend");
53 final ColumnFamilyOptions cfOpt2 = new ColumnFamilyOptions()
54 .setMergeOperatorName("stringappend")
55 ) {
56 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
57 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt1),
58 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt2)
59 );
60
61 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
62 try (final DBOptions opt = new DBOptions()
63 .setCreateIfMissing(true)
64 .setCreateMissingColumnFamilies(true);
65 final RocksDB db = RocksDB.open(opt,
66 dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
67 columnFamilyHandleList)) {
68 try {
69 // writing aa under key
70 db.put(columnFamilyHandleList.get(1),
71 "cfkey".getBytes(), "aa".getBytes());
72 // merge bb under key
73 db.merge(columnFamilyHandleList.get(1),
74 "cfkey".getBytes(), "bb".getBytes());
75
76 byte[] value = db.get(columnFamilyHandleList.get(1),
77 "cfkey".getBytes());
78 String strValue = new String(value);
79 assertThat(strValue).isEqualTo("aa,bb");
80 } finally {
81 for (final ColumnFamilyHandle handle : columnFamilyHandleList) {
82 handle.close();
83 }
84 }
85 }
86 }
87 }
88
89 @Test
90 public void operatorOption()
91 throws InterruptedException, RocksDBException {
92 try (final StringAppendOperator stringAppendOperator = new StringAppendOperator();
93 final Options opt = new Options()
94 .setCreateIfMissing(true)
95 .setMergeOperator(stringAppendOperator);
96 final RocksDB db = RocksDB.open(opt,
97 dbFolder.getRoot().getAbsolutePath())) {
98 // Writing aa under key
99 db.put("key".getBytes(), "aa".getBytes());
100
101 // Writing bb under key
102 db.merge("key".getBytes(), "bb".getBytes());
103
104 final byte[] value = db.get("key".getBytes());
105 final String strValue = new String(value);
106
107 assertThat(strValue).isEqualTo("aa,bb");
108 }
109 }
110
111 @Test
112 public void cFOperatorOption()
113 throws InterruptedException, RocksDBException {
114 try (final StringAppendOperator stringAppendOperator = new StringAppendOperator();
115 final ColumnFamilyOptions cfOpt1 = new ColumnFamilyOptions()
116 .setMergeOperator(stringAppendOperator);
117 final ColumnFamilyOptions cfOpt2 = new ColumnFamilyOptions()
118 .setMergeOperator(stringAppendOperator)
119 ) {
120 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
121 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpt1),
122 new ColumnFamilyDescriptor("new_cf".getBytes(), cfOpt2)
123 );
124 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
125 try (final DBOptions opt = new DBOptions()
126 .setCreateIfMissing(true)
127 .setCreateMissingColumnFamilies(true);
128 final RocksDB db = RocksDB.open(opt,
129 dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
130 columnFamilyHandleList)
131 ) {
132 try {
133 // writing aa under key
134 db.put(columnFamilyHandleList.get(1),
135 "cfkey".getBytes(), "aa".getBytes());
136 // merge bb under key
137 db.merge(columnFamilyHandleList.get(1),
138 "cfkey".getBytes(), "bb".getBytes());
139 byte[] value = db.get(columnFamilyHandleList.get(1),
140 "cfkey".getBytes());
141 String strValue = new String(value);
142
143 // Test also with createColumnFamily
144 try (final ColumnFamilyOptions cfHandleOpts =
145 new ColumnFamilyOptions()
146 .setMergeOperator(stringAppendOperator);
147 final ColumnFamilyHandle cfHandle =
148 db.createColumnFamily(
149 new ColumnFamilyDescriptor("new_cf2".getBytes(),
150 cfHandleOpts))
151 ) {
152 // writing xx under cfkey2
153 db.put(cfHandle, "cfkey2".getBytes(), "xx".getBytes());
154 // merge yy under cfkey2
155 db.merge(cfHandle, new WriteOptions(), "cfkey2".getBytes(),
156 "yy".getBytes());
157 value = db.get(cfHandle, "cfkey2".getBytes());
158 String strValueTmpCf = new String(value);
159
160 assertThat(strValue).isEqualTo("aa,bb");
161 assertThat(strValueTmpCf).isEqualTo("xx,yy");
162 }
163 } finally {
164 for (final ColumnFamilyHandle columnFamilyHandle :
165 columnFamilyHandleList) {
166 columnFamilyHandle.close();
167 }
168 }
169 }
170 }
171 }
172
173 @Test
174 public void operatorGcBehaviour()
175 throws RocksDBException {
176 try (final StringAppendOperator stringAppendOperator = new StringAppendOperator()) {
177 try (final Options opt = new Options()
178 .setCreateIfMissing(true)
179 .setMergeOperator(stringAppendOperator);
180 final RocksDB db = RocksDB.open(opt,
181 dbFolder.getRoot().getAbsolutePath())) {
182 //no-op
183 }
184
185
186 // test reuse
187 try (final Options opt = new Options()
188 .setMergeOperator(stringAppendOperator);
189 final RocksDB db = RocksDB.open(opt,
190 dbFolder.getRoot().getAbsolutePath())) {
191 //no-op
192 }
193
194 // test param init
195 try (final StringAppendOperator stringAppendOperator2 = new StringAppendOperator();
196 final Options opt = new Options()
197 .setMergeOperator(stringAppendOperator2);
198 final RocksDB db = RocksDB.open(opt,
199 dbFolder.getRoot().getAbsolutePath())) {
200 //no-op
201 }
202
203 // test replace one with another merge operator instance
204 try (final Options opt = new Options()
205 .setMergeOperator(stringAppendOperator);
206 final StringAppendOperator newStringAppendOperator = new StringAppendOperator()) {
207 opt.setMergeOperator(newStringAppendOperator);
208 try (final RocksDB db = RocksDB.open(opt,
209 dbFolder.getRoot().getAbsolutePath())) {
210 //no-op
211 }
212 }
213 }
214 }
215
216 @Test
217 public void emptyStringInSetMergeOperatorByName() {
218 try (final Options opt = new Options()
219 .setMergeOperatorName("");
220 final ColumnFamilyOptions cOpt = new ColumnFamilyOptions()
221 .setMergeOperatorName("")) {
222 //no-op
223 }
224 }
225
226 @Test(expected = IllegalArgumentException.class)
227 public void nullStringInSetMergeOperatorByNameOptions() {
228 try (final Options opt = new Options()) {
229 opt.setMergeOperatorName(null);
230 }
231 }
232
233 @Test(expected = IllegalArgumentException.class)
234 public void
235 nullStringInSetMergeOperatorByNameColumnFamilyOptions() {
236 try (final ColumnFamilyOptions opt = new ColumnFamilyOptions()) {
237 opt.setMergeOperatorName(null);
238 }
239 }
240 }