New API to get all merge operands for a Key (#5604)
Summary:
This is a new API added to db.h to allow for fetching all merge operands associated with a Key. The main motivation for this API is to support use cases where doing a full online merge is not necessary as it is performance sensitive. Example use-cases:
1. Update subset of columns and read subset of columns -
Imagine a SQL Table, a row is encoded as a K/V pair (as it is done in MyRocks). If there are many columns and users only updated one of them, we can use merge operator to reduce write amplification. While users only read one or two columns in the read query, this feature can avoid a full merging of the whole row, and save some CPU.
2. Updating very few attributes in a value which is a JSON-like document -
Updating one attribute can be done efficiently using merge operator, while reading back one attribute can be done more efficiently if we don't need to do a full merge.
----------------------------------------------------------------------------------------------------
API :
Status GetMergeOperands(
const ReadOptions& options, ColumnFamilyHandle* column_family,
const Slice& key, PinnableSlice* merge_operands,
GetMergeOperandsOptions* get_merge_operands_options,
int* number_of_operands)
Example usage :
int size = 100;
int number_of_operands = 0;
std::vector<PinnableSlice> values(size);
GetMergeOperandsOptions merge_operands_info;
db_->GetMergeOperands(ReadOptions(), db_->DefaultColumnFamily(), "k1", values.data(), merge_operands_info, &number_of_operands);
Description :
Returns all the merge operands corresponding to the key. If the number of merge operands in DB is greater than merge_operands_options.expected_max_number_of_operands no merge operands are returned and status is Incomplete. Merge operands returned are in the order of insertion.
merge_operands-> Points to an array of at-least merge_operands_options.expected_max_number_of_operands and the caller is responsible for allocating it. If the status returned is Incomplete then number_of_operands will contain the total number of merge operands found in DB for key.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5604
Test Plan:
Added unit test and perf test in db_bench that can be run using the command:
./db_bench -benchmarks=getmergeoperands --merge_operator=sortlist
Differential Revision: D16657366
Pulled By: vjnadimpalli
fbshipit-source-id: 0faadd752351745224ee12d4ae9ef3cb529951bf
2019-08-06 23:22:34 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2019-09-20 21:00:55 +02:00
|
|
|
#include "utilities/merge_operators/sortlist.h"
|
New API to get all merge operands for a Key (#5604)
Summary:
This is a new API added to db.h to allow for fetching all merge operands associated with a Key. The main motivation for this API is to support use cases where doing a full online merge is not necessary as it is performance sensitive. Example use-cases:
1. Update subset of columns and read subset of columns -
Imagine a SQL Table, a row is encoded as a K/V pair (as it is done in MyRocks). If there are many columns and users only updated one of them, we can use merge operator to reduce write amplification. While users only read one or two columns in the read query, this feature can avoid a full merging of the whole row, and save some CPU.
2. Updating very few attributes in a value which is a JSON-like document -
Updating one attribute can be done efficiently using merge operator, while reading back one attribute can be done more efficiently if we don't need to do a full merge.
----------------------------------------------------------------------------------------------------
API :
Status GetMergeOperands(
const ReadOptions& options, ColumnFamilyHandle* column_family,
const Slice& key, PinnableSlice* merge_operands,
GetMergeOperandsOptions* get_merge_operands_options,
int* number_of_operands)
Example usage :
int size = 100;
int number_of_operands = 0;
std::vector<PinnableSlice> values(size);
GetMergeOperandsOptions merge_operands_info;
db_->GetMergeOperands(ReadOptions(), db_->DefaultColumnFamily(), "k1", values.data(), merge_operands_info, &number_of_operands);
Description :
Returns all the merge operands corresponding to the key. If the number of merge operands in DB is greater than merge_operands_options.expected_max_number_of_operands no merge operands are returned and status is Incomplete. Merge operands returned are in the order of insertion.
merge_operands-> Points to an array of at-least merge_operands_options.expected_max_number_of_operands and the caller is responsible for allocating it. If the status returned is Incomplete then number_of_operands will contain the total number of merge operands found in DB for key.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5604
Test Plan:
Added unit test and perf test in db_bench that can be run using the command:
./db_bench -benchmarks=getmergeoperands --merge_operator=sortlist
Differential Revision: D16657366
Pulled By: vjnadimpalli
fbshipit-source-id: 0faadd752351745224ee12d4ae9ef3cb529951bf
2019-08-06 23:22:34 +02:00
|
|
|
#include "rocksdb/merge_operator.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "utilities/merge_operators.h"
|
|
|
|
|
|
|
|
using rocksdb::Logger;
|
|
|
|
using rocksdb::MergeOperator;
|
|
|
|
using rocksdb::Slice;
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
bool SortList::FullMergeV2(const MergeOperationInput& merge_in,
|
|
|
|
MergeOperationOutput* merge_out) const {
|
|
|
|
std::vector<int> left;
|
|
|
|
for (Slice slice : merge_in.operand_list) {
|
|
|
|
std::vector<int> right;
|
|
|
|
MakeVector(right, slice);
|
|
|
|
left = Merge(left, right);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < static_cast<int>(left.size()) - 1; i++) {
|
|
|
|
merge_out->new_value.append(std::to_string(left[i])).append(",");
|
|
|
|
}
|
|
|
|
merge_out->new_value.append(std::to_string(left.back()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SortList::PartialMerge(const Slice& /*key*/, const Slice& left_operand,
|
|
|
|
const Slice& right_operand, std::string* new_value,
|
|
|
|
Logger* /*logger*/) const {
|
|
|
|
std::vector<int> left;
|
|
|
|
std::vector<int> right;
|
|
|
|
MakeVector(left, left_operand);
|
|
|
|
MakeVector(right, right_operand);
|
|
|
|
left = Merge(left, right);
|
|
|
|
for (int i = 0; i < static_cast<int>(left.size()) - 1; i++) {
|
|
|
|
new_value->append(std::to_string(left[i])).append(",");
|
|
|
|
}
|
|
|
|
new_value->append(std::to_string(left.back()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SortList::PartialMergeMulti(const Slice& /*key*/,
|
|
|
|
const std::deque<Slice>& operand_list,
|
|
|
|
std::string* new_value,
|
|
|
|
Logger* /*logger*/) const {
|
|
|
|
(void)operand_list;
|
|
|
|
(void)new_value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* SortList::Name() const { return "MergeSortOperator"; }
|
|
|
|
|
|
|
|
void SortList::MakeVector(std::vector<int>& operand, Slice slice) const {
|
|
|
|
do {
|
|
|
|
const char* begin = slice.data_;
|
|
|
|
while (*slice.data_ != ',' && *slice.data_) slice.data_++;
|
|
|
|
operand.push_back(std::stoi(std::string(begin, slice.data_)));
|
|
|
|
} while (0 != *slice.data_++);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> SortList::Merge(std::vector<int>& left,
|
|
|
|
std::vector<int>& right) const {
|
|
|
|
// Fill the resultant vector with sorted results from both vectors
|
|
|
|
std::vector<int> result;
|
|
|
|
unsigned left_it = 0, right_it = 0;
|
|
|
|
|
|
|
|
while (left_it < left.size() && right_it < right.size()) {
|
|
|
|
// If the left value is smaller than the right it goes next
|
|
|
|
// into the resultant vector
|
|
|
|
if (left[left_it] < right[right_it]) {
|
|
|
|
result.push_back(left[left_it]);
|
|
|
|
left_it++;
|
|
|
|
} else {
|
|
|
|
result.push_back(right[right_it]);
|
|
|
|
right_it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the remaining data from both vectors onto the resultant
|
|
|
|
while (left_it < left.size()) {
|
|
|
|
result.push_back(left[left_it]);
|
|
|
|
left_it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (right_it < right.size()) {
|
|
|
|
result.push_back(right[right_it]);
|
|
|
|
right_it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateSortOperator() {
|
|
|
|
return std::make_shared<SortList>();
|
|
|
|
}
|
|
|
|
} // namespace rocksdb
|