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.

151 lines
4.0 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. size_t WriteBatch::ApproximateSize() {
  34. return rep_.size();
  35. }
  36. Status WriteBatch::Iterate(Handler* handler) const {
  37. Slice input(rep_);
  38. if (input.size() < kHeader) {
  39. return Status::Corruption("malformed WriteBatch (too small)");
  40. }
  41. input.remove_prefix(kHeader);
  42. Slice key, value;
  43. int found = 0;
  44. while (!input.empty()) {
  45. found++;
  46. char tag = input[0];
  47. input.remove_prefix(1);
  48. switch (tag) {
  49. case kTypeValue:
  50. if (GetLengthPrefixedSlice(&input, &key) &&
  51. GetLengthPrefixedSlice(&input, &value)) {
  52. handler->Put(key, value);
  53. } else {
  54. return Status::Corruption("bad WriteBatch Put");
  55. }
  56. break;
  57. case kTypeDeletion:
  58. if (GetLengthPrefixedSlice(&input, &key)) {
  59. handler->Delete(key);
  60. } else {
  61. return Status::Corruption("bad WriteBatch Delete");
  62. }
  63. break;
  64. default:
  65. return Status::Corruption("unknown WriteBatch tag");
  66. }
  67. }
  68. if (found != WriteBatchInternal::Count(this)) {
  69. return Status::Corruption("WriteBatch has wrong count");
  70. } else {
  71. return Status::OK();
  72. }
  73. }
  74. int WriteBatchInternal::Count(const WriteBatch* b) {
  75. return DecodeFixed32(b->rep_.data() + 8);
  76. }
  77. void WriteBatchInternal::SetCount(WriteBatch* b, int n) {
  78. EncodeFixed32(&b->rep_[8], n);
  79. }
  80. SequenceNumber WriteBatchInternal::Sequence(const WriteBatch* b) {
  81. return SequenceNumber(DecodeFixed64(b->rep_.data()));
  82. }
  83. void WriteBatchInternal::SetSequence(WriteBatch* b, SequenceNumber seq) {
  84. EncodeFixed64(&b->rep_[0], seq);
  85. }
  86. void WriteBatch::Put(const Slice& key, const Slice& value) {
  87. WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
  88. rep_.push_back(static_cast<char>(kTypeValue));
  89. PutLengthPrefixedSlice(&rep_, key);
  90. PutLengthPrefixedSlice(&rep_, value);
  91. }
  92. void WriteBatch::Delete(const Slice& key) {
  93. WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
  94. rep_.push_back(static_cast<char>(kTypeDeletion));
  95. PutLengthPrefixedSlice(&rep_, key);
  96. }
  97. namespace {
  98. class MemTableInserter : public WriteBatch::Handler {
  99. public:
  100. SequenceNumber sequence_;
  101. MemTable* mem_;
  102. virtual void Put(const Slice& key, const Slice& value) {
  103. mem_->Add(sequence_, kTypeValue, key, value);
  104. sequence_++;
  105. }
  106. virtual void Delete(const Slice& key) {
  107. mem_->Add(sequence_, kTypeDeletion, key, Slice());
  108. sequence_++;
  109. }
  110. };
  111. } // namespace
  112. Status WriteBatchInternal::InsertInto(const WriteBatch* b,
  113. MemTable* memtable) {
  114. MemTableInserter inserter;
  115. inserter.sequence_ = WriteBatchInternal::Sequence(b);
  116. inserter.mem_ = memtable;
  117. return b->Iterate(&inserter);
  118. }
  119. void WriteBatchInternal::SetContents(WriteBatch* b, const Slice& contents) {
  120. assert(contents.size() >= kHeader);
  121. b->rep_.assign(contents.data(), contents.size());
  122. }
  123. void WriteBatchInternal::Append(WriteBatch* dst, const WriteBatch* src) {
  124. SetCount(dst, Count(dst) + Count(src));
  125. assert(src->rep_.size() >= kHeader);
  126. dst->rep_.append(src->rep_.data() + kHeader, src->rep_.size() - kHeader);
  127. }
  128. } // namespace leveldb