作者: 韩晨旭 10225101440 李畅 10225102463
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.

152 lines
4.7 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 <stdio.h>
  5. #include "db/dbformat.h"
  6. #include "port/port.h"
  7. #include "util/coding.h"
  8. namespace leveldb {
  9. static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {
  10. assert(seq <= kMaxSequenceNumber);
  11. assert(t <= kValueTypeForSeek);
  12. return (seq << 8) | t;
  13. }
  14. void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
  15. result->append(key.user_key.data(), key.user_key.size());
  16. PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
  17. }
  18. std::string ParsedInternalKey::DebugString() const {
  19. char buf[50];
  20. snprintf(buf, sizeof(buf), "' @ %llu : %d",
  21. (unsigned long long) sequence,
  22. int(type));
  23. std::string result = "'";
  24. result += user_key.ToString();
  25. result += buf;
  26. return result;
  27. }
  28. const char* InternalKeyComparator::Name() const {
  29. return "leveldb.InternalKeyComparator";
  30. }
  31. int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {
  32. // Order by:
  33. // increasing user key (according to user-supplied comparator)
  34. // decreasing sequence number
  35. // decreasing type (though sequence# should be enough to disambiguate)
  36. int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
  37. if (r == 0) {
  38. const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);
  39. const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);
  40. if (anum > bnum) {
  41. r = -1;
  42. } else if (anum < bnum) {
  43. r = +1;
  44. }
  45. }
  46. return r;
  47. }
  48. void InternalKeyComparator::FindShortestSeparator(
  49. std::string* start,
  50. const Slice& limit) const {
  51. // Attempt to shorten the user portion of the key
  52. Slice user_start = ExtractUserKey(*start);
  53. Slice user_limit = ExtractUserKey(limit);
  54. std::string tmp(user_start.data(), user_start.size());
  55. user_comparator_->FindShortestSeparator(&tmp, user_limit);
  56. if (user_comparator_->Compare(*start, tmp) < 0) {
  57. // User key has become larger. Tack on the earliest possible
  58. // number to the shortened user key.
  59. PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
  60. assert(this->Compare(*start, tmp) < 0);
  61. assert(this->Compare(tmp, limit) < 0);
  62. start->swap(tmp);
  63. }
  64. }
  65. void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
  66. Slice user_key = ExtractUserKey(*key);
  67. std::string tmp(user_key.data(), user_key.size());
  68. user_comparator_->FindShortSuccessor(&tmp);
  69. if (user_comparator_->Compare(user_key, tmp) < 0) {
  70. // User key has become larger. Tack on the earliest possible
  71. // number to the shortened user key.
  72. PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
  73. assert(this->Compare(*key, tmp) < 0);
  74. key->swap(tmp);
  75. }
  76. }
  77. LargeValueRef LargeValueRef::Make(const Slice& value, CompressionType ctype) {
  78. LargeValueRef result;
  79. port::SHA1_Hash(value.data(), value.size(), &result.data[0]);
  80. EncodeFixed64(&result.data[20], value.size());
  81. result.data[28] = static_cast<unsigned char>(ctype);
  82. return result;
  83. }
  84. std::string LargeValueRefToFilenameString(const LargeValueRef& h) {
  85. assert(sizeof(h.data) == LargeValueRef::ByteSize());
  86. assert(sizeof(h.data) == 29); // So we can hardcode the array size of buf
  87. static const char tohex[] = "0123456789abcdef";
  88. char buf[20*2];
  89. for (int i = 0; i < 20; i++) {
  90. buf[2*i] = tohex[(h.data[i] >> 4) & 0xf];
  91. buf[2*i+1] = tohex[h.data[i] & 0xf];
  92. }
  93. std::string result = std::string(buf, sizeof(buf));
  94. result += "-";
  95. result += NumberToString(h.ValueSize());
  96. result += "-";
  97. result += NumberToString(static_cast<uint64_t>(h.compression_type()));
  98. return result;
  99. }
  100. static uint32_t hexvalue(char c) {
  101. if (c >= '0' && c <= '9') {
  102. return c - '0';
  103. } else if (c >= 'A' && c <= 'F') {
  104. return 10 + c - 'A';
  105. } else {
  106. assert(c >= 'a' && c <= 'f');
  107. return 10 + c - 'a';
  108. }
  109. }
  110. bool FilenameStringToLargeValueRef(const Slice& s, LargeValueRef* h) {
  111. Slice in = s;
  112. if (in.size() < 40) {
  113. return false;
  114. }
  115. for (int i = 0; i < 20; i++) {
  116. if (!isxdigit(in[i*2]) || !isxdigit(in[i*2+1])) {
  117. return false;
  118. }
  119. unsigned char c = (hexvalue(in[i*2])<<4) | hexvalue(in[i*2+1]);
  120. h->data[i] = c;
  121. }
  122. in.remove_prefix(40);
  123. uint64_t value_size, ctype;
  124. if (ConsumeChar(&in, '-') &&
  125. ConsumeDecimalNumber(&in, &value_size) &&
  126. ConsumeChar(&in, '-') &&
  127. ConsumeDecimalNumber(&in, &ctype) &&
  128. in.empty() &&
  129. (ctype <= kSnappyCompression)) {
  130. EncodeFixed64(&h->data[20], value_size);
  131. h->data[28] = static_cast<unsigned char>(ctype);
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137. }