diff --git a/cache/cache.cc b/cache/cache.cc index a7d9f86bf..2904d5ece 100644 --- a/cache/cache.cc +++ b/cache/cache.cc @@ -34,13 +34,61 @@ static std::unordered_map OptionType::kDouble, OptionVerificationType::kNormal, OptionTypeFlags::kMutable}}, }; + +static std::unordered_map + comp_sec_cache_options_type_info = { + {"capacity", + {offsetof(struct CompressedSecondaryCacheOptions, capacity), + OptionType::kSizeT, OptionVerificationType::kNormal, + OptionTypeFlags::kMutable}}, + {"num_shard_bits", + {offsetof(struct CompressedSecondaryCacheOptions, num_shard_bits), + OptionType::kInt, OptionVerificationType::kNormal, + OptionTypeFlags::kMutable}}, + {"compression_type", + {offsetof(struct CompressedSecondaryCacheOptions, compression_type), + OptionType::kCompressionType, OptionVerificationType::kNormal, + OptionTypeFlags::kMutable}}, + {"compress_format_version", + {offsetof(struct CompressedSecondaryCacheOptions, + compress_format_version), + OptionType::kUInt32T, OptionVerificationType::kNormal, + OptionTypeFlags::kMutable}}, +}; #endif // ROCKSDB_LITE Status SecondaryCache::CreateFromString( const ConfigOptions& config_options, const std::string& value, std::shared_ptr* result) { - return LoadSharedObject(config_options, value, nullptr, - result); + if (value.find("compressed_secondary_cache://") == 0) { + std::string args = value; + args.erase(0, std::strlen("compressed_secondary_cache://")); + Status status; + std::shared_ptr sec_cache; + +#ifndef ROCKSDB_LITE + CompressedSecondaryCacheOptions sec_cache_opts; + status = OptionTypeInfo::ParseStruct(config_options, "", + &comp_sec_cache_options_type_info, "", + args, &sec_cache_opts); + if (status.ok()) { + sec_cache = NewCompressedSecondaryCache(sec_cache_opts); + } + +#else + (void)config_options; + status = Status::NotSupported( + "Cannot load compressed secondary cache in LITE mode ", args); +#endif //! ROCKSDB_LITE + + if (status.ok()) { + result->swap(sec_cache); + } + return status; + } else { + return LoadSharedObject(config_options, value, nullptr, + result); + } } Status Cache::CreateFromString(const ConfigOptions& config_options, diff --git a/cache/compressed_secondary_cache_test.cc b/cache/compressed_secondary_cache_test.cc index 63f6d1703..757625ba9 100644 --- a/cache/compressed_secondary_cache_test.cc +++ b/cache/compressed_secondary_cache_test.cc @@ -10,6 +10,8 @@ #include "memory/jemalloc_nodump_allocator.h" #include "memory/memory_allocator.h" +#include "rocksdb/convenience.h" +#include "rocksdb/secondary_cache.h" #include "test_util/testharness.h" #include "test_util/testutil.h" #include "util/compression.h" @@ -79,37 +81,7 @@ class CompressedSecondaryCacheTest : public testing::Test { void SetFailCreate(bool fail) { fail_create_ = fail; } - void BasicTest(bool sec_cache_is_compressed, bool use_jemalloc) { - CompressedSecondaryCacheOptions opts; - opts.capacity = 2048; - opts.num_shard_bits = 0; - opts.metadata_charge_policy = kDontChargeCacheMetadata; - - if (sec_cache_is_compressed) { - if (!LZ4_Supported()) { - ROCKSDB_GTEST_SKIP("This test requires LZ4 support."); - opts.compression_type = CompressionType::kNoCompression; - } - } else { - opts.compression_type = CompressionType::kNoCompression; - } - - if (use_jemalloc) { - JemallocAllocatorOptions jopts; - std::shared_ptr allocator; - std::string msg; - if (JemallocNodumpAllocator::IsSupported(&msg)) { - Status s = NewJemallocNodumpAllocator(jopts, &allocator); - if (s.ok()) { - opts.memory_allocator = allocator; - } - } else { - ROCKSDB_GTEST_BYPASS("JEMALLOC not supported"); - } - } - std::shared_ptr sec_cache = - NewCompressedSecondaryCache(opts); - + void BasicTestHelper(std::shared_ptr sec_cache) { bool is_in_sec_cache{true}; // Lookup an non-existent key. std::unique_ptr handle0 = @@ -160,6 +132,38 @@ class CompressedSecondaryCacheTest : public testing::Test { sec_cache.reset(); } + void BasicTest(bool sec_cache_is_compressed, bool use_jemalloc) { + CompressedSecondaryCacheOptions opts; + opts.capacity = 2048; + opts.num_shard_bits = 0; + opts.metadata_charge_policy = kDontChargeCacheMetadata; + + if (sec_cache_is_compressed) { + if (!LZ4_Supported()) { + ROCKSDB_GTEST_SKIP("This test requires LZ4 support."); + opts.compression_type = CompressionType::kNoCompression; + } + } else { + opts.compression_type = CompressionType::kNoCompression; + } + + if (use_jemalloc) { + JemallocAllocatorOptions jopts; + std::shared_ptr allocator; + std::string msg; + if (JemallocNodumpAllocator::IsSupported(&msg)) { + Status s = NewJemallocNodumpAllocator(jopts, &allocator); + if (s.ok()) { + opts.memory_allocator = allocator; + } + } else { + ROCKSDB_GTEST_BYPASS("JEMALLOC not supported"); + } + } + std::shared_ptr sec_cache = + NewCompressedSecondaryCache(opts); + } + void FailsTest(bool sec_cache_is_compressed) { CompressedSecondaryCacheOptions secondary_cache_opts; if (sec_cache_is_compressed) { @@ -547,6 +551,42 @@ TEST_F(CompressedSecondaryCacheTest, BasicTest(true, true); } +#ifndef ROCKSDB_LITE + +TEST_F(CompressedSecondaryCacheTest, BasicTestFromStringWithNoCompression) { + std::string sec_cache_uri = + "compressed_secondary_cache://" + "capacity=2048;num_shard_bits=0;compression_type=kNoCompression"; + std::shared_ptr sec_cache; + Status s = SecondaryCache::CreateFromString(ConfigOptions(), sec_cache_uri, + &sec_cache); + EXPECT_OK(s); + BasicTestHelper(sec_cache); +} + +TEST_F(CompressedSecondaryCacheTest, BasicTestFromStringWithCompression) { + std::string sec_cache_uri; + if (LZ4_Supported()) { + sec_cache_uri = + "compressed_secondary_cache://" + "capacity=2048;num_shard_bits=0;compression_type=kLZ4Compression;" + "compress_format_version=2"; + } else { + ROCKSDB_GTEST_SKIP("This test requires LZ4 support."); + sec_cache_uri = + "compressed_secondary_cache://" + "capacity=2048;num_shard_bits=0;compression_type=kNoCompression"; + } + + std::shared_ptr sec_cache; + Status s = SecondaryCache::CreateFromString(ConfigOptions(), sec_cache_uri, + &sec_cache); + EXPECT_OK(s); + BasicTestHelper(sec_cache); +} + +#endif // ROCKSDB_LITE + TEST_F(CompressedSecondaryCacheTest, FailsTestWithNoCompression) { FailsTest(false); }