You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2011/06/10 00:37:05 UTC

svn commit: r1134120 - in /cassandra/branches/cassandra-0.8: CHANGES.txt src/java/org/apache/cassandra/db/Table.java src/java/org/apache/cassandra/io/sstable/Descriptor.java test/unit/org/apache/cassandra/io/sstable/DescriptorTest.java

Author: jbellis
Date: Thu Jun  9 22:37:05 2011
New Revision: 1134120

URL: http://svn.apache.org/viewvc?rev=1134120&view=rev
Log:
support sstable2json against snapshot sstables
patch by Patricio Echague; reviewed by jbellis for CASSANDRA-2386

Modified:
    cassandra/branches/cassandra-0.8/CHANGES.txt
    cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/db/Table.java
    cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/io/sstable/Descriptor.java
    cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/io/sstable/DescriptorTest.java

Modified: cassandra/branches/cassandra-0.8/CHANGES.txt
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/CHANGES.txt?rev=1134120&r1=1134119&r2=1134120&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.8/CHANGES.txt (original)
+++ cassandra/branches/cassandra-0.8/CHANGES.txt Thu Jun  9 22:37:05 2011
@@ -45,6 +45,7 @@
  * fix nodetool ring use with Ec2Snitch (CASSANDRA-2733)
  * fix removing columns and subcolumns that are supressed by a row or
    supercolumn tombstone during replica resolution (CASSANDRA-2590)
+ * support sstable2json against snapshot sstables (CASSANDRA-2386)
 
 
 0.8.0-final

Modified: cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/db/Table.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/db/Table.java?rev=1134120&r1=1134119&r2=1134120&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/db/Table.java (original)
+++ cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/db/Table.java Thu Jun  9 22:37:05 2011
@@ -55,8 +55,9 @@ public class Table
 {
     public static final String SYSTEM_TABLE = "system";
 
+    public static final String SNAPSHOT_SUBDIR_NAME = "snapshots";
+
     private static final Logger logger = LoggerFactory.getLogger(Table.class);
-    private static final String SNAPSHOT_SUBDIR_NAME = "snapshots";
 
     /**
      * accesses to CFS.memtable should acquire this for thread safety.

Modified: cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/io/sstable/Descriptor.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/io/sstable/Descriptor.java?rev=1134120&r1=1134119&r2=1134120&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/io/sstable/Descriptor.java (original)
+++ cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/io/sstable/Descriptor.java Thu Jun  9 22:37:05 2011
@@ -26,6 +26,7 @@ import java.util.StringTokenizer;
 
 import com.google.common.base.Objects;
 
+import org.apache.cassandra.db.Table;
 import org.apache.cassandra.utils.Pair;
 
 /**
@@ -131,7 +132,7 @@ public class Descriptor
     public static Pair<Descriptor,String> fromFilename(File directory, String name)
     {
         // name of parent directory is keyspace name
-        String ksname = directory.getName();
+        String ksname = extractKeyspaceName(directory);
 
         // tokenize the filename
         StringTokenizer st = new StringTokenizer(name, "-");
@@ -165,6 +166,43 @@ public class Descriptor
     }
 
     /**
+     * Extracts the keyspace name out of the directory name. Snapshot directories have a slightly different
+     * path structure and need to be treated differently.
+     *
+     * Regular path:   "<ksname>/<cfname>-[tmp-][<version>-]<gen>-<component>"
+     * Snapshot path: "<ksname>/snapshots/<snapshot-name>/<cfname>-[tmp-][<version>-]<gen>-<component>"
+     *
+     * @param directory a directory containing SSTables
+     * @return the keyspace name
+     */
+    public static String extractKeyspaceName(File directory) {
+
+        if (isSnapshotInPath(directory))
+        {
+            // We need to move backwards. If this is a snapshot, first parent takes us to:
+            // <ksname>/snapshots/ and second call to parent takes us to <ksname>.
+            return directory.getParentFile().getParentFile().getName();
+        }
+        return directory.getName();
+    }
+
+    /**
+     * @return <code>TRUE</code> if this directory represents a snapshot directory. <code>FALSE</code> otherwise.
+     */
+    private static boolean isSnapshotInPath(File directory) {
+        File curDirectory = directory;
+        while (curDirectory != null)
+        {
+            if (curDirectory.getName().equals(Table.SNAPSHOT_SUBDIR_NAME))
+                return true;
+            curDirectory = curDirectory.getParentFile();
+        }
+
+        // The directory does not represent a snapshot directory.
+        return false;
+    }
+
+    /**
      * @return A clone of this descriptor with the given 'temporary' status.
      */
     public Descriptor asTemporary(boolean temporary)

Modified: cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/io/sstable/DescriptorTest.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/io/sstable/DescriptorTest.java?rev=1134120&r1=1134119&r2=1134120&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/io/sstable/DescriptorTest.java (original)
+++ cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/io/sstable/DescriptorTest.java Thu Jun  9 22:37:05 2011
@@ -25,6 +25,8 @@ import java.io.File;
 
 import org.junit.Test;
 
+import org.apache.cassandra.db.Table;
+
 public class DescriptorTest
 {
     @Test
@@ -34,4 +36,28 @@ public class DescriptorTest
         assert descriptor.version.equals(Descriptor.LEGACY_VERSION);
         assert descriptor.usesOldBloomFilter;
     }
+
+    @Test
+    public void testExtractKeyspace()
+    {
+        // Test a path representing a SNAPSHOT directory
+        String dirPath = "Keyspace10" + File.separator + Table.SNAPSHOT_SUBDIR_NAME + File.separator + System.currentTimeMillis();
+        assertKeyspace("Keyspace10", dirPath);
+
+        // Test a path representing a regular SSTables directory
+        dirPath = "Keyspace11";
+        assertKeyspace("Keyspace11", dirPath);
+    }
+
+    private void assertKeyspace(String expectedKsName, String dirPath) {
+        File dir = new File(dirPath);
+        dir.deleteOnExit();
+
+        // Create and check.
+        if (!dir.mkdirs())
+            throw new RuntimeException("Unable to create directories:" + dirPath);
+
+        String currentKsName = Descriptor.extractKeyspaceName(dir);
+        assert expectedKsName.equals(currentKsName);
+    }
 }