Performance improvements

This commit is contained in:
Andras Schmelczer 2024-12-20 20:32:14 +00:00
parent 831e6f7651
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(
`Loaded documents: ${JSON.stringify(
Object.fromEntries(this._documents.entries()),
null,
2
)}`
);
Logger.getInstance().debug(`Loaded ${this._documents.size} documents`);
this._settings = {
...DEFAULT_SETTINGS,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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) {

View file

@ -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();