10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

241 lines
8.1 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)
10 years ago
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)
10 years ago
  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. #ifndef STORAGE_LEVELDB_DB_DBFORMAT_H_
  5. #define STORAGE_LEVELDB_DB_DBFORMAT_H_
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <string>
  9. #include "leveldb/comparator.h"
  10. #include "leveldb/db.h"
  11. #include "leveldb/filter_policy.h"
  12. #include "leveldb/slice.h"
  13. #include "leveldb/table_builder.h"
  14. #include "util/coding.h"
  15. #include "util/logging.h"
  16. #include "iostream"
  17. namespace leveldb {
  18. // Grouping of constants. We may want to make some of these
  19. // parameters set via options.
  20. namespace config {
  21. static const int kNumLevels = 7;
  22. // Level-0 compaction is started when we hit this many files.
  23. static const int kL0_CompactionTrigger = 4;
  24. // Soft limit on number of level-0 files. We slow down writes at this point.
  25. static const int kL0_SlowdownWritesTrigger = 8;
  26. // Maximum number of level-0 files. We stop writes at this point.
  27. static const int kL0_StopWritesTrigger = 12;
  28. // Maximum level to which a new compacted memtable is pushed if it
  29. // does not create overlap. We try to push to level 2 to avoid the
  30. // relatively expensive level 0=>1 compactions and to avoid some
  31. // expensive manifest file operations. We do not push all the way to
  32. // the largest level since that can generate a lot of wasted disk
  33. // space if the same key space is being repeatedly overwritten.
  34. static const int kMaxMemCompactLevel = 2;
  35. // Approximate gap in bytes between samples of data read during iteration.
  36. static const int kReadBytesPeriod = 1048576;
  37. } // namespace config
  38. class InternalKey;
  39. // Value types encoded as the last component of internal keys.
  40. // DO NOT CHANGE THESE ENUM VALUES: they are embedded in the on-disk
  41. // data structures.
  42. enum ValueType { kTypeDeletion = 0x0, kTypeValue = 0x1 };
  43. // kValueTypeForSeek defines the ValueType that should be passed when
  44. // constructing a ParsedInternalKey object for seeking to a particular
  45. // sequence number (since we sort sequence numbers in decreasing order
  46. // and the value type is embedded as the low 8 bits in the sequence
  47. // number in internal keys, we need to use the highest-numbered
  48. // ValueType, not the lowest).
  49. static const ValueType kValueTypeForSeek = kTypeValue;
  50. typedef uint64_t SequenceNumber;
  51. // We leave eight bits empty at the bottom so a type and sequence#
  52. // can be packed together into 64-bits.
  53. static const SequenceNumber kMaxSequenceNumber = ((0x1ull << 56) - 1);
  54. struct ParsedInternalKey {
  55. Slice user_key;
  56. SequenceNumber sequence;
  57. uint64_t deadTime;
  58. ValueType type;
  59. ParsedInternalKey() {} // Intentionally left uninitialized (for speed)
  60. ParsedInternalKey(const Slice& u, const SequenceNumber& seq,
  61. ValueType t, uint64_t d = 0)
  62. : user_key(u), sequence(seq), type(t), deadTime(d) {}
  63. std::string DebugString() const;
  64. };
  65. // Return the length of the encoding of "key".
  66. inline size_t InternalKeyEncodingLength(const ParsedInternalKey& key) {
  67. return key.user_key.size() + 8 + (key.deadTime != 0) * 8;
  68. }
  69. // Append the serialization of "key" to *result.
  70. void AppendInternalKey(std::string* result, const ParsedInternalKey& key);
  71. // Attempt to parse an internal key from "internal_key". On success,
  72. // stores the parsed data in "*result", and returns true.
  73. //
  74. // On error, returns false, leaves "*result" in an undefined state.
  75. bool ParseInternalKey(const Slice& internal_key, ParsedInternalKey* result);
  76. // Returns the user key portion of an internal key.
  77. inline Slice ExtractUserKey(const Slice& internal_key) {
  78. assert(internal_key.size() >= 8);
  79. uint64_t num = DecodeFixed64(internal_key.data() + internal_key.size() - 8);
  80. uint8_t havettl = (num & 0xff) >> 1;
  81. size_t klen = internal_key.size() - 8;
  82. if(havettl) klen -= 8;
  83. Slice user_key = Slice(internal_key.data(), klen);
  84. return user_key;
  85. }
  86. // A comparator for internal keys that uses a specified comparator for
  87. // the user key portion and breaks ties by decreasing sequence number.
  88. class InternalKeyComparator : public Comparator {
  89. private:
  90. const Comparator* user_comparator_;
  91. public:
  92. explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) {}
  93. const char* Name() const override;
  94. int Compare(const Slice& a, const Slice& b) const override;
  95. void FindShortestSeparator(std::string* start,
  96. const Slice& limit) const override;
  97. void FindShortSuccessor(std::string* key) const override;
  98. const Comparator* user_comparator() const { return user_comparator_; }
  99. int Compare(const InternalKey& a, const InternalKey& b) const;
  100. };
  101. // Filter policy wrapper that converts from internal keys to user keys
  102. class InternalFilterPolicy : public FilterPolicy {
  103. private:
  104. const FilterPolicy* const user_policy_;
  105. public:
  106. explicit InternalFilterPolicy(const FilterPolicy* p) : user_policy_(p) {}
  107. const char* Name() const override;
  108. void CreateFilter(const Slice* keys, int n, std::string* dst) const override;
  109. bool KeyMayMatch(const Slice& key, const Slice& filter) const override;
  110. };
  111. // Modules in this directory should keep internal keys wrapped inside
  112. // the following class instead of plain strings so that we do not
  113. // incorrectly use string comparisons instead of an InternalKeyComparator.
  114. class InternalKey {
  115. private:
  116. std::string rep_;
  117. public:
  118. InternalKey() {} // Leave rep_ as empty to indicate it is invalid
  119. InternalKey(const Slice& user_key, SequenceNumber s,
  120. ValueType t, uint64_t deadTime = 0) {
  121. AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t, deadTime));
  122. }
  123. bool DecodeFrom(const Slice& s) {
  124. rep_.assign(s.data(), s.size());
  125. return !rep_.empty();
  126. }
  127. Slice Encode() const {
  128. assert(!rep_.empty());
  129. return rep_;
  130. }
  131. Slice user_key() const { return ExtractUserKey(rep_); }
  132. void SetFrom(const ParsedInternalKey& p) {
  133. rep_.clear();
  134. AppendInternalKey(&rep_, p);
  135. }
  136. void Clear() { rep_.clear(); }
  137. std::string DebugString() const;
  138. };
  139. inline int InternalKeyComparator::Compare(const InternalKey& a,
  140. const InternalKey& b) const {
  141. return Compare(a.Encode(), b.Encode());
  142. }
  143. inline bool ParseInternalKey(const Slice& internal_key,
  144. ParsedInternalKey* result) {
  145. const size_t n = internal_key.size();
  146. if (n < 8) return false;
  147. uint64_t tag = DecodeFixed64(internal_key.data() + n - 8);
  148. uint8_t c = tag & 0xff;
  149. uint8_t havettl = c >> 1;
  150. assert(havettl <= 0b1);
  151. result->sequence = tag >> 8;
  152. result->type = static_cast<ValueType>(c);
  153. if(havettl){
  154. result->deadTime = DecodeFixed64(internal_key.data() + n - 16);
  155. result->user_key = Slice(internal_key.data(), n - 16);
  156. } else {
  157. result->deadTime = 0;
  158. result->user_key = Slice(internal_key.data(), n - 8);
  159. }
  160. return (c <= static_cast<uint8_t>(kTypeValue));
  161. }
  162. // A helper class useful for DBImpl::Get()
  163. class LookupKey {
  164. public:
  165. // Initialize *this for looking up user_key at a snapshot with
  166. // the specified sequence number.
  167. LookupKey(const Slice& user_key, SequenceNumber sequence);
  168. LookupKey(const LookupKey&) = delete;
  169. LookupKey& operator=(const LookupKey&) = delete;
  170. ~LookupKey();
  171. // Return a key suitable for lookup in a MemTable.
  172. Slice memtable_key() const { return Slice(start_, end_ - start_); }
  173. // Return an internal key (suitable for passing to an internal iterator)
  174. Slice internal_key() const { return Slice(kstart_, end_ - kstart_); }
  175. // Return the user key
  176. Slice user_key() const { return Slice(kstart_, end_ - kstart_ - 8); }
  177. private:
  178. // We construct a char array of the form:
  179. // klength varint32 <-- start_
  180. // userkey char[klength] <-- kstart_
  181. // tag uint64
  182. // <-- end_
  183. // The array is a suitable MemTable key.
  184. // The suffix starting with "userkey" can be used as an InternalKey.
  185. const char* start_;
  186. const char* kstart_;
  187. const char* end_;
  188. char space_[200]; // Avoid allocation for short keys
  189. };
  190. inline LookupKey::~LookupKey() {
  191. if (start_ != space_) delete[] start_;
  192. }
  193. } // namespace leveldb
  194. #endif // STORAGE_LEVELDB_DB_DBFORMAT_H_