Repository: cassandra
Updated Branches:
  refs/heads/trunk 51908e240 -> 75409a185


Remove transient RAF usage

Patch by stefania; reviewed by jmckenzie for CASSANDRA-8952


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/75409a18
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/75409a18
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/75409a18

Branch: refs/heads/trunk
Commit: 75409a185d97c566430ab6e6cfd823ceb80ff40b
Parents: 51908e2
Author: Stefania Alborghetti <stefania.alborghe...@datastax.com>
Authored: Fri Apr 3 11:37:28 2015 -0500
Committer: Joshua McKenzie <jmcken...@apache.org>
Committed: Fri Apr 3 11:37:28 2015 -0500

----------------------------------------------------------------------
 .../org/apache/cassandra/io/util/FileUtils.java | 27 ++--------
 .../org/apache/cassandra/utils/CLibrary.java    | 25 +++------
 .../apache/cassandra/io/util/FileUtilsTest.java | 55 ++++++++++++++++++++
 .../apache/cassandra/utils/CLibraryTest.java    | 37 +++++++++++++
 4 files changed, 104 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/75409a18/src/java/org/apache/cassandra/io/util/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/util/FileUtils.java 
b/src/java/org/apache/cassandra/io/util/FileUtils.java
index ef9d23b..8007039 100644
--- a/src/java/org/apache/cassandra/io/util/FileUtils.java
+++ b/src/java/org/apache/cassandra/io/util/FileUtils.java
@@ -19,10 +19,8 @@ package org.apache.cassandra.io.util;
 
 import java.io.*;
 import java.nio.ByteBuffer;
-import java.nio.file.AtomicMoveNotSupportedException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardCopyOption;
+import java.nio.channels.FileChannel;
+import java.nio.file.*;
 import java.text.DecimalFormat;
 import java.util.Arrays;
 
@@ -185,28 +183,13 @@ public class FileUtils
     }
     public static void truncate(String path, long size)
     {
-        RandomAccessFile file;
-
-        try
-        {
-            file = new RandomAccessFile(path, "rw");
-        }
-        catch (FileNotFoundException e)
-        {
-            throw new RuntimeException(e);
-        }
-
-        try
+        try(FileChannel channel = FileChannel.open(Paths.get(path), 
StandardOpenOption.READ, StandardOpenOption.WRITE))
         {
-            file.getChannel().truncate(size);
+            channel.truncate(size);
         }
         catch (IOException e)
         {
-            throw new FSWriteError(e, path);
-        }
-        finally
-        {
-            closeQuietly(file);
+            throw new RuntimeException(e);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/75409a18/src/java/org/apache/cassandra/utils/CLibrary.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/CLibrary.java 
b/src/java/org/apache/cassandra/utils/CLibrary.java
index 25f7e5a..fed314b 100644
--- a/src/java/org/apache/cassandra/utils/CLibrary.java
+++ b/src/java/org/apache/cassandra/utils/CLibrary.java
@@ -18,9 +18,12 @@
 package org.apache.cassandra.utils;
 
 import java.io.FileDescriptor;
+import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.lang.reflect.Field;
 import java.nio.channels.FileChannel;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -316,29 +319,15 @@ public final class CLibrary
 
     public static int getfd(String path)
     {
-        RandomAccessFile file = null;
-        try
+        try(FileChannel channel = FileChannel.open(Paths.get(path), 
StandardOpenOption.READ))
         {
-            file = new RandomAccessFile(path, "r");
-            return getfd(file.getFD());
+            return getfd(channel);
         }
-        catch (Throwable t)
+        catch (IOException e)
         {
-            JVMStabilityInspector.inspectThrowable(t);
+            JVMStabilityInspector.inspectThrowable(e);
             // ignore
             return -1;
         }
-        finally
-        {
-            try
-            {
-                if (file != null)
-                    file.close();
-            }
-            catch (Throwable t)
-            {
-                // ignore
-            }
-        }
     }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/75409a18/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java 
b/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
new file mode 100644
index 0000000..7110504
--- /dev/null
+++ b/test/unit/org/apache/cassandra/io/util/FileUtilsTest.java
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cassandra.io.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class FileUtilsTest
+{
+
+    @Test
+    public void testTruncate() throws IOException
+    {
+        File file = FileUtils.createTempFile("testTruncate", "1");
+        final String expected = "The quick brown fox jumps over the lazy dog";
+
+        Files.write(file.toPath(), expected.getBytes());
+        assertTrue(file.exists());
+
+        byte[] b = Files.readAllBytes(file.toPath());
+        assertEquals(expected, new String(b, Charset.forName("UTF-8")));
+
+        FileUtils.truncate(file.getAbsolutePath(), 10);
+        b = Files.readAllBytes(file.toPath());
+        assertEquals("The quick ", new String(b, Charset.forName("UTF-8")));
+
+        FileUtils.truncate(file.getAbsolutePath(), 0);
+        b = Files.readAllBytes(file.toPath());
+        assertEquals(0, b.length);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/75409a18/test/unit/org/apache/cassandra/utils/CLibraryTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/utils/CLibraryTest.java 
b/test/unit/org/apache/cassandra/utils/CLibraryTest.java
new file mode 100644
index 0000000..be52bed
--- /dev/null
+++ b/test/unit/org/apache/cassandra/utils/CLibraryTest.java
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cassandra.utils;
+
+import java.io.File;
+
+import org.junit.Test;
+
+import org.apache.cassandra.io.util.FileUtils;
+
+public class CLibraryTest
+{
+    @Test
+    public void testSkipCache()
+    {
+        File file = FileUtils.createTempFile("testSkipCache", "1");
+
+        int fd = CLibrary.getfd(file.getPath());
+        CLibrary.trySkipCache(fd, 0, 0);
+    }
+}

Reply via email to