Author: jbellis
Date: Thu Jun 25 19:14:07 2009
New Revision: 788473

URL: http://svn.apache.org/viewvc?rev=788473&view=rev
Log:
switch to passing a single filename to open an sstable for writing.  this makes 
open-for-read and open-for-write consistent.

patch by jbellis; reviewed by Eric Evans for CASSANDRA-254

Modified:
    
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/BinaryMemtable.java
    
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
    incubator/cassandra/trunk/src/java/org/apache/cassandra/db/Memtable.java
    incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTable.java
    
incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/CompactionsTest.java
    incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableTest.java

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/BinaryMemtable.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/db/BinaryMemtable.java?rev=788473&r1=788472&r2=788473&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/BinaryMemtable.java 
(original)
+++ 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/BinaryMemtable.java 
Thu Jun 25 19:14:07 2009
@@ -28,8 +28,6 @@
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
-import org.apache.cassandra.config.DatabaseDescriptor;
-import org.apache.cassandra.utils.BloomFilter;
 import org.apache.cassandra.io.SSTable;
 import org.apache.cassandra.service.StorageService;
 
@@ -137,16 +135,14 @@
     {
         if ( columnFamilies_.size() == 0 )
             return;
-        ColumnFamilyStore cfStore = 
Table.open(table_).getColumnFamilyStore(cfName_);
-        String directory = DatabaseDescriptor.getDataFileLocation();
-        String filename = cfStore.getTempFileName();
 
         /*
          * Use the SSTable to write the contents of the TreeMap
          * to disk.
         */
+        ColumnFamilyStore cfStore = 
Table.open(table_).getColumnFamilyStore(cfName_);
         List<String> keys = new ArrayList<String>( columnFamilies_.keySet() );
-        SSTable ssTable = new SSTable(directory, filename, keys.size(), 
StorageService.getPartitioner());
+        SSTable ssTable = new SSTable(cfStore.getTempSSTablePath(), 
keys.size(), StorageService.getPartitioner());
         Collections.sort(keys);
         /* Use this BloomFilter to decide if a key exists in a SSTable */
         for ( String key : keys )

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/db/ColumnFamilyStore.java?rev=788473&r1=788472&r2=788473&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
 (original)
+++ 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/ColumnFamilyStore.java
 Thu Jun 25 19:14:07 2009
@@ -358,13 +358,22 @@
     }
 
     /*
-     * Return a temporary file name.
+     * @return a temporary file name for an sstable.
+     * When the sstable object is closed, it will be renamed to a non-temporary
+     * format, so incomplete sstables can be recognized and removed on startup.
      */
