feat(phase-1/m2.3): import dedupe — importing the same file twice adds nothing
Phase 1 M2 task 3 (tools/DESKTOP_PHASE_PLAN.md §4). Logic lives in BackupBridge so both platforms get it through the one bridge: - BackupBridge.import gains skipExisting (default true) and returns ImportSummary(imported, skipped). A note is skipped when the library already holds the key (createdAt, updatedAt, sha256(content)) — the triple the export preserves verbatim, trustworthy only since the M2 import-timestamp fix. Keys are snapshotted BEFORE importing, so two genuinely identical notes inside one file still both import into a fresh library; only a second import of the same file is a no-op. Snapshotted keys include trashed notes. skipExisting=false restores the old additive behaviour (the documented override). - Android: BackupManager.import passes the flag and returns the summary (signature-identical call for SettingsViewModel); its snackbar now reports the skip count. Screen layouts untouched — exit criterion "Android backup screens unchanged" holds. - Desktop: the import confirm dialog swaps its "duplicates persist" warning for a working "Skip notes that are already here" checkbox (default ON); scripted path prints the re-import outcome. - Format unchanged: formatVersion stays 1 (streaming/dedupe-at-format level stays a Phase-2 formatVersion=2 idea). Verification passed: :core:data-jvm:backupSmoke (new step — re-import skipped=27, override added 27, store=54; RESULT M2 round-trip PASS on the real export) + :desktopApp:run --args="--data-dir /tmp/incido-m2t3 --backup-import <export> --backup-export … --exit-after 8" (imported=27 skipped=0 → exported=27 → reimport imported=0 skipped=27, clean exit) + assembleDebug + :core:data-jvm:smoke + :desktopApp:run --self-test — all green; output recorded in DESKTOP_FEASIBILITY.md. Known limitation (by design): attachments are not in the key.
This commit is contained in:
1 parent
cf0e74b68d
commit
031d8570b4
7 files changed
+138
-21
No files matched your search
@@ -6,6 +6,7 @@ import java.io.OutputStream
|
||||
import java.security.GeneralSecurityException
|
||||
import javax.crypto.AEADBadTagException
|
||||
import javax.crypto.BadPaddingException
|
||||
import kotlinx.coroutines.flow.first
|
||||
import org.incido.domain.repository.NoteRepository
|
||||
|
||||
/**
|
||||
@@ -78,24 +79,45 @@ object BackupBridge {
|
||||
* encrypted files, [passphrase] is required (caller validates with
|
||||
* `PasswordPolicy`).
|
||||
*
|
||||
* **Dedupe (M2 task 3):** with [skipExisting] (the default) a note is
|
||||
* skipped when the library already holds a note with the same
|
||||
* `(createdAt, updatedAt, sha256(content))` key — the triple the
|
||||
* export preserves verbatim on both platforms (sound only since the
|
||||
* M2 import-timestamp fix). The existing keys are snapshotted BEFORE
|
||||
* this call, so a file containing two genuinely identical notes still
|
||||
* imports both into a fresh library; only a *second import* of the
|
||||
* same file adds nothing. Pass `skipExisting = false` to force the
|
||||
* old additive behaviour.
|
||||
*
|
||||
* Errors surface as thrown exceptions, same as the old
|
||||
* `BackupManager.import`: missing passphrase →
|
||||
* [IllegalArgumentException]; bad passphrase / corrupt envelope →
|
||||
* `GeneralSecurityException` family.
|
||||
*
|
||||
* Returns the number of notes imported.
|
||||
*/
|
||||
suspend fun import(
|
||||
repository: NoteRepository,
|
||||
input: InputStream,
|
||||
passphrase: CharArray? = null,
|
||||
skipExisting: Boolean = true,
|
||||
onProgress: (current: Int, total: Int) -> Unit = { _, _ -> },
|
||||
): Int {
|
||||
): ImportSummary {
|
||||
val bytes = input.use(InputStream::readBytes)
|
||||
val json = payloadJson(bytes, passphrase)
|
||||
val file = BackupCodec.decode(json)
|
||||
BackupCodec.importSnapshot(file, repository) { cur, tot -> onProgress(cur, tot) }
|
||||
return file.notes.size
|
||||
|
||||
val toImport = if (!skipExisting) {
|
||||
file.notes
|
||||
} else {
|
||||
val existing = repository.observeAllIncludingTrashed().first()
|
||||
.map { keyFor(it.createdAt, it.updatedAt, it.content) }
|
||||
.toSet()
|
||||
file.notes.filterNot { keyFor(it.createdAt, it.updatedAt, it.content) in existing }
|
||||
}
|
||||
val skipped = file.notes.size - toImport.size
|
||||
BackupCodec.importSnapshot(file.copy(notes = toImport), repository) { cur, tot ->
|
||||
onProgress(cur, tot)
|
||||
}
|
||||
return ImportSummary(imported = toImport.size, skipped = skipped)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,8 +174,26 @@ object BackupBridge {
|
||||
?: throw IllegalArgumentException("Encrypted backup requires a passphrase")
|
||||
return EncryptedBackupCodec.decrypt(bytes, pass)
|
||||
}
|
||||
|
||||
/**
|
||||
* M2 task 3 dedupe key: `(createdAt, updatedAt, sha256(content))`.
|
||||
* Every component survives export/import on both platforms, and the
|
||||
* timestamps are only trustworthy for dedupe because the repository
|
||||
* now honours caller-supplied values (M2 import-timestamp fix).
|
||||
* NUL-separated so distinct field splits can never collide; the
|
||||
* content rides as a SHA-256 digest to keep the compared set cheap.
|
||||
*/
|
||||
private fun keyFor(createdAt: Long, updatedAt: Long, content: String): String {
|
||||
val digest = java.security.MessageDigest.getInstance("SHA-256")
|
||||
.digest(content.toByteArray(Charsets.UTF_8))
|
||||
val hex = digest.joinToString("") { "%02x".format(it) }
|
||||
return "$createdAt\u0000$updatedAt\u0000$hex"
|
||||
}
|
||||
}
|
||||
|
||||
/** Outcome of [BackupBridge.import] — what a duplicate-safe import did. */
|
||||
data class ImportSummary(val imported: Int, val skipped: Int)
|
||||
|
||||
/** Lightweight DTO shown in the import confirmation dialog. */
|
||||
data class BackupPreview(
|
||||
val noteCount: Int,
|
||||
|
||||
Reference in new issue
Block a user