nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: bmatsuo/lmdb-go
Pull request #47 (2016-02-12 10:10:11)

lmdb: method Txn.ID() exposes mdb_txn_id().


Patch:
diff --git a/lmdb/txn.go b/lmdb/txn.go
index 75ddf6c..84eb29b 100644
--- a/lmdb/txn.go
+++ b/lmdb/txn.go
@@ -71,6 +71,15 @@ func beginTxn(env *Env, parent *Txn, flags uint) (*Txn, error) {
 	return txn, nil
 }
 
+// ID returns the identifier for txn.  A view transaction identifier
+// corresponds to the Env snapshot being viewed and may be shared with other
+// view transactions.
+//
+// See mdb_txn_id.
+func (txn *Txn) ID() uintptr {
+	return uintptr(C.mdb_txn_id(txn._txn))
+}
+
 // Commit persists all transaction operations to the database and clears the
 // finalizer on txn.  A Txn cannot be used again after Commit is called.
 //


Test patch:
diff --git a/lmdb/txn_test.go b/lmdb/txn_test.go
index 74032ae..a907c95 100644
--- a/lmdb/txn_test.go
+++ b/lmdb/txn_test.go
@@ -11,6 +11,45 @@ import (
 	"time"
 )
 
+func TestTxn_ID(t *testing.T) {
+	env := setup(t)
+	defer clean(env, t)
+
+	var id1, id2, id3 uintptr
+	var txnInvalid *Txn
+	err := env.Update(func(txn *Txn) (err error) {
+		dbi, err := txn.OpenRoot(0)
+		if err != nil {
+			return err
+		}
+		id1 = txn.ID()
+		return txn.Put(dbi, []byte("key"), []byte("val"), 0)
+	})
+	if err != nil {
+		t.Errorf("%v", err)
+		return
+	}
+	err = env.View(func(txn *Txn) (err error) {
+		id2 = txn.ID()
+		txnInvalid = txn
+		return nil
+	})
+	if err != nil {
+		t.Errorf("%v", err)
+		return
+	}
+	id3 = txnInvalid.ID()
+	t.Logf("txn id: %v", id1)
+	t.Logf("ro txn id: %v", id2)
+	t.Logf("bad txn id: %v", id3)
+	if id1 != id2 {
+		t.Errorf("unexpected readonly id: %v (!= %v)", id2, id1)
+	}
+	if id2 == id3 {
+		t.Errorf("unexpected invalid id: %v (!= %v)", id3, 0)
+	}
+}
+
 func TestTxn_errLogf(t *testing.T) {
 	env := setup(t)
 	defer clean(env, t)