mirror of
https://github.com/facebook/rocksdb.git
synced 2024-11-25 22:44:05 +00:00
05e8854085
Summary: This diff introduces a new Merge operation into rocksdb. The purpose of this review is mostly getting feedback from the team (everyone please) on the design. Please focus on the four files under include/leveldb/, as they spell the client visible interface change. include/leveldb/db.h include/leveldb/merge_operator.h include/leveldb/options.h include/leveldb/write_batch.h Please go over local/my_test.cc carefully, as it is a concerete use case. Please also review the impelmentation files to see if the straw man implementation makes sense. Note that, the diff does pass all make check and truly supports forward iterator over db and a version of Get that's based on iterator. Future work: - Integration with compaction - A raw Get implementation I am working on a wiki that explains the design and implementation choices, but coding comes just naturally and I think it might be a good idea to share the code earlier. The code is heavily commented. Test Plan: run all local tests Reviewers: dhruba, heyongqiang Reviewed By: dhruba CC: leveldb, zshao, sheki, emayanke, MarkCallaghan Differential Revision: https://reviews.facebook.net/D9651
64 lines
1.6 KiB
C++
64 lines
1.6 KiB
C++
#include <memory>
|
|
#include "leveldb/env.h"
|
|
#include "leveldb/merge_operator.h"
|
|
#include "leveldb/slice.h"
|
|
#include "util/coding.h"
|
|
#include "utilities/merge_operators.h"
|
|
|
|
|
|
using namespace leveldb;
|
|
|
|
namespace { // anonymous namespace
|
|
|
|
// A 'model' merge operator with uint64 addition semantics
|
|
class UInt64AddOperator : public MergeOperator {
|
|
public:
|
|
virtual void Merge(const Slice& key,
|
|
const Slice* existing_value,
|
|
const Slice& value,
|
|
std::string* new_value,
|
|
Logger* logger) const override {
|
|
// assuming 0 if no existing value
|
|
uint64_t existing = 0;
|
|
if (existing_value) {
|
|
if (existing_value->size() == sizeof(uint64_t)) {
|
|
existing = DecodeFixed64(existing_value->data());
|
|
} else {
|
|
// if existing_value is corrupted, treat it as 0
|
|
Log(logger, "existing value corruption, size: %zu > %zu",
|
|
existing_value->size(), sizeof(uint64_t));
|
|
existing = 0;
|
|
}
|
|
}
|
|
|
|
uint64_t operand;
|
|
if (value.size() == sizeof(uint64_t)) {
|
|
operand = DecodeFixed64(value.data());
|
|
} else {
|
|
// if operand is corrupted, treat it as 0
|
|
Log(logger, "operand value corruption, size: %zu > %zu",
|
|
value.size(), sizeof(uint64_t));
|
|
operand = 0;
|
|
}
|
|
|
|
new_value->resize(sizeof(uint64_t));
|
|
EncodeFixed64(&(*new_value)[0], existing + operand);
|
|
|
|
return;
|
|
}
|
|
|
|
virtual const char* Name() const override {
|
|
return "UInt64AddOperator";
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
namespace leveldb {
|
|
|
|
std::shared_ptr<MergeOperator> MergeOperators::CreateUInt64AddOperator() {
|
|
return std::make_shared<UInt64AddOperator>();
|
|
}
|
|
|
|
}
|