Skip to content

Commit

Permalink
Add method to get current transaction id, #267
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Jan 30, 2024
1 parent 60139c2 commit c0b6a03
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ This will run the provided callback in a transaction much like `transaction` exc

The `childTransaction` function can be executed on its own (to run the child transaction inside the next queued transaction), or it can be executed inside another transaction callback, executing the child transaction within the current transaction.

### `db.getWriteTxnId(): number`
Returns the transaction id of the currently executing transaction. This is an integer that increments with each
transaction. This is only available inside transaction callbacks (for transactionSync or asynchronous transaction),
and does not provide access transaction ids for asynchronous put/delete methods (the `aftercommit` method can be
used for that).

### `db.committed: Promise`
This is a promise-like object that resolves when all previous writes have been committed.
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ declare namespace lmdb {
* @param action The function to execute within the transaction
**/
childTransaction<T>(action: () => T): Promise<T>
/**
* Returns the transaction id of the currently executing transaction. This is an integer that increments with each
* transaction. This is only available inside transaction callbacks (for transactionSync or asynchronous transaction),
* and does not provide access transaction ids for asynchronous put/delete methods (the 'aftercommit' method can be
* used for that).
*/
getWriteTxnId(): number
/**
* Returns the current transaction and marks it as in use. This can then be explicitly used for read operations
* @returns The transaction object
Expand Down
13 changes: 13 additions & 0 deletions src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,18 @@ Napi::Value EnvWrap::abortTxn(const CallbackInfo& info) {
delete currentTxn;
return info.Env().Undefined();
}
Napi::Value EnvWrap::getWriteTxnId(const Napi::CallbackInfo& info) {
TxnTracked *currentTxn = this->writeTxn;
size_t txn_id;
if (currentTxn) {
txn_id = mdb_txn_id(currentTxn->txn);
} else if (this->writeWorker) {
txn_id = mdb_txn_id(this->writeWorker->txn);
} else return throwError(info.Env(), "There is no active write transaction.");
return Number::New(info.Env(), txn_id);
}


/*Napi::Value EnvWrap::openDbi(const CallbackInfo& info) {
Expand Down Expand Up @@ -1171,6 +1183,7 @@ void EnvWrap::setupExports(Napi::Env env, Object exports) {
EnvWrap::InstanceMethod("beginTxn", &EnvWrap::beginTxn),
EnvWrap::InstanceMethod("commitTxn", &EnvWrap::commitTxn),
EnvWrap::InstanceMethod("abortTxn", &EnvWrap::abortTxn),
EnvWrap::InstanceMethod("getWriteTxnId", &EnvWrap::getWriteTxnId),
EnvWrap::InstanceMethod("sync", &EnvWrap::sync),
EnvWrap::InstanceMethod("resumeWriting", &EnvWrap::resumeWriting),
EnvWrap::InstanceMethod("startWriting", &EnvWrap::startWriting),
Expand Down
1 change: 1 addition & 0 deletions src/lmdb-js.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ class EnvWrap : public ObjectWrap<EnvWrap> {
Napi::Value beginTxn(const CallbackInfo& info);
Napi::Value commitTxn(const CallbackInfo& info);
Napi::Value abortTxn(const CallbackInfo& info);
Napi::Value getWriteTxnId(const CallbackInfo& info);

/*
Flushes all data to the disk asynchronously.
Expand Down
3 changes: 3 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ describe('lmdb-js', function () {
}
db2.put('key2-async', 'async test 2');
should.equal(db2.get('key2-async'), 'async test 2');
expect(db.getWriteTxnId()).gte(1);
});
should.equal(db.get('key1'), 'async test 1');
should.equal(db2.get('key2-async'), 'async test 2');
Expand Down Expand Up @@ -1152,9 +1153,11 @@ describe('lmdb-js', function () {
db.childTransaction(() => {
iterator = db.getRange({ start: 'c1' })[Symbol.iterator]();
should.equal(iterator.next().value.value, 'value1');
expect(db.getWriteTxnId()).gte(1);
});
}
should.equal(iterator.next().value.value, 'value2');
expect(db.getWriteTxnId()).gte(1);
});
should.equal(iterator.next().value.value, 'value3');
});
Expand Down
3 changes: 3 additions & 0 deletions write.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@ export function addWriteMethods(LMDBStore, { env, fixedBuffer, resetReadTxn, use
throw error;
}
},
getWriteTxnId() {
return env.getWriteTxnId();
},
transactionSyncStart(callback) {
return this.transactionSync(callback, 0);
},
Expand Down

0 comments on commit c0b6a03

Please sign in to comment.