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 org.junit.ClassRule;
9 import org.junit.Test;
10 
11 import static org.assertj.core.api.Assertions.assertThat;
12 
13 public class RocksMemEnvTest {
14 
15   @ClassRule
16   public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
17       new RocksNativeLibraryResource();
18 
19   @Test
memEnvFillAndReopen()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 
34     try (final Env env = new RocksMemEnv(Env.getDefault());
35          final Options options = new Options()
36              .setCreateIfMissing(true)
37              .setEnv(env);
38          final FlushOptions flushOptions = new FlushOptions()
39              .setWaitForFlush(true);
40     ) {
41       try (final RocksDB db = RocksDB.open(options, "dir/db")) {
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.
78       try (final RocksDB db = RocksDB.open(options, "dir/db")) {
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
multipleDatabaseInstances()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 
108     try (final Env env = new RocksMemEnv(Env.getDefault());
109          final Options options = new Options()
110              .setCreateIfMissing(true)
111              .setEnv(env);
112          final RocksDB db = RocksDB.open(options, "dir/db");
113          final RocksDB otherDb = RocksDB.open(options, "dir/otherDb")
114     ) {
115       // write key/value pairs using MemEnv
116       // to db and to otherDb.
117       for (int i = 0; i < keys.length; i++) {
118         db.put(keys[i], values[i]);
119         otherDb.put(otherKeys[i], values[i]);
120       }
121 
122       // verify key/value pairs after flush using MemEnv
123       for (int i = 0; i < keys.length; i++) {
124         // verify db
125         assertThat(db.get(otherKeys[i])).isNull();
126         assertThat(db.get(keys[i])).isEqualTo(values[i]);
127 
128         // verify otherDb
129         assertThat(otherDb.get(keys[i])).isNull();
130         assertThat(otherDb.get(otherKeys[i])).isEqualTo(values[i]);
131       }
132     }
133   }
134 
135   @Test(expected = RocksDBException.class)
createIfMissingFalse()136   public void createIfMissingFalse() throws RocksDBException {
137     try (final Env env = new RocksMemEnv(Env.getDefault());
138          final Options options = new Options()
139              .setCreateIfMissing(false)
140              .setEnv(env);
141          final RocksDB db = RocksDB.open(options, "db/dir")) {
142       // shall throw an exception because db dir does not
143       // exist.
144     }
145   }
146 }
147