Skip to content

Instantly share code, notes, and snippets.

@chirino
Created April 16, 2013 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chirino/5400336 to your computer and use it in GitHub Desktop.
Save chirino/5400336 to your computer and use it in GitHub Desktop.
A test case showing that leveldb compaction don't work so good /w sequencial insert and delete. Related to a mailing list post[1] and a reported bug [2]. [1]: https://groups.google.com/d/msg/leveldb/yL6h1mAOc20/vLU64RylIdMJ [2]: https://code.google.com/p/leveldb/issues/detail?id=77
import org.fusesource.leveldbjni.JniDBFactory;
import org.iq80.leveldb.*;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;
public class DBCompactionTest {
public static void main(String args[]) throws IOException, DBException {
Options options = new Options().createIfMissing(true);
WriteOptions wo = new WriteOptions().sync(false);
JniDBFactory.factory.destroy(new File("test-data"), options);
DB db = JniDBFactory.factory.open(new File("test-data"), options);
Random rand = new Random();
ByteBuffer key = ByteBuffer.allocate(8);
byte[] value = new byte[1024];
// Lets try to generate about 40 MB of live data.
long liveKeys = (1024*1024*40)/(value.length+key.capacity()+12);
long totalKeys = liveKeys * 50;
for( long i=0; i < totalKeys; i++) {
if( i%((totalKeys/100)*10) == 0 ) {
long[] approximateSizes = db.getApproximateSizes(new Range[]{new Range(new byte[]{0},new byte[]{1})});
System.out.println(String.format("%d%% done. Using: %,2f MB", (i / (totalKeys/100)), (approximateSizes[0]/(1024.0*1024))));
// System.out.println(db.getProperty("leveldb.stats"));
}
key.clear();
rand.nextBytes(value);
db.put(key.putLong(i).array(), value, wo);
if( i >= liveKeys ) {
long deleteKey = i - liveKeys;
if( deleteKey == 0 ) {
long[] approximateSizes = db.getApproximateSizes(new Range[]{new Range(new byte[]{0},new byte[]{1})});
System.out.println(String.format("Deletes starting. %d%% done. Using: %,2f MB", (i / (totalKeys/100)), (approximateSizes[0]/(1024.0*1024))));
}
key.clear();
db.delete(key.putLong(deleteKey).array(), wo);
}
}
System.out.println("Deleting remaining live records...");
// lets delete the remaining live keys.
for( long i=totalKeys-liveKeys; i < totalKeys; i++) {
key.clear();
db.delete(key.putLong(i).array(), wo);
}
System.out.println("Counting records...");
// Lets verify none of the keys are left.
int entries =0 ;
DBIterator iterator = db.iterator();
iterator.seekToFirst();
while( iterator.hasNext() ) {
iterator.next();
entries++;
}
System.out.println("Database contained "+entries+" entries.");
long[] approximateSizes = db.getApproximateSizes(new Range[]{new Range(new byte[]{0},new byte[]{1})});
System.out.println(String.format("Using: %,2f MB", (approximateSizes[0]/(1024.0*1024))));
db.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment