Author: jbellis
Date: Wed Aug 25 13:55:52 2010
New Revision: 989109

URL: http://svn.apache.org/viewvc?rev=989109&view=rev
Log:
clean up SSTableNamesIterator and SSTableSliceIterator.  patch by jbellis

Modified:
    
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
    
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableSliceIterator.java

Modified: 
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
URL: 
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java?rev=989109&r1=989108&r2=989109&view=diff
==============================================================================
--- 
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
 (original)
+++ 
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableNamesIterator.java
 Wed Aug 25 13:55:52 2010
@@ -46,85 +46,91 @@ public class SSTableNamesIterator extend
     private ColumnFamily cf;
     private Iterator<IColumn> iter;
     public final SortedSet<byte[]> columns;
-    public final DecoratedKey decoratedKey;
+    public final DecoratedKey key;
 
-    public SSTableNamesIterator(SSTableReader ssTable, DecoratedKey key, 
SortedSet<byte[]> columnNames)
+    public SSTableNamesIterator(SSTableReader sstable, DecoratedKey key, 
SortedSet<byte[]> columns)
     {
-        this (ssTable, null, key, columnNames);
-    }
+        assert columns != null;
+        this.columns = columns;
+        this.key = key;
+
+        FileDataInput file = sstable.getFileDataInput(key, 
DatabaseDescriptor.getIndexedReadBufferSizeInKB() * 1024);
+        if (file == null)
+            return;
 
-    public SSTableNamesIterator(SSTableReader sstable, FileDataInput file, 
DecoratedKey key, SortedSet<byte[]> columnNames)
-    {
-        boolean closeFileWhenDone = file == null;
-        
         try
         {
-            assert columnNames != null;
-
-            this.columns = columnNames;
-            this.decoratedKey = key;
-
-            // open the sstable file, if we don't have one passed to use from 
range scan
-            if (file == null)
+            DecoratedKey keyInDisk = 
SSTableReader.decodeKey(sstable.getPartitioner(),
+                                                             
sstable.getDescriptor(),
+                                                             
FBUtilities.readShortByteArray(file));
+            assert keyInDisk.equals(key) : String.format("%s != %s in %s", 
keyInDisk, key, file.getPath());
+            SSTableReader.readRowSize(file, sstable.getDescriptor());
+            read(sstable, file);
+        }
+        catch (IOException e)
+        {
+            throw new IOError(e);
+        }
+        finally
+        {
+            try
             {
-                file = sstable.getFileDataInput(decoratedKey, 
DatabaseDescriptor.getIndexedReadBufferSizeInKB() * 1024);
-                if (file == null)
-                    return;
-                DecoratedKey keyInDisk = 
SSTableReader.decodeKey(sstable.getPartitioner(),
-                                                                 
sstable.getDescriptor(),
-                                                                 
FBUtilities.readShortByteArray(file));
-                assert keyInDisk.equals(decoratedKey)
-                       : String.format("%s != %s in %s", keyInDisk, 
decoratedKey, file.getPath());
-                SSTableReader.readRowSize(file, sstable.getDescriptor());
+                file.close();
             }
-
-            // read the requested columns into `cf`
-            /* Read the bloom filter summarizing the columns */
-            BloomFilter bf = IndexHelper.defreezeBloomFilter(file);
-            List<IndexHelper.IndexInfo> indexList = 
IndexHelper.deserializeIndex(file);
-
-            // we can stop early if bloom filter says none of the columns 
actually exist -- but,
-            // we can't stop before initializing the cf above, in case there's 
a relevant tombstone
-            cf = 
ColumnFamily.serializer().deserializeFromSSTableNoColumns(sstable.createColumnFamily(),
 file);
-
-            List<byte[]> filteredColumnNames1 = new 
ArrayList<byte[]>(columnNames.size());
-            for (byte[] name : columnNames)
+            catch (IOException ioe)
             {
-                if (bf.isPresent(name))
-                {
-                    filteredColumnNames1.add(name);
-                }
+                logger.warn("error closing " + file.getPath());
             }
-            List<byte[]> filteredColumnNames = filteredColumnNames1;
-            if (filteredColumnNames.isEmpty())
-                return;
-
-            if (indexList == null)
-                readSimpleColumns(file, columnNames, filteredColumnNames);
-            else
-                readIndexedColumns(sstable, file, columnNames, 
filteredColumnNames, indexList);
+        }
+    }
 
