1414// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515// See the License for the specific language governing permissions and
1616// limitations under the License.
17+ use rocksdb:: DBCompressionType ;
18+ use serde:: { Deserialize , Serialize } ;
1719use snafu:: ResultExt ;
20+ use std:: fmt:: Formatter ;
1821use validator:: Validate ;
1922
2023use crate :: de_func:: { parse_bool_from_string, parse_memory, parse_redis_config} ;
2124use crate :: error:: Error ;
2225
26+ /// Compression algorithm for RocksDB column families.
27+ #[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize ) ]
28+ pub enum CompressionType {
29+ None ,
30+ Snappy ,
31+ Lz4 ,
32+ Zstd ,
33+ Zlib ,
34+ Bz2 ,
35+ }
36+
37+ impl CompressionType {
38+ pub fn to_rocksdb ( & self ) -> rocksdb:: DBCompressionType {
39+ match self {
40+ CompressionType :: None => rocksdb:: DBCompressionType :: None ,
41+ CompressionType :: Snappy => rocksdb:: DBCompressionType :: Snappy ,
42+ CompressionType :: Lz4 => rocksdb:: DBCompressionType :: Lz4 ,
43+ CompressionType :: Zstd => rocksdb:: DBCompressionType :: Zstd ,
44+ CompressionType :: Zlib => rocksdb:: DBCompressionType :: Zlib ,
45+ CompressionType :: Bz2 => rocksdb:: DBCompressionType :: Bz2 ,
46+ }
47+ }
48+ }
49+
50+ impl std:: fmt:: Display for CompressionType {
51+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
52+ let s = match self {
53+ CompressionType :: None => "none" ,
54+ CompressionType :: Snappy => "snappy" ,
55+ CompressionType :: Lz4 => "lz4" ,
56+ CompressionType :: Zstd => "zstd" ,
57+ CompressionType :: Zlib => "zlib" ,
58+ CompressionType :: Bz2 => "bz2" ,
59+ } ;
60+ write ! ( f, "{}" , s)
61+ }
62+ }
63+
64+ impl std:: str:: FromStr for CompressionType {
65+ type Err = String ;
66+
67+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
68+ match s. to_lowercase ( ) . as_str ( ) {
69+ "none" | "no" => Ok ( CompressionType :: None ) ,
70+ "snappy" => Ok ( CompressionType :: Snappy ) ,
71+ "lz4" => Ok ( CompressionType :: Lz4 ) ,
72+ "zstd" => Ok ( CompressionType :: Zstd ) ,
73+ "zlib" => Ok ( CompressionType :: Zlib ) ,
74+ "bz2" => Ok ( CompressionType :: Bz2 ) ,
75+ _ => Err ( format ! (
76+ "unknown compression type '{}', valid values: none, snappy, lz4, zstd, zlib, bz2" ,
77+ s
78+ ) ) ,
79+ }
80+ }
81+ }
82+
2383const DEFAULT_BINDING : & str = "127.0.0.1" ;
2484const DEFAULT_PORT : u16 = 7379 ; // Redis-compatible port (7xxx variant of 6379)
2585// config struct define - keeping original config items but using Redis-style format
@@ -46,6 +106,9 @@ pub struct Config {
46106 pub rocksdb_level_compaction_dynamic_level_bytes : bool ,
47107 pub rocksdb_max_open_files : i32 ,
48108 pub rocksdb_target_file_size_base : u64 ,
109+ /// Compression algorithm applied to all column families.
110+ /// Supported values: none, lz4, snappy, zstd, zlib, bz2. Default: lz4.
111+ pub rocksdb_compression_type : CompressionType ,
49112
50113 // Additional fields from original config
51114 pub binding : String ,
@@ -94,6 +157,7 @@ impl Default for Config {
94157 rocksdb_level_compaction_dynamic_level_bytes : true ,
95158 rocksdb_max_open_files : 10000 ,
96159 rocksdb_target_file_size_base : 64 << 20 , // 64MB
160+ rocksdb_compression_type : CompressionType :: Lz4 ,
97161
98162 db_instance_num : 3 ,
99163 db_path : "./db" . to_string ( ) ,
@@ -316,6 +380,15 @@ impl Config {
316380 ) ) ,
317381 } ) ?;
318382 }
383+ "rocksdb-compression-type" => {
384+ config. rocksdb_compression_type =
385+ value. parse ( ) . map_err ( |e| Error :: InvalidConfig {
386+ source : serde_ini:: de:: Error :: Custom ( format ! (
387+ "Invalid rocksdb-compression-type: {}" ,
388+ e
389+ ) ) ,
390+ } ) ?;
391+ }
319392 "raft-node-id" => {
320393 raft_node_id = Some ( value. parse ( ) . map_err ( |e| Error :: InvalidConfig {
321394 source : serde_ini:: de:: Error :: Custom ( format ! (
@@ -395,6 +468,14 @@ impl Config {
395468 options. set_max_open_files ( self . rocksdb_max_open_files ) ;
396469 options. set_target_file_size_base ( self . rocksdb_target_file_size_base ) ;
397470
471+ // Apply compression to all levels (level 0 and 1 use no compression, levels 2+ use configured type)
472+ let compression = self . rocksdb_compression_type . to_rocksdb ( ) ;
473+ let mut compressions = vec ! [ DBCompressionType :: None ; 3 ] ;
474+ for _ in 3 ..self . rocksdb_num_levels {
475+ compressions. push ( compression) ;
476+ }
477+ options. set_compression_per_level ( & compressions) ;
478+
398479 if self . rocksdb_periodic_second > 0 {
399480 options. set_periodic_compaction_seconds ( self . rocksdb_periodic_second ) ;
400481 }
0 commit comments