]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/ColumnFamilyTest.java
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / ColumnFamilyTest.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.*;
9
10 import org.junit.ClassRule;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.rules.TemporaryFolder;
14
15 import static java.nio.charset.StandardCharsets.UTF_8;
16 import static org.assertj.core.api.Assertions.assertThat;
17
18 public class ColumnFamilyTest {
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 columnFamilyDescriptorName() throws RocksDBException {
29 final byte[] cfName = "some_name".getBytes(UTF_8);
30
31 try(final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions()) {
32 final ColumnFamilyDescriptor cfDescriptor =
33 new ColumnFamilyDescriptor(cfName, cfOptions);
34 assertThat(cfDescriptor.getName()).isEqualTo(cfName);
35 }
36 }
37
38 @Test
39 public void columnFamilyDescriptorOptions() throws RocksDBException {
40 final byte[] cfName = "some_name".getBytes(UTF_8);
41
42 try(final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions()
43 .setCompressionType(CompressionType.BZLIB2_COMPRESSION)) {
44 final ColumnFamilyDescriptor cfDescriptor =
45 new ColumnFamilyDescriptor(cfName, cfOptions);
46
47 assertThat(cfDescriptor.getOptions().compressionType())
48 .isEqualTo(CompressionType.BZLIB2_COMPRESSION);
49 }
50 }
51
52 @Test
53 public void listColumnFamilies() throws RocksDBException {
54 try (final Options options = new Options().setCreateIfMissing(true);
55 final RocksDB db = RocksDB.open(options,
56 dbFolder.getRoot().getAbsolutePath())) {
57 // Test listColumnFamilies
58 final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies(options,
59 dbFolder.getRoot().getAbsolutePath());
60 assertThat(columnFamilyNames).isNotNull();
61 assertThat(columnFamilyNames.size()).isGreaterThan(0);
62 assertThat(columnFamilyNames.size()).isEqualTo(1);
63 assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default");
64 }
65 }
66
67 @Test
68 public void defaultColumnFamily() throws RocksDBException {
69 try (final Options options = new Options().setCreateIfMissing(true);
70 final RocksDB db = RocksDB.open(options,
71 dbFolder.getRoot().getAbsolutePath())) {
72 final ColumnFamilyHandle cfh = db.getDefaultColumnFamily();
73 try {
74 assertThat(cfh).isNotNull();
75
76 assertThat(cfh.getName()).isEqualTo("default".getBytes(UTF_8));
77 assertThat(cfh.getID()).isEqualTo(0);
78
79 final byte[] key = "key".getBytes();
80 final byte[] value = "value".getBytes();
81
82 db.put(cfh, key, value);
83
84 final byte[] actualValue = db.get(cfh, key);
85
86 assertThat(cfh).isNotNull();
87 assertThat(actualValue).isEqualTo(value);
88 } finally {
89 cfh.close();
90 }
91 }
92 }
93
94 @Test
95 public void createColumnFamily() throws RocksDBException {
96 final byte[] cfName = "new_cf".getBytes(UTF_8);
97 final ColumnFamilyDescriptor cfDescriptor = new ColumnFamilyDescriptor(cfName,
98 new ColumnFamilyOptions());
99
100 try (final Options options = new Options().setCreateIfMissing(true);
101 final RocksDB db = RocksDB.open(options,
102 dbFolder.getRoot().getAbsolutePath())) {
103
104 final ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily(cfDescriptor);
105
106 try {
107 assertThat(columnFamilyHandle.getName()).isEqualTo(cfName);
108 assertThat(columnFamilyHandle.getID()).isEqualTo(1);
109
110 final ColumnFamilyDescriptor latestDescriptor = columnFamilyHandle.getDescriptor();
111 assertThat(latestDescriptor.getName()).isEqualTo(cfName);
112
113 final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies(
114 options, dbFolder.getRoot().getAbsolutePath());
115 assertThat(columnFamilyNames).isNotNull();
116 assertThat(columnFamilyNames.size()).isGreaterThan(0);
117 assertThat(columnFamilyNames.size()).isEqualTo(2);
118 assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default");
119 assertThat(new String(columnFamilyNames.get(1))).isEqualTo("new_cf");
120 } finally {
121 columnFamilyHandle.close();
122 }
123 }
124 }
125
126 @Test
127 public void openWithColumnFamilies() throws RocksDBException {
128 final List<ColumnFamilyDescriptor> cfNames = Arrays.asList(
129 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
130 new ColumnFamilyDescriptor("new_cf".getBytes())
131 );
132
133 final List<ColumnFamilyHandle> columnFamilyHandleList =
134 new ArrayList<>();
135
136 // Test open database with column family names
137 try (final DBOptions options = new DBOptions()
138 .setCreateIfMissing(true)
139 .setCreateMissingColumnFamilies(true);
140 final RocksDB db = RocksDB.open(options,
141 dbFolder.getRoot().getAbsolutePath(), cfNames,
142 columnFamilyHandleList)) {
143
144 try {
145 assertThat(columnFamilyHandleList.size()).isEqualTo(2);
146 db.put("dfkey1".getBytes(), "dfvalue".getBytes());
147 db.put(columnFamilyHandleList.get(0), "dfkey2".getBytes(),
148 "dfvalue".getBytes());
149 db.put(columnFamilyHandleList.get(1), "newcfkey1".getBytes(),
150 "newcfvalue".getBytes());
151
152 String retVal = new String(db.get(columnFamilyHandleList.get(1),
153 "newcfkey1".getBytes()));
154 assertThat(retVal).isEqualTo("newcfvalue");
155 assertThat((db.get(columnFamilyHandleList.get(1),
156 "dfkey1".getBytes()))).isNull();
157 db.remove(columnFamilyHandleList.get(1), "newcfkey1".getBytes());
158 assertThat((db.get(columnFamilyHandleList.get(1),
159 "newcfkey1".getBytes()))).isNull();
160 db.remove(columnFamilyHandleList.get(0), new WriteOptions(),
161 "dfkey2".getBytes());
162 assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(),
163 "dfkey2".getBytes())).isNull();
164 } finally {
165 for (final ColumnFamilyHandle columnFamilyHandle :
166 columnFamilyHandleList) {
167 columnFamilyHandle.close();
168 }
169 }
170 }
171 }
172
173 @Test
174 public void getWithOutValueAndCf() throws RocksDBException {
175 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
176 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
177 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
178
179 // Test open database with column family names
180 try (final DBOptions options = new DBOptions()
181 .setCreateIfMissing(true)
182 .setCreateMissingColumnFamilies(true);
183 final RocksDB db = RocksDB.open(options,
184 dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
185 columnFamilyHandleList)) {
186 try {
187 db.put(columnFamilyHandleList.get(0), new WriteOptions(),
188 "key1".getBytes(), "value".getBytes());
189 db.put("key2".getBytes(), "12345678".getBytes());
190 final byte[] outValue = new byte[5];
191 // not found value
192 int getResult = db.get("keyNotFound".getBytes(), outValue);
193 assertThat(getResult).isEqualTo(RocksDB.NOT_FOUND);
194 // found value which fits in outValue
195 getResult = db.get(columnFamilyHandleList.get(0), "key1".getBytes(),
196 outValue);
197 assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND);
198 assertThat(outValue).isEqualTo("value".getBytes());
199 // found value which fits partially
200 getResult = db.get(columnFamilyHandleList.get(0), new ReadOptions(),
201 "key2".getBytes(), outValue);
202 assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND);
203 assertThat(outValue).isEqualTo("12345".getBytes());
204 } finally {
205 for (final ColumnFamilyHandle columnFamilyHandle :
206 columnFamilyHandleList) {
207 columnFamilyHandle.close();
208 }
209 }
210 }
211 }
212
213 @Test
214 public void createWriteDropColumnFamily() throws RocksDBException {
215 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
216 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
217 new ColumnFamilyDescriptor("new_cf".getBytes()));
218 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
219 try (final DBOptions options = new DBOptions()
220 .setCreateIfMissing(true)
221 .setCreateMissingColumnFamilies(true);
222 final RocksDB db = RocksDB.open(options,
223 dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
224 columnFamilyHandleList)) {
225 ColumnFamilyHandle tmpColumnFamilyHandle = null;
226 try {
227 tmpColumnFamilyHandle = db.createColumnFamily(
228 new ColumnFamilyDescriptor("tmpCF".getBytes(),
229 new ColumnFamilyOptions()));
230 db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes());
231 db.dropColumnFamily(tmpColumnFamilyHandle);
232 } finally {
233 if (tmpColumnFamilyHandle != null) {
234 tmpColumnFamilyHandle.close();
235 }
236 for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
237 columnFamilyHandle.close();
238 }
239 }
240 }
241 }
242
243 @Test
244 public void writeBatch() throws RocksDBException {
245 try (final StringAppendOperator stringAppendOperator = new StringAppendOperator();
246 final ColumnFamilyOptions defaultCfOptions = new ColumnFamilyOptions()
247 .setMergeOperator(stringAppendOperator)) {
248 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
249 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY,
250 defaultCfOptions),
251 new ColumnFamilyDescriptor("new_cf".getBytes()));
252 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
253 try (final DBOptions options = new DBOptions()
254 .setCreateIfMissing(true)
255 .setCreateMissingColumnFamilies(true);
256 final RocksDB db = RocksDB.open(options,
257 dbFolder.getRoot().getAbsolutePath(),
258 cfDescriptors, columnFamilyHandleList);
259 final WriteBatch writeBatch = new WriteBatch();
260 final WriteOptions writeOpt = new WriteOptions()) {
261 try {
262 writeBatch.put("key".getBytes(), "value".getBytes());
263 writeBatch.put(db.getDefaultColumnFamily(),
264 "mergeKey".getBytes(), "merge".getBytes());
265 writeBatch.merge(db.getDefaultColumnFamily(), "mergeKey".getBytes(),
266 "merge".getBytes());
267 writeBatch.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(),
268 "value".getBytes());
269 writeBatch.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(),
270 "value2".getBytes());
271 writeBatch.remove("xyz".getBytes());
272 writeBatch.remove(columnFamilyHandleList.get(1), "xyz".getBytes());
273 db.write(writeOpt, writeBatch);
274
275 assertThat(db.get(columnFamilyHandleList.get(1),
276 "xyz".getBytes()) == null);
277 assertThat(new String(db.get(columnFamilyHandleList.get(1),
278 "newcfkey".getBytes()))).isEqualTo("value");
279 assertThat(new String(db.get(columnFamilyHandleList.get(1),
280 "newcfkey2".getBytes()))).isEqualTo("value2");
281 assertThat(new String(db.get("key".getBytes()))).isEqualTo("value");
282 // check if key is merged
283 assertThat(new String(db.get(db.getDefaultColumnFamily(),
284 "mergeKey".getBytes()))).isEqualTo("merge,merge");
285 } finally {
286 for (final ColumnFamilyHandle columnFamilyHandle :
287 columnFamilyHandleList) {
288 columnFamilyHandle.close();
289 }
290 }
291 }
292 }
293 }
294
295 @Test
296 public void iteratorOnColumnFamily() throws RocksDBException {
297 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
298 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
299 new ColumnFamilyDescriptor("new_cf".getBytes()));
300 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
301 try (final DBOptions options = new DBOptions()
302 .setCreateIfMissing(true)
303 .setCreateMissingColumnFamilies(true);
304 final RocksDB db = RocksDB.open(options,
305 dbFolder.getRoot().getAbsolutePath(),
306 cfDescriptors, columnFamilyHandleList)) {
307 try {
308
309 db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(),
310 "value".getBytes());
311 db.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(),
312 "value2".getBytes());
313 try (final RocksIterator rocksIterator =
314 db.newIterator(columnFamilyHandleList.get(1))) {
315 rocksIterator.seekToFirst();
316 Map<String, String> refMap = new HashMap<>();
317 refMap.put("newcfkey", "value");
318 refMap.put("newcfkey2", "value2");
319 int i = 0;
320 while (rocksIterator.isValid()) {
321 i++;
322 assertThat(refMap.get(new String(rocksIterator.key()))).
323 isEqualTo(new String(rocksIterator.value()));
324 rocksIterator.next();
325 }
326 assertThat(i).isEqualTo(2);
327 }
328 } finally {
329 for (final ColumnFamilyHandle columnFamilyHandle :
330 columnFamilyHandleList) {
331 columnFamilyHandle.close();
332 }
333 }
334 }
335 }
336
337 @Test
338 public void multiGet() throws RocksDBException {
339 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
340 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
341 new ColumnFamilyDescriptor("new_cf".getBytes()));
342 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
343 try (final DBOptions options = new DBOptions()
344 .setCreateIfMissing(true)
345 .setCreateMissingColumnFamilies(true);
346 final RocksDB db = RocksDB.open(options,
347 dbFolder.getRoot().getAbsolutePath(),
348 cfDescriptors, columnFamilyHandleList)) {
349 try {
350 db.put(columnFamilyHandleList.get(0), "key".getBytes(),
351 "value".getBytes());
352 db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(),
353 "value".getBytes());
354
355 final List<byte[]> keys = Arrays.asList(new byte[][]{
356 "key".getBytes(), "newcfkey".getBytes()
357 });
358 Map<byte[], byte[]> retValues = db.multiGet(columnFamilyHandleList,
359 keys);
360 assertThat(retValues.size()).isEqualTo(2);
361 assertThat(new String(retValues.get(keys.get(0))))
362 .isEqualTo("value");
363 assertThat(new String(retValues.get(keys.get(1))))
364 .isEqualTo("value");
365 retValues = db.multiGet(new ReadOptions(), columnFamilyHandleList,
366 keys);
367 assertThat(retValues.size()).isEqualTo(2);
368 assertThat(new String(retValues.get(keys.get(0))))
369 .isEqualTo("value");
370 assertThat(new String(retValues.get(keys.get(1))))
371 .isEqualTo("value");
372 } finally {
373 for (final ColumnFamilyHandle columnFamilyHandle :
374 columnFamilyHandleList) {
375 columnFamilyHandle.close();
376 }
377 }
378 }
379 }
380
381 @Test
382 public void properties() throws RocksDBException {
383 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
384 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
385 new ColumnFamilyDescriptor("new_cf".getBytes()));
386 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
387 try (final DBOptions options = new DBOptions()
388 .setCreateIfMissing(true)
389 .setCreateMissingColumnFamilies(true);
390 final RocksDB db = RocksDB.open(options,
391 dbFolder.getRoot().getAbsolutePath(),
392 cfDescriptors, columnFamilyHandleList)) {
393 try {
394 assertThat(db.getProperty("rocksdb.estimate-num-keys")).
395 isNotNull();
396 assertThat(db.getLongProperty(columnFamilyHandleList.get(0),
397 "rocksdb.estimate-num-keys")).isGreaterThanOrEqualTo(0);
398 assertThat(db.getProperty("rocksdb.stats")).isNotNull();
399 assertThat(db.getProperty(columnFamilyHandleList.get(0),
400 "rocksdb.sstables")).isNotNull();
401 assertThat(db.getProperty(columnFamilyHandleList.get(1),
402 "rocksdb.estimate-num-keys")).isNotNull();
403 assertThat(db.getProperty(columnFamilyHandleList.get(1),
404 "rocksdb.stats")).isNotNull();
405 assertThat(db.getProperty(columnFamilyHandleList.get(1),
406 "rocksdb.sstables")).isNotNull();
407 assertThat(db.getAggregatedLongProperty("rocksdb.estimate-num-keys")).
408 isNotNull();
409 assertThat(db.getAggregatedLongProperty("rocksdb.estimate-num-keys")).
410 isGreaterThanOrEqualTo(0);
411 } finally {
412 for (final ColumnFamilyHandle columnFamilyHandle :
413 columnFamilyHandleList) {
414 columnFamilyHandle.close();
415 }
416 }
417 }
418 }
419
420
421 @Test
422 public void iterators() throws RocksDBException {
423 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
424 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
425 new ColumnFamilyDescriptor("new_cf".getBytes()));
426 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
427 try (final DBOptions options = new DBOptions()
428 .setCreateIfMissing(true)
429 .setCreateMissingColumnFamilies(true);
430 final RocksDB db = RocksDB.open(options,
431 dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
432 columnFamilyHandleList)) {
433 List<RocksIterator> iterators = null;
434 try {
435 iterators = db.newIterators(columnFamilyHandleList);
436 assertThat(iterators.size()).isEqualTo(2);
437 RocksIterator iter = iterators.get(0);
438 iter.seekToFirst();
439 final Map<String, String> defRefMap = new HashMap<>();
440 defRefMap.put("dfkey1", "dfvalue");
441 defRefMap.put("key", "value");
442 while (iter.isValid()) {
443 assertThat(defRefMap.get(new String(iter.key()))).
444 isEqualTo(new String(iter.value()));
445 iter.next();
446 }
447 // iterate over new_cf key/value pairs
448 final Map<String, String> cfRefMap = new HashMap<>();
449 cfRefMap.put("newcfkey", "value");
450 cfRefMap.put("newcfkey2", "value2");
451 iter = iterators.get(1);
452 iter.seekToFirst();
453 while (iter.isValid()) {
454 assertThat(cfRefMap.get(new String(iter.key()))).
455 isEqualTo(new String(iter.value()));
456 iter.next();
457 }
458 } finally {
459 if (iterators != null) {
460 for (final RocksIterator rocksIterator : iterators) {
461 rocksIterator.close();
462 }
463 }
464 for (final ColumnFamilyHandle columnFamilyHandle :
465 columnFamilyHandleList) {
466 columnFamilyHandle.close();
467 }
468 }
469 }
470 }
471
472 @Test(expected = RocksDBException.class)
473 public void failPutDisposedCF() throws RocksDBException {
474 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
475 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
476 new ColumnFamilyDescriptor("new_cf".getBytes()));
477 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
478 try (final DBOptions options = new DBOptions()
479 .setCreateIfMissing(true);
480 final RocksDB db = RocksDB.open(options,
481 dbFolder.getRoot().getAbsolutePath(),
482 cfDescriptors, columnFamilyHandleList)) {
483 try {
484 db.dropColumnFamily(columnFamilyHandleList.get(1));
485 db.put(columnFamilyHandleList.get(1), "key".getBytes(),
486 "value".getBytes());
487 } finally {
488 for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
489 columnFamilyHandle.close();
490 }
491 }
492 }
493 }
494
495 @Test(expected = RocksDBException.class)
496 public void failRemoveDisposedCF() throws RocksDBException {
497 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
498 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
499 new ColumnFamilyDescriptor("new_cf".getBytes()));
500 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
501 try (final DBOptions options = new DBOptions()
502 .setCreateIfMissing(true);
503 final RocksDB db = RocksDB.open(options,
504 dbFolder.getRoot().getAbsolutePath(),
505 cfDescriptors, columnFamilyHandleList)) {
506 try {
507 db.dropColumnFamily(columnFamilyHandleList.get(1));
508 db.remove(columnFamilyHandleList.get(1), "key".getBytes());
509 } finally {
510 for (final ColumnFamilyHandle columnFamilyHandle :
511 columnFamilyHandleList) {
512 columnFamilyHandle.close();
513 }
514 }
515 }
516 }
517
518 @Test(expected = RocksDBException.class)
519 public void failGetDisposedCF() throws RocksDBException {
520 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
521 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
522 new ColumnFamilyDescriptor("new_cf".getBytes()));
523 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
524 try (final DBOptions options = new DBOptions()
525 .setCreateIfMissing(true);
526 final RocksDB db = RocksDB.open(options,
527 dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
528 columnFamilyHandleList)) {
529 try {
530 db.dropColumnFamily(columnFamilyHandleList.get(1));
531 db.get(columnFamilyHandleList.get(1), "key".getBytes());
532 } finally {
533 for (final ColumnFamilyHandle columnFamilyHandle :
534 columnFamilyHandleList) {
535 columnFamilyHandle.close();
536 }
537 }
538 }
539 }
540
541 @Test(expected = RocksDBException.class)
542 public void failMultiGetWithoutCorrectNumberOfCF() throws RocksDBException {
543 final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
544 new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
545 new ColumnFamilyDescriptor("new_cf".getBytes()));
546 final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
547 try (final DBOptions options = new DBOptions()
548 .setCreateIfMissing(true);
549 final RocksDB db = RocksDB.open(options,
550 dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
551 columnFamilyHandleList)) {
552 try {
553 final List<byte[]> keys = new ArrayList<>();
554 keys.add("key".getBytes());
555 keys.add("newcfkey".getBytes());
556 final List<ColumnFamilyHandle> cfCustomList = new ArrayList<>();
557 db.multiGet(cfCustomList, keys);
558
559 } finally {
560 for (final ColumnFamilyHandle columnFamilyHandle :
561 columnFamilyHandleList) {
562 columnFamilyHandle.close();
563 }
564 }
565 }
566 }
567
568 @Test
569 public void testByteCreateFolumnFamily() throws RocksDBException {
570
571 try (final Options options = new Options().setCreateIfMissing(true);
572 final RocksDB db = RocksDB.open(options,
573 dbFolder.getRoot().getAbsolutePath())
574 ) {
575 final byte[] b0 = new byte[]{(byte) 0x00};
576 final byte[] b1 = new byte[]{(byte) 0x01};
577 final byte[] b2 = new byte[]{(byte) 0x02};
578 ColumnFamilyHandle cf1 = null, cf2 = null, cf3 = null;
579 try {
580 cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0));
581 cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1));
582 final List<byte[]> families = RocksDB.listColumnFamilies(options,
583 dbFolder.getRoot().getAbsolutePath());
584 assertThat(families).contains("default".getBytes(), b0, b1);
585 cf3 = db.createColumnFamily(new ColumnFamilyDescriptor(b2));
586 } finally {
587 if (cf1 != null) {
588 cf1.close();
589 }
590 if (cf2 != null) {
591 cf2.close();
592 }
593 if (cf3 != null) {
594 cf3.close();
595 }
596 }
597 }
598 }
599
600 @Test
601 public void testCFNamesWithZeroBytes() throws RocksDBException {
602 ColumnFamilyHandle cf1 = null, cf2 = null;
603 try (final Options options = new Options().setCreateIfMissing(true);
604 final RocksDB db = RocksDB.open(options,
605 dbFolder.getRoot().getAbsolutePath());
606 ) {
607 try {
608 final byte[] b0 = new byte[]{0, 0};
609 final byte[] b1 = new byte[]{0, 1};
610 cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0));
611 cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1));
612 final List<byte[]> families = RocksDB.listColumnFamilies(options,
613 dbFolder.getRoot().getAbsolutePath());
614 assertThat(families).contains("default".getBytes(), b0, b1);
615 } finally {
616 if (cf1 != null) {
617 cf1.close();
618 }
619 if (cf2 != null) {
620 cf2.close();
621 }
622 }
623 }
624 }
625
626 @Test
627 public void testCFNameSimplifiedChinese() throws RocksDBException {
628 ColumnFamilyHandle columnFamilyHandle = null;
629 try (final Options options = new Options().setCreateIfMissing(true);
630 final RocksDB db = RocksDB.open(options,
631 dbFolder.getRoot().getAbsolutePath());
632 ) {
633 try {
634 final String simplifiedChinese = "\u7b80\u4f53\u5b57";
635 columnFamilyHandle = db.createColumnFamily(
636 new ColumnFamilyDescriptor(simplifiedChinese.getBytes()));
637
638 final List<byte[]> families = RocksDB.listColumnFamilies(options,
639 dbFolder.getRoot().getAbsolutePath());
640 assertThat(families).contains("default".getBytes(),
641 simplifiedChinese.getBytes());
642 } finally {
643 if (columnFamilyHandle != null) {
644 columnFamilyHandle.close();
645 }
646 }
647 }
648 }
649 }