forked from chirino/lmdbjni
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set comparator is not possible on android - related to hawtjni-callba…
…ck.c
- Loading branch information
1 parent
1ef7c82
commit 3e58724
Showing
5 changed files
with
201 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
lmdbjni/src/test/java/org/fusesource/lmdbjni/ComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters