提供基本的ttl测试用例
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
8.0 KiB

  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 = NULL;
  36. if (size < Footer::kEncodedLength) {
  37. return Status::InvalidArgument("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 contents;
  49. Block* index_block = NULL;
  50. if (s.ok()) {
  51. s = ReadBlock(file, ReadOptions(), footer.index_handle(), &contents);
  52. if (s.ok()) {
  53. index_block = new Block(contents);
  54. }
  55. }
  56. if (s.ok()) {
  57. // We've successfully read the footer and the index block: we're
  58. // ready to serve requests.
  59. Rep* rep = new Table::Rep;
  60. rep->options = options;
  61. rep->file = file;
  62. rep->metaindex_handle = footer.metaindex_handle();
  63. rep->index_block = index_block;
  64. rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0);
  65. rep->filter_data = NULL;
  66. rep->filter = NULL;
  67. *table = new Table(rep);
  68. (*table)->ReadMeta(footer);
  69. } else {
  70. if (index_block) delete index_block;
  71. }
  72. return s;
  73. }
  74. void Table::ReadMeta(const Footer& footer) {
  75. if (rep_->options.filter_policy == NULL) {
  76. return; // Do not need any metadata
  77. }
  78. // TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
  79. // it is an empty block.
  80. ReadOptions opt;
  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. BlockContents block;
  107. if (!ReadBlock(rep_->file, opt, filter_handle, &block).ok()) {
  108. return;
  109. }
  110. if (block.heap_allocated) {
  111. rep_->filter_data = block.data.data(); // Will need to delete later
  112. }
  113. rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
  114. }
  115. Table::~Table() {
  116. delete rep_;
  117. }
  118. static void DeleteBlock(void* arg, void* ignored) {
  119. delete reinterpret_cast<Block*>(arg);
  120. }
  121. static void DeleteCachedBlock(const Slice& key, void* value) {
  122. Block* block = reinterpret_cast<Block*>(value);
  123. delete block;
  124. }
  125. static void ReleaseBlock(void* arg, void* h) {
  126. Cache* cache = reinterpret_cast<Cache*>(arg);
  127. Cache::Handle* handle = reinterpret_cast<Cache::Handle*>(h);
  128. cache->Release(handle);
  129. }
  130. // Convert an index iterator value (i.e., an encoded BlockHandle)
  131. // into an iterator over the contents of the corresponding block.
  132. Iterator* Table::BlockReader(void* arg,
  133. 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 = NULL;
  138. Cache::Handle* cache_handle = NULL;
  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 != NULL) {
  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 != NULL) {
  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(
  160. key, block, block->size(), &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 != NULL) {
  173. iter = block->NewIterator(table->rep_->options.comparator);
  174. if (cache_handle == NULL) {
  175. iter->RegisterCleanup(&DeleteBlock, block, NULL);
  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,
  190. void* arg,
  191. void (*saver)(void*, const Slice&, 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 != NULL &&
  200. handle.DecodeFrom(&handle_value).ok() &&
  201. !filter->KeyMayMatch(handle.offset(), k)) {
  202. // Not found
  203. } else {
  204. Iterator* block_iter = BlockReader(this, options, iiter->value());
  205. block_iter->Seek(k);
  206. if (block_iter->Valid()) {
  207. (*saver)(arg, block_iter->key(), block_iter->value());
  208. }
  209. s = block_iter->status();
  210. delete block_iter;
  211. }
  212. }
  213. if (s.ok()) {
  214. s = iiter->status();
  215. }
  216. delete iiter;
  217. return s;
  218. }
  219. uint64_t Table::ApproximateOffsetOf(const Slice& key) const {
  220. Iterator* index_iter =
  221. rep_->index_block->NewIterator(rep_->options.comparator);
  222. index_iter->Seek(key);
  223. uint64_t result;
  224. if (index_iter->Valid()) {
  225. BlockHandle handle;
  226. Slice input = index_iter->value();
  227. Status s = handle.DecodeFrom(&input);
  228. if (s.ok()) {
  229. result = handle.offset();
  230. } else {
  231. // Strange: we can't decode the block handle in the index block.
  232. // We'll just return the offset of the metaindex block, which is
  233. // close to the whole file size for this case.
  234. result = rep_->metaindex_handle.offset();
  235. }
  236. } else {
  237. // key is past the last key in the file. Approximate the offset
  238. // by returning the offset of the metaindex block (which is
  239. // right near the end of the file).
  240. result = rep_->metaindex_handle.offset();
  241. }
  242. delete index_iter;
  243. return result;
  244. }
  245. } // namespace leveldb