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 base classes for user-specified compaction #731

Merged
merged 14 commits into from
May 28, 2021
38 changes: 38 additions & 0 deletions src/server/compaction_filter_rule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

#pragma once

#include <rocksdb/slice.h>

namespace pegasus {
namespace server {
class compaction_filter_rule
{
public:
virtual ~compaction_filter_rule() = default;

// we can use `value_filed` to replace existing_value in the later,
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
// after the refactor of value schema
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactor of value schema has already been merged, you can update it in next pull request :-)

Copy link
Contributor Author

@levy5307 levy5307 May 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will refactor it in the later

virtual bool match(const std::string &hash_key,
const std::string &sort_key,
const rocksdb::Slice &existing_value) const = 0;
};
} // namespace server
} // namespace pegasus
43 changes: 43 additions & 0 deletions src/server/compaction_operation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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_operation.h"

namespace pegasus {
namespace server {
compaction_operation::~compaction_operation() = default;

bool compaction_operation::all_rules_match(const std::string &hash_key,
const std::string &sort_key,
const rocksdb::Slice &existing_value) const
{
if (rules.size() == 0) {
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

for (const auto &rule : rules) {
if (!rule->match(hash_key, sort_key, existing_value)) {
return false;
}
}
return true;
}

} // namespace server
} // namespace pegasus
58 changes: 58 additions & 0 deletions src/server/compaction_operation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.
*/

#pragma once

#include <memory>
#include <vector>
#include "compaction_filter_rule.h"

namespace pegasus {
namespace server {

typedef std::vector<std::unique_ptr<compaction_filter_rule>> filter_rules;
class compaction_operation
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
{
public:
compaction_operation(filter_rules &&rules, uint32_t pegasus_data_version)
: rules(std::move(rules)), pegasus_data_version(pegasus_data_version)
{
}
virtual ~compaction_operation() = 0;

bool all_rules_match(const std::string &hash_key,
const std::string &sort_key,
const rocksdb::Slice &existing_value) const;
/**
* @return false indicates that this key-value should be removed
* If you want to modify the existing_value, you can pass it back through new_value and
* value_changed needs to be set to true in this case.
*/
virtual bool filter(const std::string &hash_key,
const std::string &sort_key,
const rocksdb::Slice &existing_value,
std::string *new_value,
bool *value_changed) const = 0;

protected:
filter_rules rules;
uint32_t pegasus_data_version;
};
} // namespace server
} // namespace pegasus