Skip to content

Commit b016dae

Browse files
committed
WIP
1 parent 6497d9a commit b016dae

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

src/DBTransaction.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { ResourceRelease } from '@matrixai/resources';
2-
import type { LockBox, MultiLockRequest as AsyncLocksMultiLockRequest } from '@matrixai/async-locks';
2+
import type {
3+
LockBox,
4+
MultiLockRequest as AsyncLocksMultiLockRequest,
5+
} from '@matrixai/async-locks';
36
import type DB from './DB';
47
import type {
58
ToString,
@@ -31,11 +34,14 @@ class DBTransaction {
3134
protected _db: DB;
3235
protected logger: Logger;
3336
protected lockBox: LockBox<RWLockWriter>;
34-
protected _locks: Map<string, {
35-
lock: RWLockWriter,
36-
type: 'read' | 'write',
37-
release: ResourceRelease,
38-
}> = new Map();
37+
protected _locks: Map<
38+
string,
39+
{
40+
lock: RWLockWriter;
41+
type: 'read' | 'write';
42+
release: ResourceRelease;
43+
}
44+
> = new Map();
3945
protected _options: RocksDBTransactionOptions;
4046
protected _transaction: RocksDBTransaction;
4147
protected _snapshot: RocksDBTransactionSnapshot;
@@ -118,11 +124,14 @@ class DBTransaction {
118124
return this._rollbacked;
119125
}
120126

121-
get locks(): ReadonlyMap<string, {
122-
lock: RWLockWriter,
123-
type: 'read' | 'write',
124-
release: ResourceRelease,
125-
}> {
127+
get locks(): ReadonlyMap<
128+
string,
129+
{
130+
lock: RWLockWriter;
131+
type: 'read' | 'write';
132+
release: ResourceRelease;
133+
}
134+
> {
126135
return this._locks;
127136
}
128137

@@ -146,15 +155,17 @@ class DBTransaction {
146155
* There is no support for lock upgrading or downgrading
147156
* There is no deadlock detection
148157
*/
149-
public async lock(...requests: Array<MultiLockRequest | string>): Promise<void> {
158+
public async lock(
159+
...requests: Array<MultiLockRequest | string>
160+
): Promise<void> {
150161
const requests_: Array<AsyncLocksMultiLockRequest<RWLockWriter>> = [];
151162
for (const request of requests) {
152163
if (Array.isArray(request)) {
153164
const [key, ...lockingParams] = request;
154165
const key_ = key.toString();
155166
const lock = this._locks.get(key_);
156167
// Default the lock type to `write`
157-
const lockType = lockingParams[0] = lockingParams[0] ?? 'write';
168+
const lockType = (lockingParams[0] = lockingParams[0] ?? 'write');
158169
if (lock == null) {
159170
requests_.push([key_, RWLockWriter, ...lockingParams]);
160171
} else if (lock.type !== lockType) {

src/errors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class ErrorDBTransactionConflict<T> extends ErrorDBTransaction<T> {
8383
}
8484

8585
class ErrorDBTransactionLockType<T> extends ErrorDBTransaction<T> {
86-
static description = 'DBTransaction does not support upgrading or downgrading the lock type';
86+
static description =
87+
'DBTransaction does not support upgrading or downgrading the lock type';
8788
}
8889

8990
export {

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ type DBOps = Array<DBOp>;
161161

162162
type MultiLockRequest = [
163163
key: ToString,
164-
...lockingParams: Parameters<RWLockWriter['lock']>
164+
...lockingParams: Parameters<RWLockWriter['lock']>,
165165
];
166166

167167
export type {
@@ -182,5 +182,5 @@ export type {
182182
DBBatch,
183183
DBOp,
184184
DBOps,
185-
MultiLockRequest
185+
MultiLockRequest,
186186
};

tests/DBTransaction.test.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,11 +1019,17 @@ describe(DBTransaction.name, () => {
10191019
await tran1.lock(['foo', 'read']);
10201020
await tran1.lock(['bar', 'write']);
10211021
// There is no automatic lock upgrade or downgrade
1022-
await expect(tran1.lock(['foo', 'write'])).rejects.toThrow(errors.ErrorDBTransactionLockType);
1023-
await expect(tran1.lock(['bar', 'read'])).rejects.toThrow(errors.ErrorDBTransactionLockType);
1022+
await expect(tran1.lock(['foo', 'write'])).rejects.toThrow(
1023+
errors.ErrorDBTransactionLockType,
1024+
);
1025+
await expect(tran1.lock(['bar', 'read'])).rejects.toThrow(
1026+
errors.ErrorDBTransactionLockType,
1027+
);
10241028
await db.withTransactionF(async (tran2) => {
10251029
await tran2.lock(['foo', 'read']);
1026-
await expect(tran2.lock(['bar', 'write', 0])).rejects.toThrow(locksErrors.ErrorAsyncLocksTimeout);
1030+
await expect(tran2.lock(['bar', 'write', 0])).rejects.toThrow(
1031+
locksErrors.ErrorAsyncLocksTimeout,
1032+
);
10271033
expect(tran1.locks.size).toBe(2);
10281034
expect(tran1.locks.has('foo')).toBe(true);
10291035
expect(tran1.locks.get('foo')!.type).toBe('read');
@@ -1048,7 +1054,7 @@ describe(DBTransaction.name, () => {
10481054
});
10491055
});
10501056
test('locks are unlocked in reverse order', async () => {
1051-
let order: Array<string> = [];
1057+
const order: Array<string> = [];
10521058
let p1, p2;
10531059
await db.withTransactionF(async (tran) => {
10541060
// '1' and '2' are in sort order
@@ -1083,7 +1089,9 @@ describe(DBTransaction.name, () => {
10831089
// This is a noop, because `tran1` owns `key1` and `key2`
10841090
await tran2.unlock('key1', 'key2');
10851091
// This fails because `key1` is still locked by `tran1`
1086-
await expect(tran2.lock(['key1', 'write', 0])).rejects.toThrow(locksErrors.ErrorAsyncLocksTimeout);
1092+
await expect(tran2.lock(['key1', 'write', 0])).rejects.toThrow(
1093+
locksErrors.ErrorAsyncLocksTimeout,
1094+
);
10871095
await tran1.unlock('key1');
10881096
expect(tran1.locks.size).toBe(1);
10891097
// This succeeds because `key1` is now unlocked
@@ -1093,9 +1101,9 @@ describe(DBTransaction.name, () => {
10931101
await tran1.unlock('key1');
10941102
expect(tran2.locks.has('key1')).toBe(true);
10951103
expect(tran1.locks.has('key1')).toBe(false);
1096-
await expect(tran1.lock([
1097-
'key1', 'write', 0
1098-
])).rejects.toThrow(locksErrors.ErrorAsyncLocksTimeout);
1104+
await expect(tran1.lock(['key1', 'write', 0])).rejects.toThrow(
1105+
locksErrors.ErrorAsyncLocksTimeout,
1106+
);
10991107
});
11001108
await tran1.lock('key1');
11011109
expect(tran1.locks.has('key1')).toBe(true);
@@ -1113,9 +1121,12 @@ describe(DBTransaction.name, () => {
11131121
const p1 = tran1.lock(['bar', 'write', 50]);
11141122
const p2 = tran2.lock(['foo', 'write', 50]);
11151123
const results = await Promise.allSettled([p1, p2]);
1116-
expect(results.every(r =>
1117-
r.status === 'rejected' &&
1118-
r.reason instanceof locksErrors.ErrorAsyncLocksTimeout)
1124+
expect(
1125+
results.every(
1126+
(r) =>
1127+
r.status === 'rejected' &&
1128+
r.reason instanceof locksErrors.ErrorAsyncLocksTimeout,
1129+
),
11191130
).toBe(true);
11201131
});
11211132
});

0 commit comments

Comments
 (0)