2019-04-18 19:51:19 +02:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2014-04-15 22:39:26 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
#include "utilities/ttl/db_ttl_impl.h"
|
|
|
|
|
2014-04-22 20:27:33 +02:00
|
|
|
#include "db/write_batch_internal.h"
|
2019-05-30 05:44:08 +02:00
|
|
|
#include "file/filename.h"
|
2015-07-15 23:51:51 +02:00
|
|
|
#include "rocksdb/convenience.h"
|
2014-04-29 05:34:20 +02:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
2015-07-15 23:51:51 +02:00
|
|
|
#include "rocksdb/utilities/db_ttl.h"
|
|
|
|
#include "util/coding.h"
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
|
2014-05-02 16:13:51 +02:00
|
|
|
void DBWithTTLImpl::SanitizeOptions(int32_t ttl, ColumnFamilyOptions* options,
|
|
|
|
Env* env) {
|
2013-12-07 01:10:43 +01:00
|
|
|
if (options->compaction_filter) {
|
|
|
|
options->compaction_filter =
|
2014-05-02 16:13:51 +02:00
|
|
|
new TtlCompactionFilter(ttl, env, options->compaction_filter);
|
2013-08-13 19:56:20 +02:00
|
|
|
} else {
|
2013-12-07 01:10:43 +01:00
|
|
|
options->compaction_filter_factory =
|
|
|
|
std::shared_ptr<CompactionFilterFactory>(new TtlCompactionFilterFactory(
|
2014-05-02 16:13:51 +02:00
|
|
|
ttl, env, options->compaction_filter_factory));
|
2013-08-13 19:56:20 +02:00
|
|
|
}
|
2013-08-03 09:40:22 +02:00
|
|
|
|
2013-12-07 01:10:43 +01:00
|
|
|
if (options->merge_operator) {
|
|
|
|
options->merge_operator.reset(
|
2014-05-02 16:13:51 +02:00
|
|
|
new TtlMergeOperator(options->merge_operator, env));
|
2013-05-10 23:19:47 +02:00
|
|
|
}
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
// Open the db inside DBWithTTLImpl because options needs pointer to its ttl
|
2019-06-20 22:04:13 +02:00
|
|
|
DBWithTTLImpl::DBWithTTLImpl(DB* db) : DBWithTTL(db), closed_(false) {}
|
2014-04-29 05:34:20 +02:00
|
|
|
|
2015-05-06 01:54:47 +02:00
|
|
|
DBWithTTLImpl::~DBWithTTLImpl() {
|
2019-06-20 22:04:13 +02:00
|
|
|
if (!closed_) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBWithTTLImpl::Close() {
|
|
|
|
Status ret = Status::OK();
|
|
|
|
if (!closed_) {
|
|
|
|
Options default_options = GetOptions();
|
|
|
|
// Need to stop background compaction before getting rid of the filter
|
|
|
|
CancelAllBackgroundWork(db_, /* wait = */ true);
|
|
|
|
ret = db_->Close();
|
|
|
|
delete default_options.compaction_filter;
|
|
|
|
closed_ = true;
|
|
|
|
}
|
|
|
|
return ret;
|
2015-05-06 01:54:47 +02:00
|
|
|
}
|
2013-12-07 01:10:43 +01:00
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status UtilityDB::OpenTtlDB(const Options& options, const std::string& dbname,
|
|
|
|
StackableDB** dbptr, int32_t ttl, bool read_only) {
|
|
|
|
DBWithTTL* db;
|
|
|
|
Status s = DBWithTTL::Open(options, dbname, &db, ttl, read_only);
|
|
|
|
if (s.ok()) {
|
|
|
|
*dbptr = db;
|
|
|
|
} else {
|
|
|
|
*dbptr = nullptr;
|
|
|
|
}
|
|
|
|
return s;
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTL::Open(const Options& options, const std::string& dbname,
|
|
|
|
DBWithTTL** dbptr, int32_t ttl, bool read_only) {
|
2014-04-22 20:27:33 +02:00
|
|
|
|
|
|
|
DBOptions db_options(options);
|
|
|
|
ColumnFamilyOptions cf_options(options);
|
|
|
|
std::vector<ColumnFamilyDescriptor> column_families;
|
|
|
|
column_families.push_back(
|
|
|
|
ColumnFamilyDescriptor(kDefaultColumnFamilyName, cf_options));
|
|
|
|
std::vector<ColumnFamilyHandle*> handles;
|
2014-04-29 05:34:20 +02:00
|
|
|
Status s = DBWithTTL::Open(db_options, dbname, column_families, &handles,
|
|
|
|
dbptr, {ttl}, read_only);
|
2014-04-22 20:27:33 +02:00
|
|
|
if (s.ok()) {
|
|
|
|
assert(handles.size() == 1);
|
|
|
|
// i can delete the handle since DBImpl is always holding a reference to
|
|
|
|
// default column family
|
|
|
|
delete handles[0];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTL::Open(
|
2014-04-22 20:27:33 +02:00
|
|
|
const DBOptions& db_options, const std::string& dbname,
|
|
|
|
const std::vector<ColumnFamilyDescriptor>& column_families,
|
2014-04-29 05:34:20 +02:00
|
|
|
std::vector<ColumnFamilyHandle*>* handles, DBWithTTL** dbptr,
|
2020-08-25 01:22:50 +02:00
|
|
|
const std::vector<int32_t>& ttls, bool read_only) {
|
2014-04-22 20:27:33 +02:00
|
|
|
if (ttls.size() != column_families.size()) {
|
|
|
|
return Status::InvalidArgument(
|
|
|
|
"ttls size has to be the same as number of column families");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ColumnFamilyDescriptor> column_families_sanitized =
|
|
|
|
column_families;
|
|
|
|
for (size_t i = 0; i < column_families_sanitized.size(); ++i) {
|
2014-05-02 16:13:51 +02:00
|
|
|
DBWithTTLImpl::SanitizeOptions(
|
|
|
|
ttls[i], &column_families_sanitized[i].options,
|
|
|
|
db_options.env == nullptr ? Env::Default() : db_options.env);
|
2014-04-22 20:27:33 +02:00
|
|
|
}
|
2013-12-07 01:10:43 +01:00
|
|
|
DB* db;
|
|
|
|
|
2014-04-22 20:27:33 +02:00
|
|
|
Status st;
|
2013-12-07 01:10:43 +01:00
|
|
|
if (read_only) {
|
2014-04-22 20:27:33 +02:00
|
|
|
st = DB::OpenForReadOnly(db_options, dbname, column_families_sanitized,
|
|
|
|
handles, &db);
|
2013-12-07 01:10:43 +01:00
|
|
|
} else {
|
2014-04-22 20:27:33 +02:00
|
|
|
st = DB::Open(db_options, dbname, column_families_sanitized, handles, &db);
|
2013-12-07 01:10:43 +01:00
|
|
|
}
|
|
|
|
if (st.ok()) {
|
2014-04-29 05:34:20 +02:00
|
|
|
*dbptr = new DBWithTTLImpl(db);
|
2013-12-07 01:10:43 +01:00
|
|
|
} else {
|
2014-04-22 20:27:33 +02:00
|
|
|
*dbptr = nullptr;
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTLImpl::CreateColumnFamilyWithTtl(
|
|
|
|
const ColumnFamilyOptions& options, const std::string& column_family_name,
|
|
|
|
ColumnFamilyHandle** handle, int ttl) {
|
|
|
|
ColumnFamilyOptions sanitized_options = options;
|
2014-05-02 16:13:51 +02:00
|
|
|
DBWithTTLImpl::SanitizeOptions(ttl, &sanitized_options, GetEnv());
|
2014-04-29 05:34:20 +02:00
|
|
|
|
|
|
|
return DBWithTTL::CreateColumnFamily(sanitized_options, column_family_name,
|
|
|
|
handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBWithTTLImpl::CreateColumnFamily(const ColumnFamilyOptions& options,
|
|
|
|
const std::string& column_family_name,
|
|
|
|
ColumnFamilyHandle** handle) {
|
|
|
|
return CreateColumnFamilyWithTtl(options, column_family_name, handle, 0);
|
|
|
|
}
|
|
|
|
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
// Appends the current timestamp to the string.
|
|
|
|
// Returns false if could not get the current_time, true if append succeeds
|
2014-05-02 16:13:51 +02:00
|
|
|
Status DBWithTTLImpl::AppendTS(const Slice& val, std::string* val_with_ts,
|
|
|
|
Env* env) {
|
2014-04-29 05:34:20 +02:00
|
|
|
val_with_ts->reserve(kTSLength + val.size());
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
char ts_string[kTSLength];
|
2013-11-19 05:56:21 +01:00
|
|
|
int64_t curtime;
|
2014-05-02 16:13:51 +02:00
|
|
|
Status st = env->GetCurrentTime(&curtime);
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
if (!st.ok()) {
|
|
|
|
return st;
|
|
|
|
}
|
2013-11-19 05:56:21 +01:00
|
|
|
EncodeFixed32(ts_string, (int32_t)curtime);
|
2014-04-29 05:34:20 +02:00
|
|
|
val_with_ts->append(val.data(), val.size());
|
|
|
|
val_with_ts->append(ts_string, kTSLength);
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2013-05-10 02:33:27 +02:00
|
|
|
// Returns corruption if the length of the string is lesser than timestamp, or
|
|
|
|
// timestamp refers to a time lesser than ttl-feature release time
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTLImpl::SanityCheckTimestamp(const Slice& str) {
|
2013-08-03 09:40:22 +02:00
|
|
|
if (str.size() < kTSLength) {
|
2013-05-10 02:33:27 +02:00
|
|
|
return Status::Corruption("Error: value's length less than timestamp's\n");
|
|
|
|
}
|
|
|
|
// Checks that TS is not lesser than kMinTimestamp
|
|
|
|
// Gaurds against corruption & normal database opened incorrectly in ttl mode
|
2014-04-29 05:34:20 +02:00
|
|
|
int32_t timestamp_value = DecodeFixed32(str.data() + str.size() - kTSLength);
|
|
|
|
if (timestamp_value < kMinTimestamp) {
|
2013-05-10 02:33:27 +02:00
|
|
|
return Status::Corruption("Error: Timestamp < ttl feature release time!\n");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
// Checks if the string is stale or not according to TTl provided
|
2014-05-02 16:13:51 +02:00
|
|
|
bool DBWithTTLImpl::IsStale(const Slice& value, int32_t ttl, Env* env) {
|
2014-04-29 05:34:20 +02:00
|
|
|
if (ttl <= 0) { // Data is fresh if TTL is non-positive
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-11-19 05:56:21 +01:00
|
|
|
int64_t curtime;
|
2014-05-02 16:13:51 +02:00
|
|
|
if (!env->GetCurrentTime(&curtime).ok()) {
|
2014-04-29 05:34:20 +02:00
|
|
|
return false; // Treat the data as fresh if could not get current time
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
2013-08-03 09:40:22 +02:00
|
|
|
int32_t timestamp_value =
|
2014-04-29 05:34:20 +02:00
|
|
|
DecodeFixed32(value.data() + value.size() - kTSLength);
|
2013-08-03 09:40:22 +02:00
|
|
|
return (timestamp_value + ttl) < curtime;
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-13 19:44:50 +01:00
|
|
|
// Strips the TS from the end of the slice
|
|
|
|
Status DBWithTTLImpl::StripTS(PinnableSlice* pinnable_val) {
|
|
|
|
Status st;
|
|
|
|
if (pinnable_val->size() < kTSLength) {
|
|
|
|
return Status::Corruption("Bad timestamp in key-value");
|
|
|
|
}
|
|
|
|
// Erasing characters which hold the TS
|
|
|
|
pinnable_val->remove_suffix(kTSLength);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
// Strips the TS from the end of the string
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTLImpl::StripTS(std::string* str) {
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
Status st;
|
2013-08-03 09:40:22 +02:00
|
|
|
if (str->length() < kTSLength) {
|
|
|
|
return Status::Corruption("Bad timestamp in key-value");
|
|
|
|
}
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
// Erasing characters which hold the TS
|
|
|
|
str->erase(str->length() - kTSLength, kTSLength);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTLImpl::Put(const WriteOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
|
|
|
const Slice& val) {
|
2013-07-23 01:49:55 +02:00
|
|
|
WriteBatch batch;
|
2014-04-22 20:27:33 +02:00
|
|
|
batch.Put(column_family, key, val);
|
[RocksDB] [Column Family] Interface proposal
Summary:
<This diff is for Column Family branch>
Sharing some of the work I've done so far. This diff compiles and passes the tests.
The biggest change is in options.h - I broke down Options into two parts - DBOptions and ColumnFamilyOptions. DBOptions is DB-specific (env, create_if_missing, block_cache, etc.) and ColumnFamilyOptions is column family-specific (all compaction options, compresion options, etc.). Note that this does not break backwards compatibility at all.
Further, I created DBWithColumnFamily which inherits DB interface and adds new functions with column family support. Clients can transparently switch to DBWithColumnFamily and it will not break their backwards compatibility.
There are few methods worth checking out: ListColumnFamilies(), MultiNewIterator(), MultiGet() and GetSnapshot(). [GetSnapshot() returns the snapshot across all column families for now - I think that's what we agreed on]
Finally, I made small changes to WriteBatch so we are able to atomically insert data across column families.
Please provide feedback.
Test Plan: make check works, the code is backward compatible
Reviewers: dhruba, haobo, sdong, kailiu, emayanke
CC: leveldb
Differential Revision: https://reviews.facebook.net/D14445
2013-12-03 20:14:09 +01:00
|
|
|
return Write(options, &batch);
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTLImpl::Get(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
2017-03-13 19:44:50 +01:00
|
|
|
PinnableSlice* value) {
|
2017-01-08 23:08:51 +01:00
|
|
|
Status st = db_->Get(options, column_family, key, value);
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
if (!st.ok()) {
|
|
|
|
return st;
|
|
|
|
}
|
2017-01-08 23:08:51 +01:00
|
|
|
st = SanityCheckTimestamp(*value);
|
2013-05-10 02:33:27 +02:00
|
|
|
if (!st.ok()) {
|
|
|
|
return st;
|
|
|
|
}
|
2017-01-08 23:08:51 +01:00
|
|
|
return StripTS(value);
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
std::vector<Status> DBWithTTLImpl::MultiGet(
|
[RocksDB] [Column Family] Interface proposal
Summary:
<This diff is for Column Family branch>
Sharing some of the work I've done so far. This diff compiles and passes the tests.
The biggest change is in options.h - I broke down Options into two parts - DBOptions and ColumnFamilyOptions. DBOptions is DB-specific (env, create_if_missing, block_cache, etc.) and ColumnFamilyOptions is column family-specific (all compaction options, compresion options, etc.). Note that this does not break backwards compatibility at all.
Further, I created DBWithColumnFamily which inherits DB interface and adds new functions with column family support. Clients can transparently switch to DBWithColumnFamily and it will not break their backwards compatibility.
There are few methods worth checking out: ListColumnFamilies(), MultiNewIterator(), MultiGet() and GetSnapshot(). [GetSnapshot() returns the snapshot across all column families for now - I think that's what we agreed on]
Finally, I made small changes to WriteBatch so we are able to atomically insert data across column families.
Please provide feedback.
Test Plan: make check works, the code is backward compatible
Reviewers: dhruba, haobo, sdong, kailiu, emayanke
CC: leveldb
Differential Revision: https://reviews.facebook.net/D14445
2013-12-03 20:14:09 +01:00
|
|
|
const ReadOptions& options,
|
2014-02-11 02:04:44 +01:00
|
|
|
const std::vector<ColumnFamilyHandle*>& column_family,
|
[RocksDB] [Column Family] Interface proposal
Summary:
<This diff is for Column Family branch>
Sharing some of the work I've done so far. This diff compiles and passes the tests.
The biggest change is in options.h - I broke down Options into two parts - DBOptions and ColumnFamilyOptions. DBOptions is DB-specific (env, create_if_missing, block_cache, etc.) and ColumnFamilyOptions is column family-specific (all compaction options, compresion options, etc.). Note that this does not break backwards compatibility at all.
Further, I created DBWithColumnFamily which inherits DB interface and adds new functions with column family support. Clients can transparently switch to DBWithColumnFamily and it will not break their backwards compatibility.
There are few methods worth checking out: ListColumnFamilies(), MultiNewIterator(), MultiGet() and GetSnapshot(). [GetSnapshot() returns the snapshot across all column families for now - I think that's what we agreed on]
Finally, I made small changes to WriteBatch so we are able to atomically insert data across column families.
Please provide feedback.
Test Plan: make check works, the code is backward compatible
Reviewers: dhruba, haobo, sdong, kailiu, emayanke
CC: leveldb
Differential Revision: https://reviews.facebook.net/D14445
2013-12-03 20:14:09 +01:00
|
|
|
const std::vector<Slice>& keys, std::vector<std::string>* values) {
|
2014-12-20 12:46:37 +01:00
|
|
|
auto statuses = db_->MultiGet(options, column_family, keys, values);
|
|
|
|
for (size_t i = 0; i < keys.size(); ++i) {
|
|
|
|
if (!statuses[i].ok()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
statuses[i] = SanityCheckTimestamp((*values)[i]);
|
|
|
|
if (!statuses[i].ok()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
statuses[i] = StripTS(&(*values)[i]);
|
|
|
|
}
|
|
|
|
return statuses;
|
2013-06-05 20:22:38 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
bool DBWithTTLImpl::KeyMayExist(const ReadOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, std::string* value,
|
|
|
|
bool* value_found) {
|
2014-04-22 20:27:33 +02:00
|
|
|
bool ret = db_->KeyMayExist(options, column_family, key, value, value_found);
|
2013-08-24 02:36:00 +02:00
|
|
|
if (ret && value != nullptr && value_found != nullptr && *value_found) {
|
|
|
|
if (!SanityCheckTimestamp(*value).ok() || !StripTS(value).ok()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2013-07-06 03:49:18 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTLImpl::Merge(const WriteOptions& options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key,
|
|
|
|
const Slice& value) {
|
2013-07-23 01:49:55 +02:00
|
|
|
WriteBatch batch;
|
2014-04-22 20:27:33 +02:00
|
|
|
batch.Merge(column_family, key, value);
|
[RocksDB] [Column Family] Interface proposal
Summary:
<This diff is for Column Family branch>
Sharing some of the work I've done so far. This diff compiles and passes the tests.
The biggest change is in options.h - I broke down Options into two parts - DBOptions and ColumnFamilyOptions. DBOptions is DB-specific (env, create_if_missing, block_cache, etc.) and ColumnFamilyOptions is column family-specific (all compaction options, compresion options, etc.). Note that this does not break backwards compatibility at all.
Further, I created DBWithColumnFamily which inherits DB interface and adds new functions with column family support. Clients can transparently switch to DBWithColumnFamily and it will not break their backwards compatibility.
There are few methods worth checking out: ListColumnFamilies(), MultiNewIterator(), MultiGet() and GetSnapshot(). [GetSnapshot() returns the snapshot across all column families for now - I think that's what we agreed on]
Finally, I made small changes to WriteBatch so we are able to atomically insert data across column families.
Please provide feedback.
Test Plan: make check works, the code is backward compatible
Reviewers: dhruba, haobo, sdong, kailiu, emayanke
CC: leveldb
Differential Revision: https://reviews.facebook.net/D14445
2013-12-03 20:14:09 +01:00
|
|
|
return Write(options, &batch);
|
2013-03-21 23:59:47 +01:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Status DBWithTTLImpl::Write(const WriteOptions& opts, WriteBatch* updates) {
|
2013-05-16 00:00:57 +02:00
|
|
|
class Handler : public WriteBatch::Handler {
|
|
|
|
public:
|
2014-05-02 16:13:51 +02:00
|
|
|
explicit Handler(Env* env) : env_(env) {}
|
2013-05-16 00:00:57 +02:00
|
|
|
WriteBatch updates_ttl;
|
|
|
|
Status batch_rewrite_status;
|
2019-02-14 22:52:47 +01:00
|
|
|
Status PutCF(uint32_t column_family_id, const Slice& key,
|
|
|
|
const Slice& value) override {
|
2013-05-16 00:00:57 +02:00
|
|
|
std::string value_with_ts;
|
2014-05-02 16:13:51 +02:00
|
|
|
Status st = AppendTS(value, &value_with_ts, env_);
|
2013-05-16 00:00:57 +02:00
|
|
|
if (!st.ok()) {
|
|
|
|
batch_rewrite_status = st;
|
|
|
|
} else {
|
2014-04-22 20:27:33 +02:00
|
|
|
WriteBatchInternal::Put(&updates_ttl, column_family_id, key,
|
|
|
|
value_with_ts);
|
2013-05-16 00:00:57 +02:00
|
|
|
}
|
2014-04-22 20:27:33 +02:00
|
|
|
return Status::OK();
|
2013-05-16 00:00:57 +02:00
|
|
|
}
|
2019-02-14 22:52:47 +01:00
|
|
|
Status MergeCF(uint32_t column_family_id, const Slice& key,
|
|
|
|
const Slice& value) override {
|
2013-07-23 01:49:55 +02:00
|
|
|
std::string value_with_ts;
|
2014-05-02 16:13:51 +02:00
|
|
|
Status st = AppendTS(value, &value_with_ts, env_);
|
2013-07-23 01:49:55 +02:00
|
|
|
if (!st.ok()) {
|
|
|
|
batch_rewrite_status = st;
|
|
|
|
} else {
|
2014-04-22 20:27:33 +02:00
|
|
|
WriteBatchInternal::Merge(&updates_ttl, column_family_id, key,
|
|
|
|
value_with_ts);
|
2013-07-23 01:49:55 +02:00
|
|
|
}
|
2014-04-22 20:27:33 +02:00
|
|
|
return Status::OK();
|
2013-05-16 00:00:57 +02:00
|
|
|
}
|
2019-02-14 22:52:47 +01:00
|
|
|
Status DeleteCF(uint32_t column_family_id, const Slice& key) override {
|
2014-04-22 20:27:33 +02:00
|
|
|
WriteBatchInternal::Delete(&updates_ttl, column_family_id, key);
|
|
|
|
return Status::OK();
|
2013-05-16 00:00:57 +02:00
|
|
|
}
|
2019-02-14 22:52:47 +01:00
|
|
|
void LogData(const Slice& blob) override { updates_ttl.PutLogData(blob); }
|
2014-05-02 16:13:51 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Env* env_;
|
2013-05-16 00:00:57 +02:00
|
|
|
};
|
2014-05-02 16:13:51 +02:00
|
|
|
Handler handler(GetEnv());
|
2013-05-16 00:00:57 +02:00
|
|
|
updates->Iterate(&handler);
|
|
|
|
if (!handler.batch_rewrite_status.ok()) {
|
|
|
|
return handler.batch_rewrite_status;
|
|
|
|
} else {
|
|
|
|
return db_->Write(opts, &(handler.updates_ttl));
|
|
|
|
}
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 05:34:20 +02:00
|
|
|
Iterator* DBWithTTLImpl::NewIterator(const ReadOptions& opts,
|
|
|
|
ColumnFamilyHandle* column_family) {
|
[RocksDB] [Column Family] Interface proposal
Summary:
<This diff is for Column Family branch>
Sharing some of the work I've done so far. This diff compiles and passes the tests.
The biggest change is in options.h - I broke down Options into two parts - DBOptions and ColumnFamilyOptions. DBOptions is DB-specific (env, create_if_missing, block_cache, etc.) and ColumnFamilyOptions is column family-specific (all compaction options, compresion options, etc.). Note that this does not break backwards compatibility at all.
Further, I created DBWithColumnFamily which inherits DB interface and adds new functions with column family support. Clients can transparently switch to DBWithColumnFamily and it will not break their backwards compatibility.
There are few methods worth checking out: ListColumnFamilies(), MultiNewIterator(), MultiGet() and GetSnapshot(). [GetSnapshot() returns the snapshot across all column families for now - I think that's what we agreed on]
Finally, I made small changes to WriteBatch so we are able to atomically insert data across column families.
Please provide feedback.
Test Plan: make check works, the code is backward compatible
Reviewers: dhruba, haobo, sdong, kailiu, emayanke
CC: leveldb
Differential Revision: https://reviews.facebook.net/D14445
2013-12-03 20:14:09 +01:00
|
|
|
return new TtlIterator(db_->NewIterator(opts, column_family));
|
Timestamp and TTL Wrapper for rocksdb
Summary:
When opened with DBTimestamp::Open call, timestamps are prepended to and stripped from the value during subsequent Put and Get calls respectively. The Timestamp is used to discard values in Get and custom compaction filter which have exceeded their TTL which is specified during Open.
Have made a temporary change to Makefile to let us test with the temporary file TestTime.cc. Have also changed the private members of db_impl.h to protected to let them be inherited by the new class DBTimestamp
Test Plan: make db_timestamp; TestTime.cc(will not check it in) shows how to use the apis currently, but I will write unit-tests shortly
Reviewers: dhruba, vamsi, haobo, sheki, heyongqiang, vkrest
Reviewed By: vamsi
CC: zshao, xjin, vkrest, MarkCallaghan
Differential Revision: https://reviews.facebook.net/D10311
2013-04-15 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2018-01-18 19:41:26 +01:00
|
|
|
void DBWithTTLImpl::SetTtl(ColumnFamilyHandle *h, int32_t ttl) {
|
|
|
|
std::shared_ptr<TtlCompactionFilterFactory> filter;
|
|
|
|
Options opts;
|
|
|
|
opts = GetOptions(h);
|
|
|
|
filter = std::static_pointer_cast<TtlCompactionFilterFactory>(
|
|
|
|
opts.compaction_filter_factory);
|
|
|
|
if (!filter)
|
|
|
|
return;
|
|
|
|
filter->SetTtl(ttl);
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-04-15 22:39:26 +02:00
|
|
|
#endif // ROCKSDB_LITE
|