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.

102 lines
2.7 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 (literal below relies on kHeaderSize being 7)
  35. assert(kHeaderSize == 7);
  36. dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover));
  37. }
  38. block_offset_ = 0;
  39. }
  40. // Invariant: we never leave < kHeaderSize bytes in a block.
  41. const int avail = kBlockSize - block_offset_ - kHeaderSize;
  42. assert(avail >= 0);
  43. const size_t fragment_length = (left < avail) ? left : avail;
  44. RecordType type;
  45. const bool begin = (ptr == slice.data());
  46. const bool end = (left == fragment_length);
  47. if (begin && end) {
  48. type = kFullType;
  49. } else if (begin) {
  50. type = kFirstType;
  51. } else if (end) {
  52. type = kLastType;
  53. } else {
  54. type = kMiddleType;
  55. }
  56. s = EmitPhysicalRecord(type, ptr, fragment_length);
  57. ptr += fragment_length;
  58. left -= fragment_length;
  59. } while (s.ok() && left > 0);
  60. return s;
  61. }
  62. Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr, size_t n) {
  63. assert(n <= 0xffff); // Must fit in two bytes
  64. assert(block_offset_ + kHeaderSize + n <= kBlockSize);
  65. // Format the header
  66. char buf[kHeaderSize];
  67. buf[4] = static_cast<char>(n & 0xff);
  68. buf[5] = static_cast<char>(n >> 8);
  69. buf[6] = static_cast<char>(t);
  70. // Compute the crc of the record type and the payload.
  71. uint32_t crc = crc32c::Extend(type_crc_[t], ptr, n);
  72. crc = crc32c::Mask(crc); // Adjust for storage
  73. EncodeFixed32(buf, crc);
  74. // Write the header and the payload
  75. Status s = dest_->Append(Slice(buf, kHeaderSize));
  76. if (s.ok()) {
  77. s = dest_->Append(Slice(ptr, n));
  78. if (s.ok()) {
  79. s = dest_->Flush();
  80. }
  81. }
  82. block_offset_ += kHeaderSize + n;
  83. return s;
  84. }
  85. }
  86. }