Skip to content

Commit

Permalink
server: support default TTL for a table (apache#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
acelyc111 authored and qinzuoyan committed Nov 1, 2018
1 parent 7dae1c5 commit be693f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/base/pegasus_value_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ pegasus_extract_user_data(uint32_t version, std::string &&raw_value, ::dsn::blob
user_data.assign(std::move(buf), 0, static_cast<unsigned int>(view.length()));
}

/// Update expire_ts in rocksdb value with given version.
/// The value schema must be in v0.
inline void pegasus_update_expire_ts(uint32_t version, std::string &value, uint32_t new_expire_ts)
{
if (version == 0) {
dassert_f(value.length() >= sizeof(uint32_t), "value must include 'expire_ts' header");

new_expire_ts = dsn::endian::hton(new_expire_ts);
memcpy(const_cast<char *>(value.data()), &new_expire_ts, sizeof(uint32_t));
} else {
dfatal_f("unsupported value schema version: {}", version);
__builtin_unreachable();
}
}

/// \return true if expired
inline bool check_if_ts_expired(uint32_t epoch_now, uint32_t expire_ts)
{
Expand Down
12 changes: 2 additions & 10 deletions src/server/key_ttl_compaction_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,8 @@ class KeyWithTTLCompactionFilter : public rocksdb::CompactionFilter
pegasus_extract_expire_ts(_value_schema_version, utils::to_string_view(existing_value));
if (_default_ttl != 0 && expire_ts == 0) {
// should update ttl
dsn::blob user_data;
pegasus_extract_user_data(_value_schema_version,
std::string(existing_value.data(), existing_value.length()),
user_data);
rocksdb::SliceParts sparts = _gen.generate_value(_value_schema_version,
dsn::string_view(user_data),
utils::epoch_now() + _default_ttl);
for (int i = 0; i < sparts.num_parts; i++) {
*new_value += sparts.parts[i].ToString();
}
*new_value = existing_value.ToString();
pegasus_update_expire_ts(_value_schema_version, *new_value, utils::epoch_now() + _default_ttl);
*value_changed = true;
return false;
}
Expand Down

0 comments on commit be693f9

Please sign in to comment.