From a2f9b401830fa2edddc3ffaaadeb6c4433a33f9a Mon Sep 17 00:00:00 2001 From: Jonathan Ellis Date: Tue, 23 Jun 2009 17:47:53 +0000 Subject: [PATCH] refactor read-only constructor to SSTable.open patch by jbellis; reviewed by Eric Evans for CASSANDRA-224 git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@787759 13f79535-47bb-0310-9956-ffa450edef68 --- .../cassandra/db/ColumnFamilyStore.java | 2 +- .../apache/cassandra/db/ColumnIterator.java | 2 +- src/java/org/apache/cassandra/io/SSTable.java | 45 ++++++++----------- .../org/apache/cassandra/io/SSTableTest.java | 4 +- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/java/org/apache/cassandra/db/ColumnFamilyStore.java b/src/java/org/apache/cassandra/db/ColumnFamilyStore.java index d6031a6fcd..31cd970a4a 100644 --- a/src/java/org/apache/cassandra/db/ColumnFamilyStore.java +++ b/src/java/org/apache/cassandra/db/ColumnFamilyStore.java @@ -577,7 +577,7 @@ public final class ColumnFamilyStore implements ColumnFamilyStoreMBean private ColumnFamily fetchColumnFamily(String key, String cf, IFilter filter, String ssTableFile) throws IOException { - SSTable ssTable = new SSTable(ssTableFile, StorageService.getPartitioner()); + SSTable ssTable = SSTable.open(ssTableFile, StorageService.getPartitioner()); DataInputBuffer bufIn; bufIn = filter.next(key, cf, ssTable); if (bufIn.getLength() == 0) diff --git a/src/java/org/apache/cassandra/db/ColumnIterator.java b/src/java/org/apache/cassandra/db/ColumnIterator.java index fab378dc98..ac4b2aa725 100644 --- a/src/java/org/apache/cassandra/db/ColumnIterator.java +++ b/src/java/org/apache/cassandra/db/ColumnIterator.java @@ -58,7 +58,7 @@ class SSTableColumnIterator extends AbstractIterator implements ColumnI throws IOException { this.isAscending = isAscending; - SSTable ssTable = new SSTable(filename, StorageService.getPartitioner()); + SSTable ssTable = SSTable.open(filename, StorageService.getPartitioner()); reader = ssTable.getColumnGroupReader(key, cfName, startColumn, isAscending); this.startColumn = startColumn; curColumnIndex = isAscending ? 0 : -1; diff --git a/src/java/org/apache/cassandra/io/SSTable.java b/src/java/org/apache/cassandra/io/SSTable.java index 8274c4229a..2e7b156a74 100644 --- a/src/java/org/apache/cassandra/io/SSTable.java +++ b/src/java/org/apache/cassandra/io/SSTable.java @@ -156,7 +156,7 @@ public class SSTable { try { - new SSTable(filename, StorageService.getPartitioner()); + SSTable.open(filename, StorageService.getPartitioner()); } catch (IOException ex) { @@ -205,30 +205,26 @@ public class SSTable private String lastWrittenKey_; private IPartitioner partitioner_; - /** - * This ctor basically gets passed in the full path name - * of the data file associated with this SSTable. Use this - * ctor to read the data in this file. - */ - public SSTable(String dataFileName, IPartitioner partitioner) throws IOException + public static synchronized SSTable open(String dataFileName, IPartitioner partitioner) throws IOException { - dataFile_ = dataFileName; - partitioner_ = partitioner; - /* - * this is to prevent multiple threads from - * loading the same index files multiple times - * into memory. - */ - synchronized (indexLoadLock_) + SSTable sstable = new SSTable(dataFileName, partitioner); + sstable.dataWriter_.close(); // todo this is dumb + if (indexMetadataMap_.get(dataFileName) == null) { - if (indexMetadataMap_.get(dataFile_) == null) - { - long start = System.currentTimeMillis(); - loadIndexFile(); - loadBloomFilter(); - logger_.debug("INDEX LOAD TIME: " + (System.currentTimeMillis() - start) + " ms."); - } + long start = System.currentTimeMillis(); + sstable.loadIndexFile(); + sstable.loadBloomFilter(); + logger_.debug("INDEX LOAD TIME for " + dataFileName + ": " + (System.currentTimeMillis() - start) + " ms."); } + return sstable; + } + + public SSTable(String filename, IPartitioner partitioner) throws IOException + { + dataFile_ = filename; + partitioner_ = partitioner; + dataWriter_ = SequenceFile.bufferedWriter(dataFile_, 4 * 1024 * 1024); + indexRAF_ = new BufferedRandomAccessFile(indexFilename(), "rw", 1024 * 1024); } /** @@ -237,10 +233,7 @@ public class SSTable */ public SSTable(String directory, String filename, IPartitioner partitioner) throws IOException { - dataFile_ = directory + System.getProperty("file.separator") + filename + "-Data.db"; - partitioner_ = partitioner; - dataWriter_ = SequenceFile.bufferedWriter(dataFile_, 4 * 1024 * 1024); - indexRAF_ = new BufferedRandomAccessFile(indexFilename(), "rw", 1024 * 1024); + this(directory + System.getProperty("file.separator") + filename + "-Data.db", partitioner); } static String parseTableName(String filename) diff --git a/test/unit/org/apache/cassandra/io/SSTableTest.java b/test/unit/org/apache/cassandra/io/SSTableTest.java index 873465085c..7455d680d4 100644 --- a/test/unit/org/apache/cassandra/io/SSTableTest.java +++ b/test/unit/org/apache/cassandra/io/SSTableTest.java @@ -56,7 +56,7 @@ public class SSTableTest extends CleanupHelper private void verifySingle(File f, byte[] bytes, String key) throws IOException { - SSTable ssTable = new SSTable(f.getPath() + "-Data.db", new OrderPreservingPartitioner()); + SSTable ssTable = SSTable.open(f.getPath() + "-Data.db", new OrderPreservingPartitioner()); FileStruct fs = new FileStruct(SequenceFile.bufferedReader(ssTable.dataFile_, 128 * 1024), new OrderPreservingPartitioner()); fs.seekTo(key); int size = fs.getBufIn().readInt(); @@ -95,7 +95,7 @@ public class SSTableTest extends CleanupHelper { List keys = new ArrayList(map.keySet()); Collections.shuffle(keys); - SSTable ssTable = new SSTable(f.getPath() + "-Data.db", new OrderPreservingPartitioner()); + SSTable ssTable = SSTable.open(f.getPath() + "-Data.db", new OrderPreservingPartitioner()); FileStruct fs = new FileStruct(SequenceFile.bufferedReader(ssTable.dataFile_, 128 * 1024), new OrderPreservingPartitioner()); for (String key : keys) {