summaryrefslogtreecommitdiff
path: root/port
diff options
context:
space:
mode:
authorleveldb Team <no-reply@google.com>2023-03-30 07:12:55 +0000
committerAustin Sullivan <asully@chromium.org>2023-04-20 18:08:55 +0000
commitc61238dcf39bdcfb6ef27abbda35b4cbf42b9002 (patch)
tree445a68645c77033ce7d72e9c5116fa4e933108fe /port
parent77d66aaf3edd6db0f1bfb4308fdc68c62fcc7bf6 (diff)
downloadleveldb-c61238dcf39bdcfb6ef27abbda35b4cbf42b9002.tar.gz
Support Zstd compression level in Leveldb
PiperOrigin-RevId: 520556840
Diffstat (limited to 'port')
-rw-r--r--port/port_example.h3
-rw-r--r--port/port_stdcxx.h7
2 files changed, 8 insertions, 2 deletions
diff --git a/port/port_example.h b/port/port_example.h
index b1a1c32..5d50ffa 100644
--- a/port/port_example.h
+++ b/port/port_example.h
@@ -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.
// 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
// buffer, store the size of the uncompressed data in *result and
diff --git a/port/port_stdcxx.h b/port/port_stdcxx.h
index ca961e6..6f503f6 100644
--- a/port/port_stdcxx.h
+++ b/port/port_stdcxx.h
@@ -29,6 +29,7 @@
#include <snappy.h>
#endif // HAVE_SNAPPY
#if HAVE_ZSTD
+#define ZSTD_STATIC_LINKING_ONLY // For ZSTD_compressionParameters.
#include <zstd.h>
#endif // HAVE_ZSTD
@@ -129,7 +130,7 @@ inline bool Snappy_Uncompress(const char* input, size_t length, char* output) {
#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) {
#if HAVE_ZSTD
// Get the MaxCompressedLength.
@@ -139,6 +140,9 @@ inline bool Zstd_Compress(const char* input, size_t length,
}
output->resize(outlen);
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);
ZSTD_freeCCtx(ctx);
if (ZSTD_isError(outlen)) {
@@ -148,6 +152,7 @@ inline bool Zstd_Compress(const char* input, size_t length,
return true;
#else
// Silence compiler warnings about unused arguments.
+ (void)level;
(void)input;
(void)length;
(void)output;