You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2012/08/24 00:39:58 UTC

svn commit: r1376748 - /activemq/activemq-apollo/trunk/apollo-leveldb/src/main/scala/org/apache/activemq/apollo/broker/store/leveldb/LevelDBClient.scala

Author: chirino
Date: Thu Aug 23 22:39:58 2012
New Revision: 1376748

URL: http://svn.apache.org/viewvc?rev=1376748&view=rev
Log:
Store the current link strategy in a function var instead of doing a switch.

Modified:
    activemq/activemq-apollo/trunk/apollo-leveldb/src/main/scala/org/apache/activemq/apollo/broker/store/leveldb/LevelDBClient.scala

Modified: activemq/activemq-apollo/trunk/apollo-leveldb/src/main/scala/org/apache/activemq/apollo/broker/store/leveldb/LevelDBClient.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-leveldb/src/main/scala/org/apache/activemq/apollo/broker/store/leveldb/LevelDBClient.scala?rev=1376748&r1=1376747&r2=1376748&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-leveldb/src/main/scala/org/apache/activemq/apollo/broker/store/leveldb/LevelDBClient.scala (original)
+++ activemq/activemq-apollo/trunk/apollo-leveldb/src/main/scala/org/apache/activemq/apollo/broker/store/leveldb/LevelDBClient.scala Thu Aug 23 22:39:58 2012
@@ -95,72 +95,70 @@ object LevelDBClient extends Log {
   }
 
   val on_windows = System.getProperty("os.name").toLowerCase().startsWith("windows")
-  var link_strategy = 0
+  var link = (source:File, target:File) => jniLinkStrategy(source, target)
 
-  def link(source: File, target: File): Unit = {
-    link_strategy match {
-      case 0 =>
-        // We first try to link via a native system call. Fails if
-        // we cannot load the JNI module.
-        try {
-          Util.link(source, target)
-        } catch {
-          case e: IOException => throw e
-          case e: Throwable =>
-            // Fallback.. to a slower impl..
-            debug("Native link system call not available")
-            link_strategy = 2
-            link(source, target)
-        }
+  def jniLinkStrategy(source: File, target: File) {
+    // We first try to link via a native system call.
+    try {
+      Util.link(source, target)
+    } catch {
+      case e: IOException => throw e
+      case e: Throwable =>
+        // Fallback.. to a slower impl..
+        debug("Native link system call not available")
+        link = (source:File, target:File) => jnaLinkStrategy(source, target)
+        link(source, target)
+    }
+  }
 
-      // TODO: consider implementing a case which does the native system call using JNA
-      case 2 =>
-        // Next try JNA (might not be in classpath)
-        try {
-          IOHelper.hardlink(source, target)
-        } catch {
-          case e: IOException => throw e
-          case e: Throwable =>
-            // Fallback.. to a slower impl..
-            debug("JNA based hard link system call not available")
-            link_strategy = 5
-            link(source, target)
+  def jnaLinkStrategy(source: File, target: File) {
+    // Next try JNA (might not be in classpath)
+    try {
+      IOHelper.hardlink(source, target)
+    } catch {
+      case e: IOException => throw e
+      case e: Throwable =>
+        // Fallback.. to a slower impl..
+        debug("JNA based hard link system call not available")
+        link = if( on_windows ) {
+          (source:File, target:File) => windowsCliLinkStrategy(source, target)
+        } else {
+          (source:File, target:File) => unixCliLinkStrategy(source, target)
         }
+        link(source, target)
+    }
+  }
 
-      case 5 =>
-        // Next we try to do the link by executing an
-        // operating system shell command
-        try {
-          if (on_windows) {
-            system("fsutil", "hardlink", "create", target.getCanonicalPath, source.getCanonicalPath) match {
-              case (0, _, _) => // Success
-              case (_, out, err) =>
-                // TODO: we might want to look at the out/err to see why it failed
-                // to avoid falling back to the slower strategy.
-                debug("fsutil OS command not available either")
-                link_strategy = 10
-                link(source, target)
-            }
-          } else {
-            system("ln", source.getCanonicalPath, target.getCanonicalPath) match {
-              case (0, _, _) => // Success
-              case (_, out, err) => None
-              // TODO: we might want to look at the out/err to see why it failed
-              // to avoid falling back to the slower strategy.
-              debug("ln OS command not available either")
-              link_strategy = 10
-              link(source, target)
-            }
-          }
-        } catch {
-          case e: Throwable =>
-        }
-      case _ =>
-        // this final strategy is slow but sure to work.
-        source.copy_to(target)
+  def unixCliLinkStrategy(source: File, target: File) {
+    system("ln", source.getCanonicalPath, target.getCanonicalPath) match {
+      case (0, _, _) => // Success
+      case (_, out, err) => None
+      // TODO: we might want to look at the out/err to see why it failed
+      // to avoid falling back to the slower strategy.
+      debug("ln OS command not available either")
+      link = (source:File, target:File) => copyLinkStrategy(source, target)
+      link(source, target)
+    }
+  }
+
+  def windowsCliLinkStrategy(source: File, target: File) {
+    // Next try JNA (might not be in classpath)
+    system("fsutil", "hardlink", "create", target.getCanonicalPath, source.getCanonicalPath) match {
+      case (0, _, _) => // Success
+      case (_, out, err) =>
+        // TODO: we might want to look at the out/err to see why it failed
+        // to avoid falling back to the slower strategy.
+        debug("fsutil OS command not available either")
+        link = (source:File, target:File) => copyLinkStrategy(source, target)
+        link(source, target)
     }
   }
 
+  def copyLinkStrategy(source: File, target: File) {
+    // this final strategy is slow but sure to work.
+    source.copy_to(target)
+  }
+
 }
 
 /**