Permalink
Browse files

Support suspending the background compaction thread.

  • Loading branch information...
1 parent 37cc581 commit 8bac93ec1bcc97a098a1eaac265ea04b766ef574 @chirino chirino committed Sep 28, 2012
View
@@ -103,7 +103,7 @@
<configuration>
<skipAutogen>${skipAutogen}</skipAutogen>
<name>leveldbjni</name>
- <callbacks>false</callbacks>
+ <callbacks>true</callbacks>
<copyright><![CDATA[
/*******************************************************************************
* Copyright (C) 2011, FuseSource Corp. All rights reserved.
@@ -31,8 +31,11 @@
*/
package org.fusesource.leveldbjni.internal;
+import org.fusesource.hawtjni.runtime.Callback;
import org.iq80.leveldb.*;
+import java.util.concurrent.CountDownLatch;
+
/**
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
*/
@@ -175,4 +178,51 @@ public void compactRange(byte[] begin, byte[] end) throws DBException {
db.compactRange(begin, end);
}
+
+ private static class Suspension {
+ static long env = Util.EnvJNI.Default();
+
+ CountDownLatch suspended = new CountDownLatch(1);
+ CountDownLatch resumed = new CountDownLatch(1);
+ Callback callback = new Callback(this, "suspended", 1);
+
+ public Suspension() {
+ Util.EnvJNI.Schedule(env, callback.getAddress(), 0);
+ }
+
+ private long suspended(long arg) {
+ suspended.countDown();
+ try {
+ resumed.await();
+ } catch (InterruptedException e) {
+ } finally {
+ callback.dispose();
+ }
+ return 0;
+ }
+ }
+
+ int suspendCounter = 0;
+ Suspension suspension = null;
+
+ public void suspendCompactions() throws InterruptedException {
+ Suspension s = null;
+ synchronized (this) {
+ suspendCounter++;
+ if( suspendCounter==1 ) {
+ suspension = new Suspension();
+ }
+ s = suspension;
+ }
+ // Don't return until the compactions have suspended.
+ s.suspended.await();
+ }
+
+ synchronized public void resumeCompactions() {
+ suspendCounter--;
+ if( suspendCounter==0 ) {
+ suspension.resumed.countDown();
+ suspension = null;
+ }
+ }
}
@@ -51,6 +51,24 @@
*/
public class Util {
+ @JniClass(name="leveldb::Env", flags={CPP})
+ static class EnvJNI {
+
+ static {
+ NativeDB.LIBRARY.load();
+ }
+
+ @JniMethod(cast = "leveldb::Env *", accessor = "leveldb::Env::Default")
+ public static final native long Default();
+
+ @JniMethod(flags = {CPP_METHOD})
+ public static final native void Schedule(
+ long self,
+ @JniArg(cast = "void (*)(void*)") long fp,
+ @JniArg(cast = "void *") long arg);
+
+ }
+
@JniClass(flags={CPP})
static class UtilJNI {

0 comments on commit 8bac93e

Please sign in to comment.