小组成员:10215300402-朱维清 & 10222140408 谷杰
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.

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