Repository navigation
Expand file tree
/
Copy pathTtlDBTest.java
More file actions
229 lines (205 loc) · 9.04 KB
/
Copy pathTtlDBTest.java
File metadata and controls
229 lines (205 loc) · 9.04 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
226
227
228
229
// 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.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class TtlDBTest {
private static final int BATCH_ITERATION = 16;
@ClassRule
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
new RocksNativeLibraryResource();
@Rule
public TemporaryFolder dbFolder = new TemporaryFolder();
@Test
public void ttlDBOpen() throws RocksDBException, InterruptedException {
try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0);
final TtlDB ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
ttlDB.put("key".getBytes(), "value".getBytes());
assertThat(ttlDB.get("key".getBytes())).
isEqualTo("value".getBytes());
assertThat(ttlDB.get("key".getBytes())).isNotNull();
}
}
@Test
public void ttlDBOpenWithTtl() throws RocksDBException, InterruptedException {
try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0);
final TtlDB ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath(), 1, false)) {
ttlDB.put("key".getBytes(), "value".getBytes());
assertThat(ttlDB.get("key".getBytes())).
isEqualTo("value".getBytes());
TimeUnit.SECONDS.sleep(2);
ttlDB.compactRange();
assertThat(ttlDB.get("key".getBytes())).isNull();
}
}
@Test
public void metadataReadsWithSnapshot() throws RocksDBException {
final byte[] key = "key".getBytes(StandardCharsets.UTF_8);
final byte[] oldValue = "old".getBytes(StandardCharsets.UTF_8);
final byte[] newValue = "new".getBytes(StandardCharsets.UTF_8);
try (final Options options = new Options().setCreateIfMissing(true);
final TtlDB ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
ttlDB.put(key, oldValue);
try (final Snapshot snapshot = ttlDB.getSnapshot();
final ReadOptions readOptions = new ReadOptions().setSnapshot(snapshot)) {
ttlDB.put(key, newValue);
final GetWithMetadataResult result = ttlDB.getWithMetadata(readOptions, key);
assertThat(result.value).isEqualTo(oldValue);
assertThat(result.timestamp).isEmpty();
assertThat(result.newerVersionPresent).isTrue();
final List<GetWithMetadataResult> results =
ttlDB.multiGetWithMetadata(readOptions, Arrays.asList(key));
assertThat(results).hasSize(1);
assertThat(results.get(0).value).isEqualTo(oldValue);
assertThat(results.get(0).timestamp).isEmpty();
assertThat(results.get(0).newerVersionPresent).isTrue();
}
}
}
@Test
public void ttlDBSimpleIterator() throws RocksDBException {
try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0);
final TtlDB ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
ttlDB.put("keyI".getBytes(), "valueI".getBytes());
try (final RocksIterator iterator = ttlDB.newIterator()) {
iterator.seekToFirst();
assertThat(iterator.isValid()).isTrue();
assertThat(iterator.key()).isEqualTo("keyI".getBytes());
assertThat(iterator.value()).isEqualTo("valueI".getBytes());
iterator.next();
assertThat(iterator.isValid()).isFalse();
}
}
}
@Test
public void ttlDbOpenWithColumnFamilies() throws RocksDBException,
InterruptedException {
final List<ColumnFamilyDescriptor> cfNames = Arrays.asList(
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
new ColumnFamilyDescriptor("new_cf".getBytes())
);
final List<Integer> ttlValues = Arrays.asList(0, 1);
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final DBOptions dbOptions = new DBOptions()
.setCreateMissingColumnFamilies(true)
.setCreateIfMissing(true);
final TtlDB ttlDB = TtlDB.open(dbOptions,
dbFolder.getRoot().getAbsolutePath(), cfNames,
columnFamilyHandleList, ttlValues, false)) {
try {
ttlDB.put("key".getBytes(), "value".getBytes());
assertThat(ttlDB.get("key".getBytes())).
isEqualTo("value".getBytes());
ttlDB.put(columnFamilyHandleList.get(1), "key".getBytes(),
"value".getBytes());
assertThat(ttlDB.get(columnFamilyHandleList.get(1),
"key".getBytes())).isEqualTo("value".getBytes());
TimeUnit.SECONDS.sleep(2);
ttlDB.compactRange();
ttlDB.compactRange(columnFamilyHandleList.get(1));
assertThat(ttlDB.get("key".getBytes())).isNotNull();
assertThat(ttlDB.get(columnFamilyHandleList.get(1),
"key".getBytes())).isNull();
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
}
}
}
@Test
public void createTtlColumnFamily() throws RocksDBException,
InterruptedException {
try (final Options options = new Options().setCreateIfMissing(true);
final TtlDB ttlDB = TtlDB.open(options,
dbFolder.getRoot().getAbsolutePath());
final ColumnFamilyHandle columnFamilyHandle =
ttlDB.createColumnFamilyWithTtl(
new ColumnFamilyDescriptor("new_cf".getBytes()), 1)) {
ttlDB.put(columnFamilyHandle, "key".getBytes(),
"value".getBytes());
assertThat(ttlDB.get(columnFamilyHandle, "key".getBytes())).
isEqualTo("value".getBytes());
TimeUnit.SECONDS.sleep(2);
ttlDB.compactRange(columnFamilyHandle);
assertThat(ttlDB.get(columnFamilyHandle, "key".getBytes())).isNull();
}
}
@Test
public void writeBatchWithFlush() throws RocksDBException {
try (final Options dbOptions = new Options()) {
dbOptions.setCreateIfMissing(true);
dbOptions.setCreateMissingColumnFamilies(true);
try (final RocksDB db =
TtlDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(), 100, false)) {
try (WriteBatch wb = new WriteBatch()) {
for (int i = 0; i < BATCH_ITERATION; i++) {
wb.put(("key" + i).getBytes(StandardCharsets.UTF_8),
("value" + i).getBytes(StandardCharsets.UTF_8));
}
try (WriteOptions writeOptions = new WriteOptions()) {
db.write(writeOptions, wb);
}
try (FlushOptions fOptions = new FlushOptions()) {
db.flush(fOptions);
}
}
for (int i = 0; i < BATCH_ITERATION; i++) {
assertThat(db.get(("key" + i).getBytes(StandardCharsets.UTF_8)))
.isEqualTo(("value" + i).getBytes(StandardCharsets.UTF_8));
}
}
}
}
@Test
public void writeBatchWithFlushAndColumnFamily() throws RocksDBException {
try (final DBOptions dbOptions = new DBOptions()) {
System.out.println("Test start");
dbOptions.setCreateIfMissing(true);
dbOptions.setCreateMissingColumnFamilies(true);
final List<ColumnFamilyDescriptor> cfNames =
Arrays.asList(new ColumnFamilyDescriptor("new_cf".getBytes()),
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
final List<Integer> ttlValues = Arrays.asList(0, 1);
try (final RocksDB db = TtlDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(), cfNames,
columnFamilyHandleList, ttlValues, false)) {
try {
assertThat(columnFamilyHandleList.get(1).isDefaultColumnFamily()).isTrue();
try (WriteBatch wb = new WriteBatch()) {
for (int i = 0; i < BATCH_ITERATION; i++) {
wb.put(("key" + i).getBytes(StandardCharsets.UTF_8),
("value" + i).getBytes(StandardCharsets.UTF_8));
}
try (WriteOptions writeOptions = new WriteOptions()) {
db.write(writeOptions, wb);
}
try (FlushOptions fOptions = new FlushOptions()) {
// Test both flush options, db.flush(fOptions) slush only default CF
db.flush(fOptions);
db.flush(fOptions, columnFamilyHandleList);
}
}
for (int i = 0; i < BATCH_ITERATION; i++) {
assertThat(db.get(("key" + i).getBytes(StandardCharsets.UTF_8)))
.isEqualTo(("value" + i).getBytes(StandardCharsets.UTF_8));
}
} finally {
// All CF handles must be closed before we close DB.
columnFamilyHandleList.stream().forEach(ColumnFamilyHandle::close);
}
}
}
}
}