Skip to content

Commit

Permalink
feat: added vault efs resource aquisition
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Nov 25, 2024
1 parent f771659 commit 803c7bd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/vaults/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface Vault {
writeG: VaultInternal['writeG'];
readF: VaultInternal['readF'];
readG: VaultInternal['readG'];
acquireRead: VaultInternal['acquireRead'];
acquireWrite: VaultInternal['acquireWrite'];
log: VaultInternal['log'];
version: VaultInternal['version'];
}
Expand Down
87 changes: 86 additions & 1 deletion src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import {
CreateDestroyStartStop,
ready,
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import { withF, withG } from '@matrixai/resources';
import {
ResourceAcquire,
ResourceRelease,
withF,
withG,
} from '@matrixai/resources';
import { RWLockWriter } from '@matrixai/async-locks';
import * as vaultsUtils from './utils';
import * as vaultsErrors from './errors';
Expand Down Expand Up @@ -536,6 +541,86 @@ class VaultInternal {
});
}

/**
* Acquire a read-only lock on this vault
*/
@ready(new vaultsErrors.ErrorVaultNotRunning())
public acquireRead(): ResourceAcquire<FileSystemReadable> {
return async () => {
const acquire = this.lock.read();
const [release] = await acquire();
return [
async (e?: Error) => {
release(e);
},
this.efsVault,
];
};
}

/**
* Acquire a read-write lock on this vault
*/
@ready(new vaultsErrors.ErrorVaultNotRunning())
public acquireWrite(
tran?: DBTransaction,
): ResourceAcquire<FileSystemWritable> {
return async () => {
let releaseTran: ResourceRelease | undefined = undefined;
const acquire = this.lock.write();
const [release] = await acquire([tran]);
if (tran == null) {
const acquireTran = this.db.transaction();
[releaseTran, tran] = await acquireTran();
}
// The returned transaction can be undefined, too. We won't handle those
// cases.
if (tran == null) utils.never();
await tran.lock(
[...this.vaultMetadataDbPath, VaultInternal.dirtyKey].join(''),
);
if (
(await tran.get([
...this.vaultMetadataDbPath,
VaultInternal.remoteKey,
])) != null
) {
// Mirrored vaults are immutable
throw new vaultsErrors.ErrorVaultRemoteDefined();
}
await tran.put(
[...this.vaultMetadataDbPath, VaultInternal.dirtyKey],
true,
);
return [
async (e?: Error) => {
if (e != null) {
try {
// After doing mutation we need to commit the new history
await this.createCommit();
} catch (e_) {
e = e_;
}
// This would happen if an error was caught inside the catch block
if (e != null) {
// Error implies dirty state
await this.cleanWorkingDirectory();
}
}
// For some reason, the transaction type doesn't properly waterfall
// down to here.
await tran!.put(
[...this.vaultMetadataDbPath, VaultInternal.dirtyKey],
false,
);
if (releaseTran != null) releaseTran(e);
release(e);
},
this.efsVault,
];
};
}

/**
* Pulls changes to a vault from the vault's default remote.
* If `pullNodeId` and `pullVaultNameOrId` it uses that for the remote instead.
Expand Down

0 comments on commit 803c7bd

Please sign in to comment.