]>
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 | ||
8 | import org.junit.ClassRule; | |
9 | import org.junit.Rule; | |
10 | import org.junit.Test; | |
11 | import org.junit.rules.TemporaryFolder; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.Arrays; | |
15 | import java.util.List; | |
16 | import java.util.concurrent.TimeUnit; | |
17 | ||
18 | import static org.assertj.core.api.Assertions.assertThat; | |
19 | ||
20 | public class TtlDBTest { | |
21 | ||
22 | @ClassRule | |
23 | public static final RocksMemoryResource rocksMemoryResource = | |
24 | new RocksMemoryResource(); | |
25 | ||
26 | @Rule | |
27 | public TemporaryFolder dbFolder = new TemporaryFolder(); | |
28 | ||
29 | @Test | |
30 | public void ttlDBOpen() throws RocksDBException, InterruptedException { | |
31 | try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0); | |
32 | final TtlDB ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath())) { | |
33 | ttlDB.put("key".getBytes(), "value".getBytes()); | |
34 | assertThat(ttlDB.get("key".getBytes())). | |
35 | isEqualTo("value".getBytes()); | |
36 | assertThat(ttlDB.get("key".getBytes())).isNotNull(); | |
37 | } | |
38 | } | |
39 | ||
40 | @Test | |
41 | public void ttlDBOpenWithTtl() throws RocksDBException, InterruptedException { | |
42 | try (final Options options = new Options().setCreateIfMissing(true).setMaxCompactionBytes(0); | |
43 | final TtlDB ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath(), 1, false);) { | |
44 | ttlDB.put("key".getBytes(), "value".getBytes()); | |
45 | assertThat(ttlDB.get("key".getBytes())). | |
46 | isEqualTo("value".getBytes()); | |
47 | TimeUnit.SECONDS.sleep(2); | |
48 | ttlDB.compactRange(); | |
49 | assertThat(ttlDB.get("key".getBytes())).isNull(); | |
50 | } | |
51 | } | |
52 | ||
53 | @Test | |
54 | public void ttlDbOpenWithColumnFamilies() throws RocksDBException, | |
55 | InterruptedException { | |
56 | final List<ColumnFamilyDescriptor> cfNames = Arrays.asList( | |
57 | new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), | |
58 | new ColumnFamilyDescriptor("new_cf".getBytes()) | |
59 | ); | |
60 | final List<Integer> ttlValues = Arrays.asList(0, 1); | |
61 | ||
62 | final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); | |
63 | try (final DBOptions dbOptions = new DBOptions() | |
64 | .setCreateMissingColumnFamilies(true) | |
65 | .setCreateIfMissing(true); | |
66 | final TtlDB ttlDB = TtlDB.open(dbOptions, | |
67 | dbFolder.getRoot().getAbsolutePath(), cfNames, | |
68 | columnFamilyHandleList, ttlValues, false)) { | |
69 | try { | |
70 | ttlDB.put("key".getBytes(), "value".getBytes()); | |
71 | assertThat(ttlDB.get("key".getBytes())). | |
72 | isEqualTo("value".getBytes()); | |
73 | ttlDB.put(columnFamilyHandleList.get(1), "key".getBytes(), | |
74 | "value".getBytes()); | |
75 | assertThat(ttlDB.get(columnFamilyHandleList.get(1), | |
76 | "key".getBytes())).isEqualTo("value".getBytes()); | |
77 | TimeUnit.SECONDS.sleep(2); | |
78 | ||
79 | ttlDB.compactRange(); | |
80 | ttlDB.compactRange(columnFamilyHandleList.get(1)); | |
81 | ||
82 | assertThat(ttlDB.get("key".getBytes())).isNotNull(); | |
83 | assertThat(ttlDB.get(columnFamilyHandleList.get(1), | |
84 | "key".getBytes())).isNull(); | |
85 | } finally { | |
86 | for (final ColumnFamilyHandle columnFamilyHandle : | |
87 | columnFamilyHandleList) { | |
88 | columnFamilyHandle.close(); | |
89 | } | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | @Test | |
95 | public void createTtlColumnFamily() throws RocksDBException, | |
96 | InterruptedException { | |
97 | try (final Options options = new Options().setCreateIfMissing(true); | |
98 | final TtlDB ttlDB = TtlDB.open(options, | |
99 | dbFolder.getRoot().getAbsolutePath()); | |
100 | final ColumnFamilyHandle columnFamilyHandle = | |
101 | ttlDB.createColumnFamilyWithTtl( | |
102 | new ColumnFamilyDescriptor("new_cf".getBytes()), 1)) { | |
103 | ttlDB.put(columnFamilyHandle, "key".getBytes(), | |
104 | "value".getBytes()); | |
105 | assertThat(ttlDB.get(columnFamilyHandle, "key".getBytes())). | |
106 | isEqualTo("value".getBytes()); | |
107 | TimeUnit.SECONDS.sleep(2); | |
108 | ttlDB.compactRange(columnFamilyHandle); | |
109 | assertThat(ttlDB.get(columnFamilyHandle, "key".getBytes())).isNull(); | |
110 | } | |
111 | } | |
112 | } |