cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gdusba...@apache.org
Subject svn commit: r1053392 - in /cassandra/trunk/test: long/org/apache/cassandra/db/ unit/org/apache/cassandra/io/sstable/ unit/org/apache/cassandra/streaming/
Date Tue, 28 Dec 2010 17:15:26 GMT
Author: gdusbabek
Date: Tue Dec 28 17:15:26 2010
New Revision: 1053392

URL: http://svn.apache.org/viewvc?rev=1053392&view=rev
Log:
refactor SSTableUtils to add chainable configuration. patch by stuhood, reviewed by gdusbabek.
CASSANDRA-1822

Modified:
    cassandra/trunk/test/long/org/apache/cassandra/db/LongCompactionSpeedTest.java
    cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableTest.java
    cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableUtils.java
    cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java
    cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterTest.java
    cassandra/trunk/test/unit/org/apache/cassandra/streaming/StreamingTransferTest.java

Modified: cassandra/trunk/test/long/org/apache/cassandra/db/LongCompactionSpeedTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/long/org/apache/cassandra/db/LongCompactionSpeedTest.java?rev=1053392&r1=1053391&r2=1053392&view=diff
==============================================================================
--- cassandra/trunk/test/long/org/apache/cassandra/db/LongCompactionSpeedTest.java (original)
+++ cassandra/trunk/test/long/org/apache/cassandra/db/LongCompactionSpeedTest.java Tue Dec
28 17:15:26 2010
@@ -86,7 +86,7 @@ public class LongCompactionSpeedTest ext
                 }
                 rows.put(key, SSTableUtils.createCF(Long.MIN_VALUE, Integer.MIN_VALUE, cols));
             }
-            SSTableReader sstable = SSTableUtils.writeSSTable(rows);
+            SSTableReader sstable = SSTableUtils.prepare().write(rows);
             sstables.add(sstable);
             store.addSSTable(sstable);
         }

Modified: cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableTest.java?rev=1053392&r1=1053391&r2=1053392&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableTest.java (original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableTest.java Tue Dec 28
17:15:26 2010
@@ -40,7 +40,7 @@ public class SSTableTest extends Cleanup
 
         Map<ByteBuffer, ByteBuffer> map = new HashMap<ByteBuffer,ByteBuffer>();
         map.put(key, bytes);
-        SSTableReader ssTable = SSTableUtils.writeRawSSTable("Keyspace1", "Standard1", map);
+        SSTableReader ssTable = SSTableUtils.prepare().cf("Standard1").writeRaw(map);
 
         // verify
         verifySingle(ssTable, bytes, key);
@@ -68,7 +68,7 @@ public class SSTableTest extends Cleanup
         }
 
         // write
-        SSTableReader ssTable = SSTableUtils.writeRawSSTable("Keyspace1", "Standard2", map);
+        SSTableReader ssTable = SSTableUtils.prepare().cf("Standard2").writeRaw(map);
 
         // verify
         verifyMany(ssTable, map);

Modified: cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableUtils.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableUtils.java?rev=1053392&r1=1053391&r2=1053392&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableUtils.java (original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableUtils.java Tue Dec 28
17:15:26 2010
@@ -71,47 +71,98 @@ public class SSTableUtils
         return datafile;
     }
 
-    public static SSTableReader writeSSTable(Set<String> keys) throws IOException
+    /**
+     * @return A Context with chainable methods to configure and write a SSTable.
+     */
+    public static Context prepare()
     {
-        Map<String, ColumnFamily> map = new HashMap<String, ColumnFamily>();
-        for (String key : keys)
-        {
-            ColumnFamily cf = ColumnFamily.create(TABLENAME, CFNAME);
-            cf.addColumn(new Column(ByteBuffer.wrap(key.getBytes()), ByteBuffer.wrap(key.getBytes()),
0));
-            map.put(key, cf);
-        }
-        return writeSSTable(map);
+        return new Context();
     }
 
