Files
ternfs-XTXMarkets/cpp/core/RocksDBUtils.cpp
2023-11-09 13:38:32 +00:00

72 lines
3.0 KiB
C++

#include <rocksdb/env.h>
#include <rocksdb/merge_operator.h>
#include <rocksdb/slice.h>
#include "RocksDBUtils.hpp"
// 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).
namespace { // anonymous namespace
// A 'model' merge operator with int64 addition semantics
// Implemented as an AssociativeMergeOperator for simplicity and example.
class Int64AddOperator : public rocksdb::AssociativeMergeOperator {
public:
bool Merge(
const rocksdb::Slice& key, const rocksdb::Slice* existing_value,
const rocksdb::Slice& value, std::string* new_value,
rocksdb::Logger* logger
) const override {
int64_t orig_value = 0;
if (existing_value) {
orig_value = ExternalValue<I64Value>::FromSlice(*existing_value)().i64();
}
int64_t operand = ExternalValue<I64Value>::FromSlice(value)().i64();
ALWAYS_ASSERT(new_value);
new_value->resize(I64Value::MAX_SIZE);
ExternalValue<I64Value>(*new_value)().setI64(orig_value + operand);
return true;
}
static const char* kClassName() { return "Int64AddOperator"; }
static const char* kNickName() { return "int64add"; }
const char* Name() const override { return kClassName(); }
const char* NickName() const override { return kNickName(); }
};
} // anonymous namespace
std::shared_ptr<rocksdb::MergeOperator> CreateInt64AddOperator() {
return std::make_shared<Int64AddOperator>();
}
// These are low hanging fruits (int stats that are probably useful), eventually
// I want to parse level info, histograms, etc.
static const std::vector<std::pair<const std::string&, std::string>> rocksDBIntStats = {
{rocksdb::DB::Properties::kBlockCacheCapacity, "block_cache_capacity"},
{rocksdb::DB::Properties::kBlockCacheUsage, "block_cache_usage"},
{rocksdb::DB::Properties::kBlockCachePinnedUsage, "block_cache_pinned_usage"},
{rocksdb::DB::Properties::kBackgroundErrors, "background_errors"},
{rocksdb::DB::Properties::kCurSizeAllMemTables, "cur_size_all_mem_tables"},
{rocksdb::DB::Properties::kSizeAllMemTables, "size_all_mem_tables"},
{rocksdb::DB::Properties::kEstimateNumKeys, "estimate_num_keys"},
{rocksdb::DB::Properties::kEstimateTableReadersMem, "estimate_table_readers_mem"},
{rocksdb::DB::Properties::kEstimateLiveDataSize, "estimate_live_data_size"},
{rocksdb::DB::Properties::kEstimatePendingCompactionBytes, "estimate_pending_compaction_bytes"},
};
void rocksDBMetrics(Env& env, rocksdb::DB* db, std::unordered_map<std::string, uint64_t>& stats) {
for (const auto& [prop, name]: rocksDBIntStats) {
uint64_t v;
bool ok = db->GetIntProperty(prop, &v);
if (ok) {
stats.emplace(std::make_pair(name, v));
} else {
LOG_INFO(env, "Could not read RocksDB property %s, will not be included in the stats", prop);
}
}
}