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

249 righe
8.6 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 anni fa
4 settimane fa
4 settimane fa
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 anni fa
  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. if(internal_key.size() < 8) {
  79. std::cout<<"wrong key:"<<internal_key.ToString()<<std::endl;
  80. }
  81. assert(internal_key.size() >= 8);
  82. uint64_t num = DecodeFixed64(internal_key.data() + internal_key.size() - 8);
  83. uint8_t havettl = (num & 0b10) >> 1;
  84. uint8_t islookup = (num & 0b100) >> 2;
  85. size_t klen = internal_key.size() - 8;
  86. if(havettl || islookup) klen -= 8;
  87. Slice user_key = Slice(internal_key.data(), klen);
  88. return user_key;
  89. }
  90. // A comparator for internal keys that uses a specified comparator for
  91. // the user key portion and breaks ties by decreasing sequence number.
  92. class InternalKeyComparator : public Comparator {
  93. private:
  94. const Comparator* user_comparator_;
  95. public:
  96. explicit InternalKeyComparator(const Comparator* c) : user_comparator_(c) {}
  97. const char* Name() const override;
  98. int Compare(const Slice& a, const Slice& b) const override;
  99. void FindShortestSeparator(std::string* start,
  100. const Slice& limit) const override;
  101. void FindShortSuccessor(std::string* key) const override;
  102. const Comparator* user_comparator() const { return user_comparator_; }
  103. int Compare(const InternalKey& a, const InternalKey& b) const;
  104. };
  105. // Filter policy wrapper that converts from internal keys to user keys
  106. class InternalFilterPolicy : public FilterPolicy {
  107. private:
  108. const FilterPolicy* const user_policy_;
  109. public:
  110. explicit InternalFilterPolicy(const FilterPolicy* p) : user_policy_(p) {}
  111. const char* Name() const override;
  112. void CreateFilter(const Slice* keys, int n, std::string* dst) const override;
  113. bool KeyMayMatch(const Slice& key, const Slice& filter) const override;
  114. };
  115. // Modules in this directory should keep internal keys wrapped inside
  116. // the following class instead of plain strings so that we do not
  117. // incorrectly use string comparisons instead of an InternalKeyComparator.
  118. class InternalKey {
  119. private:
  120. std::string rep_;
  121. public:
  122. InternalKey() {} // Leave rep_ as empty to indicate it is invalid
  123. InternalKey(const Slice& user_key, SequenceNumber s,
  124. ValueType t, uint64_t deadTime = 0) {
  125. AppendInternalKey(&rep_, ParsedInternalKey(user_key, s, t, deadTime));
  126. }
  127. bool DecodeFrom(const Slice& s) {
  128. rep_.assign(s.data(), s.size());
  129. return !rep_.empty();
  130. }
  131. Slice Encode() const {
  132. assert(!rep_.empty());
  133. return rep_;
  134. }
  135. Slice user_key() const { return ExtractUserKey(rep_); }
  136. void SetFrom(const ParsedInternalKey& p) {
  137. rep_.clear();
  138. AppendInternalKey(&rep_, p);
  139. }
  140. void Clear() { rep_.clear(); }
  141. std::string DebugString() const;
  142. };
  143. inline int InternalKeyComparator::Compare(const InternalKey& a,
  144. const InternalKey& b) const {
  145. return Compare(a.Encode(), b.Encode());
  146. }
  147. inline bool ParseInternalKey(const Slice& internal_key,
  148. ParsedInternalKey* result) {
  149. //不确定需不需要标识islookup,先没改
  150. const size_t n = internal_key.size();
  151. if (n < 8) return false;
  152. uint64_t tag = DecodeFixed64(internal_key.data() + n - 8);
  153. uint8_t c = tag & 0xff;
  154. uint8_t havettl = (c & 0b10) >> 1;
  155. result->sequence = tag >> 8;
  156. result->type = static_cast<ValueType>(c & 0b1);
  157. if(havettl){
  158. result->deadTime = DecodeFixed64(internal_key.data() + n - 16);
  159. result->user_key = Slice(internal_key.data(), n - 16);
  160. } else {
  161. result->deadTime = 0;
  162. result->user_key = Slice(internal_key.data(), n - 8);
  163. }
  164. // return c <= 0b111;
  165. return ((c & 0b1) <= static_cast<uint8_t>(kTypeValue));
  166. }
  167. // A helper class useful for DBImpl::Get()
  168. class LookupKey {
  169. public:
  170. // Initialize *this for looking up user_key at a snapshot with
  171. // the specified sequence number.
  172. LookupKey(const Slice& user_key, SequenceNumber sequence, uint64_t nowTime);
  173. LookupKey(const LookupKey&) = delete;
  174. LookupKey& operator=(const LookupKey&) = delete;
  175. ~LookupKey();
  176. // Return a key suitable for lookup in a MemTable.
  177. Slice memtable_key() const { return Slice(start_, end_ - start_); }
  178. // Return an internal key (suitable for passing to an internal iterator)
  179. Slice internal_key() const { return Slice(kstart_, end_ - kstart_); }
  180. // Return the user key
  181. Slice user_key() const { return Slice(kstart_, end_ - kstart_ - 16); }
  182. private:
  183. // We construct a char array of the form:
  184. // klength varint32 <-- start_
  185. // userkey char[klength] <-- kstart_
  186. // nowTime uint64
  187. // tag uint64 最后一个字节为0000 0101
  188. // <-- end_
  189. // 同userkey下,原本(insert时)的比较器规则为seq优先,不考虑时间
  190. // 新增标识位(tag倒数第三位),使比较器考虑时间
  191. // The array is a suitable MemTable key.
  192. // The suffix starting with "userkey" can be used as an InternalKey.
  193. const char* start_;
  194. const char* kstart_;
  195. const char* end_;
  196. char space_[200]; // Avoid allocation for short keys
  197. };
  198. inline LookupKey::~LookupKey() {
  199. if (start_ != space_) delete[] start_;
  200. }
  201. } // namespace leveldb
  202. #endif // STORAGE_LEVELDB_DB_DBFORMAT_H_