remotely-save/docs/sync_algorithm_v1.md
fyears df2d98272c new sync algo, squashed commit of the following:
commit 692bb794aea5609b9e9abf5228620f4479e50983
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 27 17:52:43 2022 +0800

    bump to 0.3.0

commit 77335412ad
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 27 17:50:57 2022 +0800

    change titles for minimal intrusive design

commit 2812adebb8
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 27 17:30:53 2022 +0800

    remove syncv1

commit 22fc24a76c
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 27 17:30:27 2022 +0800

    full notice to any one

commit d56ea24a78
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sat Feb 26 23:11:14 2022 +0800

    fix test

commit 759f82593b
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sat Feb 26 21:59:25 2022 +0800

    obfuscate metadata on remote

commit 9b6bf05288
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sat Feb 26 21:33:52 2022 +0800

    avoid re-uploading if meta not changed

commit 03be145376
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sat Feb 26 00:35:52 2022 +0800

    add password condition

commit 7f899f7c25
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sat Feb 26 00:22:58 2022 +0800

    add decision branch for easier debugging

commit cf4071bf31
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Fri Feb 25 23:40:52 2022 +0800

    change folder error

commit 964493dd99
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Fri Feb 25 23:09:44 2022 +0800

    finnaly remote remove should be working

commit 2888e65452
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Fri Feb 25 01:18:15 2022 +0800

    optimize comparation

commit 024936951d
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Fri Feb 25 01:14:44 2022 +0800

    allow uploading extra meta

commit 007006701d
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Thu Feb 24 01:08:58 2022 +0800

    add logic to fetch extra meta

commit c9d3a05ca1
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Thu Feb 24 00:29:16 2022 +0800

    another way to deal with trash

commit 82d455f8bf
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Thu Feb 24 00:28:51 2022 +0800

    add return buffer for downloading

commit b8e6b79bc0
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Wed Feb 23 00:16:06 2022 +0800

    half way to actual sync

commit 90cceb1411
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Tue Feb 22 23:36:09 2022 +0800

    cleaner way to remember the folder being kept

commit c1afb763cc
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Tue Feb 22 00:03:21 2022 +0800

    simplified way to get plans for sync algo v2

commit 5c5ecce39e
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Mon Feb 21 23:13:58 2022 +0800

    archive the old sync algo v1 doc

commit 75cdfa1ee9
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Mon Feb 21 23:13:14 2022 +0800

    simplify sync algo v2

commit dc9275835d
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Mon Feb 21 22:58:57 2022 +0800

    add way to skip recording removing

commit f9712ef960
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Mon Feb 21 09:38:09 2022 +0800

    fix sync

commit 9007dcf22e
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Mon Feb 21 00:54:21 2022 +0800

    fix assignment

commit 77abee6ad4
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Mon Feb 21 00:28:43 2022 +0800

    draft of sync v2

commit a0c26238bf
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Mon Feb 21 00:23:19 2022 +0800

    how to deal with folders

commit c10f92a7e8
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 20 23:39:16 2022 +0800

    helper func to get parents

commit f903c98b3b
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 20 23:31:21 2022 +0800

    add optional ending slash to getFolderLevels

commit 2d67c9b2b9
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 20 14:44:44 2022 +0800

    update sync algo v2

commit 491ed1bb85
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sun Feb 20 14:34:51 2022 +0800

    dry run mode

commit dfd588dcef
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sat Feb 19 23:14:32 2022 +0800

    prettier

commit eae580f882
Author: fyears <1142836+fyears@users.noreply.github.com>
Date:   Sat Feb 19 23:12:29 2022 +0800

    draft design for sync algo v2
2022-02-27 17:54:31 +08:00

30 lines
3.9 KiB
Markdown

# Sync Algorithm
## Sources
We have three record sources:
1. Local files. By scanning all files in the vault locally. Actually Obsidian provides an api directly returning this.
2. Remote files. By scanning all files on the remote service. Some services provide an api directly returning this, and some other services require the plugin scanning the folders recursively.
3. Local "delete-or-rename" history. It's recorded by using Obsidian's tracking api. So if users delete or rename files/folders outside Obsidian, we could do nothing.
Assuming all sources are reliable.
## Deal with them
We list all combinations mutually exclusive and collectively exhaustive.
| ID | Remote Files | Local files | Local delete rename history | Extra | Decision |
| --- | ------------ | ----------- | --------------------------- | ----------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| 1 | exist | exist | ignore | mtime_remote > mtime_local | download remote file, create local folder if not exists, clear local history if exists |
| 2 | exist | exist | ignore | mtime_remote < mtime_local | upload local file, create remote folder if not exists, clear local history if exists |
| 3 | exist | exist | ignore | mtime_remote === mtime_local && password === "" && size_remote === size_local | clear local history if exists (the file was synced and no changes after last sync) |
| 4 | exist | exist | ignore | mtime_remote === mtime_local && password === "" && size_remote !== size_local | upload local file, clear local history if exists (we always prefer local to remote) |
| 5 | exist | exist | ignore | mtime_remote === mtime_local && password !== "" | clear local history if exists (in encryption mode, file sizes are unequal. we can only rely on mtime(s)) |
| 6 | exist | exist | ignore | If local is a folder. mtime_local === undefined | clear local history if exists. TODO: what if a folder and a previous file share the same name? |
| 7 | exist | not exist | exist | mtime_remote >= delete_time_local | download remote file, create folder if not exists |
| 8 | exist | not exist | exist | mtime_remote < delete_time_local | delete remote file, clear local history |
| 9 | exist | not exist | not exist | | download remote file, create folder if not exists |
| 10 | not exist | exist | ignore | local may be folder or file | upload local files recursively, create remote folder if not exists, clear local history if exists |
| 11 | not exist | not exist | ignore | | clear local history if exists |