table.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include "leveldb/table.h"
  5. #include "leveldb/cache.h"
  6. #include "leveldb/comparator.h"
  7. #include "leveldb/env.h"
  8. #include "leveldb/filter_policy.h"
  9. #include "leveldb/options.h"
  10. #include "table/block.h"
  11. #include "table/filter_block.h"
  12. #include "table/format.h"
  13. #include "table/two_level_iterator.h"
  14. #include "util/coding.h"
  15. namespace leveldb {
  16. struct Table::Rep {
  17. ~Rep() {
  18. delete filter;
  19. delete [] filter_data;
  20. delete index_block;
  21. }
  22. Options options;
  23. Status status;
  24. RandomAccessFile* file;
  25. uint64_t cache_id;
  26. FilterBlockReader* filter;
  27. const char* filter_data;
  28. BlockHandle metaindex_handle; // Handle to metaindex_block: saved from footer
  29. Block* index_block;
  30. };
  31. Status Table::Open(const Options& options,
  32. RandomAccessFile* file,
  33. uint64_t size,
  34. Table** table) {
  35. *table = nullptr;
  36. if (size < Footer::kEncodedLength) {
  37. return Status::Corruption("file is too short to be an sstable");
  38. }
  39. char footer_space[Footer::kEncodedLength];
  40. Slice footer_input;
  41. Status s = file->Read(size - Footer::kEncodedLength, Footer::kEncodedLength,
  42. &footer_input, footer_space);
  43. if (!s.ok()) return s;
  44. Footer footer;
  45. s = footer.DecodeFrom(&footer_input);
  46. if (!s.ok()) return s;
  47. // Read the index block
  48. BlockContents index_block_contents;
  49. if (s.ok()) {
  50. ReadOptions opt;
  51. if (options.paranoid_checks) {
  52. opt.verify_checksums = true;
  53. }
  54. s = ReadBlock(file, opt, footer.index_handle(), &index_block_contents);
  55. }
  56. if (s.ok()) {
  57. // We've successfully read the footer and the index block: we're
  58. // ready to serve requests.
  59. Block* index_block = new Block(index_block_contents);
  60. Rep* rep = new Table::Rep;
  61. rep->options = options;
  62. rep->file = file;
  63. rep->metaindex_handle = footer.metaindex_handle();
  64. rep->index_block = index_block;
  65. rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0);
  66. rep->filter_data = nullptr;
  67. rep->filter = nullptr;
  68. *table = new Table(rep);
  69. (*table)->ReadMeta(footer);
  70. }
  71. return s;
  72. }
  73. void Table::ReadMeta(const Footer& footer) {
  74. if (rep_->options.filter_policy == nullptr) {
  75. return; // Do not need any metadata
  76. }
  77. // TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
  78. // it is an empty block.
  79. ReadOptions opt;
  80. if (rep_->options.paranoid_checks) {
  81. opt.verify_checksums = true;
  82. }
  83. BlockContents contents;
  84. if (!ReadBlock(rep_->file, opt, footer.metaindex_handle(), &contents).ok()) {
  85. // Do not propagate errors since meta info is not needed for operation
  86. return;
  87. }
  88. Block* meta = new Block(contents);
  89. Iterator* iter = meta->NewIterator(BytewiseComparator());
  90. std::string key = "filter.";
  91. key.append(rep_->options.filter_policy->Name());
  92. iter->Seek(key);
  93. if (iter->Valid() && iter->key() == Slice(key)) {
  94. ReadFilter(iter->value());
  95. }
  96. delete iter;
  97. delete meta;
  98. }
  99. void Table::ReadFilter(const Slice& filter_handle_value) {
  100. Slice v = filter_handle_value;
  101. BlockHandle filter_handle;
  102. if (!filter_handle.DecodeFrom(&v).ok()) {
  103. return;
  104. }
  105. // We might want to unify with ReadBlock() if we start
  106. // requiring checksum verification in Table::Open.
  107. ReadOptions opt;
  108. if (rep_->options.paranoid_checks) {
  109. opt.verify_checksums = true;
  110. }
  111. BlockContents block;
  112. if (!ReadBlock(rep_->file, opt, filter_handle, &block).ok()) {
  113. return;
  114. }
  115. if (block.heap_allocated) {
  116. rep_->filter_data = block.data.data(); // Will need to delete later
  117. }
  118. rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
  119. }
  120. Table::~Table() {
  121. delete rep_;
  122. }
  123. static void DeleteBlock(void* arg, void* ignored) {
  124. delete reinterpret_cast<Block*>(arg);
  125. }
  126. static void DeleteCachedBlock(const Slice& key, void* value) {
  127. Block* block = reinterpret_cast<Block*>(value);
  128. delete block;
  129. }
  130. static void ReleaseBlock(void* arg, void* h) {
  131. Cache* cache = reinterpret_cast<Cache*>(arg);
  132. Cache::Handle* handle = reinterpret_cast<Cache::Handle*>(h);
  133. cache->Release(handle);
  134. }
  135. // Convert an index iterator value (i.e., an encoded BlockHandle)
  136. // into an iterator over the contents of the corresponding block.
  137. Iterator* Table::BlockReader(void* arg,
  138. const ReadOptions& options,
  139. const Slice& index_value) {
  140. Table* table = reinterpret_cast<Table*>(arg);
  141. Cache* block_cache = table->rep_->options.block_cache;
  142. Block* block = nullptr;
  143. Cache::Handle* cache_handle = nullptr;
  144. BlockHandle handle;
  145. Slice input = index_value;
  146. Status s = handle.DecodeFrom(&input);
  147. // We intentionally allow extra stuff in index_value so that we
  148. // can add more features in the future.
  149. if (s.ok()) {
  150. BlockContents contents;
  151. if (block_cache != nullptr) {
  152. char cache_key_buffer[16];
  153. EncodeFixed64(cache_key_buffer, table->rep_->cache_id);
  154. EncodeFixed64(cache_key_buffer+8, handle.offset());
  155. Slice key(cache_key_buffer, sizeof(cache_key_buffer));
  156. cache_handle = block_cache->Lookup(key);
  157. if (cache_handle != nullptr) {
  158. block = reinterpret_cast<Block*>(block_cache->Value(cache_handle));
  159. } else {
  160. s = ReadBlock(table->rep_->file, options, handle, &contents);
  161. if (s.ok()) {
  162. block = new Block(contents);
  163. if (contents.cachable && options.fill_cache) {
  164. cache_handle = block_cache->Insert(
  165. key, block, block->size(), &DeleteCachedBlock);
  166. }
  167. }
  168. }
  169. } else {
  170. s = ReadBlock(table->rep_->file, options, handle, &contents);
  171. if (s.ok()) {
  172. block = new Block(contents);
  173. }
  174. }
  175. }
  176. Iterator* iter;
  177. if (block != nullptr) {
  178. iter = block->NewIterator(table->rep_->options.comparator);
  179. if (cache_handle == nullptr) {
  180. iter->RegisterCleanup(&DeleteBlock, block, nullptr);
  181. } else {
  182. iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle);
  183. }
  184. } else {
  185. iter = NewErrorIterator(s);
  186. }
  187. return iter;
  188. }
  189. Iterator* Table::NewIterator(const ReadOptions& options) const {
  190. return NewTwoLevelIterator(
  191. rep_->index_block->NewIterator(rep_->options.comparator),
  192. &Table::BlockReader, const_cast<Table*>(this), options);
  193. }
  194. Status Table::InternalGet(const ReadOptions& options, const Slice& k,
  195. void* arg,
  196. void (*saver)(void*, const Slice&, const Slice&)) {
  197. Status s;
  198. Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator);
  199. iiter->Seek(k);
  200. if (iiter->Valid()) {
  201. Slice handle_value = iiter->value();
  202. FilterBlockReader* filter = rep_->filter;
  203. BlockHandle handle;
  204. if (filter != nullptr &&
  205. handle.DecodeFrom(&handle_value).ok() &&
  206. !filter->KeyMayMatch(handle.offset(), k)) {
  207. // Not found
  208. } else {
  209. Iterator* block_iter = BlockReader(this, options, iiter->value());
  210. block_iter->Seek(k);
  211. if (block_iter->Valid()) {
  212. (*saver)(arg, block_iter->key(), block_iter->value());
  213. }
  214. s = block_iter->status();
  215. delete block_iter;
  216. }
  217. }
  218. if (s.ok()) {
  219. s = iiter->status();
  220. }
  221. delete iiter;
  222. return s;
  223. }
  224. uint64_t Table::ApproximateOffsetOf(const Slice& key) const {
  225. Iterator* index_iter =
  226. rep_->index_block->NewIterator(rep_->options.comparator);
  227. index_iter->Seek(key);
  228. uint64_t result;
  229. if (index_iter->Valid()) {
  230. BlockHandle handle;
  231. Slice input = index_iter->value();
  232. Status s = handle.DecodeFrom(&input);
  233. if (s.ok()) {
  234. result = handle.offset();
  235. } else {
  236. // Strange: we can't decode the block handle in the index block.
  237. // We'll just return the offset of the metaindex block, which is
  238. // close to the whole file size for this case.
  239. result = rep_->metaindex_handle.offset();
  240. }
  241. } else {
  242. // key is past the last key in the file. Approximate the offset
  243. // by returning the offset of the metaindex block (which is
  244. // right near the end of the file).
  245. result = rep_->metaindex_handle.offset();
  246. }
  247. delete index_iter;
  248. return result;
  249. }
  250. } // namespace leveldb