Performance improvements
This commit is contained in:
parent
831e6f7651
commit
90bc893007
6 changed files with 65 additions and 46 deletions
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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,40 +102,44 @@ 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;
|
||||||
if (relativePath !== remoteVersion.relativePath) {
|
}
|
||||||
await operations.move(
|
|
||||||
relativePath,
|
|
||||||
remoteVersion.relativePath
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await operations.write(
|
const content = (
|
||||||
remoteVersion.relativePath,
|
await syncServer.get({
|
||||||
currentContent,
|
|
||||||
contentBytes
|
|
||||||
);
|
|
||||||
await database.moveDocument({
|
|
||||||
documentId: remoteVersion.documentId,
|
documentId: remoteVersion.documentId,
|
||||||
oldRelativePath: relativePath,
|
})
|
||||||
relativePath: remoteVersion.relativePath,
|
).contentBase64;
|
||||||
parentVersionId: remoteVersion.vaultUpdateId,
|
const contentBytes = lib.base64_to_bytes(content);
|
||||||
hash: contentHash,
|
const contentHash = hash(contentBytes);
|
||||||
});
|
|
||||||
|
|
||||||
history.addHistoryEntry({
|
if (relativePath !== remoteVersion.relativePath) {
|
||||||
status: SyncStatus.SUCCESS,
|
await operations.move(
|
||||||
source: SyncSource.PULL,
|
relativePath,
|
||||||
relativePath: remoteVersion.relativePath,
|
remoteVersion.relativePath
|
||||||
message: `Successfully updated remotely updated file locally`,
|
|
||||||
type: SyncType.UPDATE,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
{
|
|
||||||
Logger.getInstance().debug(
|
|
||||||
`Document ${relativePath} is already up to date`
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
} finally {
|
||||||
if (relativePath !== remoteVersion.relativePath) {
|
if (relativePath !== remoteVersion.relativePath) {
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue