]>
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 | package org.rocksdb; |
6 | ||
7 | import org.junit.ClassRule; | |
8 | import org.junit.Rule; | |
9 | import org.junit.Test; | |
10 | import org.junit.rules.TemporaryFolder; | |
11 | ||
12 | import java.util.ArrayList; | |
13 | import java.util.Arrays; | |
14 | import java.util.List; | |
15 | ||
16 | import static org.assertj.core.api.Assertions.assertThat; | |
17 | ||
18 | public class ReadOnlyTest { | |
19 | ||
20 | @ClassRule | |
f67539c2 TL |
21 | public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = |
22 | new RocksNativeLibraryResource(); | |
7c673cae FG |
23 | |
24 | @Rule | |
25 | public TemporaryFolder dbFolder = new TemporaryFolder(); | |
26 | ||
27 | @Test | |
28 | public void readOnlyOpen() throws RocksDBException { | |
29 | try (final Options options = new Options() | |
30 | .setCreateIfMissing(true); | |
31 | final RocksDB db = RocksDB.open(options, | |
32 | dbFolder.getRoot().getAbsolutePath())) { | |
33 | db.put("key".getBytes(), "value".getBytes()); | |
20effc67 TL |
34 | } |
35 | try (final RocksDB db = RocksDB.openReadOnly(dbFolder.getRoot().getAbsolutePath())) { | |
36 | assertThat("value").isEqualTo(new String(db.get("key".getBytes()))); | |
7c673cae FG |
37 | } |
38 | ||
39 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
40 | final List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); | |
20effc67 | 41 | cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)); |
7c673cae | 42 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
20effc67 TL |
43 | try (final RocksDB db = RocksDB.open( |
44 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList)) { | |
45 | columnFamilyHandleList.add( | |
46 | db.createColumnFamily(new ColumnFamilyDescriptor("new_cf".getBytes(), cfOpts))); | |
47 | columnFamilyHandleList.add( | |
48 | db.createColumnFamily(new ColumnFamilyDescriptor("new_cf2".getBytes(), cfOpts))); | |
49 | db.put(columnFamilyHandleList.get(2), "key2".getBytes(), "value2".getBytes()); | |
50 | } | |
7c673cae | 51 | |
20effc67 TL |
52 | columnFamilyHandleList.clear(); |
53 | try (final RocksDB db = RocksDB.openReadOnly( | |
54 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList)) { | |
55 | assertThat(db.get("key2".getBytes())).isNull(); | |
56 | assertThat(db.get(columnFamilyHandleList.get(0), "key2".getBytes())).isNull(); | |
57 | } | |
7c673cae | 58 | |
20effc67 TL |
59 | cfDescriptors.clear(); |
60 | cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)); | |
61 | cfDescriptors.add(new ColumnFamilyDescriptor("new_cf2".getBytes(), cfOpts)); | |
62 | columnFamilyHandleList.clear(); | |
63 | try (final RocksDB db = RocksDB.openReadOnly( | |
64 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList)) { | |
65 | assertThat(new String(db.get(columnFamilyHandleList.get(1), "key2".getBytes()))) | |
66 | .isEqualTo("value2"); | |
7c673cae FG |
67 | } |
68 | } | |
69 | } | |
70 | ||
71 | @Test(expected = RocksDBException.class) | |
72 | public void failToWriteInReadOnly() throws RocksDBException { | |
20effc67 TL |
73 | try (final Options options = new Options().setCreateIfMissing(true)) { |
74 | try (final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { | |
75 | // no-op | |
7c673cae FG |
76 | } |
77 | } | |
78 | ||
79 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
20effc67 TL |
80 | final List<ColumnFamilyDescriptor> cfDescriptors = |
81 | Arrays.asList(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)); | |
82 | ||
83 | final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = new ArrayList<>(); | |
84 | try (final RocksDB rDb = RocksDB.openReadOnly(dbFolder.getRoot().getAbsolutePath(), | |
85 | cfDescriptors, readOnlyColumnFamilyHandleList)) { | |
86 | // test that put fails in readonly mode | |
87 | rDb.put("key".getBytes(), "value".getBytes()); | |
7c673cae FG |
88 | } |
89 | } | |
90 | } | |
91 | ||
92 | @Test(expected = RocksDBException.class) | |
93 | public void failToCFWriteInReadOnly() throws RocksDBException { | |
94 | try (final Options options = new Options().setCreateIfMissing(true); | |
95 | final RocksDB db = RocksDB.open(options, | |
96 | dbFolder.getRoot().getAbsolutePath())) { | |
97 | //no-op | |
98 | } | |
99 | ||
100 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
101 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
102 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) | |
103 | ); | |
104 | final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = | |
105 | new ArrayList<>(); | |
106 | try (final RocksDB rDb = RocksDB.openReadOnly( | |
107 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
108 | readOnlyColumnFamilyHandleList)) { | |
20effc67 | 109 | rDb.put(readOnlyColumnFamilyHandleList.get(0), "key".getBytes(), "value".getBytes()); |
7c673cae FG |
110 | } |
111 | } | |
112 | } | |
113 | ||
114 | @Test(expected = RocksDBException.class) | |
115 | public void failToRemoveInReadOnly() throws RocksDBException { | |
116 | try (final Options options = new Options().setCreateIfMissing(true); | |
117 | final RocksDB db = RocksDB.open(options, | |
118 | dbFolder.getRoot().getAbsolutePath())) { | |
119 | //no-op | |
120 | } | |
121 | ||
122 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
123 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
124 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) | |
125 | ); | |
126 | ||
127 | final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = | |
128 | new ArrayList<>(); | |
129 | ||
130 | try (final RocksDB rDb = RocksDB.openReadOnly( | |
131 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
132 | readOnlyColumnFamilyHandleList)) { | |
20effc67 | 133 | rDb.delete("key".getBytes()); |
7c673cae FG |
134 | } |
135 | } | |
136 | } | |
137 | ||
138 | @Test(expected = RocksDBException.class) | |
139 | public void failToCFRemoveInReadOnly() throws RocksDBException { | |
140 | try (final Options options = new Options().setCreateIfMissing(true); | |
141 | final RocksDB db = RocksDB.open(options, | |
142 | dbFolder.getRoot().getAbsolutePath())) { | |
143 | //no-op | |
144 | } | |
145 | ||
146 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
147 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
148 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) | |
149 | ); | |
150 | ||
151 | final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = | |
152 | new ArrayList<>(); | |
153 | try (final RocksDB rDb = RocksDB.openReadOnly( | |
154 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
155 | readOnlyColumnFamilyHandleList)) { | |
f67539c2 | 156 | rDb.delete(readOnlyColumnFamilyHandleList.get(0), |
7c673cae | 157 | "key".getBytes()); |
7c673cae FG |
158 | } |
159 | } | |
160 | } | |
161 | ||
162 | @Test(expected = RocksDBException.class) | |
163 | public void failToWriteBatchReadOnly() throws RocksDBException { | |
164 | try (final Options options = new Options().setCreateIfMissing(true); | |
165 | final RocksDB db = RocksDB.open(options, | |
166 | dbFolder.getRoot().getAbsolutePath())) { | |
167 | //no-op | |
168 | } | |
169 | ||
170 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
171 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
172 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) | |
173 | ); | |
174 | ||
175 | final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = | |
176 | new ArrayList<>(); | |
177 | try (final RocksDB rDb = RocksDB.openReadOnly( | |
178 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
179 | readOnlyColumnFamilyHandleList); | |
180 | final WriteBatch wb = new WriteBatch(); | |
181 | final WriteOptions wOpts = new WriteOptions()) { | |
7c673cae FG |
182 | wb.put("key".getBytes(), "value".getBytes()); |
183 | rDb.write(wOpts, wb); | |
7c673cae FG |
184 | } |
185 | } | |
186 | } | |
187 | ||
188 | @Test(expected = RocksDBException.class) | |
189 | public void failToCFWriteBatchReadOnly() throws RocksDBException { | |
190 | try (final Options options = new Options().setCreateIfMissing(true); | |
191 | final RocksDB db = RocksDB.open(options, | |
192 | dbFolder.getRoot().getAbsolutePath())) { | |
193 | //no-op | |
194 | } | |
195 | ||
196 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
197 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
198 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) | |
199 | ); | |
200 | ||
201 | final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = | |
202 | new ArrayList<>(); | |
203 | try (final RocksDB rDb = RocksDB.openReadOnly( | |
204 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
205 | readOnlyColumnFamilyHandleList); | |
206 | final WriteBatch wb = new WriteBatch(); | |
207 | final WriteOptions wOpts = new WriteOptions()) { | |
7c673cae FG |
208 | wb.put(readOnlyColumnFamilyHandleList.get(0), "key".getBytes(), |
209 | "value".getBytes()); | |
210 | rDb.write(wOpts, wb); | |
20effc67 TL |
211 | } |
212 | } | |
213 | } | |
214 | ||
215 | @Test(expected = RocksDBException.class) | |
216 | public void errorIfWalFileExists() throws RocksDBException { | |
217 | try (final Options options = new Options().setCreateIfMissing(true); | |
218 | final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { | |
219 | // no-op | |
220 | } | |
221 | ||
222 | try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { | |
223 | final List<ColumnFamilyDescriptor> cfDescriptors = | |
224 | Arrays.asList(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts)); | |
225 | ||
226 | final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = new ArrayList<>(); | |
227 | try (final DBOptions options = new DBOptions(); | |
228 | final RocksDB rDb = RocksDB.openReadOnly(options, dbFolder.getRoot().getAbsolutePath(), | |
229 | cfDescriptors, readOnlyColumnFamilyHandleList, true);) { | |
230 | // no-op... should have raised an error as errorIfWalFileExists=true | |
7c673cae FG |
231 | } |
232 | } | |
233 | } | |
234 | } |