You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pegasus.apache.org by zh...@apache.org on 2021/06/08 07:01:11 UTC

[incubator-pegasus] branch master updated: feat: add ttl range rule (#750)

This is an automated email from the ASF dual-hosted git repository.

zhaoliwei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new b0782fd  feat: add ttl range rule (#750)
b0782fd is described below

commit b0782fda2703551b3a2813f1a19ec56034f0e738
Author: zhao liwei <zl...@163.com>
AuthorDate: Tue Jun 8 15:01:01 2021 +0800

    feat: add ttl range rule (#750)
---
 src/server/compaction_filter_rule.cpp           | 25 +++++++++++++++++
 src/server/compaction_filter_rule.h             | 17 ++++++++++++
 src/server/test/compaction_filter_rule_test.cpp | 36 +++++++++++++++++++++++++
 3 files changed, 78 insertions(+)

diff --git a/src/server/compaction_filter_rule.cpp b/src/server/compaction_filter_rule.cpp
index 1e0ea45..a1a66e0 100644
--- a/src/server/compaction_filter_rule.cpp
+++ b/src/server/compaction_filter_rule.cpp
@@ -22,6 +22,8 @@
 #include <dsn/dist/fmt_logging.h>
 #include <dsn/utility/string_view.h>
 #include <dsn/c/api_utilities.h>
+#include "base/pegasus_utils.h"
+#include "base/pegasus_value_schema.h"
 
 namespace pegasus {
 namespace server {
@@ -62,5 +64,28 @@ bool sortkey_pattern_rule::match(const std::string &hash_key,
 {
     return string_pattern_match(sort_key, match_type, pattern);
 }
+
+ttl_range_rule::ttl_range_rule(uint32_t pegasus_data_version)
+    : pegasus_data_version(pegasus_data_version)
+{
+}
+
+bool ttl_range_rule::match(const std::string &hash_key,
+                           const std::string &sort_key,
+                           const rocksdb::Slice &existing_value) const
+{
+    uint32_t expire_ts =
+        pegasus_extract_expire_ts(pegasus_data_version, utils::to_string_view(existing_value));
+    // if start_ttl and stop_ttl = 0, it means we want to delete keys which have no ttl
+    if (0 == expire_ts && 0 == start_ttl && 0 == stop_ttl) {
+        return true;
+    }
+
+    auto now_ts = utils::epoch_now();
+    if (start_ttl + now_ts <= expire_ts && stop_ttl + now_ts >= expire_ts) {
+        return true;
+    }
+    return false;
+}
 } // namespace server
 } // namespace pegasus
diff --git a/src/server/compaction_filter_rule.h b/src/server/compaction_filter_rule.h
index da65661..165b4c6 100644
--- a/src/server/compaction_filter_rule.h
+++ b/src/server/compaction_filter_rule.h
@@ -84,5 +84,22 @@ private:
     FRIEND_TEST(sortkey_pattern_rule_test, match);
 };
 
+class ttl_range_rule : public compaction_filter_rule
+{
+public:
+    explicit ttl_range_rule(uint32_t pegasus_data_version);
+
+    bool match(const std::string &hash_key,
+               const std::string &sort_key,
+               const rocksdb::Slice &existing_value) const;
+
+private:
+    // = 0 means no limit
+    uint32_t start_ttl;
+    uint32_t stop_ttl;
+    uint32_t pegasus_data_version;
+
+    FRIEND_TEST(ttl_range_rule_test, match);
+};
 } // namespace server
 } // namespace pegasus
diff --git a/src/server/test/compaction_filter_rule_test.cpp b/src/server/test/compaction_filter_rule_test.cpp
index a91b3f7..a8f436f 100644
--- a/src/server/test/compaction_filter_rule_test.cpp
+++ b/src/server/test/compaction_filter_rule_test.cpp
@@ -18,6 +18,8 @@
  */
 
 #include <gtest/gtest.h>
+#include "base/pegasus_value_schema.h"
+#include "base/pegasus_utils.h"
 #include "server/compaction_filter_rule.h"
 
 namespace pegasus {
@@ -95,5 +97,39 @@ TEST(sortkey_pattern_rule_test, match)
     }
 }
 
+TEST(ttl_range_rule_test, match)
+{
+    struct test_case
+    {
+        int32_t start_ttl;
+        int32_t stop_ttl;
+        int32_t expire_ttl;
+        bool match;
+    } tests[] = {
+        {100, 1000, 1100, false},
+        {100, 1000, 500, true},
+        {100, 1000, 20, false},
+        {100, 1000, 0, false},
+        {1000, 100, 1100, false},
+        {1000, 100, 500, false},
+        {1000, 100, 20, false},
+        {1000, 100, 0, false},
+        {0, 1000, 500, true},
+        {1000, 0, 500, false},
+        {0, 0, 0, true},
+    };
+
+    const uint32_t data_version = 1;
+    ttl_range_rule rule(data_version);
+    pegasus_value_generator gen;
+    auto now_ts = utils::epoch_now();
+    for (const auto &test : tests) {
+        rule.start_ttl = test.start_ttl;
+        rule.stop_ttl = test.stop_ttl;
+        rocksdb::SliceParts svalue =
+            gen.generate_value(data_version, "", test.expire_ttl + now_ts, 0);
+        ASSERT_EQ(rule.match("", "", svalue.parts[0]), test.match);
+    }
+}
 } // namespace server
 } // namespace pegasus

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pegasus.apache.org
For additional commands, e-mail: commits-help@pegasus.apache.org