-    public static SSTableReader writeSSTable(Map<String, ColumnFamily> entries) throws
IOException
+    public static class Context
     {
-        Map<ByteBuffer, ByteBuffer> map = new HashMap<ByteBuffer, ByteBuffer>();
-        for (Map.Entry<String, ColumnFamily> entry : entries.entrySet())
+        private String ksname = TABLENAME;
+        private String cfname = CFNAME;
+        private Descriptor dest = null;
+        private boolean cleanup = true;
+        private int generation = 0;
+
+        Context() {}
+
+        public Context ks(String ksname)
         {
-            DataOutputBuffer buffer = new DataOutputBuffer();
-            ColumnFamily.serializer().serializeWithIndexes(entry.getValue(), buffer);
-            map.put(ByteBuffer.wrap(entry.getKey().getBytes()), ByteBuffer.wrap(Arrays.copyOf(buffer.getData(),
buffer.getLength())));
+            this.ksname = ksname;
+            return this;
         }
-        return writeRawSSTable(TABLENAME, CFNAME, map);
-    }
 
-    public static SSTableReader writeRawSSTable(String tablename, String cfname, Map<ByteBuffer,
ByteBuffer> entries) throws IOException
-    {
-        return writeRawSSTable(tablename, cfname, entries, 0);
-    }
+        public Context cf(String cfname)
+        {
+            this.cfname = cfname;
+            return this;
+        }
 
-    public static SSTableReader writeRawSSTable(String tablename, String cfname, Map<ByteBuffer,
ByteBuffer> entries, int generation) throws IOException
-    {
-        File datafile = tempSSTableFile(tablename, cfname, generation);
-        SSTableWriter writer = new SSTableWriter(datafile.getAbsolutePath(), entries.size());
-        SortedMap<DecoratedKey, ByteBuffer> sortedEntries = new TreeMap<DecoratedKey,
ByteBuffer>();
-        for (Map.Entry<ByteBuffer, ByteBuffer> entry : entries.entrySet())
-            sortedEntries.put(writer.partitioner.decorateKey(entry.getKey()), entry.getValue());
-        for (Map.Entry<DecoratedKey, ByteBuffer> entry : sortedEntries.entrySet())
-            writer.append(entry.getKey(), entry.getValue());
-        new File(writer.descriptor.filenameFor(Component.PRIMARY_INDEX)).deleteOnExit();
-        new File(writer.descriptor.filenameFor(Component.FILTER)).deleteOnExit();
-        return writer.closeAndOpenReader();
-    }
+        /**
+         * Set an alternate path for the written SSTable: if unset, the SSTable will
+         * be cleaned up on JVM exit.
+         */
+        public Context dest(Descriptor dest)
+        {
+            this.dest = dest;
+            this.cleanup = false;
+            return this;
+        }
 
+        /**
+         * Sets the generation number for the generated SSTable. Ignored if "dest()" is set.
+         */
+        public Context generation(int generation)
+        {
+            this.generation = generation;
+            return this;
+        }
+
+        public SSTableReader write(Set<String> keys) throws IOException
+        {
+            Map<String, ColumnFamily> map = new HashMap<String, ColumnFamily>();
+            for (String key : keys)
+            {
+                ColumnFamily cf = ColumnFamily.create(ksname, cfname);
+                cf.addColumn(new Column(ByteBuffer.wrap(key.getBytes()), ByteBuffer.wrap(key.getBytes()),
0));
+                map.put(key, cf);
+            }
+            return write(map);
+        }
+
+        public SSTableReader write(Map<String, ColumnFamily> entries) throws IOException
+        {
+            Map<ByteBuffer, ByteBuffer> map = new HashMap<ByteBuffer, ByteBuffer>();
+            for (Map.Entry<String, ColumnFamily> entry : entries.entrySet())
+            {
+                DataOutputBuffer buffer = new DataOutputBuffer();
+                ColumnFamily.serializer().serializeWithIndexes(entry.getValue(), buffer);
+                map.put(ByteBuffer.wrap(entry.getKey().getBytes()),
+                        ByteBuffer.wrap(buffer.asByteArray()));
+            }
+            return writeRaw(map);
+        }
+
+        /**
+         * @Deprecated: Writes the binary content of a row, which should be encapsulated.
+         */
+        public SSTableReader writeRaw(Map<ByteBuffer, ByteBuffer> entries) throws IOException
+        {
+            File datafile = (dest == null) ? tempSSTableFile(ksname, cfname, generation)
: new File(dest.filenameFor(Component.DATA));
+            SSTableWriter writer = new SSTableWriter(datafile.getAbsolutePath(), entries.size());
+            SortedMap<DecoratedKey, ByteBuffer> sortedEntries = new TreeMap<DecoratedKey,
ByteBuffer>();
+            for (Map.Entry<ByteBuffer, ByteBuffer> entry : entries.entrySet())
+                sortedEntries.put(writer.partitioner.decorateKey(entry.getKey()), entry.getValue());
+            for (Map.Entry<DecoratedKey, ByteBuffer> entry : sortedEntries.entrySet())
+                writer.append(entry.getKey(), entry.getValue());
+            SSTableReader reader = writer.closeAndOpenReader();
+            if (cleanup)
+                for (Component comp : reader.components)
+                    new File(reader.descriptor.filenameFor(comp)).deleteOnExit();
+            return reader;
+        }
+    }
 }

