Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support suspending the background compaction thread.

  • Loading branch information...
commit 8bac93ec1bcc97a098a1eaac265ea04b766ef574 1 parent 37cc581
@chirino chirino authored
View
2  leveldbjni/pom.xml
@@ -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.
View
50 leveldbjni/src/main/java/org/fusesource/leveldbjni/internal/JniDB.java
@@ -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;
+ }
+ }
}
View
18 leveldbjni/src/main/java/org/fusesource/leveldbjni/internal/Util.java
@@ -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 {
Please sign in to comment.
Something went wrong with that request. Please try again.