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.

147 rivejä
3.9 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. //
  5. // WriteBatch::rep_ :=
  6. // sequence: fixed64
  7. // count: fixed32
  8. // data: record[count]
  9. // record :=
  10. // kTypeValue varstring varstring |
  11. // kTypeDeletion varstring
  12. // varstring :=
  13. // len: varint32
  14. // data: uint8[len]
  15. #include "leveldb/write_batch.h"
  16. #include "leveldb/db.h"
  17. #include "db/dbformat.h"
  18. #include "db/memtable.h"
  19. #include "db/write_batch_internal.h"
  20. #include "util/coding.h"
  21. namespace leveldb {
  22. // WriteBatch header has an 8-byte sequence number followed by a 4-byte count.
  23. static const size_t kHeader = 12;
  24. WriteBatch::WriteBatch() {
  25. Clear();
  26. }
  27. WriteBatch::~WriteBatch() { }
  28. WriteBatch::Handler::~Handler() { }
  29. void WriteBatch::Clear() {
  30. rep_.clear();
  31. rep_.resize(kHeader);
  32. }
  33. Status WriteBatch::Iterate(Handler* handler) const {
  34. Slice input(rep_);
  35. if (input.size() < kHeader) {
  36. return Status::Corruption("malformed WriteBatch (too small)");
  37. }
  38. input.remove_prefix(kHeader);
  39. Slice key, value;
  40. int found = 0;
  41. while (!input.empty()) {
  42. found++;
  43. char tag = input[0];
  44. input.remove_prefix(1);
  45. switch (tag) {
  46. case kTypeValue:
  47. if (GetLengthPrefixedSlice(&input, &key) &&
  48. GetLengthPrefixedSlice(&input, &value)) {
  49. handler->Put(key, value);
  50. } else {
  51. return Status::Corruption("bad WriteBatch Put");
  52. }
  53. break;
  54. case kTypeDeletion:
  55. if (GetLengthPrefixedSlice(&input, &key)) {
  56. handler->Delete(key);
  57. } else {
  58. return Status::Corruption("bad WriteBatch Delete");
  59. }
  60. break;
  61. default:
  62. return Status::Corruption("unknown WriteBatch tag");
  63. }
  64. }
  65. if (found != WriteBatchInternal::Count(this)) {
  66. return Status::Corruption("WriteBatch has wrong count");
  67. } else {
  68. return Status::OK();
  69. }
  70. }
  71. int WriteBatchInternal::Count(const WriteBatch* b) {
  72. return DecodeFixed32(b->rep_.data() + 8);
  73. }
  74. void WriteBatchInternal::SetCount(WriteBatch* b, int n) {
  75. EncodeFixed32(&b->rep_[8], n);
  76. }
  77. SequenceNumber WriteBatchInternal::Sequence(const WriteBatch* b) {
  78. return SequenceNumber(DecodeFixed64(b->rep_.data()));
  79. }
  80. void WriteBatchInternal::SetSequence(WriteBatch* b, SequenceNumber seq) {
  81. EncodeFixed64(&b->rep_[0], seq);
  82. }
  83. void WriteBatch::Put(const Slice& key, const Slice& value) {
  84. WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
  85. rep_.push_back(static_cast<char>(kTypeValue));
  86. PutLengthPrefixedSlice(&rep_, key);
  87. PutLengthPrefixedSlice(&rep_, value);
  88. }
  89. void WriteBatch::Delete(const Slice& key) {
  90. WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
  91. rep_.push_back(static_cast<char>(kTypeDeletion));
  92. PutLengthPrefixedSlice(&rep_, key);
  93. }
  94. namespace {
  95. class MemTableInserter : public WriteBatch::Handler {
  96. public:
  97. SequenceNumber sequence_;
  98. MemTable* mem_;
  99. virtual void Put(const Slice& key, const Slice& value) {
  100. mem_->Add(sequence_, kTypeValue, key, value);
  101. sequence_++;
  102. }
  103. virtual void Delete(const Slice& key) {
  104. mem_->Add(sequence_, kTypeDeletion, key, Slice());
  105. sequence_++;
  106. }
  107. };
  108. } // namespace
  109. Status WriteBatchInternal::InsertInto(const WriteBatch* b,
  110. MemTable* memtable) {
  111. MemTableInserter inserter;
  112. inserter.sequence_ = WriteBatchInternal::Sequence(b);
  113. inserter.mem_ = memtable;
  114. return b->Iterate(&inserter);
  115. }
  116. void WriteBatchInternal::SetContents(WriteBatch* b, const Slice& contents) {
  117. assert(contents.size() >= kHeader);
  118. b->rep_.assign(contents.data(), contents.size());
  119. }
  120. void WriteBatchInternal::Append(WriteBatch* dst, const WriteBatch* src) {
  121. SetCount(dst, Count(dst) + Count(src));
  122. assert(src->rep_.size() >= kHeader);
  123. dst->rep_.append(src->rep_.data() + kHeader, src->rep_.size() - kHeader);
  124. }
  125. } // namespace leveldb