Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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