@@ -2,34 +2,39 @@ use crate::{buffer::tip::Buffer, Blob, Buf, BufferPool, BufferPooler, Error, IoB
22use commonware_utils:: sync:: AsyncRwLock ;
33use std:: { num:: NonZeroUsize , sync:: Arc } ;
44
5- /// Buffered tip data, blob handle, and durability bookkeeping .
5+ /// Shared writer state .
66struct State < B : Blob > {
77 /// The underlying blob to write to.
88 blob : B ,
99
10- /// The buffer containing the data yet to be appended to the tip of the
11- /// underlying blob.
10+ /// Buffered bytes at the logical tip of the blob.
1211 buffer : Buffer ,
1312
14- /// Whether prior blob mutation requires a full sync barrier .
13+ /// Whether a prior plain mutation must be persisted with [`Blob::sync`] .
1514 ///
16- /// Set after plain [Blob::write_at] or [ Blob::resize]. While set, [Write::sync]
17- /// cannot use [Blob::write_at_sync] because that only makes the current write
18- /// durable .
15+ /// [`State::write_at_sync`] uses [` Blob::write_at_sync`] only when this is
16+ /// false, otherwise it must use [` Blob::sync`] to cover earlier unsynced
17+ /// mutations .
1918 needs_sync : bool ,
2019}
2120
2221impl < B : Blob > State < B > {
22+ /// Read bytes from the underlying blob.
2323 async fn read_at ( & self , offset : u64 , len : usize ) -> Result < IoBufs , Error > {
2424 Ok ( self . blob . read_at ( offset, len) . await ?. freeze ( ) )
2525 }
2626
27+ /// Write bytes to the underlying blob and mark them as needing sync.
2728 async fn write_at ( & mut self , offset : u64 , bufs : impl Into < IoBufs > + Send ) -> Result < ( ) , Error > {
2829 self . blob . write_at ( offset, bufs) . await ?;
2930 self . needs_sync = true ;
3031 Ok ( ( ) )
3132 }
3233
34+ /// Write bytes to the underlying blob and make them durable.
35+ ///
36+ /// Uses [`Blob::write_at_sync`] when there are no earlier unsynced
37+ /// mutations. Otherwise, writes the bytes and then syncs the blob.
3338 async fn write_at_sync (
3439 & mut self ,
3540 offset : u64 ,
@@ -43,12 +48,14 @@ impl<B: Blob> State<B> {
4348 }
4449 }
4550
51+ /// Resize the underlying blob and mark the resize as needing sync.
4652 async fn resize ( & mut self , len : u64 ) -> Result < ( ) , Error > {
4753 self . blob . resize ( len) . await ?;
4854 self . needs_sync = true ;
4955 Ok ( ( ) )
5056 }
5157
58+ /// Sync the underlying blob if there are unsynced mutations.
5259 async fn sync ( & mut self ) -> Result < ( ) , Error > {
5360 if !self . needs_sync {
5461 return Ok ( ( ) ) ;
0 commit comments