UBERF-8578: Fix extra stat call for storage adapter (#7132)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev
2024-11-08 17:36:15 +07:00
committed by GitHub
parent a05e2a31d9
commit 04c8c2ffa0
5 changed files with 79 additions and 63 deletions
+13 -10
View File
@@ -28,17 +28,20 @@ export async function yDocFromStorage (
ydoc?: YDoc
): Promise<YDoc | undefined> {
// stat the object to ensure it exists, because read will throw an error in this case
const blob = await storageAdapter.stat(ctx, workspace, documentId)
if (blob === undefined) {
return undefined
try {
const buffer = await storageAdapter.read(ctx, workspace, documentId)
// no need to apply gc because we load existing document
// it is either already gc-ed, or gc not needed and it is disabled
ydoc ??= new YDoc({ guid: generateId(), gc: false })
return yDocFromBuffer(Buffer.concat(buffer as any), ydoc)
} catch (err: any) {
if (err.code === 'NoSuchKey') {
return undefined
}
throw err
}
// no need to apply gc because we load existing document
// it is either already gc-ed, or gc not needed and it is disabled
ydoc ??= new YDoc({ guid: generateId(), gc: false })
const buffer = await storageAdapter.read(ctx, workspace, documentId)
return yDocFromBuffer(Buffer.concat(buffer as any), ydoc)
}
/** @public */