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