]>
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 | |
6 | package org.rocksdb; | |
7 | ||
20effc67 TL |
8 | import static java.nio.charset.StandardCharsets.UTF_8; |
9 | import static org.assertj.core.api.Assertions.assertThat; | |
10 | import static org.junit.Assert.assertFalse; | |
11 | import static org.junit.Assert.assertTrue; | |
7c673cae | 12 | |
20effc67 | 13 | import java.util.*; |
7c673cae FG |
14 | import org.junit.ClassRule; |
15 | import org.junit.Rule; | |
16 | import org.junit.Test; | |
17 | import org.junit.rules.TemporaryFolder; | |
18 | ||
7c673cae FG |
19 | public class ColumnFamilyTest { |
20 | ||
21 | @ClassRule | |
f67539c2 TL |
22 | public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = |
23 | new RocksNativeLibraryResource(); | |
7c673cae FG |
24 | |
25 | @Rule | |
26 | public TemporaryFolder dbFolder = new TemporaryFolder(); | |
27 | ||
11fdf7f2 TL |
28 | @Test |
29 | public void columnFamilyDescriptorName() throws RocksDBException { | |
30 | final byte[] cfName = "some_name".getBytes(UTF_8); | |
31 | ||
32 | try(final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions()) { | |
33 | final ColumnFamilyDescriptor cfDescriptor = | |
34 | new ColumnFamilyDescriptor(cfName, cfOptions); | |
35 | assertThat(cfDescriptor.getName()).isEqualTo(cfName); | |
36 | } | |
37 | } | |
38 | ||
39 | @Test | |
40 | public void columnFamilyDescriptorOptions() throws RocksDBException { | |
41 | final byte[] cfName = "some_name".getBytes(UTF_8); | |
42 | ||
43 | try(final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions() | |
44 | .setCompressionType(CompressionType.BZLIB2_COMPRESSION)) { | |
45 | final ColumnFamilyDescriptor cfDescriptor = | |
46 | new ColumnFamilyDescriptor(cfName, cfOptions); | |
47 | ||
48 | assertThat(cfDescriptor.getOptions().compressionType()) | |
49 | .isEqualTo(CompressionType.BZLIB2_COMPRESSION); | |
50 | } | |
51 | } | |
52 | ||
7c673cae FG |
53 | @Test |
54 | public void listColumnFamilies() throws RocksDBException { | |
55 | try (final Options options = new Options().setCreateIfMissing(true); | |
56 | final RocksDB db = RocksDB.open(options, | |
57 | dbFolder.getRoot().getAbsolutePath())) { | |
58 | // Test listColumnFamilies | |
59 | final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies(options, | |
60 | dbFolder.getRoot().getAbsolutePath()); | |
61 | assertThat(columnFamilyNames).isNotNull(); | |
62 | assertThat(columnFamilyNames.size()).isGreaterThan(0); | |
63 | assertThat(columnFamilyNames.size()).isEqualTo(1); | |
64 | assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); | |
65 | } | |
66 | } | |
67 | ||
68 | @Test | |
69 | public void defaultColumnFamily() throws RocksDBException { | |
70 | try (final Options options = new Options().setCreateIfMissing(true); | |
71 | final RocksDB db = RocksDB.open(options, | |
72 | dbFolder.getRoot().getAbsolutePath())) { | |
73 | final ColumnFamilyHandle cfh = db.getDefaultColumnFamily(); | |
74 | try { | |
75 | assertThat(cfh).isNotNull(); | |
76 | ||
11fdf7f2 TL |
77 | assertThat(cfh.getName()).isEqualTo("default".getBytes(UTF_8)); |
78 | assertThat(cfh.getID()).isEqualTo(0); | |
20effc67 | 79 | assertThat(cfh.getDescriptor().getName()).isEqualTo("default".getBytes(UTF_8)); |
11fdf7f2 | 80 | |
7c673cae FG |
81 | final byte[] key = "key".getBytes(); |
82 | final byte[] value = "value".getBytes(); | |
83 | ||
84 | db.put(cfh, key, value); | |
85 | ||
86 | final byte[] actualValue = db.get(cfh, key); | |
87 | ||
88 | assertThat(cfh).isNotNull(); | |
89 | assertThat(actualValue).isEqualTo(value); | |
90 | } finally { | |
91 | cfh.close(); | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | @Test | |
97 | public void createColumnFamily() throws RocksDBException { | |
11fdf7f2 TL |
98 | final byte[] cfName = "new_cf".getBytes(UTF_8); |
99 | final ColumnFamilyDescriptor cfDescriptor = new ColumnFamilyDescriptor(cfName, | |
100 | new ColumnFamilyOptions()); | |
101 | ||
7c673cae FG |
102 | try (final Options options = new Options().setCreateIfMissing(true); |
103 | final RocksDB db = RocksDB.open(options, | |
11fdf7f2 TL |
104 | dbFolder.getRoot().getAbsolutePath())) { |
105 | ||
106 | final ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily(cfDescriptor); | |
107 | ||
7c673cae | 108 | try { |
11fdf7f2 TL |
109 | assertThat(columnFamilyHandle.getName()).isEqualTo(cfName); |
110 | assertThat(columnFamilyHandle.getID()).isEqualTo(1); | |
111 | ||
112 | final ColumnFamilyDescriptor latestDescriptor = columnFamilyHandle.getDescriptor(); | |
113 | assertThat(latestDescriptor.getName()).isEqualTo(cfName); | |
114 | ||
7c673cae | 115 | final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies( |
11fdf7f2 | 116 | options, dbFolder.getRoot().getAbsolutePath()); |
7c673cae FG |
117 | assertThat(columnFamilyNames).isNotNull(); |
118 | assertThat(columnFamilyNames.size()).isGreaterThan(0); | |
119 | assertThat(columnFamilyNames.size()).isEqualTo(2); | |
120 | assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); | |
121 | assertThat(new String(columnFamilyNames.get(1))).isEqualTo("new_cf"); | |
122 | } finally { | |
123 | columnFamilyHandle.close(); | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | @Test | |
129 | public void openWithColumnFamilies() throws RocksDBException { | |
130 | final List<ColumnFamilyDescriptor> cfNames = Arrays.asList( | |
131 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
132 | new ColumnFamilyDescriptor("new_cf".getBytes()) | |
133 | ); | |
134 | ||
135 | final List<ColumnFamilyHandle> columnFamilyHandleList = | |
136 | new ArrayList<>(); | |
137 | ||
138 | // Test open database with column family names | |
139 | try (final DBOptions options = new DBOptions() | |
140 | .setCreateIfMissing(true) | |
141 | .setCreateMissingColumnFamilies(true); | |
142 | final RocksDB db = RocksDB.open(options, | |
143 | dbFolder.getRoot().getAbsolutePath(), cfNames, | |
144 | columnFamilyHandleList)) { | |
20effc67 TL |
145 | assertThat(columnFamilyHandleList.size()).isEqualTo(2); |
146 | db.put("dfkey1".getBytes(), "dfvalue".getBytes()); | |
147 | db.put(columnFamilyHandleList.get(0), "dfkey2".getBytes(), "dfvalue".getBytes()); | |
148 | db.put(columnFamilyHandleList.get(1), "newcfkey1".getBytes(), "newcfvalue".getBytes()); | |
149 | ||
150 | String retVal = new String(db.get(columnFamilyHandleList.get(1), "newcfkey1".getBytes())); | |
151 | assertThat(retVal).isEqualTo("newcfvalue"); | |
152 | assertThat((db.get(columnFamilyHandleList.get(1), "dfkey1".getBytes()))).isNull(); | |
153 | db.delete(columnFamilyHandleList.get(1), "newcfkey1".getBytes()); | |
154 | assertThat((db.get(columnFamilyHandleList.get(1), "newcfkey1".getBytes()))).isNull(); | |
155 | db.delete(columnFamilyHandleList.get(0), new WriteOptions(), "dfkey2".getBytes()); | |
156 | assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(), "dfkey2".getBytes())) | |
157 | .isNull(); | |
7c673cae FG |
158 | } |
159 | } | |
160 | ||
161 | @Test | |
162 | public void getWithOutValueAndCf() throws RocksDBException { | |
163 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
164 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); | |
165 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
166 | ||
167 | // Test open database with column family names | |
168 | try (final DBOptions options = new DBOptions() | |
169 | .setCreateIfMissing(true) | |
170 | .setCreateMissingColumnFamilies(true); | |
171 | final RocksDB db = RocksDB.open(options, | |
172 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
173 | columnFamilyHandleList)) { | |
20effc67 TL |
174 | db.put( |
175 | columnFamilyHandleList.get(0), new WriteOptions(), "key1".getBytes(), "value".getBytes()); | |
176 | db.put("key2".getBytes(), "12345678".getBytes()); | |
177 | final byte[] outValue = new byte[5]; | |
178 | // not found value | |
179 | int getResult = db.get("keyNotFound".getBytes(), outValue); | |
180 | assertThat(getResult).isEqualTo(RocksDB.NOT_FOUND); | |
181 | // found value which fits in outValue | |
182 | getResult = db.get(columnFamilyHandleList.get(0), "key1".getBytes(), outValue); | |
183 | assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); | |
184 | assertThat(outValue).isEqualTo("value".getBytes()); | |
185 | // found value which fits partially | |
186 | getResult = | |
187 | db.get(columnFamilyHandleList.get(0), new ReadOptions(), "key2".getBytes(), outValue); | |
188 | assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); | |
189 | assertThat(outValue).isEqualTo("12345".getBytes()); | |
7c673cae FG |
190 | } |
191 | } | |
192 | ||
193 | @Test | |
194 | public void createWriteDropColumnFamily() throws RocksDBException { | |
195 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
196 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
197 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
198 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
199 | try (final DBOptions options = new DBOptions() | |
200 | .setCreateIfMissing(true) | |
201 | .setCreateMissingColumnFamilies(true); | |
202 | final RocksDB db = RocksDB.open(options, | |
203 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
204 | columnFamilyHandleList)) { | |
20effc67 TL |
205 | ColumnFamilyHandle tmpColumnFamilyHandle; |
206 | tmpColumnFamilyHandle = db.createColumnFamily( | |
207 | new ColumnFamilyDescriptor("tmpCF".getBytes(), new ColumnFamilyOptions())); | |
208 | db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); | |
209 | db.dropColumnFamily(tmpColumnFamilyHandle); | |
210 | assertThat(tmpColumnFamilyHandle.isOwningHandle()).isTrue(); | |
7c673cae FG |
211 | } |
212 | } | |
213 | ||
494da23a TL |
214 | @Test |
215 | public void createWriteDropColumnFamilies() throws RocksDBException { | |
216 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
217 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
218 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
219 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
220 | try (final DBOptions options = new DBOptions() | |
221 | .setCreateIfMissing(true) | |
222 | .setCreateMissingColumnFamilies(true); | |
223 | final RocksDB db = RocksDB.open(options, | |
224 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
225 | columnFamilyHandleList)) { | |
226 | ColumnFamilyHandle tmpColumnFamilyHandle = null; | |
227 | ColumnFamilyHandle tmpColumnFamilyHandle2 = null; | |
20effc67 TL |
228 | tmpColumnFamilyHandle = db.createColumnFamily( |
229 | new ColumnFamilyDescriptor("tmpCF".getBytes(), new ColumnFamilyOptions())); | |
230 | tmpColumnFamilyHandle2 = db.createColumnFamily( | |
231 | new ColumnFamilyDescriptor("tmpCF2".getBytes(), new ColumnFamilyOptions())); | |
232 | db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); | |
233 | db.put(tmpColumnFamilyHandle2, "key".getBytes(), "value".getBytes()); | |
234 | db.dropColumnFamilies(Arrays.asList(tmpColumnFamilyHandle, tmpColumnFamilyHandle2)); | |
235 | assertThat(tmpColumnFamilyHandle.isOwningHandle()).isTrue(); | |
236 | assertThat(tmpColumnFamilyHandle2.isOwningHandle()).isTrue(); | |
494da23a TL |
237 | } |
238 | } | |
239 | ||
7c673cae FG |
240 | @Test |
241 | public void writeBatch() throws RocksDBException { | |
242 | try (final StringAppendOperator stringAppendOperator = new StringAppendOperator(); | |
243 | final ColumnFamilyOptions defaultCfOptions = new ColumnFamilyOptions() | |
244 | .setMergeOperator(stringAppendOperator)) { | |
245 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
246 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, | |
247 | defaultCfOptions), | |
248 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
249 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
250 | try (final DBOptions options = new DBOptions() | |
251 | .setCreateIfMissing(true) | |
252 | .setCreateMissingColumnFamilies(true); | |
253 | final RocksDB db = RocksDB.open(options, | |
254 | dbFolder.getRoot().getAbsolutePath(), | |
255 | cfDescriptors, columnFamilyHandleList); | |
256 | final WriteBatch writeBatch = new WriteBatch(); | |
257 | final WriteOptions writeOpt = new WriteOptions()) { | |
20effc67 TL |
258 | writeBatch.put("key".getBytes(), "value".getBytes()); |
259 | writeBatch.put(db.getDefaultColumnFamily(), "mergeKey".getBytes(), "merge".getBytes()); | |
260 | writeBatch.merge(db.getDefaultColumnFamily(), "mergeKey".getBytes(), "merge".getBytes()); | |
261 | writeBatch.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), "value".getBytes()); | |
262 | writeBatch.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), "value2".getBytes()); | |
263 | writeBatch.delete("xyz".getBytes()); | |
264 | writeBatch.delete(columnFamilyHandleList.get(1), "xyz".getBytes()); | |
265 | db.write(writeOpt, writeBatch); | |
266 | ||
267 | assertThat(db.get(columnFamilyHandleList.get(1), "xyz".getBytes()) == null); | |
268 | assertThat(new String(db.get(columnFamilyHandleList.get(1), "newcfkey".getBytes()))) | |
269 | .isEqualTo("value"); | |
270 | assertThat(new String(db.get(columnFamilyHandleList.get(1), "newcfkey2".getBytes()))) | |
271 | .isEqualTo("value2"); | |
272 | assertThat(new String(db.get("key".getBytes()))).isEqualTo("value"); | |
273 | // check if key is merged | |
274 | assertThat(new String(db.get(db.getDefaultColumnFamily(), "mergeKey".getBytes()))) | |
275 | .isEqualTo("merge,merge"); | |
7c673cae FG |
276 | } |
277 | } | |
278 | } | |
279 | ||
280 | @Test | |
281 | public void iteratorOnColumnFamily() throws RocksDBException { | |
282 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
283 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
284 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
285 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
286 | try (final DBOptions options = new DBOptions() | |
287 | .setCreateIfMissing(true) | |
288 | .setCreateMissingColumnFamilies(true); | |
289 | final RocksDB db = RocksDB.open(options, | |
290 | dbFolder.getRoot().getAbsolutePath(), | |
291 | cfDescriptors, columnFamilyHandleList)) { | |
20effc67 TL |
292 | db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), "value".getBytes()); |
293 | db.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), "value2".getBytes()); | |
294 | try (final RocksIterator rocksIterator = db.newIterator(columnFamilyHandleList.get(1))) { | |
295 | rocksIterator.seekToFirst(); | |
296 | Map<String, String> refMap = new HashMap<>(); | |
297 | refMap.put("newcfkey", "value"); | |
298 | refMap.put("newcfkey2", "value2"); | |
299 | int i = 0; | |
300 | while (rocksIterator.isValid()) { | |
301 | i++; | |
302 | assertThat(refMap.get(new String(rocksIterator.key()))) | |
303 | .isEqualTo(new String(rocksIterator.value())); | |
304 | rocksIterator.next(); | |
7c673cae | 305 | } |
20effc67 | 306 | assertThat(i).isEqualTo(2); |
7c673cae FG |
307 | } |
308 | } | |
309 | } | |
310 | ||
311 | @Test | |
312 | public void multiGet() throws RocksDBException { | |
313 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
314 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
315 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
316 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
317 | try (final DBOptions options = new DBOptions() | |
318 | .setCreateIfMissing(true) | |
319 | .setCreateMissingColumnFamilies(true); | |
320 | final RocksDB db = RocksDB.open(options, | |
321 | dbFolder.getRoot().getAbsolutePath(), | |
322 | cfDescriptors, columnFamilyHandleList)) { | |
20effc67 TL |
323 | db.put(columnFamilyHandleList.get(0), "key".getBytes(), "value".getBytes()); |
324 | db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), "value".getBytes()); | |
325 | ||
326 | final List<byte[]> keys = | |
327 | Arrays.asList(new byte[][] {"key".getBytes(), "newcfkey".getBytes()}); | |
328 | ||
329 | List<byte[]> retValues = db.multiGetAsList(columnFamilyHandleList, keys); | |
330 | assertThat(retValues.size()).isEqualTo(2); | |
331 | assertThat(new String(retValues.get(0))).isEqualTo("value"); | |
332 | assertThat(new String(retValues.get(1))).isEqualTo("value"); | |
333 | retValues = db.multiGetAsList(new ReadOptions(), columnFamilyHandleList, keys); | |
334 | assertThat(retValues.size()).isEqualTo(2); | |
335 | assertThat(new String(retValues.get(0))).isEqualTo("value"); | |
336 | assertThat(new String(retValues.get(1))).isEqualTo("value"); | |
7c673cae FG |
337 | } |
338 | } | |
339 | ||
494da23a TL |
340 | @Test |
341 | public void multiGetAsList() throws RocksDBException { | |
342 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
343 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
344 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
345 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
346 | try (final DBOptions options = new DBOptions() | |
347 | .setCreateIfMissing(true) | |
348 | .setCreateMissingColumnFamilies(true); | |
349 | final RocksDB db = RocksDB.open(options, | |
350 | dbFolder.getRoot().getAbsolutePath(), | |
351 | cfDescriptors, columnFamilyHandleList)) { | |
20effc67 TL |
352 | db.put(columnFamilyHandleList.get(0), "key".getBytes(), "value".getBytes()); |
353 | db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), "value".getBytes()); | |
354 | ||
355 | final List<byte[]> keys = | |
356 | Arrays.asList(new byte[][] {"key".getBytes(), "newcfkey".getBytes()}); | |
357 | List<byte[]> retValues = db.multiGetAsList(columnFamilyHandleList, keys); | |
358 | assertThat(retValues.size()).isEqualTo(2); | |
359 | assertThat(new String(retValues.get(0))).isEqualTo("value"); | |
360 | assertThat(new String(retValues.get(1))).isEqualTo("value"); | |
361 | retValues = db.multiGetAsList(new ReadOptions(), columnFamilyHandleList, keys); | |
362 | assertThat(retValues.size()).isEqualTo(2); | |
363 | assertThat(new String(retValues.get(0))).isEqualTo("value"); | |
364 | assertThat(new String(retValues.get(1))).isEqualTo("value"); | |
494da23a TL |
365 | } |
366 | } | |
367 | ||
7c673cae FG |
368 | @Test |
369 | public void properties() throws RocksDBException { | |
370 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
371 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
372 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
373 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
374 | try (final DBOptions options = new DBOptions() | |
375 | .setCreateIfMissing(true) | |
376 | .setCreateMissingColumnFamilies(true); | |
377 | final RocksDB db = RocksDB.open(options, | |
378 | dbFolder.getRoot().getAbsolutePath(), | |
379 | cfDescriptors, columnFamilyHandleList)) { | |
20effc67 TL |
380 | assertThat(db.getProperty("rocksdb.estimate-num-keys")).isNotNull(); |
381 | assertThat(db.getLongProperty(columnFamilyHandleList.get(0), "rocksdb.estimate-num-keys")) | |
382 | .isGreaterThanOrEqualTo(0); | |
383 | assertThat(db.getProperty("rocksdb.stats")).isNotNull(); | |
384 | assertThat(db.getProperty(columnFamilyHandleList.get(0), "rocksdb.sstables")).isNotNull(); | |
385 | assertThat(db.getProperty(columnFamilyHandleList.get(1), "rocksdb.estimate-num-keys")) | |
386 | .isNotNull(); | |
387 | assertThat(db.getProperty(columnFamilyHandleList.get(1), "rocksdb.stats")).isNotNull(); | |
388 | assertThat(db.getProperty(columnFamilyHandleList.get(1), "rocksdb.sstables")).isNotNull(); | |
389 | assertThat(db.getAggregatedLongProperty("rocksdb.estimate-num-keys")).isNotNull(); | |
390 | assertThat(db.getAggregatedLongProperty("rocksdb.estimate-num-keys")) | |
391 | .isGreaterThanOrEqualTo(0); | |
7c673cae FG |
392 | } |
393 | } | |
394 | ||
395 | ||
396 | @Test | |
397 | public void iterators() throws RocksDBException { | |
398 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
399 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
400 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
401 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
402 | try (final DBOptions options = new DBOptions() | |
403 | .setCreateIfMissing(true) | |
404 | .setCreateMissingColumnFamilies(true); | |
405 | final RocksDB db = RocksDB.open(options, | |
406 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
407 | columnFamilyHandleList)) { | |
408 | List<RocksIterator> iterators = null; | |
409 | try { | |
410 | iterators = db.newIterators(columnFamilyHandleList); | |
411 | assertThat(iterators.size()).isEqualTo(2); | |
412 | RocksIterator iter = iterators.get(0); | |
413 | iter.seekToFirst(); | |
414 | final Map<String, String> defRefMap = new HashMap<>(); | |
415 | defRefMap.put("dfkey1", "dfvalue"); | |
416 | defRefMap.put("key", "value"); | |
417 | while (iter.isValid()) { | |
418 | assertThat(defRefMap.get(new String(iter.key()))). | |
419 | isEqualTo(new String(iter.value())); | |
420 | iter.next(); | |
421 | } | |
422 | // iterate over new_cf key/value pairs | |
423 | final Map<String, String> cfRefMap = new HashMap<>(); | |
424 | cfRefMap.put("newcfkey", "value"); | |
425 | cfRefMap.put("newcfkey2", "value2"); | |
426 | iter = iterators.get(1); | |
427 | iter.seekToFirst(); | |
428 | while (iter.isValid()) { | |
429 | assertThat(cfRefMap.get(new String(iter.key()))). | |
430 | isEqualTo(new String(iter.value())); | |
431 | iter.next(); | |
432 | } | |
433 | } finally { | |
434 | if (iterators != null) { | |
435 | for (final RocksIterator rocksIterator : iterators) { | |
436 | rocksIterator.close(); | |
437 | } | |
438 | } | |
7c673cae FG |
439 | } |
440 | } | |
441 | } | |
442 | ||
443 | @Test(expected = RocksDBException.class) | |
444 | public void failPutDisposedCF() throws RocksDBException { | |
445 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
446 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
447 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
448 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
449 | try (final DBOptions options = new DBOptions() | |
450 | .setCreateIfMissing(true); | |
451 | final RocksDB db = RocksDB.open(options, | |
452 | dbFolder.getRoot().getAbsolutePath(), | |
453 | cfDescriptors, columnFamilyHandleList)) { | |
20effc67 TL |
454 | db.dropColumnFamily(columnFamilyHandleList.get(1)); |
455 | db.put(columnFamilyHandleList.get(1), "key".getBytes(), "value".getBytes()); | |
7c673cae FG |
456 | } |
457 | } | |
458 | ||
459 | @Test(expected = RocksDBException.class) | |
460 | public void failRemoveDisposedCF() throws RocksDBException { | |
461 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
462 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
463 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
464 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
465 | try (final DBOptions options = new DBOptions() | |
466 | .setCreateIfMissing(true); | |
467 | final RocksDB db = RocksDB.open(options, | |
468 | dbFolder.getRoot().getAbsolutePath(), | |
469 | cfDescriptors, columnFamilyHandleList)) { | |
20effc67 TL |
470 | db.dropColumnFamily(columnFamilyHandleList.get(1)); |
471 | db.delete(columnFamilyHandleList.get(1), "key".getBytes()); | |
7c673cae FG |
472 | } |
473 | } | |
474 | ||
475 | @Test(expected = RocksDBException.class) | |
476 | public void failGetDisposedCF() throws RocksDBException { | |
477 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
478 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
479 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
480 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
481 | try (final DBOptions options = new DBOptions() | |
482 | .setCreateIfMissing(true); | |
483 | final RocksDB db = RocksDB.open(options, | |
484 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
485 | columnFamilyHandleList)) { | |
20effc67 TL |
486 | db.dropColumnFamily(columnFamilyHandleList.get(1)); |
487 | db.get(columnFamilyHandleList.get(1), "key".getBytes()); | |
7c673cae FG |
488 | } |
489 | } | |
490 | ||
491 | @Test(expected = RocksDBException.class) | |
492 | public void failMultiGetWithoutCorrectNumberOfCF() throws RocksDBException { | |
493 | final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( | |
494 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
495 | new ColumnFamilyDescriptor("new_cf".getBytes())); | |
496 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
497 | try (final DBOptions options = new DBOptions() | |
498 | .setCreateIfMissing(true); | |
499 | final RocksDB db = RocksDB.open(options, | |
500 | dbFolder.getRoot().getAbsolutePath(), cfDescriptors, | |
501 | columnFamilyHandleList)) { | |
20effc67 TL |
502 | final List<byte[]> keys = new ArrayList<>(); |
503 | keys.add("key".getBytes()); | |
504 | keys.add("newcfkey".getBytes()); | |
505 | final List<ColumnFamilyHandle> cfCustomList = new ArrayList<>(); | |
506 | db.multiGetAsList(cfCustomList, keys); | |
7c673cae FG |
507 | } |
508 | } | |
509 | ||
510 | @Test | |
511 | public void testByteCreateFolumnFamily() throws RocksDBException { | |
512 | ||
513 | try (final Options options = new Options().setCreateIfMissing(true); | |
514 | final RocksDB db = RocksDB.open(options, | |
515 | dbFolder.getRoot().getAbsolutePath()) | |
516 | ) { | |
517 | final byte[] b0 = new byte[]{(byte) 0x00}; | |
518 | final byte[] b1 = new byte[]{(byte) 0x01}; | |
519 | final byte[] b2 = new byte[]{(byte) 0x02}; | |
20effc67 TL |
520 | db.createColumnFamily(new ColumnFamilyDescriptor(b0)); |
521 | db.createColumnFamily(new ColumnFamilyDescriptor(b1)); | |
522 | final List<byte[]> families = | |
523 | RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); | |
524 | assertThat(families).contains("default".getBytes(), b0, b1); | |
525 | db.createColumnFamily(new ColumnFamilyDescriptor(b2)); | |
7c673cae FG |
526 | } |
527 | } | |
528 | ||
529 | @Test | |
530 | public void testCFNamesWithZeroBytes() throws RocksDBException { | |
531 | ColumnFamilyHandle cf1 = null, cf2 = null; | |
532 | try (final Options options = new Options().setCreateIfMissing(true); | |
533 | final RocksDB db = RocksDB.open(options, | |
534 | dbFolder.getRoot().getAbsolutePath()); | |
535 | ) { | |
20effc67 TL |
536 | final byte[] b0 = new byte[] {0, 0}; |
537 | final byte[] b1 = new byte[] {0, 1}; | |
538 | cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0)); | |
539 | cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1)); | |
540 | final List<byte[]> families = | |
541 | RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); | |
542 | assertThat(families).contains("default".getBytes(), b0, b1); | |
7c673cae FG |
543 | } |
544 | } | |
545 | ||
546 | @Test | |
547 | public void testCFNameSimplifiedChinese() throws RocksDBException { | |
548 | ColumnFamilyHandle columnFamilyHandle = null; | |
549 | try (final Options options = new Options().setCreateIfMissing(true); | |
550 | final RocksDB db = RocksDB.open(options, | |
551 | dbFolder.getRoot().getAbsolutePath()); | |
552 | ) { | |
20effc67 TL |
553 | final String simplifiedChinese = "\u7b80\u4f53\u5b57"; |
554 | columnFamilyHandle = | |
555 | db.createColumnFamily(new ColumnFamilyDescriptor(simplifiedChinese.getBytes())); | |
556 | ||
557 | final List<byte[]> families = | |
558 | RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); | |
559 | assertThat(families).contains("default".getBytes(), simplifiedChinese.getBytes()); | |
560 | } | |
561 | } | |
562 | ||
563 | @Test | |
564 | public void testDestroyColumnFamilyHandle() throws RocksDBException { | |
565 | try (final Options options = new Options().setCreateIfMissing(true); | |
566 | final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath());) { | |
567 | final byte[] name1 = "cf1".getBytes(); | |
568 | final byte[] name2 = "cf2".getBytes(); | |
569 | final ColumnFamilyDescriptor desc1 = new ColumnFamilyDescriptor(name1); | |
570 | final ColumnFamilyDescriptor desc2 = new ColumnFamilyDescriptor(name2); | |
571 | final ColumnFamilyHandle cf1 = db.createColumnFamily(desc1); | |
572 | final ColumnFamilyHandle cf2 = db.createColumnFamily(desc2); | |
573 | assertTrue(cf1.isOwningHandle()); | |
574 | assertTrue(cf2.isOwningHandle()); | |
575 | assertFalse(cf1.isDefaultColumnFamily()); | |
576 | db.destroyColumnFamilyHandle(cf1); | |
577 | // At this point cf1 should not be used! | |
578 | assertFalse(cf1.isOwningHandle()); | |
579 | assertTrue(cf2.isOwningHandle()); | |
580 | } | |
581 | } | |
7c673cae | 582 | } |