Skip to content

Commit

Permalink
set comparator is not possible on android - related to hawtjni-callba…
Browse files Browse the repository at this point in the history
…ck.c
  • Loading branch information
krisskross committed Mar 30, 2015
1 parent 1ef7c82 commit 3e58724
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 122 deletions.
8 changes: 8 additions & 0 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>
* Does not work on Android at the moment (related to hawtjni-callback).
* </p>
*
* @param tx Transaction handle.
* @param comparator a byte array comparator
*/
Expand All @@ -613,6 +617,10 @@ public void setComparator(Transaction tx, Comparator<byte[]> comparator) {
* Keep in mind that the comparator is called a huge number of times in any db operation which
* will degrade performance substantially.
*
* <p>
* Does not work on Android at the moment (related to hawtjni-callback).
* </p>
*
* @param tx Transaction handle.
* @param comparator a zero copy comparator
*/
Expand Down
2 changes: 1 addition & 1 deletion lmdbjni/src/main/java/org/fusesource/lmdbjni/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
178 changes: 178 additions & 0 deletions lmdbjni/src/test/java/org/fusesource/lmdbjni/ComparatorTest.java
Original file line number Diff line number Diff line change
@@ -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<byte[]>() {
@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<byte[]>() {
@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<DirectBuffer>() {
@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<DirectBuffer>() {
@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;
}
}
}
}
}
121 changes: 0 additions & 121 deletions lmdbjni/src/test/java/org/fusesource/lmdbjni/DatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte[]>() {
@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<byte[]>() {
@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<DirectBuffer>() {
@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<DirectBuffer>() {
@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;
}
}
}
}
}
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@
<modules>
<module>lmdbjni-android</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<!-- hawtjni-callback.c bind() does not work on android -->
<exclude>**/ComparatorTest*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
Expand Down

0 comments on commit 3e58724

Please sign in to comment.