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.
8 import org
.junit
.ClassRule
;
10 import org
.junit
.Test
;
11 import org
.junit
.rules
.TemporaryFolder
;
13 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
15 public class RocksMemEnvTest
{
18 public static final RocksMemoryResource rocksMemoryResource
=
19 new RocksMemoryResource();
22 public void memEnvFillAndReopen() throws RocksDBException
{
24 final byte[][] keys
= {
30 final byte[][] values
= {
36 try (final Env env
= new RocksMemEnv();
37 final Options options
= new Options()
38 .setCreateIfMissing(true)
40 final FlushOptions flushOptions
= new FlushOptions()
41 .setWaitForFlush(true);
43 try (final RocksDB db
= RocksDB
.open(options
, "dir/db")) {
44 // write key/value pairs using MemEnv
45 for (int i
= 0; i
< keys
.length
; i
++) {
46 db
.put(keys
[i
], values
[i
]);
49 // read key/value pairs using MemEnv
50 for (int i
= 0; i
< keys
.length
; i
++) {
51 assertThat(db
.get(keys
[i
])).isEqualTo(values
[i
]);
54 // Check iterator access
55 try (final RocksIterator iterator
= db
.newIterator()) {
56 iterator
.seekToFirst();
57 for (int i
= 0; i
< keys
.length
; i
++) {
58 assertThat(iterator
.isValid()).isTrue();
59 assertThat(iterator
.key()).isEqualTo(keys
[i
]);
60 assertThat(iterator
.value()).isEqualTo(values
[i
]);
63 // reached end of database
64 assertThat(iterator
.isValid()).isFalse();
68 db
.flush(flushOptions
);
70 // read key/value pairs after flush using MemEnv
71 for (int i
= 0; i
< keys
.length
; i
++) {
72 assertThat(db
.get(keys
[i
])).isEqualTo(values
[i
]);
76 options
.setCreateIfMissing(false);
78 // After reopen the values shall still be in the mem env.
79 // as long as the env is not freed.
80 try (final RocksDB db
= RocksDB
.open(options
, "dir/db")) {
81 // read key/value pairs using MemEnv
82 for (int i
= 0; i
< keys
.length
; i
++) {
83 assertThat(db
.get(keys
[i
])).isEqualTo(values
[i
]);
90 public void multipleDatabaseInstances() throws RocksDBException
{
92 final byte[][] keys
= {
98 final byte[][] otherKeys
= {
104 final byte[][] values
= {
110 try (final Env env
= new RocksMemEnv();
111 final Options options
= new Options()
112 .setCreateIfMissing(true)
114 final RocksDB db
= RocksDB
.open(options
, "dir/db");
115 final RocksDB otherDb
= RocksDB
.open(options
, "dir/otherDb")
117 // write key/value pairs using MemEnv
118 // to db and to otherDb.
119 for (int i
= 0; i
< keys
.length
; i
++) {
120 db
.put(keys
[i
], values
[i
]);
121 otherDb
.put(otherKeys
[i
], values
[i
]);
124 // verify key/value pairs after flush using MemEnv
125 for (int i
= 0; i
< keys
.length
; i
++) {
127 assertThat(db
.get(otherKeys
[i
])).isNull();
128 assertThat(db
.get(keys
[i
])).isEqualTo(values
[i
]);
131 assertThat(otherDb
.get(keys
[i
])).isNull();
132 assertThat(otherDb
.get(otherKeys
[i
])).isEqualTo(values
[i
]);
137 @Test(expected
= RocksDBException
.class)
138 public void createIfMissingFalse() throws RocksDBException
{
139 try (final Env env
= new RocksMemEnv();
140 final Options options
= new Options()
141 .setCreateIfMissing(false)
143 final RocksDB db
= RocksDB
.open(options
, "db/dir")) {
144 // shall throw an exception because db dir does not