Remove extra hashing and only update changed files when syncing
This commit is contained in:
parent
c41ce7ef68
commit
cacd0243de
2 changed files with 49 additions and 57 deletions
|
|
@ -75,7 +75,7 @@ export class SyncService {
|
||||||
relativePath: RelativePath;
|
relativePath: RelativePath;
|
||||||
contentBytes: Uint8Array;
|
contentBytes: Uint8Array;
|
||||||
createdDate: Date;
|
createdDate: Date;
|
||||||
}): Promise<components["schemas"]["DocumentVersion"]> {
|
}): Promise<components["schemas"]["DocumentUpdateResponse"]> {
|
||||||
const response = await this.client.POST(
|
const response = await this.client.POST(
|
||||||
"/vaults/{vault_id}/documents",
|
"/vaults/{vault_id}/documents",
|
||||||
{
|
{
|
||||||
|
|
@ -126,7 +126,7 @@ export class SyncService {
|
||||||
relativePath: RelativePath;
|
relativePath: RelativePath;
|
||||||
contentBytes: Uint8Array;
|
contentBytes: Uint8Array;
|
||||||
createdDate: Date;
|
createdDate: Date;
|
||||||
}): Promise<components["schemas"]["DocumentVersion"]> {
|
}): Promise<components["schemas"]["DocumentUpdateResponse"]> {
|
||||||
const response = await this.client.PUT(
|
const response = await this.client.PUT(
|
||||||
"/vaults/{vault_id}/documents/{document_id}",
|
"/vaults/{vault_id}/documents/{document_id}",
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -148,30 +148,16 @@ export class Syncer {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = await this.operations.read(
|
|
||||||
relativePath
|
|
||||||
);
|
|
||||||
if (metadata.hash !== hash(content)) {
|
|
||||||
Logger.getInstance().debug(
|
Logger.getInstance().debug(
|
||||||
`Document ${relativePath} has been updated locally, scheduling sync to update it`
|
`Document ${relativePath} has been updated locally, scheduling sync to update it`
|
||||||
);
|
);
|
||||||
return this.internalSyncLocallyUpdatedFile({
|
return this.internalSyncLocallyUpdatedFile({
|
||||||
relativePath: relativePath,
|
relativePath,
|
||||||
updateTime:
|
updateTime:
|
||||||
await this.operations.getModificationTime(
|
await this.operations.getModificationTime(
|
||||||
relativePath
|
relativePath
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
this.history.addHistoryEntry({
|
|
||||||
status: SyncStatus.NO_OP,
|
|
||||||
source: SyncSource.PUSH,
|
|
||||||
relativePath,
|
|
||||||
message:
|
|
||||||
"Document hasn't been updated locally, no need to sync",
|
|
||||||
});
|
|
||||||
return Promise.resolve();
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
@ -218,7 +204,7 @@ export class Syncer {
|
||||||
SyncSource.PUSH,
|
SyncSource.PUSH,
|
||||||
async () => {
|
async () => {
|
||||||
const contentBytes = await this.operations.read(relativePath);
|
const contentBytes = await this.operations.read(relativePath);
|
||||||
const contentHash = hash(contentBytes);
|
let contentHash = hash(contentBytes);
|
||||||
|
|
||||||
const metadata = this.database.getDocument(relativePath);
|
const metadata = this.database.getDocument(relativePath);
|
||||||
if (metadata) {
|
if (metadata) {
|
||||||
|
|
@ -251,10 +237,12 @@ export class Syncer {
|
||||||
type: SyncType.CREATE,
|
type: SyncType.CREATE,
|
||||||
});
|
});
|
||||||
|
|
||||||
const responseBytes = lib.base64ToBytes(response.contentBase64);
|
if (response.type === "MergingUpdate") {
|
||||||
const responseHash = hash(responseBytes);
|
const responseBytes = lib.base64ToBytes(
|
||||||
|
response.contentBase64
|
||||||
|
);
|
||||||
|
contentHash = hash(responseBytes);
|
||||||
|
|
||||||
if (contentHash !== responseHash) {
|
|
||||||
await this.operations.write(
|
await this.operations.write(
|
||||||
relativePath,
|
relativePath,
|
||||||
contentBytes,
|
contentBytes,
|
||||||
|
|
@ -273,7 +261,7 @@ export class Syncer {
|
||||||
documentId: response.documentId,
|
documentId: response.documentId,
|
||||||
relativePath: response.relativePath,
|
relativePath: response.relativePath,
|
||||||
parentVersionId: response.vaultUpdateId,
|
parentVersionId: response.vaultUpdateId,
|
||||||
hash: responseHash,
|
hash: contentHash,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.tryIncrementVaultUpdateId(response.vaultUpdateId);
|
await this.tryIncrementVaultUpdateId(response.vaultUpdateId);
|
||||||
|
|
@ -315,7 +303,7 @@ export class Syncer {
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentBytes = await this.operations.read(relativePath);
|
const contentBytes = await this.operations.read(relativePath);
|
||||||
const contentHash = hash(contentBytes);
|
let contentHash = hash(contentBytes);
|
||||||
|
|
||||||
if (metadata.hash === contentHash && oldPath === undefined) {
|
if (metadata.hash === contentHash && oldPath === undefined) {
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
|
|
@ -362,50 +350,54 @@ export class Syncer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseBytes = lib.base64ToBytes(response.contentBase64);
|
|
||||||
const responseHash = hash(responseBytes);
|
|
||||||
|
|
||||||
if (response.relativePath != relativePath) {
|
if (response.relativePath != relativePath) {
|
||||||
await waitForDocumentLock(response.relativePath);
|
await waitForDocumentLock(response.relativePath);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (response.relativePath != relativePath) {
|
||||||
await this.operations.move(
|
await this.operations.move(
|
||||||
oldPath ?? relativePath,
|
oldPath ?? relativePath,
|
||||||
response.relativePath
|
response.relativePath
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.type === "MergingUpdate") {
|
||||||
|
const responseBytes = lib.base64ToBytes(
|
||||||
|
response.contentBase64
|
||||||
|
);
|
||||||
|
contentHash = hash(responseBytes);
|
||||||
await this.operations.write(
|
await this.operations.write(
|
||||||
response.relativePath,
|
response.relativePath,
|
||||||
contentBytes,
|
contentBytes,
|
||||||
responseBytes
|
responseBytes
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
status: SyncStatus.SUCCESS,
|
status: SyncStatus.SUCCESS,
|
||||||
source: SyncSource.PULL,
|
source: SyncSource.PULL,
|
||||||
relativePath,
|
relativePath,
|
||||||
message:
|
message: `The file we updated had been updated remotely, so we downloaded the merged version`,
|
||||||
"The file we updated had been moved remotely, therefore, we have moved it locally as well",
|
|
||||||
type: SyncType.UPDATE,
|
type: SyncType.UPDATE,
|
||||||
});
|
});
|
||||||
} finally {
|
|
||||||
unlockDocument(response.relativePath);
|
|
||||||
}
|
|
||||||
} else if (contentHash !== responseHash) {
|
|
||||||
await this.operations.write(
|
|
||||||
relativePath,
|
|
||||||
contentBytes,
|
|
||||||
responseBytes
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.database.moveDocument({
|
await this.database.moveDocument({
|
||||||
documentId: metadata.documentId,
|
documentId: metadata.documentId,
|
||||||
oldRelativePath: oldPath ?? relativePath,
|
oldRelativePath: oldPath ?? relativePath,
|
||||||
relativePath: response.relativePath,
|
relativePath: response.relativePath,
|
||||||
parentVersionId: response.vaultUpdateId,
|
parentVersionId: response.vaultUpdateId,
|
||||||
hash: responseHash,
|
hash: contentHash,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.tryIncrementVaultUpdateId(response.vaultUpdateId);
|
await this.tryIncrementVaultUpdateId(
|
||||||
|
response.vaultUpdateId
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
if (response.relativePath != relativePath) {
|
||||||
|
unlockDocument(response.relativePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue