小组成员:10215300402-朱维清 & 10222140408 谷杰
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

280 rindas
8.2 KiB

Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
pirms 10 gadiem
Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
pirms 10 gadiem
Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
pirms 10 gadiem
Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
pirms 10 gadiem
  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::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 = NULL;
  67. rep->filter = NULL;
  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 == NULL) {
  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 = NULL;
  143. Cache::Handle* cache_handle = NULL;
  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 != NULL) {
  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 != NULL) {
  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 != NULL) {
  178. iter = block->NewIterator(table->rep_->options.comparator);
  179. if (cache_handle == NULL) {
  180. iter->RegisterCleanup(&DeleteBlock, block, NULL);
  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 != NULL &&
  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