Skip to content

Commit f868dcb

Browse files
committed
Support for adding TTL-ed column family
Summary: This enables user to add a TTL column family to normal DB. Next step should be to expand StackableDB and create StackableColumnFamily, such that users can for example add geo-spatial column families to normal DB. Test Plan: added a test Reviewers: dhruba, haobo, ljin Reviewed By: haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D18201
1 parent 72ff275 commit f868dcb

File tree

8 files changed

+282
-231
lines changed

8 files changed

+282
-231
lines changed

include/utilities/db_ttl.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2013, 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.
5+
6+
#pragma once
7+
#ifndef ROCKSDB_LITE
8+
9+
#include <string>
10+
#include <vector>
11+
12+
#include "utilities/stackable_db.h"
13+
#include "rocksdb/db.h"
14+
15+
namespace rocksdb {
16+
17+
// Database with TTL support.
18+
//
19+
// USE-CASES:
20+
// This API should be used to open the db when key-values inserted are
21+
// meant to be removed from the db in a non-strict 'ttl' amount of time
22+
// Therefore, this guarantees that key-values inserted will remain in the
23+
// db for >= ttl amount of time and the db will make efforts to remove the
24+
// key-values as soon as possible after ttl seconds of their insertion.
25+
//
26+
// BEHAVIOUR:
27+
// TTL is accepted in seconds
28+
// (int32_t)Timestamp(creation) is suffixed to values in Put internally
29+
// Expired TTL values deleted in compaction only:(Timestamp+ttl<time_now)
30+
// Get/Iterator may return expired entries(compaction not run on them yet)
31+
// Different TTL may be used during different Opens
32+
// Example: Open1 at t=0 with ttl=4 and insert k1,k2, close at t=2
33+
// Open2 at t=3 with ttl=5. Now k1,k2 should be deleted at t>=5
34+
// read_only=true opens in the usual read-only mode. Compactions will not be
35+
// triggered(neither manual nor automatic), so no expired entries removed
36+
//
37+
// CONSTRAINTS:
38+
// Not specifying/passing or non-positive TTL behaves like TTL = infinity
39+
//
40+
// !!!WARNING!!!:
41+
// Calling DB::Open directly to re-open a db created by this API will get
42+
// corrupt values(timestamp suffixed) and no ttl effect will be there
43+
// during the second Open, so use this API consistently to open the db
44+
// Be careful when passing ttl with a small positive value because the
45+
// whole database may be deleted in a small amount of time
46+
47+
class DBWithTTL : public StackableDB {
48+
public:
49+
virtual Status CreateColumnFamilyWithTtl(
50+
const ColumnFamilyOptions& options, const std::string& column_family_name,
51+
ColumnFamilyHandle** handle, int ttl) = 0;
52+
53+
static Status Open(const Options& options, const std::string& dbname,
54+
DBWithTTL** dbptr, int32_t ttl = 0,
55+
bool read_only = false);
56+
57+
static Status Open(const DBOptions& db_options, const std::string& dbname,
58+
const std::vector<ColumnFamilyDescriptor>& column_families,
59+
std::vector<ColumnFamilyHandle*>* handles,
60+
DBWithTTL** dbptr, std::vector<int32_t> ttls,
61+
bool read_only = false);
62+
63+
protected:
64+
explicit DBWithTTL(DB* db) : StackableDB(db) {}
65+
};
66+
67+
} // namespace rocksdb
68+
#endif // ROCKSDB_LITE

include/utilities/stackable_db.h

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class StackableDB : public DB {
2121
return db_;
2222
}
2323

