Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CMCDragonkai committed Jun 26, 2022
1 parent b9849c2 commit 731d1e0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/DBTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ class DBTransaction {
} catch (e) {
if (e.code === 'TRANSACTION_CONFLICT') {
this.logger.debug(
`Failed Committing ${this.constructor.name} ${this._id} due to ${errors.ErrorDBTransactionConflict.name}`
`Failed Committing ${this.constructor.name} ${this._id} due to ${errors.ErrorDBTransactionConflict.name}`,
);
throw new errors.ErrorDBTransactionConflict(undefined, { cause: e });
} else {
this.logger.debug(
`Failed Committing ${this.constructor.name} ${this._id} due to ${e.message}`
`Failed Committing ${this.constructor.name} ${this._id} due to ${e.message}`,
);
throw e;
}
Expand Down
4 changes: 1 addition & 3 deletions src/rocksdb/rocksdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ interface RocksDB {
database: RocksDBDatabase,
options: RocksDBTransactionOptions,
): RocksDBTransaction;
transactionId(
transaction: RocksDBTransaction,
): number;
transactionId(transaction: RocksDBTransaction): number;
transactionCommit(
transaction: RocksDBTransaction,
callback: Callback<[], void>,
Expand Down
4 changes: 1 addition & 3 deletions src/rocksdb/rocksdbP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ interface RocksDBP {
database: RocksDBDatabase,
options: RocksDBTransactionOptions,
): RocksDBTransaction;
transactionId(
transaction: RocksDBTransaction,
): number;
transactionId(transaction: RocksDBTransaction): number;
transactionCommit(transaction: RocksDBTransaction): Promise<void>;
transactionRollback(transaction: RocksDBTransaction): Promise<void>;
transactionGet(
Expand Down
66 changes: 36 additions & 30 deletions tests/DBTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,21 @@ describe(DBTransaction.name, () => {
});
});
await db.clear();
await expect(withF([db.transaction()], async ([tran1]) => {
expect(await tran1.get('hello')).toBeUndefined();
await tran1.put('hello', 'foo');
// This transaction commits, but the outside transaction will fail
await withF([db.transaction()], async ([tran2]) => {
// `tran1` has not yet committed
expect(await tran2.get('hello')).toBeUndefined();
// This will cause a conflict with the external transaction
await tran2.put('hello', 'bar');
// `tran2` has not yet committed
expect(await tran1.get('hello')).toBe('foo');
});
})).rejects.toThrow(errors.ErrorDBTransactionConflict);
await expect(
withF([db.transaction()], async ([tran1]) => {
expect(await tran1.get('hello')).toBeUndefined();
await tran1.put('hello', 'foo');
// This transaction commits, but the outside transaction will fail
await withF([db.transaction()], async ([tran2]) => {
// `tran1` has not yet committed
expect(await tran2.get('hello')).toBeUndefined();
// This will cause a conflict with the external transaction
await tran2.put('hello', 'bar');
// `tran2` has not yet committed
expect(await tran1.get('hello')).toBe('foo');
});
}),
).rejects.toThrow(errors.ErrorDBTransactionConflict);
});
test('repeatable reads', async () => {
await withF([db.transaction()], async ([tran1]) => {
Expand All @@ -146,19 +148,23 @@ describe(DBTransaction.name, () => {
});
expect(await db.get('hello')).toBe('world');
await db.clear();
await expect(db.withTransactionF(async (tran1) => {
expect(await tran1.get('hello')).toBeUndefined();
await tran1.put('hello', 'foo');
await expect(withF([db.transaction()], async ([tran2]) => {
// `tran1` has not yet committed
expect(await tran2.get('hello')).toBeUndefined();
await tran2.put('hello', 'bar');
})).resolves.toBeUndefined();
// `tran2` is now committed
// however because `foo` has been written in tran1, it stays as `foo`
expect(await tran1.get('hello')).toBe('foo');
// `hello` -> `foo` conflicts with `hello` -> `bar`
})).rejects.toThrow(errors.ErrorDBTransactionConflict);
await expect(
db.withTransactionF(async (tran1) => {
expect(await tran1.get('hello')).toBeUndefined();
await tran1.put('hello', 'foo');
await expect(
withF([db.transaction()], async ([tran2]) => {
// `tran1` has not yet committed
expect(await tran2.get('hello')).toBeUndefined();
await tran2.put('hello', 'bar');
}),
).resolves.toBeUndefined();
// `tran2` is now committed
// however because `foo` has been written in tran1, it stays as `foo`
expect(await tran1.get('hello')).toBe('foo');
// `hello` -> `foo` conflicts with `hello` -> `bar`
}),
).rejects.toThrow(errors.ErrorDBTransactionConflict);
expect(await db.get('hello')).toBe('bar');
});
test('no phantom reads', async () => {
Expand Down Expand Up @@ -284,10 +290,10 @@ describe(DBTransaction.name, () => {
const results: Array<[string, string]> = [];
await withF([db.transaction()], async ([tran]) => {
await tran.del(['a', 'b']);
for await (const [kP, v] of tran.iterator<string>(
['a'],
{ keyAsBuffer: false, valueAsBuffer: false },
)) {
for await (const [kP, v] of tran.iterator<string>(['a'], {
keyAsBuffer: false,
valueAsBuffer: false,
})) {
results.push([kP[0] as string, v]);
}
});
Expand Down

0 comments on commit 731d1e0

Please sign in to comment.