@@ -4,8 +4,8 @@ use std::{
44} ;
55
66use log:: { debug, warn} ;
7-
8- pub use spacetimedb_fs_utils:: compression:: CompressionStats ;
7+ use spacetimedb_fs_utils :: compression :: Zstd ;
8+ pub use spacetimedb_fs_utils:: compression:: { CompressOnce , CompressionStats } ;
99
1010use crate :: {
1111 commit:: Commit ,
@@ -123,7 +123,13 @@ pub trait Repo: Clone + fmt::Display {
123123 fn remove_segment ( & self , offset : u64 ) -> io:: Result < ( ) > ;
124124
125125 /// Compress a segment in storage, marking it as immutable.
126- fn compress_segment ( & self , offset : u64 ) -> io:: Result < CompressionStats > ;
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 > ;
127133
128134 /// Traverse all segments in this repository and return list of their
129135 /// offsets, sorted in ascending order.
@@ -166,8 +172,8 @@ impl<T: Repo> Repo for &T {
166172 T :: remove_segment ( self , offset)
167173 }
168174
169- fn compress_segment ( & self , offset : u64 ) -> io:: Result < CompressionStats > {
170- 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 )
171177 }
172178
173179 fn existing_offsets ( & self ) -> io:: Result < Vec < u64 > > {
@@ -356,6 +362,17 @@ pub fn open_segment_reader<R: Repo>(
356362 . map_err ( |source| with_segment_context ( "reading segment header" , repo, offset, source) )
357363}
358364
365+ /// Obtain the canonical [CompressOnce] compressor for segments.
366+ ///
367+ /// The compressor will create seekable [Zstd] archives with a max frame size
368+ /// of 4KiB. That is, seeking to an arbitrary byte offset (of the uncompressed
369+ /// segment) within the archive will decompress 4KiB of data on average.
370+ pub fn segment_compressor ( ) -> Zstd {
371+ Zstd {
372+ max_frame_size : Some ( 0x1000 ) ,
373+ }
374+ }
375+
359376fn segment_label < R : Repo > ( repo : & R , offset : u64 ) -> String {
360377 repo. segment_file_path ( offset)
361378 . unwrap_or_else ( || format ! ( "offset {offset}" ) )
0 commit comments