1+ #include  < chrono> 
12#include  < sstream> 
23#include  < thread> 
34#include  " database.h" 
@@ -140,7 +141,12 @@ napi_value Transaction::Abort(napi_env env, napi_callback_info info) {
140141/* *
141142 * State for the `Commit` async work. 
142143 */  
143- typedef  BaseAsyncState<std::shared_ptr<TransactionHandle>> TransactionCommitState;
144+ struct  TransactionCommitState  final  : BaseAsyncState<std::shared_ptr<TransactionHandle>> {
145+ 	TransactionCommitState (napi_env env, std::shared_ptr<TransactionHandle> txnHandle)
146+ 		: BaseAsyncState<std::shared_ptr<TransactionHandle>>(env, txnHandle), timestamp(0 ) {}
147+ 
148+ 	uint64_t  timestamp;
149+ };
144150
145151/* *
146152 * Commits the transaction. 
@@ -186,8 +192,14 @@ napi_value Transaction::Commit(napi_env env, napi_callback_info info) {
186192			if  (!state->handle  || !state->handle ->dbHandle  || !state->handle ->dbHandle ->opened () || state->handle ->dbHandle ->isCancelled ()) {
187193				state->status  = rocksdb::Status::Aborted (" Database closed during transaction commit operation" 
188194			} else  {
195+ 				//  set current timestamp before committing
196+ 				auto  now = std::chrono::system_clock::now ();
197+ 				auto  timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch ()).count ();
198+ 				state->handle ->txn ->SetCommitTimestamp (timestamp);
199+ 
189200				state->status  = state->handle ->txn ->Commit ();
190201				if  (state->status .ok ()) {
202+ 					state->timestamp  = timestamp;
191203					DEBUG_LOG (" Transaction::Commit emitted committed event\n " 
192204					state->handle ->state  = TransactionState::Committed;
193205					state->handle ->dbHandle ->descriptor ->notify (env, " committed" nullptr );
@@ -207,17 +219,20 @@ napi_value Transaction::Commit(napi_env env, napi_callback_info info) {
207219				if  (state->status .ok ()) {
208220					DEBUG_LOG (" Transaction::Commit complete closing handle=%p\n " handle .get ())
209221
210- 					//  BUG!
211222					if  (state->handle ) {
212223						state->handle ->close ();
213224					} else  {
214225						DEBUG_LOG (" Transaction::Commit complete, but handle is null!\n " 
215226					}
216227
228+ 					napi_value result;
229+ 					double  milliseconds = static_cast <double >(state->timestamp ) / 1000.0 ;
230+ 					NAPI_STATUS_THROWS_VOID (::napi_create_double (env, milliseconds, &result))
231+ 
217232					DEBUG_LOG (" Transaction::Commit complete calling resolve\n " 
218233					napi_value resolve;
219234					NAPI_STATUS_THROWS_VOID (::napi_get_reference_value (env, state->resolveRef , &resolve))
220- 					NAPI_STATUS_THROWS_VOID (::napi_call_function (env, global, resolve, 0 ,  nullptr , nullptr ))
235+ 					NAPI_STATUS_THROWS_VOID (::napi_call_function (env, global, resolve, 1 , &result , nullptr ))
221236				} else  {
222237					napi_value reject;
223238					napi_value error;
@@ -257,6 +272,11 @@ napi_value Transaction::CommitSync(napi_env env, napi_callback_info info) {
257272	}
258273	(*txnHandle)->state  = TransactionState::Committing;
259274
275+ 	//  set current timestamp before committing
276+ 	auto  now = std::chrono::system_clock::now ();
277+ 	auto  timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch ()).count ();
278+ 	(*txnHandle)->txn ->SetCommitTimestamp (timestamp);
279+ 
260280	rocksdb::Status status = (*txnHandle)->txn ->Commit ();
261281	if  (status.ok ()) {
262282		DEBUG_LOG (" Transaction::CommitSync emitted committed event\n " 
@@ -265,13 +285,18 @@ napi_value Transaction::CommitSync(napi_env env, napi_callback_info info) {
265285
266286		DEBUG_LOG (" Transaction::CommitSync closing txnHandle=%p\n " get ())
267287		(*txnHandle)->close ();
268- 	} else  {
269- 		napi_value error;
270- 		ROCKSDB_CREATE_ERROR_LIKE_VOID (error, status, " Transaction commit failed" 
271- 		NAPI_STATUS_THROWS (::napi_throw (env, error))
288+ 
289+ 		//  Return the timestamp as milliseconds (convert from microseconds)
290+ 		napi_value result;
291+ 		double  milliseconds = static_cast <double >(timestamp) / 1000.0 ;
292+ 		NAPI_STATUS_THROWS (::napi_create_double (env, milliseconds, &result))
293+ 		return  result;
272294	}
273295
274- 	NAPI_RETURN_UNDEFINED ()
296+ 	napi_value error;
297+ 	ROCKSDB_CREATE_ERROR_LIKE_VOID (error, status, " Transaction commit failed" 
298+ 	NAPI_STATUS_THROWS (::napi_throw (env, error))
299+ 	return  nullptr ;
275300}
276301
277302/* *
0 commit comments