]>
Commit | Line | Data |
---|---|---|
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 | |
6 | package org.rocksdb; | |
7 | ||
8 | import org.junit.ClassRule; | |
9 | import org.junit.Rule; | |
10 | import org.junit.Test; | |
11 | import org.junit.rules.TemporaryFolder; | |
12 | ||
13 | import static org.assertj.core.api.Assertions.assertThat; | |
14 | ||
15 | public class RocksMemEnvTest { | |
16 | ||
17 | @ClassRule | |
18 | public static final RocksMemoryResource rocksMemoryResource = | |
19 | new RocksMemoryResource(); | |
20 | ||
21 | @Test | |
22 | public void memEnvFillAndReopen() throws RocksDBException { | |
23 | ||
24 | final byte[][] keys = { | |
25 | "aaa".getBytes(), | |
26 | "bbb".getBytes(), | |
27 | "ccc".getBytes() | |
28 | }; | |
29 | ||
30 | final byte[][] values = { | |
31 | "foo".getBytes(), | |
32 | "bar".getBytes(), | |
33 | "baz".getBytes() | |
34 | }; | |
35 | ||
36 | try (final Env env = new RocksMemEnv(); | |
37 | final Options options = new Options() | |
38 | .setCreateIfMissing(true) | |
39 | .setEnv(env); | |
40 | final FlushOptions flushOptions = new FlushOptions() | |
41 | .setWaitForFlush(true); | |
42 | ) { | |
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]); | |
47 | } | |
48 | ||
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]); | |
52 | } | |
53 | ||
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]); | |
61 | iterator.next(); | |
62 | } | |
63 | // reached end of database | |
64 | assertThat(iterator.isValid()).isFalse(); | |
65 | } | |
66 | ||
67 | // flush | |
68 | db.flush(flushOptions); | |
69 | ||
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]); | |
73 | } | |
74 | } | |
75 | ||
76 | options.setCreateIfMissing(false); | |
77 | ||
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]); | |
84 | } | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
89 | @Test | |
90 | public void multipleDatabaseInstances() throws RocksDBException { | |
91 | // db - keys | |
92 | final byte[][] keys = { | |
93 | "aaa".getBytes(), | |
94 | "bbb".getBytes(), | |
95 | "ccc".getBytes() | |
96 | }; | |
97 | // otherDb - keys | |
98 | final byte[][] otherKeys = { | |
99 | "111".getBytes(), | |
100 | "222".getBytes(), | |
101 | "333".getBytes() | |
102 | }; | |
103 | // values | |
104 | final byte[][] values = { | |
105 | "foo".getBytes(), | |
106 | "bar".getBytes(), | |
107 | "baz".getBytes() | |
108 | }; | |
109 | ||
110 | try (final Env env = new RocksMemEnv(); | |
111 | final Options options = new Options() | |
112 | .setCreateIfMissing(true) | |
113 | .setEnv(env); | |
114 | final RocksDB db = RocksDB.open(options, "dir/db"); | |
115 | final RocksDB otherDb = RocksDB.open(options, "dir/otherDb") | |
116 | ) { | |
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]); | |
122 | } | |
123 | ||
124 | // verify key/value pairs after flush using MemEnv | |
125 | for (int i = 0; i < keys.length; i++) { | |
126 | // verify db | |
127 | assertThat(db.get(otherKeys[i])).isNull(); | |
128 | assertThat(db.get(keys[i])).isEqualTo(values[i]); | |
129 | ||
130 | // verify otherDb | |
131 | assertThat(otherDb.get(keys[i])).isNull(); | |
132 | assertThat(otherDb.get(otherKeys[i])).isEqualTo(values[i]); | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
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) | |
142 | .setEnv(env); | |
143 | final RocksDB db = RocksDB.open(options, "db/dir")) { | |
144 | // shall throw an exception because db dir does not | |
145 | // exist. | |
146 | } | |
147 | } | |
148 | } |