From 90bc8930076e4c60a0fc0be85861872d50e6d3d6 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Fri, 20 Dec 2024 20:32:14 +0000 Subject: [PATCH] Performance improvements --- plugin/src/database/database.ts | 8 +- .../sync-locally-created-file.ts | 4 + .../sync-locally-deleted-file.ts | 4 +- .../sync-locally-updated-file.ts | 8 ++ .../sync-remotely-updated-file.ts | 86 +++++++++++-------- plugin/src/tracing/logger.ts | 1 + 6 files changed, 65 insertions(+), 46 deletions(-) diff --git a/plugin/src/database/database.ts b/plugin/src/database/database.ts index 48c619ac..70b3bcfa 100644 --- a/plugin/src/database/database.ts +++ b/plugin/src/database/database.ts @@ -43,13 +43,7 @@ export class Database { } } - Logger.getInstance().debug( - `Loaded documents: ${JSON.stringify( - Object.fromEntries(this._documents.entries()), - null, - 2 - )}` - ); + Logger.getInstance().debug(`Loaded ${this._documents.size} documents`); this._settings = { ...DEFAULT_SETTINGS, diff --git a/plugin/src/sync-operations/sync-locally-created-file.ts b/plugin/src/sync-operations/sync-locally-created-file.ts index 0fa37fc2..22c59169 100644 --- a/plugin/src/sync-operations/sync-locally-created-file.ts +++ b/plugin/src/sync-operations/sync-locally-created-file.ts @@ -79,6 +79,10 @@ export async function syncLocallyCreatedFile({ parentVersionId: response.vaultUpdateId, hash: responseHash, }); + + if (database.getLastSeenUpdateId() === response.vaultUpdateId - 1) { + await database.setLastSeenUpdateId(response.vaultUpdateId); + } } catch (e) { history.addHistoryEntry({ status: SyncStatus.ERROR, diff --git a/plugin/src/sync-operations/sync-locally-deleted-file.ts b/plugin/src/sync-operations/sync-locally-deleted-file.ts index 7bfb0b0b..4e7e1db6 100644 --- a/plugin/src/sync-operations/sync-locally-deleted-file.ts +++ b/plugin/src/sync-operations/sync-locally-deleted-file.ts @@ -46,8 +46,6 @@ export async function syncLocallyDeletedFile({ createdDate: new Date(), }); - await database.removeDocument(relativePath); - history.addHistoryEntry({ status: SyncStatus.SUCCESS, source: SyncSource.PUSH, @@ -55,6 +53,8 @@ export async function syncLocallyDeletedFile({ message: `Successfully deleted locally deleted file on the remote server`, type: SyncType.DELETE, }); + + await database.removeDocument(relativePath); } catch (e) { history.addHistoryEntry({ status: SyncStatus.ERROR, diff --git a/plugin/src/sync-operations/sync-locally-updated-file.ts b/plugin/src/sync-operations/sync-locally-updated-file.ts index f95e0fb7..e27873f1 100644 --- a/plugin/src/sync-operations/sync-locally-updated-file.ts +++ b/plugin/src/sync-operations/sync-locally-updated-file.ts @@ -78,6 +78,10 @@ export async function syncLocallyUpdatedFile({ await operations.remove(oldPath ?? relativePath); await database.removeDocument(oldPath ?? relativePath); + if (database.getLastSeenUpdateId() === response.vaultUpdateId - 1) { + await database.setLastSeenUpdateId(response.vaultUpdateId); + } + history.addHistoryEntry({ status: SyncStatus.SUCCESS, source: SyncSource.PULL, @@ -128,6 +132,10 @@ export async function syncLocallyUpdatedFile({ parentVersionId: response.vaultUpdateId, hash: responseHash, }); + + if (database.getLastSeenUpdateId() === response.vaultUpdateId - 1) { + await database.setLastSeenUpdateId(response.vaultUpdateId); + } } catch (e) { history.addHistoryEntry({ status: SyncStatus.ERROR, diff --git a/plugin/src/sync-operations/sync-remotely-updated-file.ts b/plugin/src/sync-operations/sync-remotely-updated-file.ts index 9632a46f..d26d0806 100644 --- a/plugin/src/sync-operations/sync-remotely-updated-file.ts +++ b/plugin/src/sync-operations/sync-remotely-updated-file.ts @@ -26,14 +26,6 @@ export async function syncRemotelyUpdatedFile({ `Syncing remotely updated file ${remoteVersion.relativePath}` ); - const content = ( - await syncServer.get({ - documentId: remoteVersion.documentId, - }) - ).contentBase64; - const contentBytes = lib.base64_to_bytes(content); - const contentHash = hash(contentBytes); - await waitForDocumentLock(remoteVersion.relativePath); try { @@ -53,6 +45,14 @@ export async function syncRemotelyUpdatedFile({ return; } + const content = ( + await syncServer.get({ + documentId: remoteVersion.documentId, + }) + ).contentBase64; + const contentBytes = lib.base64_to_bytes(content); + const contentHash = hash(contentBytes); + await operations.create(remoteVersion.relativePath, contentBytes); await database.setDocument({ documentId: remoteVersion.documentId, @@ -71,9 +71,17 @@ export async function syncRemotelyUpdatedFile({ } const [relativePath, metadata] = currentVersion; + if (metadata.parentVersionId === remoteVersion.vaultUpdateId) { + Logger.getInstance().debug( + `Document ${relativePath} is already up to date` + ); + return; + } + if (relativePath !== remoteVersion.relativePath) { await waitForDocumentLock(relativePath); } + try { if (remoteVersion.isDeleted) { await operations.remove(relativePath); @@ -94,40 +102,44 @@ export async function syncRemotelyUpdatedFile({ Logger.getInstance().info( `Document ${relativePath} has been updated both remotely and locally, skipping until the event is processed` ); - } else if (contentHash !== metadata.hash) { - if (relativePath !== remoteVersion.relativePath) { - await operations.move( - relativePath, - remoteVersion.relativePath - ); - } + return; + } - await operations.write( - remoteVersion.relativePath, - currentContent, - contentBytes - ); - await database.moveDocument({ + const content = ( + await syncServer.get({ documentId: remoteVersion.documentId, - oldRelativePath: relativePath, - relativePath: remoteVersion.relativePath, - parentVersionId: remoteVersion.vaultUpdateId, - hash: contentHash, - }); + }) + ).contentBase64; + const contentBytes = lib.base64_to_bytes(content); + const contentHash = hash(contentBytes); - history.addHistoryEntry({ - status: SyncStatus.SUCCESS, - source: SyncSource.PULL, - relativePath: remoteVersion.relativePath, - message: `Successfully updated remotely updated file locally`, - type: SyncType.UPDATE, - }); - } - { - Logger.getInstance().debug( - `Document ${relativePath} is already up to date` + if (relativePath !== remoteVersion.relativePath) { + await operations.move( + relativePath, + remoteVersion.relativePath ); } + + await operations.write( + remoteVersion.relativePath, + currentContent, + contentBytes + ); + await database.moveDocument({ + documentId: remoteVersion.documentId, + oldRelativePath: relativePath, + relativePath: remoteVersion.relativePath, + parentVersionId: remoteVersion.vaultUpdateId, + hash: contentHash, + }); + + history.addHistoryEntry({ + status: SyncStatus.SUCCESS, + source: SyncSource.PULL, + relativePath: remoteVersion.relativePath, + message: `Successfully updated remotely updated file locally`, + type: SyncType.UPDATE, + }); } } finally { if (relativePath !== remoteVersion.relativePath) { diff --git a/plugin/src/tracing/logger.ts b/plugin/src/tracing/logger.ts index 2db69e15..1d4bb717 100644 --- a/plugin/src/tracing/logger.ts +++ b/plugin/src/tracing/logger.ts @@ -72,6 +72,7 @@ export class Logger { } private pushMessage(message: string, level: LogLevel): void { + console.log(`[${level}] ${message}`); this.messages.push(new LogLine(level, message)); if (this.messages.length > Logger.MAX_MESSAGES) { this.messages.shift();