Skip to content

Commit dc2ba7f

Browse files
committed
sync: Do not trim, if there were no changes
1 parent 54e401f commit dc2ba7f

3 files changed

Lines changed: 59 additions & 50 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "historyprovider"
33
description = "historyprovider-rs"
44
license = "MIT"
55
repository = "https://github.com/silicon-heaven/historyprovider-rs"
6-
version = "2.12.1"
6+
version = "2.12.2"
77
edition = "2024"
88

99
[profile.release]

src/sync.rs

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ async fn get_files_to_sync(
253253
.collect()
254254
}
255255

256+
enum ShouldTrim {
257+
Yes,
258+
No,
259+
}
260+
256261
async fn sync_site_by_download(
257262
site_path: impl AsRef<str>,
258263
remote_journal_path: impl AsRef<str>,
@@ -261,7 +266,7 @@ async fn sync_site_by_download(
261266
journal_dir: impl AsRef<str>,
262267
sync_logger: impl SyncLogger,
263268
file_list: Option<&[LsFilesEntry]>,
264-
) -> Result<(), String>
269+
) -> Result<ShouldTrim, String>
265270
{
266271
let (site_path, remote_journal_path) = (site_path.as_ref(), remote_journal_path.as_ref());
267272
let local_journal_path = Path::new(journal_dir.as_ref()).join(site_path);
@@ -369,32 +374,34 @@ async fn sync_site_by_download(
369374
.collect::<Vec<_>>()
370375
.await;
371376

372-
if let Some((first_file, _, _)) = files_to_sync.first() {
373-
let read_api = RpcCall::new(&join_path!(remote_journal_path, first_file), METH_DIR)
374-
.param("sha1")
375-
.exec(&client_cmd_tx)
376-
.await
377-
.map(|v: RpcValue| if v.is_imap() { ReadApi::List } else { ReadApi::Map })
378-
.map_err(|e| format!("Cannot get read param API for {remote_journal_path}: {e}"))?;
379-
380-
// Sync from the remote to the sync directory
381-
for (file_name, sync_offset, file_size) in files_to_sync {
382-
sync_file(
383-
client_cmd_tx.clone(),
384-
join_path!(remote_journal_path, &file_name),
385-
local_journal_path.join(&file_name),
386-
download_chunk_size,
387-
sync_offset,
388-
file_size,
389-
read_api,
390-
sync_logger.clone(),
391-
)
392-
.await
393-
.map_err(to_string)?;
394-
}
377+
let Some((first_file, _, _)) = files_to_sync.first() else {
378+
return Ok(ShouldTrim::No);
379+
};
380+
381+
let read_api = RpcCall::new(&join_path!(remote_journal_path, first_file), METH_DIR)
382+
.param("sha1")
383+
.exec(&client_cmd_tx)
384+
.await
385+
.map(|v: RpcValue| if v.is_imap() { ReadApi::List } else { ReadApi::Map })
386+
.map_err(|e| format!("Cannot get read param API for {remote_journal_path}: {e}"))?;
387+
388+
// Sync from the remote to the sync directory
389+
for (file_name, sync_offset, file_size) in files_to_sync {
390+
sync_file(
391+
client_cmd_tx.clone(),
392+
join_path!(remote_journal_path, &file_name),
393+
local_journal_path.join(&file_name),
394+
download_chunk_size,
395+
sync_offset,
396+
file_size,
397+
read_api,
398+
sync_logger.clone(),
399+
)
400+
.await
401+
.map_err(to_string)?;
395402
}
396403

397-
Ok(())
404+
Ok(ShouldTrim::Yes)
398405
}
399406

400407
#[derive(Copy,Clone)]
@@ -504,7 +511,7 @@ async fn sync_site_legacy(
504511
client_cmd_tx: ClientCommandSender,
505512
journal_dir: impl AsRef<str>,
506513
sync_logger: impl SyncLogger,
507-
) -> Result<(), String>
514+
) -> Result<ShouldTrim, String>
508515
{
509516
let (site_path, getlog_path) = (site_path.as_ref(), getlog_path.as_ref());
510517
let local_journal_path = Path::new(journal_dir.as_ref()).join(site_path);
@@ -644,6 +651,8 @@ async fn sync_site_legacy(
644651
Ok(())
645652
}
646653

654+
let mut should_trim = ShouldTrim::No;
655+
647656
loop {
648657
sync_logger.log(
649658
log::Level::Info,
@@ -687,6 +696,8 @@ async fn sync_site_legacy(
687696
break;
688697
};
689698

699+
should_trim = ShouldTrim::Yes;
700+
690701
log_file_entries.append(&mut log_entries);
691702

692703
getlog_params.since = GetLog2Since::DateTime(shvproto::DateTime::from_epoch_msec(last_entry_ms));
@@ -706,7 +717,7 @@ async fn sync_site_legacy(
706717
getlog_params.with_snapshot = false;
707718
}
708719
}
709-
Ok(())
720+
Ok(should_trim)
710721
}
711722

712723
pub(crate) async fn sync_task(
@@ -739,11 +750,21 @@ pub(crate) async fn sync_task(
739750
let days_to_keep = app_state.config.days_to_keep;
740751

741752
while let Some(cmd) = sync_cmd_rx.next().await {
742-
fn send_trim(site_path: String, dirtylog_cmd_tx: UnboundedSender<DirtyLogCommand>) {
743-
if let Err(e) = dirtylog_cmd_tx.unbounded_send(DirtyLogCommand::Trim { site: site_path }) {
744-
let err_msg = e.to_string();
745-
let command = e.into_inner();
746-
log::error!("Cannot send dirtylog Trim command {command:?}: {err_msg}")
753+
fn send_trim(sync_result: Result<ShouldTrim, String>, site_path: String, dirtylog_cmd_tx: UnboundedSender<DirtyLogCommand>, sync_logger: &SyncSiteLogger) {
754+
match sync_result {
755+
Ok(ShouldTrim::Yes) => {
756+
if let Err(e) = dirtylog_cmd_tx.unbounded_send(DirtyLogCommand::Trim { site: site_path }) {
757+
let err_msg = e.to_string();
758+
let command = e.into_inner();
759+
log::error!("Cannot send dirtylog Trim command {command:?}: {err_msg}")
760+
}
761+
},
762+
Ok(ShouldTrim::No) => {
763+
sync_logger.log(log::Level::Info, format!("Not trimming {site_path}, because no changes were made"));
764+
},
765+
Err(err) => {
766+
sync_logger.log(log::Level::Error, format!("site sync error: {err}"));
767+
},
747768
}
748769
}
749770

@@ -804,11 +825,8 @@ pub(crate) async fn sync_task(
804825
sync_logger.clone(),
805826
Some(&file_list),
806827
).await;
807-
if let Err(err) = sync_result {
808-
sync_logger.log(log::Level::Error, format!("site sync error: {err}"));
809-
}
810828
sync_logger.log(log::Level::Info, "syncing done");
811-
send_trim(site_path, app_state.dirtylog_cmd_tx.clone());
829+
send_trim(sync_result, site_path, app_state.dirtylog_cmd_tx.clone(), &sync_logger);
812830
drop(permit);
813831
});
814832
sync_tasks.push(sync_task);
@@ -826,11 +844,8 @@ pub(crate) async fn sync_task(
826844
&app_state.config.journal_dir,
827845
sync_logger.clone()
828846
).await;
829-
if let Err(err) = sync_result {
830-
sync_logger.log(log::Level::Error, format!("site sync error: {err}"));
831-
}
832847
sync_logger.log(log::Level::Info, "syncing done");
833-
send_trim(site_path, app_state.dirtylog_cmd_tx.clone());
848+
send_trim(sync_result, site_path, app_state.dirtylog_cmd_tx.clone(), &sync_logger);
834849
drop(permit);
835850
});
836851
sync_tasks.push(sync_task);
@@ -876,10 +891,7 @@ pub(crate) async fn sync_task(
876891
sync_logger.clone(),
877892
None,
878893
).await;
879-
if let Err(err) = sync_result {
880-
sync_logger.log(log::Level::Error, format!("site sync error: {err}"));
881-
}
882-
send_trim(site_path, app_state.dirtylog_cmd_tx.clone());
894+
send_trim(sync_result, site_path, app_state.dirtylog_cmd_tx.clone(), &sync_logger);
883895
sync_logger.log(log::Level::Info, "syncing done");
884896
}
885897
SubHpInfo::Legacy { getlog_path } => {
@@ -894,10 +906,7 @@ pub(crate) async fn sync_task(
894906
&app_state.config.journal_dir,
895907
sync_logger.clone()
896908
).await;
897-
if let Err(err) = sync_result {
898-
sync_logger.log(log::Level::Error, format!("site sync error: {err}"));
899-
}
900-
send_trim(site_path, app_state.dirtylog_cmd_tx.clone());
909+
send_trim(sync_result, site_path, app_state.dirtylog_cmd_tx.clone(), &sync_logger);
901910
sync_logger.log(log::Level::Info, "syncing done");
902911
}
903912
SubHpInfo::PushLog => {

0 commit comments

Comments
 (0)