Performance improvements

This commit is contained in:
Andras Schmelczer 2024-12-20 20:32:14 +00:00
commit 90bc893007
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 65 additions and 46 deletions

View file

@ -43,13 +43,7 @@ export class Database {
} }
} }
Logger.getInstance().debug( Logger.getInstance().debug(`Loaded ${this._documents.size} documents`);
`Loaded documents: ${JSON.stringify(
Object.fromEntries(this._documents.entries()),
null,
2
)}`
);
this._settings = { this._settings = {
...DEFAULT_SETTINGS, ...DEFAULT_SETTINGS,

View file

@ -79,6 +79,10 @@ export async function syncLocallyCreatedFile({
parentVersionId: response.vaultUpdateId, parentVersionId: response.vaultUpdateId,
hash: responseHash, hash: responseHash,
}); });
if (database.getLastSeenUpdateId() === response.vaultUpdateId - 1) {
await database.setLastSeenUpdateId(response.vaultUpdateId);
}
} catch (e) { } catch (e) {
history.addHistoryEntry({ history.addHistoryEntry({
status: SyncStatus.ERROR, status: SyncStatus.ERROR,

View file

@ -46,8 +46,6 @@ export async function syncLocallyDeletedFile({
createdDate: new Date(), createdDate: new Date(),
}); });
await database.removeDocument(relativePath);
history.addHistoryEntry({ history.addHistoryEntry({
status: SyncStatus.SUCCESS, status: SyncStatus.SUCCESS,
source: SyncSource.PUSH, source: SyncSource.PUSH,
@ -55,6 +53,8 @@ export async function syncLocallyDeletedFile({
message: `Successfully deleted locally deleted file on the remote server`, message: `Successfully deleted locally deleted file on the remote server`,
type: SyncType.DELETE, type: SyncType.DELETE,
}); });
await database.removeDocument(relativePath);
} catch (e) { } catch (e) {
history.addHistoryEntry({ history.addHistoryEntry({
status: SyncStatus.ERROR, status: SyncStatus.ERROR,

View file

@ -78,6 +78,10 @@ export async function syncLocallyUpdatedFile({
await operations.remove(oldPath ?? relativePath); await operations.remove(oldPath ?? relativePath);
await database.removeDocument(oldPath ?? relativePath); await database.removeDocument(oldPath ?? relativePath);
if (database.getLastSeenUpdateId() === response.vaultUpdateId - 1) {
await database.setLastSeenUpdateId(response.vaultUpdateId);
}
history.addHistoryEntry({ history.addHistoryEntry({
status: SyncStatus.SUCCESS, status: SyncStatus.SUCCESS,
source: SyncSource.PULL, source: SyncSource.PULL,
@ -128,6 +132,10 @@ export async function syncLocallyUpdatedFile({
parentVersionId: response.vaultUpdateId, parentVersionId: response.vaultUpdateId,
hash: responseHash, hash: responseHash,
}); });
if (database.getLastSeenUpdateId() === response.vaultUpdateId - 1) {
await database.setLastSeenUpdateId(response.vaultUpdateId);
}
} catch (e) { } catch (e) {
history.addHistoryEntry({ history.addHistoryEntry({
status: SyncStatus.ERROR, status: SyncStatus.ERROR,

View file

@ -26,14 +26,6 @@ export async function syncRemotelyUpdatedFile({
`Syncing remotely updated file ${remoteVersion.relativePath}` `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); await waitForDocumentLock(remoteVersion.relativePath);
try { try {
@ -53,6 +45,14 @@ export async function syncRemotelyUpdatedFile({
return; 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 operations.create(remoteVersion.relativePath, contentBytes);
await database.setDocument({ await database.setDocument({
documentId: remoteVersion.documentId, documentId: remoteVersion.documentId,
@ -71,9 +71,17 @@ export async function syncRemotelyUpdatedFile({
} }
const [relativePath, metadata] = currentVersion; const [relativePath, metadata] = currentVersion;
if (metadata.parentVersionId === remoteVersion.vaultUpdateId) {
Logger.getInstance().debug(
`Document ${relativePath} is already up to date`
);
return;
}
if (relativePath !== remoteVersion.relativePath) { if (relativePath !== remoteVersion.relativePath) {
await waitForDocumentLock(relativePath); await waitForDocumentLock(relativePath);
} }
try { try {
if (remoteVersion.isDeleted) { if (remoteVersion.isDeleted) {
await operations.remove(relativePath); await operations.remove(relativePath);
@ -94,7 +102,17 @@ export async function syncRemotelyUpdatedFile({
Logger.getInstance().info( Logger.getInstance().info(
`Document ${relativePath} has been updated both remotely and locally, skipping until the event is processed` `Document ${relativePath} has been updated both remotely and locally, skipping until the event is processed`
); );
} else if (contentHash !== metadata.hash) { return;
}
const content = (
await syncServer.get({
documentId: remoteVersion.documentId,
})
).contentBase64;
const contentBytes = lib.base64_to_bytes(content);
const contentHash = hash(contentBytes);
if (relativePath !== remoteVersion.relativePath) { if (relativePath !== remoteVersion.relativePath) {
await operations.move( await operations.move(
relativePath, relativePath,
@ -123,12 +141,6 @@ export async function syncRemotelyUpdatedFile({
type: SyncType.UPDATE, type: SyncType.UPDATE,
}); });
} }
{
Logger.getInstance().debug(
`Document ${relativePath} is already up to date`
);
}
}
} finally { } finally {
if (relativePath !== remoteVersion.relativePath) { if (relativePath !== remoteVersion.relativePath) {
unlockDocument(relativePath); unlockDocument(relativePath);

View file

@ -72,6 +72,7 @@ export class Logger {
} }
private pushMessage(message: string, level: LogLevel): void { private pushMessage(message: string, level: LogLevel): void {
console.log(`[${level}] ${message}`);
this.messages.push(new LogLine(level, message)); this.messages.push(new LogLine(level, message));
if (this.messages.length > Logger.MAX_MESSAGES) { if (this.messages.length > Logger.MAX_MESSAGES) {
this.messages.shift(); this.messages.shift();