Packages
rocksdb
2.4.1
3.1.1
3.1.0
3.0.0
2.6.2
2.6.1
retired
2.6.0
retired
2.5.0
2.4.1
2.4.0
2.3.0
2.2.0
2.1.0
2.0.0
1.9.0
1.8.0
1.7.0
1.6.0
1.5.1
1.5.0
1.4.0
1.3.2
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.0
0.26.2
0.26.1
0.26.0
0.25.0
0.24.0
0.23.3
0.23.2
0.23.1
0.23.0
0.22.0
0.21.0
0.20.1
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.0
0.14.0
0.13.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
RocksDB for Erlang
Current section
Files
Jump to
Current section
Files
deps/rocksdb/test_util/secondary_cache_test_util.h
// Copyright (c) Meta Platforms, Inc. and affiliates.
// 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).
#pragma once
#include <gtest/gtest.h>
#include <functional>
#include "rocksdb/advanced_cache.h"
namespace ROCKSDB_NAMESPACE {
namespace secondary_cache_test_util {
struct TestCreateContext : public Cache::CreateContext {
void SetFailCreate(bool fail) { fail_create_ = fail; }
bool fail_create_ = false;
};
class WithCacheType : public TestCreateContext {
public:
WithCacheType() {}
virtual ~WithCacheType() {}
class TestItem {
public:
TestItem(const char* buf, size_t size) : buf_(new char[size]), size_(size) {
memcpy(buf_.get(), buf, size);
}
~TestItem() = default;
char* Buf() { return buf_.get(); }
[[nodiscard]] size_t Size() const { return size_; }
std::string ToString() { return std::string(Buf(), Size()); }
private:
std::unique_ptr<char[]> buf_;
size_t size_;
};
static constexpr auto kLRU = "lru";
static constexpr auto kFixedHyperClock = "fixed_hyper_clock";
static constexpr auto kAutoHyperClock = "auto_hyper_clock";
// For options other than capacity
size_t estimated_value_size_ = 1;
virtual const std::string& Type() const = 0;
static bool IsHyperClock(const std::string& type) {
return type == kFixedHyperClock || type == kAutoHyperClock;
}
bool IsHyperClock() const { return IsHyperClock(Type()); }
std::shared_ptr<Cache> NewCache(
size_t capacity,
std::function<void(ShardedCacheOptions&)> modify_opts_fn = {}) {
const auto& type = Type();
if (type == kLRU) {
LRUCacheOptions lru_opts;
lru_opts.capacity = capacity;
lru_opts.hash_seed = 0; // deterministic tests
if (modify_opts_fn) {
modify_opts_fn(lru_opts);
}
return lru_opts.MakeSharedCache();
}
if (IsHyperClock(type)) {
HyperClockCacheOptions hc_opts{
capacity, type == kFixedHyperClock ? estimated_value_size_ : 0};
hc_opts.min_avg_entry_charge =
std::max(size_t{1}, estimated_value_size_ / 2);
hc_opts.hash_seed = 0; // deterministic tests
if (modify_opts_fn) {
modify_opts_fn(hc_opts);
}
return hc_opts.MakeSharedCache();
}
assert(false);
return nullptr;
}
std::shared_ptr<Cache> NewCache(
size_t capacity, int num_shard_bits, bool strict_capacity_limit,
CacheMetadataChargePolicy charge_policy = kDontChargeCacheMetadata) {
return NewCache(capacity, [=](ShardedCacheOptions& opts) {
opts.num_shard_bits = num_shard_bits;
opts.strict_capacity_limit = strict_capacity_limit;
opts.metadata_charge_policy = charge_policy;
});
}
std::shared_ptr<Cache> NewCache(
size_t capacity, int num_shard_bits, bool strict_capacity_limit,
std::shared_ptr<SecondaryCache> secondary_cache) {
return NewCache(capacity, [=](ShardedCacheOptions& opts) {
opts.num_shard_bits = num_shard_bits;
opts.strict_capacity_limit = strict_capacity_limit;
opts.metadata_charge_policy = kDontChargeCacheMetadata;
opts.secondary_cache = secondary_cache;
});
}
static const Cache::CacheItemHelper* GetHelper(
CacheEntryRole r = CacheEntryRole::kDataBlock,
bool secondary_compatible = true, bool fail = false);
static const Cache::CacheItemHelper* GetHelperFail(
CacheEntryRole r = CacheEntryRole::kDataBlock);
};
class WithCacheTypeParam : public WithCacheType,
public testing::WithParamInterface<std::string> {
const std::string& Type() const override { return GetParam(); }
};
constexpr auto kLRU = WithCacheType::kLRU;
constexpr auto kFixedHyperClock = WithCacheType::kFixedHyperClock;
constexpr auto kAutoHyperClock = WithCacheType::kAutoHyperClock;
inline auto GetTestingCacheTypes() {
return testing::Values(std::string(kLRU), std::string(kFixedHyperClock),
std::string(kAutoHyperClock));
}
} // namespace secondary_cache_test_util
} // namespace ROCKSDB_NAMESPACE