From 3e587242e3cade9ce504e3d867d081158b06df71 Mon Sep 17 00:00:00 2001 From: Kristoffer Sjogren Date: Mon, 30 Mar 2015 22:30:21 +0200 Subject: [PATCH] set comparator is not possible on android - related to hawtjni-callback.c --- .../java/org/fusesource/lmdbjni/Database.java | 8 + .../java/org/fusesource/lmdbjni/Util.java | 2 +- .../fusesource/lmdbjni/ComparatorTest.java | 178 ++++++++++++++++++ .../org/fusesource/lmdbjni/DatabaseTest.java | 121 ------------ pom.xml | 14 ++ 5 files changed, 201 insertions(+), 122 deletions(-) create mode 100644 lmdbjni/src/test/java/org/fusesource/lmdbjni/ComparatorTest.java diff --git a/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java b/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java index e8fb599..7dbe612 100644 --- a/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java +++ b/lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java @@ -592,6 +592,10 @@ public Cursor openCursor(Transaction tx) { * Keep in mind that the comparator is called a huge number of times in any db operation which * will degrade performance substantially. * + *

+ * Does not work on Android at the moment (related to hawtjni-callback). + *

+ * * @param tx Transaction handle. * @param comparator a byte array comparator */ @@ -613,6 +617,10 @@ public void setComparator(Transaction tx, Comparator comparator) { * Keep in mind that the comparator is called a huge number of times in any db operation which * will degrade performance substantially. * + *

+ * Does not work on Android at the moment (related to hawtjni-callback). + *

+ * * @param tx Transaction handle. * @param comparator a zero copy comparator */ diff --git a/lmdbjni/src/main/java/org/fusesource/lmdbjni/Util.java b/lmdbjni/src/main/java/org/fusesource/lmdbjni/Util.java index 334a32a..ac58e0c 100644 --- a/lmdbjni/src/main/java/org/fusesource/lmdbjni/Util.java +++ b/lmdbjni/src/main/java/org/fusesource/lmdbjni/Util.java @@ -51,7 +51,7 @@ public static void checkArgNotNull(Object value, String name) { } } - private static boolean isAndroid() { + static boolean isAndroid() { try { Class.forName("android.os.Process"); return true; diff --git a/lmdbjni/src/test/java/org/fusesource/lmdbjni/ComparatorTest.java b/lmdbjni/src/test/java/org/fusesource/lmdbjni/ComparatorTest.java new file mode 100644 index 0000000..e50f2a3 --- /dev/null +++ b/lmdbjni/src/test/java/org/fusesource/lmdbjni/ComparatorTest.java @@ -0,0 +1,178 @@ +package org.fusesource.lmdbjni; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Comparator; + +import static org.fusesource.lmdbjni.Bytes.fromLong; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.*; + +public class ComparatorTest { + static { + Setup.setLmdbLibraryPath(); + } + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + Env env; + Database db; + byte[] data = new byte[] {1,2,3}; + + @Before + public void before() throws IOException { + String path = tmp.newFolder().getCanonicalPath(); + env = new Env(); + env.setMapSize(16 * 4096); + env.open(path); + db = env.openDatabase(); + } + + @After + public void after() { + db.close(); + env.close(); + } + + @Test + public void testSetComparatorAsc() { + if (Util.isAndroid()) { + return; + } + + Transaction writeTransaction = env.createWriteTransaction(); + db.setComparator(writeTransaction, new Comparator() { + @Override + public int compare(byte[] key1, byte[] key2) { + return (int) (Bytes.getLong(key1) - Bytes.getLong(key2)); + } + }); + writeTransaction.commit(); + writeTransaction = env.createWriteTransaction(); + + for (int i = 0; i < 1000; i++) { + db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); + } + writeTransaction.commit(); + try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ + long prev = -1; + while(it.hasNext()) { + if (prev == -1) { + prev = Bytes.getLong(it.next().getKey()); + } else { + long now = Bytes.getLong(it.next().getKey()); + assertThat((prev+1), is(now)); + prev = now; + } + } + } + } + + @Test + public void testSetComparatorDesc() { + if (Util.isAndroid()) { + return; + } + Transaction writeTransaction = env.createWriteTransaction(); + db.setComparator(writeTransaction, new Comparator() { + @Override + public int compare(byte[] key1, byte[] key2) { + return (int) (Bytes.getLong(key2) - Bytes.getLong(key1)); + } + }); + writeTransaction.commit(); + writeTransaction = env.createWriteTransaction(); + + for (int i = 0; i < 1000; i++) { + db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); + } + writeTransaction.commit(); + try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ + long prev = -1; + while(it.hasNext()) { + if (prev == -1) { + prev = Bytes.getLong(it.next().getKey()); + } else { + long now = Bytes.getLong(it.next().getKey()); + assertThat((prev-1), is(now)); + prev = now; + } + } + } + } + + + @Test + public void testSetDirectComparatorAsc() { + if (Util.isAndroid()) { + return; + } + Transaction writeTransaction = env.createWriteTransaction(); + db.setDirectComparator(writeTransaction, new Comparator() { + @Override + public int compare(DirectBuffer key1, DirectBuffer key2) { + return (int) (key1.getLong(0) - key2.getLong(0)); + } + }); + writeTransaction.commit(); + writeTransaction = env.createWriteTransaction(); + + for (int i = 0; i < 1000; i++) { + db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); + } + writeTransaction.commit(); + try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ + long prev = -1; + while(it.hasNext()) { + if (prev == -1) { + prev = Bytes.getLong(it.next().getKey()); + } else { + long now = Bytes.getLong(it.next().getKey()); + assertThat((prev+1), is(now)); + prev = now; + } + } + } + } + + @Test + public void testSetDirectComparatorDesc() { + if (Util.isAndroid()) { + return; + } + Transaction writeTransaction = env.createWriteTransaction(); + db.setDirectComparator(writeTransaction, new Comparator() { + @Override + public int compare(DirectBuffer key1, DirectBuffer key2) { + return (int) (key2.getLong(0) - key1.getLong(0)); + } + }); + writeTransaction.commit(); + writeTransaction = env.createWriteTransaction(); + + for (int i = 0; i < 1000; i++) { + db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); + } + writeTransaction.commit(); + try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ + long prev = -1; + while(it.hasNext()) { + if (prev == -1) { + prev = Bytes.getLong(it.next().getKey()); + } else { + long now = Bytes.getLong(it.next().getKey()); + assertThat((prev-1), is(now)); + prev = now; + } + } + } + } +} diff --git a/lmdbjni/src/test/java/org/fusesource/lmdbjni/DatabaseTest.java b/lmdbjni/src/test/java/org/fusesource/lmdbjni/DatabaseTest.java index 5af19e8..1703643 100644 --- a/lmdbjni/src/test/java/org/fusesource/lmdbjni/DatabaseTest.java +++ b/lmdbjni/src/test/java/org/fusesource/lmdbjni/DatabaseTest.java @@ -96,125 +96,4 @@ public void testDeleteBuffer() { db.delete(key); assertNull(db.get(new byte[]{1})); } - - @Test - public void testSetComparatorAsc() { - Transaction writeTransaction = env.createWriteTransaction(); - db.setComparator(writeTransaction, new Comparator() { - @Override - public int compare(byte[] key1, byte[] key2) { - return (int) (Bytes.getLong(key1) - Bytes.getLong(key2)); - } - }); - writeTransaction.commit(); - writeTransaction = env.createWriteTransaction(); - - for (int i = 0; i < 1000; i++) { - db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); - } - writeTransaction.commit(); - try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ - long prev = -1; - while(it.hasNext()) { - if (prev == -1) { - prev = Bytes.getLong(it.next().getKey()); - } else { - long now = Bytes.getLong(it.next().getKey()); - assertThat((prev+1), is(now)); - prev = now; - } - } - } - } - - @Test - public void testSetComparatorDesc() { - Transaction writeTransaction = env.createWriteTransaction(); - db.setComparator(writeTransaction, new Comparator() { - @Override - public int compare(byte[] key1, byte[] key2) { - return (int) (Bytes.getLong(key2) - Bytes.getLong(key1)); - } - }); - writeTransaction.commit(); - writeTransaction = env.createWriteTransaction(); - - for (int i = 0; i < 1000; i++) { - db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); - } - writeTransaction.commit(); - try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ - long prev = -1; - while(it.hasNext()) { - if (prev == -1) { - prev = Bytes.getLong(it.next().getKey()); - } else { - long now = Bytes.getLong(it.next().getKey()); - assertThat((prev-1), is(now)); - prev = now; - } - } - } - } - - - @Test - public void testSetDirectComparatorAsc() { - Transaction writeTransaction = env.createWriteTransaction(); - db.setDirectComparator(writeTransaction, new Comparator() { - @Override - public int compare(DirectBuffer key1, DirectBuffer key2) { - return (int) (key1.getLong(0) - key2.getLong(0)); - } - }); - writeTransaction.commit(); - writeTransaction = env.createWriteTransaction(); - - for (int i = 0; i < 1000; i++) { - db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); - } - writeTransaction.commit(); - try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ - long prev = -1; - while(it.hasNext()) { - if (prev == -1) { - prev = Bytes.getLong(it.next().getKey()); - } else { - long now = Bytes.getLong(it.next().getKey()); - assertThat((prev+1), is(now)); - prev = now; - } - } - } - } - - @Test - public void testSetDirectComparatorDesc() { - Transaction writeTransaction = env.createWriteTransaction(); - db.setDirectComparator(writeTransaction, new Comparator() { - @Override - public int compare(DirectBuffer key1, DirectBuffer key2) { - return (int) (key2.getLong(0) - key1.getLong(0)); - } - }); - writeTransaction.commit(); - writeTransaction = env.createWriteTransaction(); - - for (int i = 0; i < 1000; i++) { - db.put(writeTransaction, Bytes.fromLong(i), Bytes.fromLong(i)); - } - writeTransaction.commit(); - try (EntryIterator it = db.iterate(env.createReadTransaction()) ){ - long prev = -1; - while(it.hasNext()) { - if (prev == -1) { - prev = Bytes.getLong(it.next().getKey()); - } else { - long now = Bytes.getLong(it.next().getKey()); - assertThat((prev-1), is(now)); - prev = now; - } - } - } - } } diff --git a/pom.xml b/pom.xml index 6178bd3..1361a30 100755 --- a/pom.xml +++ b/pom.xml @@ -265,6 +265,20 @@ lmdbjni-android + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + **/ComparatorTest* + + + + +