@@ -2,7 +2,10 @@ use crate::common::{
22 Engine , PREFIX_SIZE , Pre , PreBytes , RESERVED_ID_CNT , RawKey , RawValue ,
33 vsdb_get_base_dir, vsdb_set_base_dir,
44} ;
5- use fjall:: { Config , Keyspace , Partition , PartitionCreateOptions } ;
5+ use fjall:: {
6+ CompressionType , Database , Keyspace , KeyspaceCreateOptions , PersistMode ,
7+ config:: CompressionPolicy ,
8+ } ;
69use parking_lot:: Mutex ;
710use ruc:: * ;
811use std:: {
@@ -21,10 +24,32 @@ const SHARD_CNT: usize = 16;
2124const META_KEY_MAX_KEYLEN : [ u8 ; 1 ] = [ u8:: MAX ] ;
2225const META_KEY_PREFIX_ALLOCATOR : [ u8 ; 1 ] = [ u8:: MIN ] ;
2326
27+ fn keyspace_create_options ( ) -> KeyspaceCreateOptions {
28+ let mut opts = KeyspaceCreateOptions :: default ( ) ;
29+
30+ #[ cfg( feature = "compress" ) ]
31+ {
32+ // When compress feature is enabled, use LZ4 compression for data blocks
33+ // L0 and L1 are uncompressed for performance, L2+ use LZ4
34+ opts = opts. data_block_compression_policy ( CompressionPolicy :: new ( [
35+ CompressionType :: None ,
36+ CompressionType :: None ,
37+ CompressionType :: Lz4 ,
38+ ] ) ) ;
39+ }
40+
41+ #[ cfg( not( feature = "compress" ) ) ]
42+ {
43+ opts = opts. data_block_compression_policy ( CompressionPolicy :: disabled ( ) ) ;
44+ }
45+
46+ opts
47+ }
48+
2449pub struct FjallEngine {
25- meta : Partition ,
26- shards : Vec < Keyspace > ,
27- shards_parts : Vec < Vec < Partition > > ,
50+ meta : Keyspace ,
51+ shards : Vec < Database > ,
52+ shards_parts : Vec < Vec < Keyspace > > ,
2853 prefix_allocator : PreAllocator ,
2954 max_keylen : AtomicUsize ,
3055}
@@ -36,7 +61,7 @@ impl FjallEngine {
3661 }
3762
3863 #[ inline( always) ]
39- fn get_part ( & self , prefix : PreBytes ) -> & Partition {
64+ fn get_part ( & self , prefix : PreBytes ) -> & Keyspace {
4065 let shard_idx = self . get_shard_idx ( prefix) ;
4166 let part_idx = self . area_idx ( prefix) ;
4267 & self . shards_parts [ shard_idx] [ part_idx]
@@ -70,25 +95,22 @@ impl Engine for FjallEngine {
7095
7196 for i in 0 ..SHARD_CNT {
7297 let dir = base_dir. join ( format ! ( "shard_{}" , i) ) ;
73- let ks = Config :: new ( dir) . open ( ) . c ( d ! ( ) ) ?;
98+ let db = Database :: builder ( & dir) . open ( ) . c ( d ! ( ) ) ?;
7499
75100 let mut parts = Vec :: with_capacity ( DATA_SET_NUM ) ;
76101 for j in 0 ..DATA_SET_NUM {
77- let p = ks
78- . open_partition (
79- & format ! ( "part_{}" , j) ,
80- PartitionCreateOptions :: default ( ) ,
81- )
102+ let p = db
103+ . keyspace ( & format ! ( "part_{}" , j) , keyspace_create_options)
82104 . c ( d ! ( ) ) ?;
83105 parts. push ( p) ;
84106 }
85- shards. push ( ks ) ;
107+ shards. push ( db ) ;
86108 shards_parts. push ( parts) ;
87109 }
88110
89- // Use a dedicated partition in shard 0 for meta
111+ // Use a dedicated keyspace in shard 0 for meta
90112 let meta = shards[ 0 ]
91- . open_partition ( "meta" , PartitionCreateOptions :: default ( ) )
113+ . keyspace ( "meta" , keyspace_create_options )
92114 . c ( d ! ( ) ) ?;
93115
94116 let ( prefix_allocator, initial_value) = PreAllocator :: init ( ) ;
@@ -142,16 +164,16 @@ impl Engine for FjallEngine {
142164 }
143165
144166 fn flush ( & self ) {
145- for ks in & self . shards {
146- ks . persist ( fjall :: PersistMode :: SyncAll ) . unwrap ( ) ;
167+ for db in & self . shards {
168+ db . persist ( PersistMode :: SyncAll ) . unwrap ( ) ;
147169 }
148170 }
149171
150172 fn iter ( & self , meta_prefix : PreBytes ) -> FjallIter {
151173 let part = self . get_part ( meta_prefix) ;
152174 let inner = part
153175 . prefix ( meta_prefix)
154- . map ( |res| res . map ( |( k, v) | ( k. to_vec ( ) , v. to_vec ( ) ) ) ) ;
176+ . map ( |guard| guard . into_inner ( ) . map ( |( k, v) | ( k. to_vec ( ) , v. to_vec ( ) ) ) ) ;
155177
156178 FjallIter {
157179 inner : Box :: new ( inner) ,
@@ -196,7 +218,7 @@ impl Engine for FjallEngine {
196218
197219 let inner = part
198220 . range ( ( start, end) )
199- . map ( |res| res . map ( |( k, v) | ( k. to_vec ( ) , v. to_vec ( ) ) ) ) ;
221+ . map ( |guard| guard . into_inner ( ) . map ( |( k, v) | ( k. to_vec ( ) , v. to_vec ( ) ) ) ) ;
200222
201223 FjallIter {
202224 inner : Box :: new ( inner) ,
0 commit comments