24+
virtual Status CreateColumnFamily(const ColumnFamilyOptions& options,
25+
const std::string& column_family_name,
26+
ColumnFamilyHandle** handle) {
27+
return db_->CreateColumnFamily(options, column_family_name, handle);
28+
}
29+
30+
virtual Status DropColumnFamily(ColumnFamilyHandle* column_family) {
31+
return db_->DropColumnFamily(column_family);
32+
}
33+
2434
using DB::Put;
2535
virtual Status Put(const WriteOptions& options,
2636
ColumnFamilyHandle* column_family, const Slice& key,

include/utilities/utility_db.h

+11-44
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,22 @@
88
#include <string>
99

1010
#include "utilities/stackable_db.h"
11+
#include "utilities/db_ttl.h"
1112
#include "rocksdb/db.h"
1213

1314
namespace rocksdb {
1415

15-
// This class contains APIs to open rocksdb with specific support eg. TTL
16+
// Please don't use this class. It's deprecated
1617
class UtilityDB {
17-
18-
public:
19-
// Open the database with TTL support.
20-
//
21-
// USE-CASES:
22-
// This API should be used to open the db when key-values inserted are
23-
// meant to be removed from the db in a non-strict 'ttl' amount of time
24-
// Therefore, this guarantees that key-values inserted will remain in the
25-
// db for >= ttl amount of time and the db will make efforts to remove the
26-
// key-values as soon as possible after ttl seconds of their insertion.
27-
//
28-
// BEHAVIOUR:
29-
// TTL is accepted in seconds
30-
// (int32_t)Timestamp(creation) is suffixed to values in Put internally
31-
// Expired TTL values deleted in compaction only:(Timestamp+ttl<time_now)
32-
// Get/Iterator may return expired entries(compaction not run on them yet)
33-
// Different TTL may be used during different Opens
34-
// Example: Open1 at t=0 with ttl=4 and insert k1,k2, close at t=2
35-
// Open2 at t=3 with ttl=5. Now k1,k2 should be deleted at t>=5
36-
// read_only=true opens in the usual read-only mode. Compactions will not be
37-
// triggered(neither manual nor automatic), so no expired entries removed
38-
//
39-
// CONSTRAINTS:
40-
// Not specifying/passing or non-positive TTL behaves like TTL = infinity
41-
//
42-
// !!!WARNING!!!:
43-
// Calling DB::Open directly to re-open a db created by this API will get
44-
// corrupt values(timestamp suffixed) and no ttl effect will be there
45-
// during the second Open, so use this API consistently to open the db
46-
// Be careful when passing ttl with a small positive value because the
47-
// whole database may be deleted in a small amount of time
48-
static Status OpenTtlDB(const Options& options,
49-
const std::string& name,
50-
StackableDB** dbptr,
51-
int32_t ttl = 0,
52-
bool read_only = false);
53-
54-
// OpenTtlDB with column family support
55-
static Status OpenTtlDB(
56-
const DBOptions& db_options, const std::string& name,
57-
const std::vector<ColumnFamilyDescriptor>& column_families,
58-
std::vector<ColumnFamilyHandle*>* handles, StackableDB** dbptr,
59-
std::vector<int32_t> ttls, bool read_only = false);
18+
public:
19+
// This function is here only for backwards compatibility. Please use the
20+
// functions defined in DBWithTTl (utilities/db_ttl.h)
21+
// (deprecated)
22+
__attribute__((deprecated)) static Status OpenTtlDB(const Options& options,
23+
const std::string& name,
24+
StackableDB** dbptr,
25+
int32_t ttl = 0,
26+
bool read_only = false);
6027
};
6128

6229
} // namespace rocksdb

util/ldb_cmd.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "rocksdb/write_batch.h"
1515
#include "rocksdb/cache.h"
1616
#include "util/coding.h"
17+
#include "utilities/ttl/db_ttl_impl.h"
1718

1819
#include <ctime>
1920
#include <dirent.h>
@@ -909,11 +910,11 @@ void DBDumperCommand::DoCommand() {
909910
int max_keys = max_keys_;
910911
int ttl_start;
911912
if (!ParseIntOption(option_map_, ARG_TTL_START, ttl_start, exec_state_)) {
912-
ttl_start = DBWithTTL::kMinTimestamp; // TTL introduction time
913+
ttl_start = DBWithTTLImpl::kMinTimestamp; // TTL introduction time
913914
}
914915
int ttl_end;
915916
if (!ParseIntOption(option_map_, ARG_TTL_END, ttl_end, exec_state_)) {
916-
ttl_end = DBWithTTL::kMaxTimestamp; // Max time allowed by TTL feature
917+
ttl_end = DBWithTTLImpl::kMaxTimestamp; // Max time allowed by TTL feature
917918
}
918919
if (ttl_end < ttl_start) {
919920
fprintf(stderr, "Error: End time can't be less than start time\n");
@@ -1600,11 +1601,11 @@ void ScanCommand::DoCommand() {
16001601
}
16011602
int ttl_start;
16021603
if (!ParseIntOption(option_map_, ARG_TTL_START, ttl_start, exec_state_)) {
1603-
ttl_start = DBWithTTL::kMinTimestamp; // TTL introduction time
1604+
ttl_start = DBWithTTLImpl::kMinTimestamp; // TTL introduction time
16041605
}
16051606
int ttl_end;
16061607
if (!ParseIntOption(option_map_, ARG_TTL_END, ttl_end, exec_state_)) {
1607-
ttl_end = DBWithTTL::kMaxTimestamp; // Max time allowed by TTL feature
1608+
ttl_end = DBWithTTLImpl::kMaxTimestamp; // Max time allowed by TTL feature
16081609
}
16091610
if (ttl_end < ttl_start) {
16101611
fprintf(stderr, "Error: End time can't be less than start time\n");

util/ldb_cmd.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "util/logging.h"
2020
#include "util/ldb_cmd_execute_result.h"
2121
#include "util/string_util.h"
22-
#include "utilities/utility_db.h"
23-
#include "utilities/ttl/db_ttl.h"
22+
#include "utilities/db_ttl.h"
23+
#include "utilities/ttl/db_ttl_impl.h"
2424

2525
using std::string;
2626
using std::map;
@@ -149,7 +149,7 @@ class LDBCommand {
149149
LDBCommandExecuteResult exec_state_;
150150
string db_path_;
151151
DB* db_;
152-
StackableDB* sdb_;
152+
DBWithTTL* db_ttl_;
153153

154154
/**
155155
* true implies that this command can work if the db is opened in read-only
@@ -217,11 +217,11 @@ class LDBCommand {
217217
Status st;
218218
if (is_db_ttl_) {
219219
if (is_read_only_) {
220-
st = UtilityDB::OpenTtlDB(opt, db_path_, &sdb_, 0, true);
220+
st = DBWithTTL::Open(opt, db_path_, &db_ttl_, 0, true);
221221
} else {
222-
st = UtilityDB::OpenTtlDB(opt, db_path_, &sdb_);
222+
st = DBWithTTL::Open(opt, db_path_, &db_ttl_);
223223
}
224-
db_ = sdb_;
224+
db_ = db_ttl_;
225225
} else if (is_read_only_) {
226226
st = DB::OpenForReadOnly(opt, db_path_, &db_);
227227
} else {

0 commit comments

Comments
 (0)