作者: 韩晨旭 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.

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