作者: 谢瑞阳 10225101483 徐翔宇 10225101535
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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