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).
7 import org
.junit
.ClassRule
;
10 import org
.junit
.rules
.TemporaryFolder
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
16 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
18 public class ReadOnlyTest
{
21 public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE
=
22 new RocksNativeLibraryResource();
25 public TemporaryFolder dbFolder
= new TemporaryFolder();
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());
35 try (final RocksDB db
= RocksDB
.openReadOnly(dbFolder
.getRoot().getAbsolutePath())) {
36 assertThat("value").isEqualTo(new String(db
.get("key".getBytes())));
39 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
40 final List
<ColumnFamilyDescriptor
> cfDescriptors
= new ArrayList
<>();
41 cfDescriptors
.add(new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
));
42 final List
<ColumnFamilyHandle
> columnFamilyHandleList
= new ArrayList
<>();
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());
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();
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())))
71 @Test(expected
= RocksDBException
.class)
72 public void failToWriteInReadOnly() throws RocksDBException
{
73 try (final Options options
= new Options().setCreateIfMissing(true)) {
74 try (final RocksDB db
= RocksDB
.open(options
, dbFolder
.getRoot().getAbsolutePath())) {
79 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
80 final List
<ColumnFamilyDescriptor
> cfDescriptors
=
81 Arrays
.asList(new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
));
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());
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())) {
100 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
101 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
102 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
)
104 final List
<ColumnFamilyHandle
> readOnlyColumnFamilyHandleList
=
106 try (final RocksDB rDb
= RocksDB
.openReadOnly(
107 dbFolder
.getRoot().getAbsolutePath(), cfDescriptors
,
108 readOnlyColumnFamilyHandleList
)) {
109 rDb
.put(readOnlyColumnFamilyHandleList
.get(0), "key".getBytes(), "value".getBytes());
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())) {
122 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
123 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
124 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
)
127 final List
<ColumnFamilyHandle
> readOnlyColumnFamilyHandleList
=
130 try (final RocksDB rDb
= RocksDB
.openReadOnly(
131 dbFolder
.getRoot().getAbsolutePath(), cfDescriptors
,
132 readOnlyColumnFamilyHandleList
)) {
133 rDb
.delete("key".getBytes());
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())) {
146 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
147 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
148 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
)
151 final List
<ColumnFamilyHandle
> readOnlyColumnFamilyHandleList
=
153 try (final RocksDB rDb
= RocksDB
.openReadOnly(
154 dbFolder
.getRoot().getAbsolutePath(), cfDescriptors
,
155 readOnlyColumnFamilyHandleList
)) {
156 rDb
.delete(readOnlyColumnFamilyHandleList
.get(0),
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())) {
170 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
171 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
172 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
)
175 final List
<ColumnFamilyHandle
> readOnlyColumnFamilyHandleList
=
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()) {
182 wb
.put("key".getBytes(), "value".getBytes());
183 rDb
.write(wOpts
, wb
);
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())) {
196 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
197 final List
<ColumnFamilyDescriptor
> cfDescriptors
= Arrays
.asList(
198 new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
)
201 final List
<ColumnFamilyHandle
> readOnlyColumnFamilyHandleList
=
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()) {
208 wb
.put(readOnlyColumnFamilyHandleList
.get(0), "key".getBytes(),
210 rDb
.write(wOpts
, wb
);
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())) {
222 try (final ColumnFamilyOptions cfOpts
= new ColumnFamilyOptions()) {
223 final List
<ColumnFamilyDescriptor
> cfDescriptors
=
224 Arrays
.asList(new ColumnFamilyDescriptor(RocksDB
.DEFAULT_COLUMN_FAMILY
, cfOpts
));
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