Fix syncing logic
This commit is contained in:
parent
0d7d36e971
commit
7c991c3b4d
10 changed files with 223 additions and 184 deletions
|
|
@ -9,24 +9,24 @@ server:
|
|||
max_clients_per_vault: 256
|
||||
response_timeout: 30m
|
||||
mergeable_file_extensions:
|
||||
- md
|
||||
- txt
|
||||
- md
|
||||
- txt
|
||||
users:
|
||||
user_configs:
|
||||
- name: admin
|
||||
token: test-token-change-me
|
||||
vault_access:
|
||||
type: allow_access_to_all
|
||||
- name: other-admin
|
||||
token: test-token-change-me2
|
||||
vault_access:
|
||||
type: allow_access_to_all
|
||||
- name: test
|
||||
token: other-test-token
|
||||
vault_access:
|
||||
type: allow_list
|
||||
allowed:
|
||||
- default
|
||||
- name: admin
|
||||
token: test-token-change-me
|
||||
vault_access:
|
||||
type: allow_access_to_all
|
||||
- name: other-admin
|
||||
token: test-token-change-me2
|
||||
vault_access:
|
||||
type: allow_access_to_all
|
||||
- name: test
|
||||
token: other-test-token
|
||||
vault_access:
|
||||
type: allow_list
|
||||
allowed:
|
||||
- default
|
||||
logging:
|
||||
log_directory: logs
|
||||
log_rotation: 7days
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@ impl Database {
|
|||
},
|
||||
);
|
||||
}
|
||||
info!("Database migrations applied");
|
||||
|
||||
let database = Self {
|
||||
config: config.clone(),
|
||||
|
|
@ -301,7 +302,7 @@ impl Database {
|
|||
.context("Cannot fetch max update id in vault")
|
||||
}
|
||||
|
||||
pub async fn get_latest_document_by_path(
|
||||
pub async fn get_latest_non_deleted_document_by_path(
|
||||
&self,
|
||||
vault: &VaultId,
|
||||
relative_path: &str,
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ pub async fn create_document(
|
|||
if request.force_merge.unwrap_or_default() {
|
||||
let latest_version = state
|
||||
.database
|
||||
.get_latest_document_by_path(
|
||||
.get_latest_non_deleted_document_by_path(
|
||||
&vault_id,
|
||||
&sanitized_relative_path,
|
||||
Some(&mut transaction),
|
||||
|
|
@ -65,7 +65,8 @@ pub async fn create_document(
|
|||
);
|
||||
|
||||
return merge_with_stored_version(
|
||||
latest_version.clone(),
|
||||
&sanitized_relative_path,
|
||||
&Vec::new(),
|
||||
latest_version,
|
||||
vault_id,
|
||||
user,
|
||||
|
|
|
|||
|
|
@ -172,7 +172,8 @@ async fn update_document(
|
|||
}
|
||||
|
||||
merge_with_stored_version(
|
||||
parent_document,
|
||||
&parent_document.relative_path,
|
||||
&parent_document.content,
|
||||
latest_version,
|
||||
vault_id,
|
||||
user,
|
||||
|
|
@ -187,7 +188,8 @@ async fn update_document(
|
|||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn merge_with_stored_version(
|
||||
parent_document: StoredDocumentVersion,
|
||||
parent_document_path: &str,
|
||||
parent_document_content: &[u8],
|
||||
latest_version: StoredDocumentVersion,
|
||||
vault_id: VaultId,
|
||||
user: User,
|
||||
|
|
@ -203,7 +205,7 @@ pub async fn merge_with_stored_version(
|
|||
{
|
||||
info!(
|
||||
"Document content is the same as the latest version for `{}`, skipping update",
|
||||
parent_document.document_id
|
||||
latest_version.document_id
|
||||
);
|
||||
transaction
|
||||
.rollback()
|
||||
|
|
@ -219,17 +221,17 @@ pub async fn merge_with_stored_version(
|
|||
let are_all_participants_mergable = is_file_type_mergable(
|
||||
sanitized_relative_path,
|
||||
&state.config.server.mergeable_file_extensions,
|
||||
) && !is_binary(&parent_document.content)
|
||||
) && !is_binary(parent_document_content)
|
||||
&& !is_binary(&latest_version.content)
|
||||
&& !is_binary(&content);
|
||||
|
||||
let merged_content = if are_all_participants_mergable {
|
||||
info!(
|
||||
"Merging changes for document `{}` in vault `{vault_id}`",
|
||||
parent_document.document_id
|
||||
latest_version.document_id
|
||||
);
|
||||
reconcile(
|
||||
str::from_utf8(&parent_document.content)
|
||||
str::from_utf8(parent_document_content)
|
||||
.expect("parent must be valid UTF-8 because it's not binary"),
|
||||
&str::from_utf8(&latest_version.content)
|
||||
.expect("latest_version must be valid UTF-8 because it's not binary")
|
||||
|
|
@ -247,7 +249,7 @@ pub async fn merge_with_stored_version(
|
|||
};
|
||||
|
||||
// We can only update the relative path if we're the first one to do so
|
||||
let new_relative_path = if parent_document.relative_path == latest_version.relative_path
|
||||
let new_relative_path = if parent_document_path == &latest_version.relative_path
|
||||
&& latest_version.relative_path != sanitized_relative_path
|
||||
{
|
||||
let new_path = find_first_available_path(
|
||||
|
|
@ -279,7 +281,7 @@ pub async fn merge_with_stored_version(
|
|||
let is_different_from_request_content = merged_content != content;
|
||||
|
||||
let new_version = StoredDocumentVersion {
|
||||
document_id: parent_document.document_id,
|
||||
document_id: latest_version.document_id,
|
||||
vault_update_id: last_update_id + 1,
|
||||
relative_path: new_relative_path,
|
||||
content: merged_content,
|
||||
|
|
|
|||
|
|
@ -9,17 +9,19 @@ pub async fn find_first_available_path(
|
|||
database: &crate::app_state::database::Database,
|
||||
transaction: &mut Transaction<'_>,
|
||||
) -> Result<String> {
|
||||
info!("Finding first available path for `{sanitized_relative_path}` in vault `{vault_id}`");
|
||||
for candidate in dedup_paths(sanitized_relative_path) {
|
||||
debug!("Checking candidate path for deconflicting names: `{candidate}`");
|
||||
if database
|
||||
.get_latest_document_by_path(vault_id, &candidate, Some(transaction))
|
||||
.get_latest_non_deleted_document_by_path(vault_id, &candidate, Some(transaction))
|
||||
.await?
|
||||
.is_none()
|
||||
{
|
||||
info!("Selected available path: `{candidate}`");
|
||||
return Ok(candidate);
|
||||
}
|
||||
|
||||
info!(
|
||||
"Finding first available path for `{sanitized_relative_path}` in vault `{vault_id}` as `{candidate}` is already taken"
|
||||
);
|
||||
}
|
||||
|
||||
unreachable!("dedup_paths produces infinite paths");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue