[optim] Download 30 changed items at once in a single HTTP request from the server

This commit is contained in:
daladim 2021-12-24 14:56:01 +01:00
parent 159b6199fa
commit 7b13d74edc

View file

@ -334,30 +334,13 @@ where
&cal_name &cal_name
).await; ).await;
for url_change in remote_changes { Self::apply_remote_changes(
progress.debug(&format!("> Applying remote change {} locally", url_change)); remote_changes,
progress.feedback(SyncEvent::InProgress{ &mut *cal_local,
calendar: cal_name.clone(), &mut *cal_remote,
details: Self::item_name(&cal_local, &url_change).await, progress,
}); &cal_name
match cal_remote.get_item_by_url(&url_change).await { ).await;
Err(err) => {
progress.warn(&format!("Unable to get remote item {}: {}. Skipping it", url_change, err));
continue;
},
Ok(item) => match item {
None => {
progress.error(&format!("Inconsistency: modified item {} has vanished from the remote end", url_change));
continue;
},
Some(item) => {
if let Err(err) = cal_local.update_item(item.clone()).await {
progress.error(&format!("Unable to update item {} in local calendar: {}", url_change, err));
}
},
}
}
}
for url_add in local_additions { for url_add in local_additions {
@ -426,6 +409,18 @@ where
} }
} }
async fn apply_remote_changes(
mut remote_changes: HashSet<Url>,
cal_local: &mut T,
cal_remote: &mut U,
progress: &mut SyncProgress,
cal_name: &str
) {
for batch in remote_changes.drain().chunks(DOWNLOAD_BATCH_SIZE).into_iter() {
Self::fetch_batch_and_apply(BatchDownloadType::RemoteChanges, batch, cal_local, cal_remote, progress, cal_name).await;
}
}
async fn fetch_batch_and_apply<I: Iterator<Item = Url>>( async fn fetch_batch_and_apply<I: Iterator<Item = Url>>(
batch_type: BatchDownloadType, batch_type: BatchDownloadType,
remote_additions: I, remote_additions: I,
@ -449,7 +444,11 @@ where
continue; continue;
}, },
Some(new_item) => { Some(new_item) => {
if let Err(err) = cal_local.add_item(new_item.clone()).await { let local_update_result = match batch_type {
BatchDownloadType::RemoteAdditions => cal_local.add_item(new_item.clone()).await,
BatchDownloadType::RemoteChanges => cal_local.update_item(new_item.clone()).await,
};
if let Err(err) = local_update_result {
progress.error(&format!("Not able to add item {} to local calendar: {}", new_item.url(), err)); progress.error(&format!("Not able to add item {} to local calendar: {}", new_item.url(), err));
} }
}, },