Skip to content

Commit 712a3bb

Browse files
author
Andrei Popescu
authored
Add compression settings to CacheSettings. (#859)
This commit adds the possible compression settings to the CacheSettings structure to be passed on to leveldb. Possible values so far are only no compression or Snappy compression. Nevertheless there are user who will profit from not applying any compression if the data added to the cache is already compressed, e.g. protobuf. Additionally this commit adds filter policy to bloom filter with 10 bits precision which will enhance reading speeds. Relates-To: OLPEDGE-1984, OLPEDGE-1985 Signed-off-by: Andrei Popescu <andrei.popescu@here.com>
1 parent 8705ba2 commit 712a3bb

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

olp-cpp-sdk-core/include/olp/core/cache/CacheSettings.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ enum class EvictionPolicy : unsigned char {
4545
kLeastRecentlyUsed /*!< Evict least recently used key/value. */
4646
};
4747

48+
/**
49+
* @brief Options for database compression.
50+
*/
51+
enum class CompressionType {
52+
kNoCompression, /*!< No compression applied on the data before storing. */
53+
kDefaultCompression /*!< Default compression will be applied to data before
54+
storing. */
55+
};
56+
4857
/**
4958
* @brief Settings for memory and disk caching.
5059
*/
@@ -113,6 +122,20 @@ struct CORE_API CacheSettings {
113122
*/
114123
EvictionPolicy eviction_policy = EvictionPolicy::kLeastRecentlyUsed;
115124

125+
/**
126+
* @brief This flag sets the compression policy to be applied on the database.
127+
*
128+
* In some cases though, when all the data to be inserted is already
129+
* compressed by any means, e.g. protobuf or other serialization protocols, it
130+
* might not be worth to enable any compression at all as it will eat up some
131+
* CPU to compress and decompress the metadata without major gain. This
132+
* parameter is dynamic and can be changed between runs. If changed only new
133+
* values which are added will be using the new compression policy all
134+
* existing entries will remain unchanged. The default value is
135+
* CompressionType::kDefaultCompression.
136+
*/
137+
CompressionType compression = CompressionType::kDefaultCompression;
138+
116139
/**
117140
* @brief The path to the protected (read-only) cache.
118141
*

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ size_t StoreExpiry(const std::string& key, leveldb::WriteBatch& batch,
8585
return expiry_key.size() + time_str.size();
8686
}
8787

88+
leveldb::CompressionType GetCompression(
89+
olp::cache::CompressionType compression) {
90+
return (compression == olp::cache::CompressionType::kNoCompression)
91+
? leveldb::kNoCompression
92+
: leveldb::kSnappyCompression;
93+
}
94+
8895
} // namespace
8996

9097
namespace olp {
@@ -471,6 +478,7 @@ DefaultCache::StorageOpenResult DefaultCacheImpl::SetupStorage() {
471478
storage_settings.enforce_immediate_flush =
472479
settings_.enforce_immediate_flush;
473480
storage_settings.max_file_size = settings_.max_file_size;
481+
storage_settings.compression = GetCompression(settings_.compression);
474482

475483
mutable_cache_ = std::make_unique<DiskCache>();
476484
auto status = mutable_cache_->Open(settings_.disk_path_mutable.get(),

olp-cpp-sdk-core/src/cache/DiskCache.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ OpenResult DiskCache::Open(const std::string& data_path,
183183
max_size_ = settings.max_disk_storage;
184184

185185
leveldb::Options open_options;
186+
open_options.compression = settings.compression;
186187
open_options.info_log = leveldb_logger_.get();
187188
open_options.write_buffer_size = settings.max_chunk_size;
189+
open_options.filter_policy = leveldb::NewBloomFilterPolicy(10);
188190
if (settings.max_file_size != 0) {
189191
open_options.max_file_size = settings.max_file_size;
190192
}

olp-cpp-sdk-core/src/cache/DiskCache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,23 @@ enum class OpenResult {
6060
struct StorageSettings {
6161
/// The maximum allowed size of storage on disk in bytes.
6262
uint64_t max_disk_storage = 0u;
63+
6364
/// The maximum size of data in memory before it gets flushed to disk.
6465
/// Data is kept in memory until its size reaches this value and then data is
6566
/// flushed to disk. A maximum write buffer of 32 MB is most optimal even for
6667
/// batch imports.
6768
uint64_t max_chunk_size = 32 * 1024u * 1024u;
69+
6870
/// Flag to enable double-writes to disk to avoid data losses between ignition
6971
/// cycles.
7072
bool enforce_immediate_flush = true;
73+
7174
/// Maximum size of one file in storage, default 2MBytes.
7275
size_t max_file_size = 2 * 1024u * 1024u;
76+
77+
/// Compression type to be applied on the data before storing it.
78+
leveldb::CompressionType compression =
79+
leveldb::CompressionType::kSnappyCompression;
7380
};
7481

7582
/**

0 commit comments

Comments
 (0)