-            // create an iterator view of the columns we read
-            iter = cf.getSortedColumns().iterator();
+    public SSTableNamesIterator(SSTableReader sstable, FileDataInput file, 
DecoratedKey key, SortedSet<byte[]> columns)
+    {
+        assert columns != null;
+        this.columns = columns;
+        this.key = key;
+
+        try
+        {
+            read(sstable, file);
         }
         catch (IOException ioe)
         {
             throw new IOError(ioe);
         }
-        finally
+    }
+
+    private void read(SSTableReader sstable, FileDataInput file)
+    throws IOException
+    {
+
+        // read the requested columns into `cf`
+        /* Read the bloom filter summarizing the columns */
+        BloomFilter bf = IndexHelper.defreezeBloomFilter(file);
+        List<IndexHelper.IndexInfo> indexList = 
IndexHelper.deserializeIndex(file);
+
+        // we can stop early if bloom filter says none of the columns actually 
exist -- but,
+        // we can't stop before initializing the cf above, in case there's a 
relevant tombstone
+        cf = 
ColumnFamily.serializer().deserializeFromSSTableNoColumns(sstable.createColumnFamily(),
 file);
+
+        List<byte[]> filteredColumnNames = new 
ArrayList<byte[]>(columns.size());
+        for (byte[] name : columns)
         {
-            if (closeFileWhenDone && file != null)
+            if (bf.isPresent(name))
             {
-                try
-                {
-                    file.close();
-                }
-                catch (IOException ioe)
-                {
-                    logger.warn("error closing " + file.getPath());
-                }
+                filteredColumnNames.add(name);
             }
         }
+        if (filteredColumnNames.isEmpty())
+            return;
+
+        if (indexList == null)
+            readSimpleColumns(file, columns, filteredColumnNames);
+        else
+            readIndexedColumns(sstable, file, columns, filteredColumnNames, 
indexList);
+
+        // create an iterator view of the columns we read
+        iter = cf.getSortedColumns().iterator();
     }
 
     private void readSimpleColumns(FileDataInput file, SortedSet<byte[]> 
columnNames, List<byte[]> filteredColumnNames) throws IOException
@@ -183,7 +189,7 @@ public class SSTableNamesIterator extend
 
     public DecoratedKey getKey()
     {
-        return decoratedKey;
+        return key;
     }
 
     public ColumnFamily getColumnFamily()

Modified: 
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableSliceIterator.java
URL: 
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableSliceIterator.java?rev=989109&r1=989108&r2=989109&view=diff
==============================================================================
--- 
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableSliceIterator.java
 (original)
+++ 
cassandra/trunk/src/java/org/apache/cassandra/db/columniterator/SSTableSliceIterator.java
 Wed Aug 25 13:55:52 2010
