table.cc 8.2 KB

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