]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/java/src/test/java/org/rocksdb/RocksMemEnvTest.java
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / RocksMemEnvTest.java
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae
FG
5
6package org.rocksdb;
7
8import org.junit.ClassRule;
7c673cae 9import org.junit.Test;
7c673cae
FG
10
11import static org.assertj.core.api.Assertions.assertThat;
12
13public class RocksMemEnvTest {
14
15 @ClassRule
f67539c2
TL
16 public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
17 new RocksNativeLibraryResource();
7c673cae
FG
18
19 @Test
20 public void memEnvFillAndReopen() throws RocksDBException {
21
22 final byte[][] keys = {
23 "aaa".getBytes(),
24 "bbb".getBytes(),
25 "ccc".getBytes()
26 };
27
28 final byte[][] values = {
29 "foo".getBytes(),
30 "bar".getBytes(),
31 "baz".getBytes()
32 };
33
494da23a 34 try (final Env env = new RocksMemEnv(Env.getDefault());
7c673cae
FG
35 final Options options = new Options()
36 .setCreateIfMissing(true)
37 .setEnv(env);
38 final FlushOptions flushOptions = new FlushOptions()
39 .setWaitForFlush(true);
40 ) {
1e59de90 41 try (final RocksDB db = RocksDB.open(options, "/dir/db")) {
7c673cae
FG
42 // write key/value pairs using MemEnv
43 for (int i = 0; i < keys.length; i++) {
44 db.put(keys[i], values[i]);
45 }
46
47 // read key/value pairs using MemEnv
48 for (int i = 0; i < keys.length; i++) {
49 assertThat(db.get(keys[i])).isEqualTo(values[i]);
50 }
51
52 // Check iterator access
53 try (final RocksIterator iterator = db.newIterator()) {
54 iterator.seekToFirst();
55 for (int i = 0; i < keys.length; i++) {
56 assertThat(iterator.isValid()).isTrue();
57 assertThat(iterator.key()).isEqualTo(keys[i]);
58 assertThat(iterator.value()).isEqualTo(values[i]);
59 iterator.next();
60 }
61 // reached end of database
62 assertThat(iterator.isValid()).isFalse();
63 }
64
65 // flush
66 db.flush(flushOptions);
67
68 // read key/value pairs after flush using MemEnv
69 for (int i = 0; i < keys.length; i++) {
70 assertThat(db.get(keys[i])).isEqualTo(values[i]);
71 }
72 }
73
74 options.setCreateIfMissing(false);
75
76 // After reopen the values shall still be in the mem env.
77 // as long as the env is not freed.
1e59de90 78 try (final RocksDB db = RocksDB.open(options, "/dir/db")) {
7c673cae
FG
79 // read key/value pairs using MemEnv
80 for (int i = 0; i < keys.length; i++) {
81 assertThat(db.get(keys[i])).isEqualTo(values[i]);
82 }
83 }
84 }
85 }
86
87 @Test
88 public void multipleDatabaseInstances() throws RocksDBException {
89 // db - keys
90 final byte[][] keys = {
91 "aaa".getBytes(),
92 "bbb".getBytes(),
93 "ccc".getBytes()
94 };
95 // otherDb - keys
96 final byte[][] otherKeys = {
97 "111".getBytes(),
98 "222".getBytes(),
99 "333".getBytes()
100 };
101 // values
102 final byte[][] values = {
103 "foo".getBytes(),
104 "bar".getBytes(),
105 "baz".getBytes()
106 };
107
494da23a 108 try (final Env env = new RocksMemEnv(Env.getDefault());
1e59de90
TL
109 final Options options = new Options().setCreateIfMissing(true).setEnv(env);
110 final RocksDB db = RocksDB.open(options, "/dir/db");
111 final RocksDB otherDb = RocksDB.open(options, "/dir/otherDb")) {
7c673cae
FG
112 // write key/value pairs using MemEnv
113 // to db and to otherDb.
114 for (int i = 0; i < keys.length; i++) {
115 db.put(keys[i], values[i]);
116 otherDb.put(otherKeys[i], values[i]);
117 }
118
119 // verify key/value pairs after flush using MemEnv
120 for (int i = 0; i < keys.length; i++) {
121 // verify db
122 assertThat(db.get(otherKeys[i])).isNull();
123 assertThat(db.get(keys[i])).isEqualTo(values[i]);
124
125 // verify otherDb
126 assertThat(otherDb.get(keys[i])).isNull();
127 assertThat(otherDb.get(otherKeys[i])).isEqualTo(values[i]);
128 }
129 }
130 }
131
132 @Test(expected = RocksDBException.class)
133 public void createIfMissingFalse() throws RocksDBException {
494da23a 134 try (final Env env = new RocksMemEnv(Env.getDefault());
1e59de90
TL
135 final Options options = new Options().setCreateIfMissing(false).setEnv(env);
136 final RocksDB db = RocksDB.open(options, "/db/dir")) {
7c673cae
FG
137 // shall throw an exception because db dir does not
138 // exist.
139 }
140 }
141}