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

[incubator-pegasus] branch master updated: feat: add hashkey pattern rule (#741)

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

laiyingchun 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 32aa673  feat: add hashkey pattern rule (#741)
32aa673 is described below

commit 32aa6736509dbff3f4c0386f5013272f3dfa783e
Author: zhao liwei <zl...@163.com>
AuthorDate: Wed Jun 2 12:11:45 2021 +0800

    feat: add hashkey pattern rule (#741)
---
 src/server/compaction_filter_rule.cpp           | 60 ++++++++++++++++++++++++
 src/server/compaction_filter_rule.h             | 31 +++++++++++++
 src/server/test/CMakeLists.txt                  |  3 +-
 src/server/test/compaction_filter_rule_test.cpp | 62 +++++++++++++++++++++++++
 4 files changed, 155 insertions(+), 1 deletion(-)

diff --git a/src/server/compaction_filter_rule.cpp b/src/server/compaction_filter_rule.cpp
new file mode 100644
index 0000000..7e64e9f
--- /dev/null
+++ b/src/server/compaction_filter_rule.cpp
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "compaction_filter_rule.h"
+
+#include <dsn/dist/fmt_logging.h>
+#include <dsn/utility/string_view.h>
+#include <dsn/c/api_utilities.h>
+
+namespace pegasus {
+namespace server {
+bool string_pattern_match(const std::string &value,
+                          string_match_type type,
+                          const std::string &filter_pattern)
+{
+    if (filter_pattern.empty())
+        return false;
+    if (value.length() < filter_pattern.length())
+        return false;
+
+    switch (type) {
+    case string_match_type::SMT_MATCH_ANYWHERE:
+        return dsn::string_view(value).find(filter_pattern) != dsn::string_view::npos;
+    case string_match_type::SMT_MATCH_PREFIX:
+        return memcmp(value.data(), filter_pattern.data(), filter_pattern.length()) == 0;
+    case string_match_type::SMT_MATCH_POSTFIX:
+        return memcmp(value.data() + value.length() - filter_pattern.length(),
+                      filter_pattern.data(),
+                      filter_pattern.length()) == 0;
+    default:
+        derror_f("invalid match type {}", type);
+        return false;
+    }
+}
+
+bool hashkey_pattern_rule::match(const std::string &hash_key,
+                                 const std::string &sort_key,
+                                 const rocksdb::Slice &existing_value) const
+{
+    return string_pattern_match(hash_key, match_type, pattern);
+}
+
+} // namespace server
+} // namespace pegasus
diff --git a/src/server/compaction_filter_rule.h b/src/server/compaction_filter_rule.h
index 2c18209..435aeb5 100644
--- a/src/server/compaction_filter_rule.h
+++ b/src/server/compaction_filter_rule.h
@@ -20,6 +20,8 @@
 #pragma once
 
 #include <rocksdb/slice.h>
+#include <dsn/utility/enum_helper.h>
+#include <gtest/gtest.h>
 
 namespace pegasus {
 namespace server {
@@ -36,5 +38,34 @@ public:
                        const std::string &sort_key,
                        const rocksdb::Slice &existing_value) const = 0;
 };
+
+enum string_match_type
+{
+    SMT_MATCH_ANYWHERE,
+    SMT_MATCH_PREFIX,
+    SMT_MATCH_POSTFIX,
+    SMT_INVALID,
+};
+ENUM_BEGIN(string_match_type, SMT_INVALID)
+ENUM_REG(SMT_MATCH_ANYWHERE)
+ENUM_REG(SMT_MATCH_PREFIX)
+ENUM_REG(SMT_MATCH_POSTFIX)
+ENUM_END(string_match_type)
+
+class hashkey_pattern_rule : public compaction_filter_rule
+{
+public:
+    explicit hashkey_pattern_rule() = default;
+
+    bool match(const std::string &hash_key,
+               const std::string &sort_key,
+               const rocksdb::Slice &existing_value) const;
+
+private:
+    std::string pattern;
+    string_match_type match_type;
+
+    FRIEND_TEST(hashkey_pattern_rule_test, match);
+};
 } // namespace server
 } // namespace pegasus
diff --git a/src/server/test/CMakeLists.txt b/src/server/test/CMakeLists.txt
index 80dc4d6..1beddd4 100644
--- a/src/server/test/CMakeLists.txt
+++ b/src/server/test/CMakeLists.txt
@@ -29,7 +29,8 @@ set(MY_PROJ_SRC "../pegasus_server_impl.cpp"
                 "../meta_store.cpp"
                 "../hotkey_collector.cpp"
                 "../rocksdb_wrapper.cpp"
-)
+                "../compaction_filter_rule.cpp"
+        )
 
 set(MY_SRC_SEARCH_MODE "GLOB")
 
diff --git a/src/server/test/compaction_filter_rule_test.cpp b/src/server/test/compaction_filter_rule_test.cpp
new file mode 100644
index 0000000..0d9d3fd
--- /dev/null
+++ b/src/server/test/compaction_filter_rule_test.cpp
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <gtest/gtest.h>
+#include "server/compaction_filter_rule.h"
+
+namespace pegasus {
+namespace server {
+
+TEST(hashkey_pattern_rule_test, match)
+{
+    struct test_case
+    {
+        std::string hashkey;
+        std::string pattern;
+        string_match_type match_type;
+        bool match;
+    } tests[] = {
+        {"sortkey", "", SMT_MATCH_ANYWHERE, false},
+        {"hashkey", "hashkey", SMT_MATCH_ANYWHERE, true},
+        {"hashkey", "shke", SMT_MATCH_ANYWHERE, true},
+        {"hashkey", "hash", SMT_MATCH_ANYWHERE, true},
+        {"hashkey", "key", SMT_MATCH_ANYWHERE, true},
+        {"hashkey", "sortkey", SMT_MATCH_ANYWHERE, false},
+        {"hashkey", "hashkey", SMT_MATCH_PREFIX, true},
+        {"hashkey", "hash", SMT_MATCH_PREFIX, true},
+        {"hashkey", "key", SMT_MATCH_PREFIX, false},
+        {"hashkey", "sortkey", SMT_MATCH_PREFIX, false},
+        {"hashkey", "hashkey", SMT_MATCH_POSTFIX, true},
+        {"hashkey", "hash", SMT_MATCH_POSTFIX, false},
+        {"hashkey", "key", SMT_MATCH_POSTFIX, true},
+        {"hashkey", "sortkey", SMT_MATCH_POSTFIX, false},
+        {"hash", "hashkey", SMT_MATCH_POSTFIX, false},
+        {"hashkey", "hashkey", SMT_INVALID, false},
+    };
+
+    rocksdb::Slice slice;
+    hashkey_pattern_rule rule;
+    for (const auto &test : tests) {
+        rule.match_type = test.match_type;
+        rule.pattern = test.pattern;
+        ASSERT_EQ(rule.match(test.hashkey, "", slice), 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