From 840e03a2d3c143c422e6b27a2605e14aabf4c3df Mon Sep 17 00:00:00 2001 From: vorotamoroz Date: Tue, 6 Dec 2022 17:59:48 +0900 Subject: [PATCH] Fixed: - Now we can verify and repair database again. --- src/ObsidianLiveSyncSettingTab.ts | 25 ++++++++++++++++--------- src/lib | 2 +- src/main.ts | 27 ++++++++++++++++++--------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/ObsidianLiveSyncSettingTab.ts b/src/ObsidianLiveSyncSettingTab.ts index d723622..2b1ccec 100644 --- a/src/ObsidianLiveSyncSettingTab.ts +++ b/src/ObsidianLiveSyncSettingTab.ts @@ -1,7 +1,7 @@ import { App, PluginSettingTab, Setting, sanitizeHTMLToDom, RequestUrlParam, requestUrl, TextAreaComponent, MarkdownRenderer, stringifyYaml } from "obsidian"; import { DEFAULT_SETTINGS, LOG_LEVEL, ObsidianLiveSyncSettings, RemoteDBSettings } from "./lib/src/types"; import { path2id, id2path } from "./utils"; -import { delay, versionNumberString2Number } from "./lib/src/utils"; +import { delay, Semaphore, versionNumberString2Number } from "./lib/src/utils"; import { Logger } from "./lib/src/logger"; import { checkSyncInfo, isCloudantURI } from "./lib/src/utils_couchdb.js"; import { testCrypt } from "./lib/src/e2ee_v2"; @@ -1367,21 +1367,28 @@ ${stringifyYaml(pluginConfig)}`; .setDisabled(false) .setWarning() .onClick(async () => { + const semaphore = Semaphore(10); const files = this.app.vault.getFiles(); - Logger("Verify and repair all files started", LOG_LEVEL.NOTICE, "verify"); - // const notice = NewNotice("", 0); let i = 0; - for (const file of files) { - i++; - Logger(`Update into ${file.path}`); - Logger(`${i}/${files.length}\n${file.path}`, LOG_LEVEL.NOTICE, "verify"); + const processes = files.map(e => (async (file) => { + const releaser = await semaphore.acquire(1, "verifyAndRepair"); + try { - await this.plugin.updateIntoDB(file); + Logger(`Update into ${file.path}`); + await this.plugin.updateIntoDB(file, false, null, true); + i++; + Logger(`${i}/${files.length}\n${file.path}`, LOG_LEVEL.NOTICE, "verify"); + } catch (ex) { - Logger("could not update:"); + i++; + Logger(`Error while verifyAndRepair`, LOG_LEVEL.NOTICE); Logger(ex); + } finally { + releaser(); } } + )(e)); + await Promise.all(processes); Logger("done", LOG_LEVEL.NOTICE, "verify"); }) ); diff --git a/src/lib b/src/lib index 300bf7e..a13d921 160000 --- a/src/lib +++ b/src/lib @@ -1 +1 @@ -Subproject commit 300bf7e9b1b240bf9ef9d55bde10d7dde741839e +Subproject commit a13d921fb7e4b79232fb040e257b3b06151b2bfa diff --git a/src/main.ts b/src/main.ts index 61d7d2e..d8a4254 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2192,7 +2192,7 @@ export default class ObsidianLiveSyncPlugin extends Plugin { } - async updateIntoDB(file: TFile, initialScan?: boolean, cache?: CacheData) { + async updateIntoDB(file: TFile, initialScan?: boolean, cache?: CacheData, force?: boolean) { if (!this.isTargetFile(file)) return; if (shouldBeIgnored(file.path)) { return; @@ -2234,15 +2234,24 @@ export default class ObsidianLiveSyncPlugin extends Plugin { if (recentlyTouched(file)) { return true; } - const old = await this.localDatabase.getDBEntry(fullPath, null, false, false); - if (old !== false) { - const oldData = { data: old.data, deleted: old._deleted || old.deleted, }; - const newData = { data: d.data, deleted: d._deleted || d.deleted }; - if (JSON.stringify(oldData) == JSON.stringify(newData)) { - Logger(msg + "Skipped (not changed) " + fullPath + ((d._deleted || d.deleted) ? " (deleted)" : ""), LOG_LEVEL.VERBOSE); - return true; + try { + const old = await this.localDatabase.getDBEntry(fullPath, null, false, false); + if (old !== false) { + const oldData = { data: old.data, deleted: old._deleted || old.deleted, }; + const newData = { data: d.data, deleted: d._deleted || d.deleted }; + if (JSON.stringify(oldData) == JSON.stringify(newData)) { + Logger(msg + "Skipped (not changed) " + fullPath + ((d._deleted || d.deleted) ? " (deleted)" : ""), LOG_LEVEL.VERBOSE); + return true; + } + // d._rev = old._rev; } - // d._rev = old._rev; + } catch (ex) { + if (force) { + Logger(msg + "Error, Could not check the diff for the old one." + (force ? "force writing." : "") + fullPath + ((d._deleted || d.deleted) ? " (deleted)" : ""), LOG_LEVEL.VERBOSE); + } else { + Logger(msg + "Error, Could not check the diff for the old one." + fullPath + ((d._deleted || d.deleted) ? " (deleted)" : ""), LOG_LEVEL.VERBOSE); + } + return !force; } return false; });