log_writer.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "db/log_writer.h"
  5. #include <cstdint>
  6. #include "leveldb/env.h"
  7. #include "util/coding.h"
  8. #include "util/crc32c.h"
  9. namespace leveldb {
  10. namespace log {
  11. static void InitTypeCrc(uint32_t* type_crc) {
  12. for (int i = 0; i <= kMaxRecordType; i++) {
  13. char t = static_cast<char>(i);
  14. type_crc[i] = crc32c::Value(&t, 1);
  15. }
  16. }
  17. Writer::Writer(WritableFile* dest) : dest_(dest), block_offset_(0) {
  18. InitTypeCrc(type_crc_);
  19. }
  20. Writer::Writer(WritableFile* dest, uint64_t dest_length)
  21. : dest_(dest), block_offset_(dest_length % kBlockSize) {
  22. InitTypeCrc(type_crc_);
  23. }
  24. Writer::~Writer() = default;
  25. Status Writer::AddRecord(const Slice& slice) {
  26. const char* ptr = slice.data();
  27. size_t left = slice.size();
  28. // Fragment the record if necessary and emit it. Note that if slice
  29. // is empty, we still want to iterate once to emit a single
  30. // zero-length record
  31. Status s;
  32. bool begin = true;
  33. do {
  34. const int leftover = kBlockSize - block_offset_;
  35. assert(leftover >= 0);
  36. if (leftover < kHeaderSize) {
  37. // Switch to a new block
  38. if (leftover > 0) {
  39. // Fill the trailer (literal below relies on kHeaderSize being 7)
  40. static_assert(kHeaderSize == 7, "");
  41. dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover));
  42. }
  43. block_offset_ = 0;
  44. }
  45. // Invariant: we never leave < kHeaderSize bytes in a block.
  46. assert(kBlockSize - block_offset_ - kHeaderSize >= 0);
  47. const size_t avail = kBlockSize - block_offset_ - kHeaderSize;
  48. const size_t fragment_length = (left < avail) ? left : avail;
  49. RecordType type;
  50. const bool end = (left == fragment_length);
  51. if (begin && end) {
  52. type = kFullType;
  53. } else if (begin) {
  54. type = kFirstType;
  55. } else if (end) {
  56. type = kLastType;
  57. } else {
  58. type = kMiddleType;
  59. }
  60. s = EmitPhysicalRecord(type, ptr, fragment_length);
  61. ptr += fragment_length;
  62. left -= fragment_length;
  63. begin = false;
  64. } while (s.ok() && left > 0);
  65. return s;
  66. }
  67. Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr,
  68. size_t length) {
  69. assert(length <= 0xffff); // Must fit in two bytes
  70. assert(block_offset_ + kHeaderSize + length <= kBlockSize);
  71. // Format the header
  72. char buf[kHeaderSize];
  73. buf[4] = static_cast<char>(length & 0xff);
  74. buf[5] = static_cast<char>(length >> 8);
  75. buf[6] = static_cast<char>(t);
  76. // Compute the crc of the record type and the payload.
  77. uint32_t crc = crc32c::Extend(type_crc_[t], ptr, length);
  78. crc = crc32c::Mask(crc); // Adjust for storage
  79. EncodeFixed32(buf, crc);
  80. // Write the header and the payload
  81. Status s = dest_->Append(Slice(buf, kHeaderSize));
  82. if (s.ok()) {
  83. s = dest_->Append(Slice(ptr, length));
  84. if (s.ok()) {
  85. s = dest_->Flush();
  86. }
  87. }
  88. block_offset_ += kHeaderSize + length;
  89. return s;
  90. }
  91. } // namespace log
  92. } // namespace leveldb