Skip to content

Commit 636da34

Browse files
committed
Add pluggable compression
1 parent 439bc30 commit 636da34

33 files changed

+978
-90
lines changed

db/blob/blob_file_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
#include "rocksdb/advanced_options.h"
1414
#include "rocksdb/compression_type.h"
15+
#include "rocksdb/compressor.h"
1516
#include "rocksdb/env.h"
1617
#include "rocksdb/rocksdb_namespace.h"
1718
#include "rocksdb/types.h"
18-
#include "util/compressor.h"
1919

2020
namespace ROCKSDB_NAMESPACE {
2121

db/blob/blob_file_reader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "rocksdb/compression_type.h"
1414
#include "rocksdb/rocksdb_namespace.h"
1515
#include "util/autovector.h"
16-
#include "util/compressor.h"
1716

1817
namespace ROCKSDB_NAMESPACE {
1918

db/blob/blob_read_request.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#include <cinttypes>
99

1010
#include "rocksdb/compression_type.h"
11+
#include "rocksdb/compressor.h"
1112
#include "rocksdb/slice.h"
1213
#include "rocksdb/status.h"
1314
#include "util/autovector.h"
14-
#include "util/compressor.h"
1515

1616
namespace ROCKSDB_NAMESPACE {
1717

db/blob/blob_source.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#include "db/blob/blob_file_cache.h"
1515
#include "db/blob/blob_read_request.h"
1616
#include "rocksdb/cache.h"
17+
#include "rocksdb/compressor.h"
1718
#include "rocksdb/rocksdb_namespace.h"
1819
#include "table/block_based/cachable_entry.h"
1920
#include "util/autovector.h"
20-
#include "util/compressor.h"
2121

2222
namespace ROCKSDB_NAMESPACE {
2323

db/compact_files_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525

2626
#include "db/db_impl/db_impl.h"
2727
#include "port/port.h"
28+
#include "rocksdb/compressor.h"
2829
#include "rocksdb/db.h"
2930
#include "rocksdb/env.h"
3031
#include "test_util/sync_point.h"
3132
#include "test_util/testharness.h"
3233
#include "util/cast_util.h"
33-
#include "util/compressor.h"
3434
#include "util/string_util.h"
3535

3636
namespace ROCKSDB_NAMESPACE {

db/db_basic_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "db/db_test_util.h"
2727
#include "options/options_helper.h"
2828
#include "port/stack_trace.h"
29+
#include "rocksdb/compressor.h"
2930
#include "rocksdb/filter_policy.h"
3031
#include "rocksdb/flush_block_policy.h"
3132
#include "rocksdb/merge_operator.h"
@@ -35,7 +36,6 @@
3536
#include "table/block_based/block_based_table_reader.h"
3637
#include "table/block_based/block_builder.h"
3738
#include "test_util/sync_point.h"
38-
#include "util/compressor.h"
3939
#include "util/file_checksum_helper.h"
4040
#include "util/random.h"
4141
#include "utilities/counted_fs.h"

db/db_block_cache_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
#include "db/db_test_util.h"
3535
#include "env/unique_id_gen.h"
3636
#include "port/stack_trace.h"
37+
#include "rocksdb/compressor.h"
3738
#include "rocksdb/persistent_cache.h"
3839
#include "rocksdb/statistics.h"
3940
#include "rocksdb/table.h"
4041
#include "rocksdb/table_properties.h"
4142
#include "table/block_based/block_based_table_reader.h"
4243
#include "table/unique_id_impl.h"
43-
#include "util/compressor.h"
4444
#include "util/defer.h"
4545
#include "util/hash.h"
4646
#include "util/math.h"

db/db_options_test.cc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,8 @@ TEST_F(DBOptionsTest, ChangeCompression) {
11541154
compression_used = CompressionType::kLZ4Compression;
11551155
compacted = false;
11561156
ASSERT_OK(dbfull()->SetOptions(
1157-
{{"bottommost_compression", "kSnappyCompression"},
1157+
{{"bottommost_compressor", "nullptr"},
1158+
{"bottommost_compression", "kSnappyCompression"},
11581159
{"bottommost_compression_opts", "0:6:0:0:4:true"}}));
11591160
ASSERT_OK(Put("foo", "foofoofoo"));
11601161
ASSERT_OK(Put("bar", "foofoofoo"));
@@ -1169,6 +1170,26 @@ TEST_F(DBOptionsTest, ChangeCompression) {
11691170
ASSERT_EQ(32767, compression_opt_used.level);
11701171
// Right now parallel_level is not yet allowed to be changed.
11711172

1173+
if (!Zlib_Supported()) {
1174+
return;
1175+
}
1176+
compression_used = CompressionType::kLZ4Compression;
1177+
compacted = false;
1178+
ASSERT_OK(dbfull()->SetOptions(
1179+
{{"bottommost_compressor", "nullptr"},
1180+
{"bottommost_compression", "kZlibCompression"},
1181+
{"bottommost_compression_opts", "0:6:0:0:4:true"}}));
1182+
ASSERT_OK(Put("foo", "foofoofoo"));
1183+
ASSERT_OK(Put("bar", "foofoofoo"));
1184+
ASSERT_OK(Flush());
1185+
ASSERT_OK(Put("foo", "foofoofoo"));
1186+
ASSERT_OK(Put("bar", "foofoofoo"));
1187+
ASSERT_OK(Flush());
1188+
ASSERT_OK(dbfull()->TEST_WaitForCompact());
1189+
ASSERT_TRUE(compacted);
1190+
ASSERT_EQ(CompressionType::kZlibCompression, compression_used);
1191+
ASSERT_EQ(6, compression_opt_used.level);
1192+
11721193
SyncPoint::GetInstance()->DisableProcessing();
11731194
}
11741195

db/db_test2.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "options/options_helper.h"
3333
#include "port/port.h"
3434
#include "port/stack_trace.h"
35+
#include "rocksdb/compressor.h"
3536
#include "rocksdb/experimental.h"
3637
#include "rocksdb/iostats_context.h"
3738
#include "rocksdb/persistent_cache.h"
@@ -40,7 +41,6 @@
4041
#include "rocksdb/utilities/replayer.h"
4142
#include "rocksdb/wal_filter.h"
4243
#include "test_util/testutil.h"
43-
#include "util/compressor.h"
4444
#include "util/random.h"
4545
#include "utilities/fault_injection_env.h"
4646

include/rocksdb/advanced_options.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace ROCKSDB_NAMESPACE {
1919

20+
class Compressor;
2021
class Slice;
2122
class SliceTransform;
2223
class TablePropertiesCollectorFactory;
@@ -529,6 +530,19 @@ struct AdvancedColumnFamilyOptions {
529530
// Dynamically changeable through SetOptions() API
530531
std::vector<CompressionType> compression_per_level;
531532

533+
// Similar to compression_per_level, but the algorithms are encapsulated in
534+
// Compressor objects. This adds the ability to select custom compressors,
535+
// beyond the built-in ones provided through CompressionType.
536+
//
537+
// If compressor_per_level is specified (not empty), it overrides
538+
// compression_per_level.
539+
//
540+
// If compressor_per_level is not specified (empty),
541+
// compression_per_level is applied as described for that option.
542+
//
543+
// Default: empty
544+
std::vector<std::shared_ptr<Compressor>> compressor_per_level;
545+
532546
// Number of levels for this database
533547
int num_levels = 7;
534548

@@ -993,6 +1007,19 @@ struct AdvancedColumnFamilyOptions {
9931007
// Dynamically changeable through the SetOptions() API
9941008
CompressionType blob_compression_type = kNoCompression;
9951009

1010+
// Similar to blob_compression_type, but the algorithm is encapsulated in a
1011+
// Compressor class. This adds the ability to select plugin compressors,
1012+
// beyond the built-in ones provided through CompressionType.
1013+
//
1014+
// If blob_compressor is specified (not null), it overrides
1015+
// blob_compression_type.
1016+
//
1017+
// If blob_compressor is not specified (null), blob_compression_type is
1018+
// applied.
1019+
//
1020+
// Default: nullptr
1021+
std::shared_ptr<Compressor> blob_compressor = nullptr;
1022+
9961023
// Enables garbage collection of blobs. Blob GC is performed as part of
9971024
// compaction. Valid blobs residing in blob files older than a cutoff get
9981025
// relocated to new files as they are encountered during compaction, which

include/rocksdb/compression_type.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ enum CompressionType : unsigned char {
3333
// eventually remove the option from the public API.
3434
kZSTDNotFinalCompression = 0x40,
3535

36+
// Used as block compression type identifier when using a non built-in
37+
// compressor.
38+
kPluginCompression = 0x41,
39+
3640
// kDisableCompressionOption is used to disable some compression options.
3741
kDisableCompressionOption = 0xff,
3842
};

util/compressor.h renamed to include/rocksdb/compressor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ class Compressor : public Customizable {
164164
static std::vector<std::string> GetDictSupported();
165165

166166
// Get the numeric type associated with this compressor
167-
virtual CompressionType GetCompressionType() const = 0;
167+
virtual CompressionType GetCompressionType() const {
168+
return kPluginCompression;
169+
};
168170

169171
// Whether the compressor is supported.
170172
// For example, a compressor can implement this method to verify its

include/rocksdb/options.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ struct ColumnFamilyOptions : public AdvancedColumnFamilyOptions {
241241
// Dynamically changeable through SetOptions() API
242242
CompressionType compression;
243243

244+
// Similar to compression, but the algorithm is encapsulated in a Compressor
245+
// class. This adds the ability to select plugin compressors, beyond the
246+
// built-in ones provided through CompressionType.
247+
//
248+
// If compressor is specified (not null), it overrides
249+
// compression/compression_opts (the compressor includes values for its
250+
// options).
251+
//
252+
// If compressor is not specified (null), compression/compression_opts are
253+
// applied as described for those options.
254+
//
255+
// Default: nullptr
256+
std::shared_ptr<Compressor> compressor = nullptr;
257+
244258
// Compression algorithm that will be used for the bottommost level that
245259
// contain files. The behavior for num_levels = 1 is not well defined.
246260
// Right now, with num_levels = 1, all compaction outputs will use
@@ -250,6 +264,22 @@ struct ColumnFamilyOptions : public AdvancedColumnFamilyOptions {
250264
// Default: kDisableCompressionOption (Disabled)
251265
CompressionType bottommost_compression = kDisableCompressionOption;
252266

267+
// Similar to bottommost_compression, but the algorithm is encapsulated in a
268+
// Compressor object. This adds the ability to select custom compressors,
269+
// beyond the built-in ones provided through CompressionType.
270+
//
271+
// If bottommost_compressor is specified (not null), it overrides
272+
// bottommost_compression/compression_opts/bottommost_compression_opts (the
273+
// compressor includes values for its options).
274+
//
275+
// If bottommost_compressor is not specified (null),
276+
// bottommost_compression/compression_opts/bottommost_compression_opts are
277+
// applied as described for those options.
278+
//
279+
// Default: nullptr (equivalent to bottommost_compression =
280+
// kDisableCompressionOption)
281+
std::shared_ptr<Compressor> bottommost_compressor = nullptr;
282+
253283
// different options for compression algorithms used by bottommost_compression
254284
// if it is enabled. To enable it, please see the definition of
255285
// CompressionOptions. Behavior for num_levels = 1 is the same as
@@ -1920,6 +1950,18 @@ struct CompactionOptions {
19201950
// according to the `ColumnFamilyOptions`, taking into account the output
19211951
// level if `compression_per_level` is specified.
19221952
CompressionType compression;
1953+
1954+
// Similar to compression, but the algorithm is encapsulated in a Compressor
1955+
// class. This adds the ability to select plugin compressors, beyond the
1956+
// built-in ones provided through CompressionType.
1957+
//
1958+
// If compressor is specified (not nullptr), it overrides compression.
1959+
//
1960+
// If compressor is not specified (nullptr), compression is applied.
1961+
//
1962+
// Default: nullptr
1963+
std::shared_ptr<Compressor> compressor;
1964+
19231965
// Compaction will create files of size `output_file_size_limit`.
19241966
// Default: MAX, which means that compaction will create a single file
19251967
uint64_t output_file_size_limit;
@@ -1928,6 +1970,7 @@ struct CompactionOptions {
19281970

19291971
CompactionOptions()
19301972
: compression(kSnappyCompression),
1973+
compressor(nullptr),
19311974
output_file_size_limit(std::numeric_limits<uint64_t>::max()),
19321975
max_subcompactions(0) {}
19331976
};

0 commit comments

Comments
 (0)