-    String getTempFileName()
+    String getTempSSTablePath()
+    {
+        // increment twice so that we do not generate consecutive numbers
+        String fname = getTempSSTableFileName();
+        return new File(DatabaseDescriptor.getDataFileLocation(), 
fname).getAbsolutePath();
+    }
+
+    String getTempSSTableFileName()
     {
-        // Psuedo increment so that we do not generate consecutive numbers
         fileIndexGenerator_.incrementAndGet();
-        return table_ + "-" + columnFamily_ + "-" + SSTable.temporaryFile_ + 
"-" + fileIndexGenerator_.incrementAndGet();
+        return String.format("%s-%s-%s-%s-Data.db",
+                             table_, columnFamily_, SSTable.temporaryFile_, 
fileIndexGenerator_.incrementAndGet());
     }
 
     /*
@@ -388,7 +397,8 @@
 
         index = lowestIndex + 1;
 
-        return table_ + "-" + columnFamily_ + "-" + SSTable.temporaryFile_ + 
"-" + index;
+        return String.format("%s-%s-%s-%s-Data.db",
+                             table_, columnFamily_, SSTable.temporaryFile_, 
index);
     }
 
     void switchMemtable()
@@ -1036,7 +1046,7 @@
             return result;
         }
 
-        mergedFileName = getTempFileName();
+        mergedFileName = getTempSSTableFileName();
         SSTable ssTableRange = null;
         String lastkey = null;
         List<FileStruct> lfs = new ArrayList<FileStruct>();
@@ -1115,7 +1125,8 @@
                             rangeFileLocation = rangeFileLocation + 
System.getProperty("file.separator") + "bootstrap";
                         }
                         FileUtils.createDirectory(rangeFileLocation);
-                        ssTableRange = new SSTable(rangeFileLocation, 
mergedFileName, expectedBloomFilterSize, StorageService.getPartitioner());
+                        String fname = new File(rangeFileLocation, 
mergedFileName).getAbsolutePath();
+                        ssTableRange = new SSTable(fname, 
expectedBloomFilterSize, StorageService.getPartitioner());
                     }
                     try
                     {
@@ -1304,7 +1315,8 @@
 
                 if (ssTable == null)
                 {
-                    ssTable = new SSTable(compactionFileLocation, 
mergedFileName, expectedBloomFilterSize, StorageService.getPartitioner());
+                    String fname = new File(compactionFileLocation, 
mergedFileName).getAbsolutePath();
+                    ssTable = new SSTable(fname, expectedBloomFilterSize, 
StorageService.getPartitioner());
                 }
                 ssTable.append(lastkey, bufOut);
                 totalkeysWritten++;

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/Memtable.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/db/Memtable.java?rev=788473&r1=788472&r2=788473&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/db/Memtable.java 
(original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/db/Memtable.java 
Thu Jun 25 19:14:07 2009
@@ -31,7 +31,6 @@
 import org.apache.cassandra.io.DataOutputBuffer;
 import org.apache.cassandra.io.SSTable;
 import org.apache.cassandra.service.StorageService;
-import org.apache.cassandra.utils.BloomFilter;
 import org.apache.cassandra.utils.DestructivePQIterator;
 import org.apache.log4j.Logger;
 
@@ -251,9 +250,7 @@
         logger_.info("Flushing " + this);
         ColumnFamilyStore cfStore = 
Table.open(table_).getColumnFamilyStore(cfName_);
 
-        String directory = DatabaseDescriptor.getDataFileLocation();
-        String filename = cfStore.getTempFileName();
-        SSTable ssTable = new SSTable(directory, filename, 
columnFamilies_.size(), StorageService.getPartitioner());
+        SSTable ssTable = new SSTable(cfStore.getTempSSTablePath(), 
columnFamilies_.size(), StorageService.getPartitioner());
 
         // sort keys in the order they would be in when decorated
         final IPartitioner partitioner = StorageService.getPartitioner();

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTable.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTable.java?rev=788473&r1=788472&r2=788473&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTable.java 
(original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/io/SSTable.java Thu 
Jun 25 19:14:07 2009
@@ -132,12 +132,14 @@
 
     private SSTable(String filename, IPartitioner partitioner)
     {
+        assert filename.endsWith("-Data.db");
         dataFile_ = filename;
         partitioner_ = partitioner;
     }
 
-    private SSTable(String filename, int keyCount, IPartitioner partitioner) 
throws IOException
+    public SSTable(String filename, int keyCount, IPartitioner partitioner) 
throws IOException
     {
+        assert filename.endsWith("-Data.db");
         dataFile_ = filename;
         partitioner_ = partitioner;
         dataWriter_ = SequenceFile.bufferedWriter(dataFile_, 4 * 1024 * 1024);
@@ -145,15 +147,6 @@
         bf = new BloomFilter(keyCount, 15);
     }
 
-    /**
-     * This ctor is used for writing data into the SSTable. Use this
-     * version for non DB writes to the SSTable.
-     */
-    public SSTable(String directory, String filename, int keyCount, 
IPartitioner partitioner) throws IOException
-    {
-        this(directory + System.getProperty("file.separator") + filename + 
"-Data.db", keyCount, partitioner);
-    }
-
     static String parseTableName(String filename)
     {
         String[] parts = new File(filename).getName().split("-"); // table, 
cf, index, [filetype]

Modified: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/CompactionsTest.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/CompactionsTest.java?rev=788473&r1=788472&r2=788473&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/CompactionsTest.java
 (original)
+++ 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/db/CompactionsTest.java
 Thu Jun 25 19:14:07 2009
@@ -28,9 +28,10 @@
 import org.junit.Test;
 
 import org.apache.cassandra.io.SSTable;
+import org.apache.cassandra.CleanupHelper;
 import static junit.framework.Assert.assertEquals;
 
-public class CompactionsTest
+public class CompactionsTest extends CleanupHelper
 {
     @Test
     public void testCompactions() throws IOException, ExecutionException, 
InterruptedException

Modified: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableTest.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableTest.java?rev=788473&r1=788472&r2=788473&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableTest.java 
(original)
+++ 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/io/SSTableTest.java 
Thu Jun 25 19:14:07 2009
@@ -32,10 +32,10 @@
 {
     @Test
     public void testSingleWrite() throws IOException {
-        File f = File.createTempFile("sstable", "-" + SSTable.temporaryFile_);
+        File f = tempSSTableFileName();
 
         // write test data
-        SSTable ssTable = new SSTable(f.getParent(), f.getName(), 1, new 
OrderPreservingPartitioner());
+        SSTable ssTable = new SSTable(f.getAbsolutePath(), 1, new 
OrderPreservingPartitioner());
         Random random = new Random();
         byte[] bytes = new byte[1024];
         random.nextBytes(bytes);
@@ -50,6 +50,11 @@
         verifySingle(ssTable, bytes, key);
     }
 
+    private File tempSSTableFileName() throws IOException
+    {
+        return File.createTempFile("sstable", "-" + SSTable.temporaryFile_ + 
"-Data.db");
+    }
+
     private void verifySingle(SSTable sstable, byte[] bytes, String key) 
throws IOException
     {
         FileStruct fs = sstable.getFileStruct();
@@ -62,7 +67,7 @@
 
     @Test
     public void testManyWrites() throws IOException {
-        File f = File.createTempFile("sstable", "-" + SSTable.temporaryFile_);
+        File f = tempSSTableFileName();
 
         TreeMap<String, byte[]> map = new TreeMap<String,byte[]>();
         for ( int i = 100; i < 1000; ++i )
@@ -71,7 +76,7 @@
         }
 
         // write
-        SSTable ssTable = new SSTable(f.getParent(), f.getName(), 1000, new 
OrderPreservingPartitioner());
+        SSTable ssTable = new SSTable(f.getAbsolutePath(), 1000, new 
OrderPreservingPartitioner());
         for (String key: map.navigableKeySet())
         {
             ssTable.append(key, map.get(key));


Reply via email to