|
1 | 1 | //! Utilities used when using a replicated version of libsql. |
2 | 2 |
|
3 | 3 | use std::path::PathBuf; |
| 4 | +use std::sync::atomic::AtomicUsize; |
4 | 5 | use std::sync::Arc; |
5 | 6 | use std::time::Duration; |
6 | 7 |
|
@@ -39,10 +40,16 @@ pub struct Replicated { |
39 | 40 | } |
40 | 41 |
|
41 | 42 | impl Replicated { |
| 43 | + /// The currently synced frame number. This can be used to track |
| 44 | + /// where in the log you might be. Beware that this value can be reset to a lower value by the |
| 45 | + /// server in certain situations. Please use `frames_synced` if you want to track the amount of |
| 46 | + /// work a sync has done. |
42 | 47 | pub fn frame_no(&self) -> Option<FrameNo> { |
43 | 48 | self.frame_no |
44 | 49 | } |
45 | 50 |
|
| 51 | + /// The count of frames synced during this call of `sync`. A frame is a 4kB frame from the |
| 52 | + /// libsql write ahead log. |
46 | 53 | pub fn frames_synced(&self) -> usize { |
47 | 54 | self.frames_synced |
48 | 55 | } |
@@ -124,6 +131,7 @@ impl Writer { |
124 | 131 | pub(crate) struct EmbeddedReplicator { |
125 | 132 | replicator: Arc<Mutex<Replicator<Either<RemoteClient, LocalClient>>>>, |
126 | 133 | bg_abort: Option<Arc<DropAbort>>, |
| 134 | + last_frames_synced: Arc<AtomicUsize>, |
127 | 135 | } |
128 | 136 |
|
129 | 137 | impl From<libsql_replication::replicator::Error> for errors::Error { |
@@ -153,6 +161,7 @@ impl EmbeddedReplicator { |
153 | 161 | let mut replicator = Self { |
154 | 162 | replicator, |
155 | 163 | bg_abort: None, |
| 164 | + last_frames_synced: Arc::new(AtomicUsize::new(0)), |
156 | 165 | }; |
157 | 166 |
|
158 | 167 | if let Some(sync_duration) = perodic_sync { |
@@ -196,6 +205,7 @@ impl EmbeddedReplicator { |
196 | 205 | Ok(Self { |
197 | 206 | replicator, |
198 | 207 | bg_abort: None, |
| 208 | + last_frames_synced: Arc::new(AtomicUsize::new(0)), |
199 | 209 | }) |
200 | 210 | } |
201 | 211 |
|
@@ -245,9 +255,13 @@ impl EmbeddedReplicator { |
245 | 255 | } |
246 | 256 | } |
247 | 257 |
|
| 258 | + let last_frames_synced = self |
| 259 | + .last_frames_synced |
| 260 | + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 261 | + |
248 | 262 | let replicated = Replicated { |
249 | 263 | frame_no: replicator.client_mut().committed_frame_no(), |
250 | | - frames_synced: replicator.frames_synced(), |
| 264 | + frames_synced: replicator.frames_synced() - last_frames_synced, |
251 | 265 | }; |
252 | 266 |
|
253 | 267 | Ok(replicated) |
|
0 commit comments