Skip to content

Commit

Permalink
add Transaction.getId
Browse files Browse the repository at this point in the history
  • Loading branch information
krisskross committed Aug 18, 2015
1 parent 0034b71 commit 3ca2943
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,14 @@ public static final native int mdb_txn_begin(
@JniArg(cast = "unsigned int") long flags,
@JniArg(cast = "MDB_txn **", flags = {NO_IN}) long[] txn);


/**
* <a href="http://symas.com/mdb/doc/group__mdb.html#">details</a>
*/
@JniMethod
public static final native int mdb_txn_id(
@JniArg(cast = "MDB_txn *") long txn);

/**
* <a href="http://symas.com/mdb/doc/group__mdb.html#">details</a>
*/
Expand Down
15 changes: 15 additions & 0 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public class Transaction extends NativeObject implements Closeable {
this.readOnly = readOnly;
}

/**
* <p>
* Return the transaction's ID.
* </p>
*
* This returns the identifier associated with this transaction. For a
* read-only transaction, this corresponds to the snapshot being read;
* concurrent readers will frequently have the same transaction ID.
*
* @return A transaction ID, valid if input is an active transaction.
*/
public long getId() {
return mdb_txn_id(pointer());
}

/**
* <p>
* Renew a read-only transaction.
Expand Down
24 changes: 20 additions & 4 deletions lmdbjni/src/test/java/org/fusesource/lmdbjni/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class TransactionTest {
static {
Expand All @@ -22,7 +23,7 @@ public class TransactionTest {

Env env;
Database db;
byte[] data = new byte[] {1,2,3};
byte[] data = new byte[]{1, 2, 3};

@Before
public void before() throws IOException {
Expand All @@ -37,6 +38,21 @@ public void after() {
env.close();
}

@Test
public void testGetId() throws Exception {
final AtomicLong tx1 = new AtomicLong();
final AtomicLong tx2 = new AtomicLong();
try (Transaction tx = env.createReadTransaction()) {
tx1.set(tx.getId());
}
db.put(new byte[] {1}, new byte[]{2});
try (Transaction tx = env.createReadTransaction()) {
tx2.set(tx.getId());
}
// should not see the same snapshot
assertThat(tx1.get(), is(not(tx2.get())));
}

@Test
public void testCommit() {
try (Transaction tx = env.createWriteTransaction()) {
Expand Down

0 comments on commit 3ca2943

Please sign in to comment.