Skip to content

Commit

Permalink
Dispose comparator on database close, #54
Browse files Browse the repository at this point in the history
  • Loading branch information
krisskross committed Mar 21, 2016
1 parent 6ad885f commit 9a29e34
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
public class Database extends NativeObject implements AutoCloseable {

private final Env env;
private Callback comparatorCallback;
private Callback directComparatorCallback;

Database(Env env, long self) {
super(self);
Expand All @@ -60,6 +62,14 @@ public class Database extends NativeObject implements AutoCloseable {
*/
@Override
public void close() {
if (comparatorCallback != null) {
comparatorCallback.dispose();
comparatorCallback = null;
}
if (directComparatorCallback != null) {
directComparatorCallback.dispose();
directComparatorCallback = null;
}
if (self != 0) {
mdb_dbi_close(env.pointer(), self);
self = 0;
Expand Down Expand Up @@ -242,6 +252,7 @@ public EntryIterator seekBackward(Transaction tx, byte[] key) {
public EntryIterator iterate(Transaction tx) {
return iterate(tx, null, IteratorType.FORWARD);
}

/**
* <p>
* Creates a backward sequential iterator from the last key.
Expand Down Expand Up @@ -348,7 +359,7 @@ public int put(Transaction tx, DirectBuffer key, DirectBuffer value, int flags)

/**
* Just reserve space for data in the database, don't copy it.
*
*
* @return a pointer to the reserved space.
*/
public DirectBuffer reserve(Transaction tx, DirectBuffer key, int size) {
Expand Down Expand Up @@ -638,8 +649,11 @@ public Cursor openCursor(Transaction tx) {
* @param comparator a byte array comparator
*/
public void setComparator(Transaction tx, Comparator<byte[]> comparator) {
Callback callback = new Callback(new ByteArrayComparator(comparator), "compare", 2);
JNI.mdb_set_compare(tx.pointer(), this.pointer(), callback.getAddress());
if (comparatorCallback != null) {
comparatorCallback.dispose();
}
comparatorCallback = new Callback(new ByteArrayComparator(comparator), "compare", 2);
JNI.mdb_set_compare(tx.pointer(), this.pointer(), comparatorCallback.getAddress());
}

/**
Expand All @@ -663,8 +677,11 @@ public void setComparator(Transaction tx, Comparator<byte[]> comparator) {
* @param comparator a zero copy comparator
*/
public void setDirectComparator(Transaction tx, Comparator<DirectBuffer> comparator) {
Callback callback = new Callback(new DirectBufferComparator(comparator), "compare", 2);
JNI.mdb_set_compare(tx.pointer(), this.pointer(), callback.getAddress());
if (directComparatorCallback != null) {
directComparatorCallback.dispose();
}
directComparatorCallback = new Callback(new DirectBufferComparator(comparator), "compare", 2);
JNI.mdb_set_compare(tx.pointer(), this.pointer(), directComparatorCallback.getAddress());
}

private static final class ByteArrayComparator {
Expand Down

0 comments on commit 9a29e34

Please sign in to comment.