Skip to content

Commit

Permalink
feat: add base classes for user-specified compaction (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
levy5307 authored May 28, 2021
1 parent 14c87d9 commit 376dfc8
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/server/compaction_filter_rule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 {
/** compaction_filter_rule represents the compaction rule to filter the keys which are stored in
* rocksdb. */
class compaction_filter_rule
{
public:
virtual ~compaction_filter_rule() = default;

// TODO(zhaoliwei): we can use `value_filed` to replace existing_value in the later,
// after the refactor of value schema
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.empty()) {
return false;
}

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

} // namespace server
} // namespace pegasus
60 changes: 60 additions & 0 deletions src/server/compaction_operation.h
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.
*/

#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;
/** compaction_operation represents the compaction operation. A compaction operation will be
* executed when all the corresponding compaction rules are matched. */
class compaction_operation
{
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

0 comments on commit 376dfc8

Please sign in to comment.