1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
8 import org
.junit
.ClassRule
;
10 import org
.junit
.Test
;
11 import org
.junit
.rules
.TemporaryFolder
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
16 import java
.util
.concurrent
.TimeUnit
;
18 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
20 public class TtlDBTest
{
23 public static final RocksMemoryResource rocksMemoryResource
=
24 new RocksMemoryResource();
27 public TemporaryFolder dbFolder
= new TemporaryFolder();
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();
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);
49 assertThat(ttlDB
.get("key".getBytes())).isNull();
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())
60 final List
<Integer
> ttlValues
= Arrays
.asList(0, 1);
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)) {
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(),
75 assertThat(ttlDB
.get(columnFamilyHandleList
.get(1),
76 "key".getBytes())).isEqualTo("value".getBytes());
77 TimeUnit
.SECONDS
.sleep(2);
80 ttlDB
.compactRange(columnFamilyHandleList
.get(1));
82 assertThat(ttlDB
.get("key".getBytes())).isNotNull();
83 assertThat(ttlDB
.get(columnFamilyHandleList
.get(1),
84 "key".getBytes())).isNull();
86 for (final ColumnFamilyHandle columnFamilyHandle
:
87 columnFamilyHandleList
) {
88 columnFamilyHandle
.close();
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(),
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();