Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hashkey pattern rule #741

Merged
merged 6 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/server/compaction_filter_rule.cpp
Original file line number Diff line number Diff line change
@@ -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;
levy5307 marked this conversation as resolved.
Show resolved Hide resolved

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);
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
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
31 changes: 31 additions & 0 deletions src/server/compaction_filter_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#pragma once

#include <rocksdb/slice.h>
#include <dsn/utility/enum_helper.h>
#include <gtest/gtest.h>

namespace pegasus {
namespace server {
Expand All @@ -36,5 +38,34 @@ class compaction_filter_rule
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
3 changes: 2 additions & 1 deletion src/server/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
62 changes: 62 additions & 0 deletions src/server/test/compaction_filter_rule_test.cpp
Original file line number Diff line number Diff line change
@@ -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