Modified: cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java?rev=1053392&r1=1053391&r2=1053392&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java
(original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java
Tue Dec 28 17:15:26 2010
@@ -157,7 +157,7 @@ public class SSTableWriterAESCommutative
         cf.clear();
 
         // write out unmodified CF
-        SSTableReader orig = SSTableUtils.writeRawSSTable(keyspace, cfname, entries, 0);
+        SSTableReader orig = SSTableUtils.prepare().ks(keyspace).cf(cfname).generation(0).writeRaw(entries);
 
         // whack the index to trigger the recover
         FileUtils.deleteWithConfirm(orig.descriptor.filenameFor(Component.PRIMARY_INDEX));
@@ -170,7 +170,7 @@ public class SSTableWriterAESCommutative
             ).get();
 
         // write out cleaned CF
-        SSTableReader cleaned = SSTableUtils.writeRawSSTable(keyspace, cfname, cleanedEntries,
0);
+        SSTableReader cleaned = SSTableUtils.prepare().ks(keyspace).cf(cfname).generation(0).writeRaw(cleanedEntries);
 
         // verify
         BufferedRandomAccessFile origFile    = new BufferedRandomAccessFile(orig.descriptor.filenameFor(SSTable.COMPONENT_DATA),
"r", 8 * 1024 * 1024);

Modified: cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterTest.java?rev=1053392&r1=1053391&r2=1053392&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterTest.java (original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterTest.java Tue Dec
28 17:15:26 2010
@@ -76,7 +76,7 @@ public class SSTableWriterTest extends C
         ColumnFamily.serializer().serializeWithIndexes(cf, buffer);               
         entries.put(ByteBufferUtil.bytes("k3"), ByteBuffer.wrap(Arrays.copyOf(buffer.getData(),
buffer.getLength())));
         
-        SSTableReader orig = SSTableUtils.writeRawSSTable("Keyspace1", "Indexed1", entries);
       
+        SSTableReader orig = SSTableUtils.prepare().cf("Indexed1").writeRaw(entries);   
    
         // whack the index to trigger the recover
         FileUtils.deleteWithConfirm(orig.descriptor.filenameFor(Component.PRIMARY_INDEX));
         FileUtils.deleteWithConfirm(orig.descriptor.filenameFor(Component.FILTER));

Modified: cassandra/trunk/test/unit/org/apache/cassandra/streaming/StreamingTransferTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/streaming/StreamingTransferTest.java?rev=1053392&r1=1053391&r2=1053392&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/streaming/StreamingTransferTest.java (original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/streaming/StreamingTransferTest.java Tue
Dec 28 17:15:26 2010
@@ -119,7 +119,7 @@ public class StreamingTransferTest exten
         content.add("transfer1");
         content.add("transfer2");
         content.add("transfer3");
-        SSTableReader sstable = SSTableUtils.writeSSTable(content);
+        SSTableReader sstable = SSTableUtils.prepare().write(content);
         String tablename = sstable.getTableName();
         String cfname = sstable.getColumnFamilyName();
 
@@ -127,7 +127,7 @@ public class StreamingTransferTest exten
         content2.add("test");
         content2.add("test2");
         content2.add("test3");
-        SSTableReader sstable2 = SSTableUtils.writeSSTable(content2);
+        SSTableReader sstable2 = SSTableUtils.prepare().write(content2);
 
         // transfer the first and last key
         IPartitioner p = StorageService.getPartitioner();



Mime
View raw message