Skip to content

Commit

Permalink
Merge pull request #774 from MatrixAI/feature-eng-364-implement-a-fil…
Browse files Browse the repository at this point in the history
…e-tree-serialiserde-serialiser

Implementing file tree serialisation utilities
  • Loading branch information
tegefaulkes authored Jul 26, 2024
2 parents 2a88416 + 262b213 commit 76b4cf2
Show file tree
Hide file tree
Showing 9 changed files with 1,579 additions and 586 deletions.
39 changes: 39 additions & 0 deletions src/vaults/VaultManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,45 @@ class VaultManager {
});
}

/**
* Takes a generator and runs it with the listed vaults. locking is handled automatically
* @param vaultIds List of vault ID for vaults you wish to use
* @param g Generator you wish to run with the provided vaults
* @param tran
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async *withVaultsG<T, Treturn, Tnext>(
vaultIds: Array<VaultId>,
g: (...args: Array<Vault>) => AsyncGenerator<T, Treturn, Tnext>,
tran?: DBTransaction,
): AsyncGenerator<T, Treturn, Tnext> {
if (tran == null) {
return yield* this.db.withTransactionG((tran) =>
this.withVaultsG(vaultIds, g, tran),
);
}

// Obtaining locks
const vaultLocks: Array<LockRequest<RWLockWriter>> = vaultIds.map(
(vaultId) => {
return [vaultId.toString(), RWLockWriter, 'read'];
},
);
// Running the function with locking
return yield* this.vaultLocks.withG(
...vaultLocks,
async function* (): AsyncGenerator<T, Treturn, Tnext> {
// Getting the vaults while locked
const vaults = await Promise.all(
vaultIds.map(async (vaultId) => {
return await this.getVault(vaultId, tran);
}),
);
return yield* g(...vaults);
},
);
}

protected async setupKey(tran: DBTransaction): Promise<Buffer> {
let key: Buffer | undefined;
key = await tran.get([...this.vaultsDbPath, 'key'], true);
Expand Down
Loading

0 comments on commit 76b4cf2

Please sign in to comment.