Skip to content

Commit 731d1e0

Browse files
committed
WIP
1 parent b9849c2 commit 731d1e0

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

src/DBTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,12 @@ class DBTransaction {
323323
} catch (e) {
324324
if (e.code === 'TRANSACTION_CONFLICT') {
325325
this.logger.debug(
326-
`Failed Committing ${this.constructor.name} ${this._id} due to ${errors.ErrorDBTransactionConflict.name}`
326+
`Failed Committing ${this.constructor.name} ${this._id} due to ${errors.ErrorDBTransactionConflict.name}`,
327327
);
328328
throw new errors.ErrorDBTransactionConflict(undefined, { cause: e });
329329
} else {
330330
this.logger.debug(
331-
`Failed Committing ${this.constructor.name} ${this._id} due to ${e.message}`
331+
`Failed Committing ${this.constructor.name} ${this._id} due to ${e.message}`,
332332
);
333333
throw e;
334334
}

src/rocksdb/rocksdb.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ interface RocksDB {
149149
database: RocksDBDatabase,
150150
options: RocksDBTransactionOptions,
151151
): RocksDBTransaction;
152-
transactionId(
153-
transaction: RocksDBTransaction,
154-
): number;
152+
transactionId(transaction: RocksDBTransaction): number;
155153
transactionCommit(
156154
transaction: RocksDBTransaction,
157155
callback: Callback<[], void>,

src/rocksdb/rocksdbP.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ interface RocksDBP {
128128
database: RocksDBDatabase,
129129
options: RocksDBTransactionOptions,
130130
): RocksDBTransaction;
131-
transactionId(
132-
transaction: RocksDBTransaction,
133-
): number;
131+
transactionId(transaction: RocksDBTransaction): number;
134132
transactionCommit(transaction: RocksDBTransaction): Promise<void>;
135133
transactionRollback(transaction: RocksDBTransaction): Promise<void>;
136134
transactionGet(

tests/DBTransaction.test.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,21 @@ describe(DBTransaction.name, () => {
118118
});
119119
});
120120
await db.clear();
121-
await expect(withF([db.transaction()], async ([tran1]) => {
122-
expect(await tran1.get('hello')).toBeUndefined();
123-
await tran1.put('hello', 'foo');
124-
// This transaction commits, but the outside transaction will fail
125-
await withF([db.transaction()], async ([tran2]) => {
126-
// `tran1` has not yet committed
127-
expect(await tran2.get('hello')).toBeUndefined();
128-
// This will cause a conflict with the external transaction
129-
await tran2.put('hello', 'bar');
130-
// `tran2` has not yet committed
131-
expect(await tran1.get('hello')).toBe('foo');
132-
});
133-
})).rejects.toThrow(errors.ErrorDBTransactionConflict);
121+
await expect(
122+
withF([db.transaction()], async ([tran1]) => {
123+
expect(await tran1.get('hello')).toBeUndefined();
124+
await tran1.put('hello', 'foo');
125+
// This transaction commits, but the outside transaction will fail
126+
await withF([db.transaction()], async ([tran2]) => {
127+
// `tran1` has not yet committed
128+
expect(await tran2.get('hello')).toBeUndefined();
129+
// This will cause a conflict with the external transaction
130+
await tran2.put('hello', 'bar');
131+
// `tran2` has not yet committed
132+
expect(await tran1.get('hello')).toBe('foo');
133+
});
134+
}),
135+
).rejects.toThrow(errors.ErrorDBTransactionConflict);
134136
});
135137
test('repeatable reads', async () => {
136138
await withF([db.transaction()], async ([tran1]) => {
@@ -146,19 +148,23 @@ describe(DBTransaction.name, () => {
146148
});
147149
expect(await db.get('hello')).toBe('world');
148150
await db.clear();
149-
await expect(db.withTransactionF(async (tran1) => {
150-
expect(await tran1.get('hello')).toBeUndefined();
151-
await tran1.put('hello', 'foo');
152-
await expect(withF([db.transaction()], async ([tran2]) => {
153-
// `tran1` has not yet committed
154-
expect(await tran2.get('hello')).toBeUndefined();
155-
await tran2.put('hello', 'bar');
156-
})).resolves.toBeUndefined();
157-
// `tran2` is now committed
158-
// however because `foo` has been written in tran1, it stays as `foo`
159-
expect(await tran1.get('hello')).toBe('foo');
160-
// `hello` -> `foo` conflicts with `hello` -> `bar`
161-
})).rejects.toThrow(errors.ErrorDBTransactionConflict);
151+
await expect(
152+
db.withTransactionF(async (tran1) => {
153+
expect(await tran1.get('hello')).toBeUndefined();
154+
await tran1.put('hello', 'foo');
155+
await expect(
156+
withF([db.transaction()], async ([tran2]) => {
157+
// `tran1` has not yet committed
158+
expect(await tran2.get('hello')).toBeUndefined();
159+
await tran2.put('hello', 'bar');
160+
}),
161+
).resolves.toBeUndefined();
162+
// `tran2` is now committed
163+
// however because `foo` has been written in tran1, it stays as `foo`
164+
expect(await tran1.get('hello')).toBe('foo');
165+
// `hello` -> `foo` conflicts with `hello` -> `bar`
166+
}),
167+
).rejects.toThrow(errors.ErrorDBTransactionConflict);
162168
expect(await db.get('hello')).toBe('bar');
163169
});
164170
test('no phantom reads', async () => {
@@ -284,10 +290,10 @@ describe(DBTransaction.name, () => {
284290
const results: Array<[string, string]> = [];
285291
await withF([db.transaction()], async ([tran]) => {
286292
await tran.del(['a', 'b']);
287-
for await (const [kP, v] of tran.iterator<string>(
288-
['a'],
289-
{ keyAsBuffer: false, valueAsBuffer: false },
290-
)) {
293+
for await (const [kP, v] of tran.iterator<string>(['a'], {
294+
keyAsBuffer: false,
295+
valueAsBuffer: false,
296+
})) {
291297
results.push([kP[0] as string, v]);
292298
}
293299
});

0 commit comments

Comments
 (0)