@@ -4,6 +4,8 @@ use std::{
44} ;
55
66use log:: { debug, warn} ;
7+ use spacetimedb_fs_utils:: compression:: Zstd ;
8+ pub use spacetimedb_fs_utils:: compression:: { CompressOnce , CompressionStats } ;
79
810use crate :: {
911 commit:: Commit ,
@@ -121,7 +123,13 @@ pub trait Repo: Clone + fmt::Display {
121123 fn remove_segment ( & self , offset : u64 ) -> io:: Result < ( ) > ;
122124
123125 /// Compress a segment in storage, marking it as immutable.
124- fn compress_segment ( & self , offset : u64 ) -> io:: Result < ( ) > ;
126+ fn compress_segment ( & self , offset : u64 ) -> io:: Result < CompressionStats > {
127+ self . compress_segment_with ( offset, segment_compressor ( ) )
128+ }
129+
130+ /// Compress a segment using a supplied [CompressOnce], marking it as
131+ /// immutable.
132+ fn compress_segment_with ( & self , offset : u64 , f : impl CompressOnce ) -> io:: Result < CompressionStats > ;
125133
126134 /// Traverse all segments in this repository and return list of their
127135 /// offsets, sorted in ascending order.
@@ -164,8 +172,8 @@ impl<T: Repo> Repo for &T {
164172 T :: remove_segment ( self , offset)
165173 }
166174
167- fn compress_segment ( & self , offset : u64 ) -> io:: Result < ( ) > {
168- T :: compress_segment ( self , offset)
175+ fn compress_segment_with ( & self , offset : u64 , f : impl CompressOnce ) -> io:: Result < CompressionStats > {
176+ T :: compress_segment_with ( self , offset, f )
169177 }
170178
171179 fn existing_offsets ( & self ) -> io:: Result < Vec < u64 > > {
@@ -408,6 +416,17 @@ pub fn open_segment_reader<R: Repo>(
408416 . map_err ( |source| with_segment_context ( "reading segment header" , repo, offset, source) )
409417}
410418
419+ /// Obtain the canonical [CompressOnce] compressor for segments.
420+ ///
421+ /// The compressor will create seekable [Zstd] archives with a max frame size
422+ /// of 4KiB. That is, seeking to an arbitrary byte offset (of the uncompressed
423+ /// segment) within the archive will decompress 4KiB of data on average.
424+ pub fn segment_compressor ( ) -> Zstd {
425+ Zstd {
426+ max_frame_size : Some ( 0x1000 ) ,
427+ }
428+ }
429+
411430fn segment_label < R : Repo > ( repo : & R , offset : u64 ) -> String {
412431 repo. segment_file_path ( offset)
413432 . unwrap_or_else ( || format ! ( "offset {offset}" ) )
0 commit comments