| 1 |
|
| // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
| 2 |
|
| // Use of this source code is governed by a BSD-style license that can be |
| 3 |
|
| // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 |
|
| |
| 5 |
|
| #include "leveldb/table.h" |
| 6 |
|
| |
| 7 |
|
| #include "leveldb/cache.h" |
| 8 |
|
| #include "leveldb/env.h" |
| 9 |
|
| #include "table/block.h" |
| 10 |
|
| #include "table/format.h" |
| 11 |
|
| #include "table/two_level_iterator.h" |
| 12 |
|
| #include "util/coding.h" |
| 13 |
|
| |
| 14 |
|
| namespace leveldb { |
| 15 |
|
| |
| 16 |
|
| struct Table::Rep { |
| 17 |
1 |
| ~Rep() { |
| 18 |
1 |
▶ | delete index_block; |
Branch: 1 branch
| Branch | True | False | Total |
| delete index_block; |
1 |
0 |
1 |
|
| 19 |
1 |
| } |
| 20 |
|
| |
| 21 |
|
| Options options; |
| 22 |
|
| Status status; |
| 23 |
|
| RandomAccessFile* file; |
| 24 |
|
| uint64_t cache_id; |
| 25 |
|
| |
| 26 |
|
| BlockHandle metaindex_handle; // Handle to metaindex_block: saved from footer |
| 27 |
|
| Block* index_block; |
| 28 |
|
| }; |
| 29 |
|
| |
| 30 |
1 |
| Status Table::Open(const Options& options, |
| 31 |
|
| RandomAccessFile* file, |
| 32 |
|
| uint64_t size, |
| 33 |
|
| Table** table) { |
| 34 |
1 |
| *table = NULL; |
| 35 |
1 |
▶ | if (size < Footer::kEncodedLength) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size < Footer::kEncodedLength) { |
0 |
1 |
1 |
|
| 36 |
0 |
▶ | return Status::InvalidArgument("file is too short to be an sstable"); |
Branch: 1 branch
| Branch | True | False | Total |
| return Status::InvalidArgument("file is too short to be an sstable"); |
0 |
0 |
0 |
|
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
|
| char footer_space[Footer::kEncodedLength]; |
| 40 |
1 |
| Slice footer_input; |
| 41 |
1 |
| Status s = file->Read(size - Footer::kEncodedLength, Footer::kEncodedLength, |
| 42 |
1 |
▶ | &footer_input, footer_space); |
Branch: 1 branch
| Branch | True | False | Total |
| &footer_input, footer_space); |
1 |
0 |
1 |
|
| 43 |
1 |
▶ | if (!s.ok()) return s; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s.ok()) return s; |
0 |
1 |
1 |
| if (!s.ok()) return s; |
0 |
0 |
0 |
|
| 44 |
|
| |
| 45 |
1 |
▶ | Footer footer; |
Branch: 1 branch
| Branch | True | False | Total |
| Footer footer; |
1 |
0 |
1 |
|
| 46 |
1 |
▶ | s = footer.DecodeFrom(&footer_input); |
Branch: 2 branches
| Branch | True | False | Total |
| s = footer.DecodeFrom(&footer_input); |
1 |
0 |
1 |
| s = footer.DecodeFrom(&footer_input); |
1 |
0 |
1 |
|
| 47 |
1 |
▶ | if (!s.ok()) return s; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!s.ok()) return s; |
0 |
1 |
1 |
| if (!s.ok()) return s; |
0 |
0 |
0 |
|
| 48 |
|
| |
| 49 |
|
| // Read the index block |
| 50 |
1 |
| Block* index_block = NULL; |
| 51 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 52 |
1 |
▶ | s = ReadBlock(file, ReadOptions(), footer.index_handle(), &index_block); |
Branch: 2 branches
| Branch | True | False | Total |
| s = ReadBlock(file, ReadOptions(), footer.index_handle(), &index_block); |
1 |
0 |
1 |
| s = ReadBlock(file, ReadOptions(), footer.index_handle(), &index_block); |
1 |
0 |
1 |
|
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 56 |
|
| // We've successfully read the footer and the index block: we're |
| 57 |
|
| // ready to serve requests. |
| 58 |
1 |
▶ | Rep* rep = new Table::Rep; |
Branch: 3 branches
| Branch | True | False | Total |
| Rep* rep = new Table::Rep; |
1 |
0 |
1 |
| Rep* rep = new Table::Rep; |
1 |
0 |
1 |
| Rep* rep = new Table::Rep; |
0 |
0 |
0 |
|
| 59 |
1 |
| rep->options = options; |
| 60 |
1 |
| rep->file = file; |
| 61 |
1 |
| rep->metaindex_handle = footer.metaindex_handle(); |
| 62 |
1 |
| rep->index_block = index_block; |
| 63 |
1 |
▶ | rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0); |
Branch: 2 branches
| Branch | True | False | Total |
| rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0); |
1 |
0 |
1 |
| rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0); |
1 |
0 |
1 |
|
| 64 |
1 |
▶ | *table = new Table(rep); |
Branch: 1 branch
| Branch | True | False | Total |
| *table = new Table(rep); |
1 |
0 |
1 |
|
| 65 |
|
| } else { |
| 66 |
0 |
▶ | if (index_block) delete index_block; |
Branch: 2 branches
| Branch | True | False | Total |
| if (index_block) delete index_block; |
0 |
0 |
0 |
| if (index_block) delete index_block; |
0 |
0 |
0 |
|
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
1 |
▶ | return s; |
Branch: 1 branch
| Branch | True | False | Total |
| return s; |
1 |
0 |
1 |
|
| 70 |
1 |
| } |
| 71 |
|
| |
| 72 |
1 |
| Table::~Table() { |
| 73 |
1 |
▶ | delete rep_; |
Branch: 1 branch
| Branch | True | False | Total |
| delete rep_; |
1 |
0 |
1 |
|
| 74 |
1 |
| } |
| 75 |
|
| |
| 76 |
0 |
| static void DeleteBlock(void* arg, void* ignored) { |
| 77 |
0 |
▶ | delete reinterpret_cast<Block*>(arg); |
Branch: 1 branch
| Branch | True | False | Total |
| delete reinterpret_cast<Block*>(arg); |
0 |
0 |
0 |
|
| 78 |
0 |
| } |
| 79 |
|
| |
| 80 |
1 |
| static void DeleteCachedBlock(const Slice& key, void* value) { |
| 81 |
1 |
| Block* block = reinterpret_cast<Block*>(value); |
| 82 |
1 |
▶ | delete block; |
Branch: 1 branch
| Branch | True | False | Total |
| delete block; |
1 |
0 |
1 |
|
| 83 |
1 |
| } |
| 84 |
|
| |
| 85 |
1 |
| static void ReleaseBlock(void* arg, void* h) { |
| 86 |
1 |
| Cache* cache = reinterpret_cast<Cache*>(arg); |
| 87 |
1 |
| Cache::Handle* handle = reinterpret_cast<Cache::Handle*>(h); |
| 88 |
1 |
| cache->Release(handle); |
| 89 |
1 |
| } |
| 90 |
|
| |
| 91 |
|
| // Convert an index iterator value (i.e., an encoded BlockHandle) |
| 92 |
|
| // into an iterator over the contents of the corresponding block. |
| 93 |
1 |
| Iterator* Table::BlockReader(void* arg, |
| 94 |
|
| const ReadOptions& options, |
| 95 |
|
| const Slice& index_value) { |
| 96 |
1 |
| Table* table = reinterpret_cast<Table*>(arg); |
| 97 |
1 |
| Cache* block_cache = table->rep_->options.block_cache; |
| 98 |
1 |
| Block* block = NULL; |
| 99 |
1 |
| Cache::Handle* cache_handle = NULL; |
| 100 |
|
| |
| 101 |
1 |
▶ | BlockHandle handle; |
Branch: 1 branch
| Branch | True | False | Total |
| BlockHandle handle; |
1 |
0 |
1 |
|
| 102 |
1 |
| Slice input = index_value; |
| 103 |
1 |
▶ | Status s = handle.DecodeFrom(&input); |
Branch: 1 branch
| Branch | True | False | Total |
| Status s = handle.DecodeFrom(&input); |
1 |
0 |
1 |
|
| 104 |
|
| // We intentionally allow extra stuff in index_value so that we |
| 105 |
|
| // can add more features in the future. |
| 106 |
|
| |
| 107 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 108 |
1 |
▶ | if (block_cache != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (block_cache != NULL) { |
1 |
0 |
1 |
|
| 109 |
|
| char cache_key_buffer[16]; |
| 110 |
1 |
▶ | EncodeFixed64(cache_key_buffer, table->rep_->cache_id); |
Branch: 1 branch
| Branch | True | False | Total |
| EncodeFixed64(cache_key_buffer, table->rep_->cache_id); |
1 |
0 |
1 |
|
| 111 |
1 |
▶ | EncodeFixed64(cache_key_buffer+8, handle.offset()); |
Branch: 1 branch
| Branch | True | False | Total |
| EncodeFixed64(cache_key_buffer+8, handle.offset()); |
1 |
0 |
1 |
|
| 112 |
1 |
| Slice key(cache_key_buffer, sizeof(cache_key_buffer)); |
| 113 |
1 |
▶ | cache_handle = block_cache->Lookup(key); |
Branch: 1 branch
| Branch | True | False | Total |
| cache_handle = block_cache->Lookup(key); |
1 |
0 |
1 |
|
| 114 |
1 |
▶ | if (cache_handle != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache_handle != NULL) { |
0 |
1 |
1 |
|
| 115 |
0 |
▶ | block = reinterpret_cast<Block*>(block_cache->Value(cache_handle)); |
Branch: 1 branch
| Branch | True | False | Total |
| block = reinterpret_cast<Block*>(block_cache->Value(cache_handle)); |
0 |
0 |
0 |
|
| 116 |
|
| } else { |
| 117 |
1 |
▶ | s = ReadBlock(table->rep_->file, options, handle, &block); |
Branch: 2 branches
| Branch | True | False | Total |
| s = ReadBlock(table->rep_->file, options, handle, &block); |
1 |
0 |
1 |
| s = ReadBlock(table->rep_->file, options, handle, &block); |
1 |
0 |
1 |
|
| 118 |
1 |
▶ | if (s.ok() && options.fill_cache) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (s.ok() && options.fill_cache) { |
1 |
0 |
1 |
| if (s.ok() && options.fill_cache) { |
1 |
0 |
1 |
| if (s.ok() && options.fill_cache) { |
1 |
0 |
1 |
|
| 119 |
1 |
▶ | cache_handle = block_cache->Insert( |
Branch: 1 branch
| Branch | True | False | Total |
| cache_handle = block_cache->Insert( |
1 |
0 |
1 |
|
| 120 |
|
| key, block, block->size(), &DeleteCachedBlock); |
| 121 |
|
| } |
| 122 |
|
| } |
| 123 |
|
| } else { |
| 124 |
0 |
▶ | s = ReadBlock(table->rep_->file, options, handle, &block); |
Branch: 2 branches
| Branch | True | False | Total |
| s = ReadBlock(table->rep_->file, options, handle, &block); |
0 |
0 |
0 |
| s = ReadBlock(table->rep_->file, options, handle, &block); |
0 |
0 |
0 |
|
| 125 |
|
| } |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
|
| Iterator* iter; |
| 129 |
1 |
▶ | if (block != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (block != NULL) { |
1 |
0 |
1 |
|
| 130 |
1 |
▶ | iter = block->NewIterator(table->rep_->options.comparator); |
Branch: 1 branch
| Branch | True | False | Total |
| iter = block->NewIterator(table->rep_->options.comparator); |
1 |
0 |
1 |
|
| 131 |
1 |
▶ | if (cache_handle == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache_handle == NULL) { |
0 |
1 |
1 |
|
| 132 |
0 |
▶ | iter->RegisterCleanup(&DeleteBlock, block, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| iter->RegisterCleanup(&DeleteBlock, block, NULL); |
0 |
0 |
0 |
|
| 133 |
|
| } else { |
| 134 |
1 |
▶ | iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle); |
Branch: 1 branch
| Branch | True | False | Total |
| iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle); |
1 |
0 |
1 |
|
| 135 |
|
| } |
| 136 |
|
| } else { |
| 137 |
0 |
▶ | iter = NewErrorIterator(s); |
Branch: 1 branch
| Branch | True | False | Total |
| iter = NewErrorIterator(s); |
0 |
0 |
0 |
|
| 138 |
|
| } |
| 139 |
1 |
| return iter; |
| 140 |
1 |
| } |
| 141 |
|
| |
| 142 |
2 |
| Iterator* Table::NewIterator(const ReadOptions& options) const { |
| 143 |
2 |
| return NewTwoLevelIterator( |
| 144 |
2 |
| rep_->index_block->NewIterator(rep_->options.comparator), |
| 145 |
2 |
| &Table::BlockReader, const_cast<Table*>(this), options); |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
0 |
| uint64_t Table::ApproximateOffsetOf(const Slice& key) const { |
| 149 |
|
| Iterator* index_iter = |
| 150 |
0 |
| rep_->index_block->NewIterator(rep_->options.comparator); |
| 151 |
0 |
| index_iter->Seek(key); |
| 152 |
|
| uint64_t result; |
| 153 |
0 |
▶ | if (index_iter->Valid()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (index_iter->Valid()) { |
0 |
0 |
0 |
|
| 154 |
0 |
▶ | BlockHandle handle; |
Branch: 1 branch
| Branch | True | False | Total |
| BlockHandle handle; |
0 |
0 |
0 |
|
| 155 |
0 |
▶ | Slice input = index_iter->value(); |
Branch: 1 branch
| Branch | True | False | Total |
| Slice input = index_iter->value(); |
0 |
0 |
0 |
|
| 156 |
0 |
▶ | Status s = handle.DecodeFrom(&input); |
Branch: 1 branch
| Branch | True | False | Total |
| Status s = handle.DecodeFrom(&input); |
0 |
0 |
0 |
|
| 157 |
0 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
0 |
0 |
0 |
|
| 158 |
0 |
| result = handle.offset(); |
| 159 |
|
| } else { |
| 160 |
|
| // Strange: we can't decode the block handle in the index block. |
| 161 |
|
| // We'll just return the offset of the metaindex block, which is |
| 162 |
|
| // close to the whole file size for this case. |
| 163 |
0 |
| result = rep_->metaindex_handle.offset(); |
| 164 |
|
| } |
| 165 |
0 |
| } else { |
| 166 |
|
| // key is past the last key in the file. Approximate the offset |
| 167 |
|
| // by returning the offset of the metaindex block (which is |
| 168 |
|
| // right near the end of the file). |
| 169 |
0 |
| result = rep_->metaindex_handle.offset(); |
| 170 |
|
| } |
| 171 |
0 |
▶ | delete index_iter; |
Branch: 1 branch
| Branch | True | False | Total |
| delete index_iter; |
0 |
0 |
0 |
|
| 172 |
0 |
| return result; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| } |