@@ -38,19 +38,37 @@ import org.apache.cassandra.utils.FBUtil
  */
 public class SSTableSliceIterator implements IColumnIterator
 {
-    private final FileDataInput file;
+    private final FileDataInput fileToClose;
     private IColumnIterator reader;
-    private boolean closeFileWhenDone = false;
-    private DecoratedKey decoratedKey;
+    private DecoratedKey key;
 
-    public SSTableSliceIterator(SSTableReader ssTable, DecoratedKey key, 
byte[] startColumn, byte[] finishColumn, boolean reversed)
+    public SSTableSliceIterator(SSTableReader sstable, DecoratedKey key, 
byte[] startColumn, byte[] finishColumn, boolean reversed)
     {
-        this(ssTable, null, key, startColumn, finishColumn, reversed);
+        this.key = key;
+        fileToClose = sstable.getFileDataInput(this.key, 
DatabaseDescriptor.getSlicedReadBufferSizeInKB() * 1024);
+        if (fileToClose == null)
+            return;
+
+        try
+        {
+            DecoratedKey keyInDisk = 
SSTableReader.decodeKey(sstable.getPartitioner(),
+                                                             
sstable.getDescriptor(),
+                                                             
FBUtilities.readShortByteArray(fileToClose));
+            assert keyInDisk.equals(key)
+                   : String.format("%s != %s in %s", keyInDisk, key, 
fileToClose.getPath());
+            SSTableReader.readRowSize(fileToClose, sstable.getDescriptor());
+        }
+        catch (IOException e)
+        {
+            throw new IOError(e);
+        }
+
+        reader = createReader(sstable, fileToClose, startColumn, finishColumn, 
reversed);
     }
 
     /**
      * An iterator for a slice within an SSTable
-     * @param ssTable The SSTable to iterate over
+     * @param sstable The SSTable to iterate over
      * @param file Optional parameter that input is read from.  If null is 
passed, this class creates an appropriate one automatically.
      * If this class creates, it will close the underlying file when #close() 
is called.
      * If a caller passes a non-null argument, this class will NOT close the 
underlying file when the iterator is closed (i.e. the caller is responsible for 
closing the file)
@@ -60,43 +78,23 @@ public class SSTableSliceIterator implem
      * @param finishColumn The end of the slice
      * @param reversed Results are returned in reverse order iff reversed is 
true.
      */
-    public SSTableSliceIterator(SSTableReader ssTable, FileDataInput file, 
DecoratedKey key, byte[] startColumn, byte[] finishColumn, boolean reversed)
+    public SSTableSliceIterator(SSTableReader sstable, FileDataInput file, 
DecoratedKey key, byte[] startColumn, byte[] finishColumn, boolean reversed)
     {
-        this.decoratedKey = key;
-
-        if (file == null)
-        {
-            closeFileWhenDone = true; //if we create it, we close it
-            file = ssTable.getFileDataInput(decoratedKey, 
DatabaseDescriptor.getSlicedReadBufferSizeInKB() * 1024);
-            if (file == null)
-            {
-                this.file = null;
-                return;
-            }
-            try
-            {
-                DecoratedKey keyInDisk = 
SSTableReader.decodeKey(ssTable.getPartitioner(),
-                                                                 
ssTable.getDescriptor(),
-                                                                 
FBUtilities.readShortByteArray(file));
-                assert keyInDisk.equals(decoratedKey)
-                       : String.format("%s != %s in %s", keyInDisk, 
decoratedKey, file.getPath());
-                SSTableReader.readRowSize(file, ssTable.getDescriptor());
-            }
-            catch (IOException e)
-            {
-                throw new IOError(e);
-            }
-        }
-        this.file = file;
+        this.key = key;
+        fileToClose = null;
+        reader = createReader(sstable, file, startColumn, finishColumn, 
reversed);
+    }
 
-        reader = startColumn.length == 0 && !reversed
+    private static IColumnIterator createReader(SSTableReader ssTable, 
FileDataInput file, byte[] startColumn, byte[] finishColumn, boolean reversed)
+    {
+        return startColumn.length == 0 && !reversed
                  ? new SimpleSliceReader(ssTable, file, finishColumn)
                  : new IndexedSliceReader(ssTable, file, startColumn, 
finishColumn, reversed);
     }
-    
+
     public DecoratedKey getKey()
     {
-        return decoratedKey;
+        return key;
     }
 
     public ColumnFamily getColumnFamily() throws IOException
@@ -121,8 +119,8 @@ public class SSTableSliceIterator implem
 
     public void close() throws IOException
     {
-        if (closeFileWhenDone && file != null)
-            file.close();
+        if (fileToClose != null)
+            fileToClose.close();
     }
 
 }


Reply via email to