Browse Source

Support Zstd compression level in Leveldb

PiperOrigin-RevId: 520556840
master
leveldb Team 1 year ago
committed by Austin Sullivan
parent
commit
c61238dcf3
6 changed files with 30 additions and 6 deletions
  1. +15
    -2
      benchmarks/db_bench.cc
  2. +4
    -0
      include/leveldb/options.h
  3. +2
    -1
      port/port_example.h
  4. +6
    -1
      port/port_stdcxx.h
  5. +2
    -1
      table/table_builder.cc
  6. +1
    -1
      table/table_test.cc

+ 15
- 2
benchmarks/db_bench.cc View File

@ -126,6 +126,9 @@ static bool FLAGS_compression = true;
// Use the db with the following name. // Use the db with the following name.
static const char* FLAGS_db = nullptr; static const char* FLAGS_db = nullptr;
// ZSTD compression level to try out
static int FLAGS_zstd_compression_level = 1;
namespace leveldb { namespace leveldb {
namespace { namespace {
@ -779,11 +782,21 @@ class Benchmark {
} }
void ZstdCompress(ThreadState* thread) { void ZstdCompress(ThreadState* thread) {
Compress(thread, "zstd", &port::Zstd_Compress);
Compress(thread, "zstd",
[](const char* input, size_t length, std::string* output) {
return port::Zstd_Compress(FLAGS_zstd_compression_level, input,
length, output);
});
} }
void ZstdUncompress(ThreadState* thread) { void ZstdUncompress(ThreadState* thread) {
Uncompress(thread, "zstd", &port::Zstd_Compress, &port::Zstd_Uncompress);
Uncompress(
thread, "zstd",
[](const char* input, size_t length, std::string* output) {
return port::Zstd_Compress(FLAGS_zstd_compression_level, input,
length, output);
},
&port::Zstd_Uncompress);
} }
void Open() { void Open() {

+ 4
- 0
include/leveldb/options.h View File

@ -131,6 +131,10 @@ struct LEVELDB_EXPORT Options {
// efficiently detect that and will switch to uncompressed mode. // efficiently detect that and will switch to uncompressed mode.
CompressionType compression = kSnappyCompression; CompressionType compression = kSnappyCompression;
// Compression level for zstd.
// Currently only the range [-5,22] is supported. Default is 1.
int zstd_compression_level = 1;
// EXPERIMENTAL: If true, append to existing MANIFEST and log files // EXPERIMENTAL: If true, append to existing MANIFEST and log files
// when a database is opened. This can significantly speed up open. // when a database is opened. This can significantly speed up open.
// //

+ 2
- 1
port/port_example.h View File

@ -83,7 +83,8 @@ bool Snappy_Uncompress(const char* input_data, size_t input_length,
// Store the zstd compression of "input[0,input_length-1]" in *output. // Store the zstd compression of "input[0,input_length-1]" in *output.
// Returns false if zstd is not supported by this port. // Returns false if zstd is not supported by this port.
bool Zstd_Compress(const char* input, size_t input_length, std::string* output);
bool Zstd_Compress(int level, const char* input, size_t input_length,
std::string* output);
// If input[0,input_length-1] looks like a valid zstd compressed // If input[0,input_length-1] looks like a valid zstd compressed
// buffer, store the size of the uncompressed data in *result and // buffer, store the size of the uncompressed data in *result and

+ 6
- 1
port/port_stdcxx.h View File

@ -29,6 +29,7 @@
#include <snappy.h> #include <snappy.h>
#endif // HAVE_SNAPPY #endif // HAVE_SNAPPY
#if HAVE_ZSTD #if HAVE_ZSTD
#define ZSTD_STATIC_LINKING_ONLY // For ZSTD_compressionParameters.
#include <zstd.h> #include <zstd.h>
#endif // HAVE_ZSTD #endif // HAVE_ZSTD
@ -129,7 +130,7 @@ inline bool Snappy_Uncompress(const char* input, size_t length, char* output) {
#endif // HAVE_SNAPPY #endif // HAVE_SNAPPY
} }
inline bool Zstd_Compress(const char* input, size_t length,
inline bool Zstd_Compress(int level, const char* input, size_t length,
std::string* output) { std::string* output) {
#if HAVE_ZSTD #if HAVE_ZSTD
// Get the MaxCompressedLength. // Get the MaxCompressedLength.
@ -139,6 +140,9 @@ inline bool Zstd_Compress(const char* input, size_t length,
} }
output->resize(outlen); output->resize(outlen);
ZSTD_CCtx* ctx = ZSTD_createCCtx(); ZSTD_CCtx* ctx = ZSTD_createCCtx();
ZSTD_compressionParameters parameters =
ZSTD_getCParams(level, std::max(length, size_t{1}), /*dictSize=*/0);
ZSTD_CCtx_setCParams(ctx, parameters);
outlen = ZSTD_compress2(ctx, &(*output)[0], output->size(), input, length); outlen = ZSTD_compress2(ctx, &(*output)[0], output->size(), input, length);
ZSTD_freeCCtx(ctx); ZSTD_freeCCtx(ctx);
if (ZSTD_isError(outlen)) { if (ZSTD_isError(outlen)) {
@ -148,6 +152,7 @@ inline bool Zstd_Compress(const char* input, size_t length,
return true; return true;
#else #else
// Silence compiler warnings about unused arguments. // Silence compiler warnings about unused arguments.
(void)level;
(void)input; (void)input;
(void)length; (void)length;
(void)output; (void)output;

+ 2
- 1
table/table_builder.cc View File

@ -171,7 +171,8 @@ void TableBuilder::WriteBlock(BlockBuilder* block, BlockHandle* handle) {
case kZstdCompression: { case kZstdCompression: {
std::string* compressed = &r->compressed_output; std::string* compressed = &r->compressed_output;
if (port::Zstd_Compress(raw.data(), raw.size(), compressed) &&
if (port::Zstd_Compress(r->options.zstd_compression_level, raw.data(),
raw.size(), compressed) &&
compressed->size() < raw.size() - (raw.size() / 8u)) { compressed->size() < raw.size() - (raw.size() / 8u)) {
block_contents = *compressed; block_contents = *compressed;
} else { } else {

+ 1
- 1
table/table_test.cc View File

@ -791,7 +791,7 @@ static bool CompressionSupported(CompressionType type) {
if (type == kSnappyCompression) { if (type == kSnappyCompression) {
return port::Snappy_Compress(in.data(), in.size(), &out); return port::Snappy_Compress(in.data(), in.size(), &out);
} else if (type == kZstdCompression) { } else if (type == kZstdCompression) {
return port::Zstd_Compress(in.data(), in.size(), &out);
return port::Zstd_Compress(/*level=*/1, in.data(), in.size(), &out);
} }
return false; return false;
} }

Loading…
Cancel
Save