@@ -5,6 +5,7 @@ use std::sync::Arc;
55
66use log:: { debug, warn} ;
77use spacetimedb_fs_utils:: compression:: { compress_with_zstd, CompressReader } ;
8+ use spacetimedb_fs_utils:: direct_io;
89use spacetimedb_paths:: server:: { CommitLogDir , SegmentFile } ;
910use tempfile:: NamedTempFile ;
1011
@@ -29,7 +30,7 @@ pub type OnNewSegmentFn = dyn Fn() + Send + Sync + 'static;
2930/// Size on disk of a [Fs] repo.
3031///
3132/// Created by [Fs::size_on_disk].
32- #[ derive( Clone , Copy , Default ) ]
33+ #[ derive( Clone , Copy , Debug , Default ) ]
3334pub struct SizeOnDisk {
3435 /// The total size in bytes of all segments and offset indexes in the repo.
3536 pub total_bytes : u64 ,
@@ -156,7 +157,7 @@ impl FileLike for NamedTempFile {
156157
157158impl Repo for Fs {
158159 type SegmentWriter = File ;
159- type SegmentReader = CompressReader ;
160+ type SegmentReader = CompressReader < File > ;
160161
161162 fn create_segment ( & self , offset : u64 ) -> io:: Result < Self :: SegmentWriter > {
162163 File :: options ( )
@@ -213,18 +214,21 @@ impl Repo for Fs {
213214 }
214215
215216 fn compress_segment ( & self , offset : u64 ) -> io:: Result < ( ) > {
216- let src = self . open_segment_reader ( offset) ?;
217+ let segment_path = self . segment_path ( offset) ;
218+ let src = direct_io:: file_reader ( & segment_path) . and_then ( CompressReader :: new) ?;
217219 // if it's already compressed, leave it be
218220 let CompressReader :: None ( mut src) = src else {
219221 return Ok ( ( ) ) ;
220222 } ;
221223
222- let mut dst = NamedTempFile :: new_in ( & self . root ) ?;
224+ let tmp = NamedTempFile :: new_in ( & self . root ) ?. into_temp_path ( ) ;
225+ let mut dst = direct_io:: file_writer ( & tmp) ?;
223226 // bytes per frame. in the future, it might be worth looking into putting
224227 // every commit into its own frame, to make seeking more efficient.
225228 let max_frame_size = 0x1000 ;
226229 compress_with_zstd ( & mut src, & mut dst, Some ( max_frame_size) ) ?;
227- dst. persist ( self . segment_path ( offset) ) ?;
230+ dst. get_ref ( ) . sync_all ( ) ?;
231+ tmp. persist ( segment_path) ?;
228232
229233 Ok ( ( ) )
230234 }
@@ -266,7 +270,7 @@ impl Repo for Fs {
266270 }
267271}
268272
269- impl SegmentLen for CompressReader { }
273+ impl < R : io :: Read + io :: Seek > SegmentLen for CompressReader < R > { }
270274
271275#[ cfg( feature = "streaming" ) ]
272276impl crate :: stream:: AsyncRepo for Fs {
0 commit comments