2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "db/log_writer.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-05-02 11:01:00 -07:00
|
|
|
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/env.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/crc32c.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
namespace log {
|
|
|
|
|
2014-12-11 08:13:18 -08:00
|
|
|
static void InitTypeCrc(uint32_t* type_crc) {
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i <= kMaxRecordType; i++) {
|
|
|
|
char t = static_cast<char>(i);
|
2014-12-11 08:13:18 -08:00
|
|
|
type_crc[i] = crc32c::Value(&t, 1);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Writer::Writer(WritableFile* dest) : dest_(dest), block_offset_(0) {
|
2014-12-11 08:13:18 -08:00
|
|
|
InitTypeCrc(type_crc_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Writer::Writer(WritableFile* dest, uint64_t dest_length)
|
|
|
|
: dest_(dest), block_offset_(dest_length % kBlockSize) {
|
|
|
|
InitTypeCrc(type_crc_);
|
|
|
|
}
|
|
|
|
|
2019-05-04 17:40:21 -07:00
|
|
|
Writer::~Writer() = default;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
Status Writer::AddRecord(const Slice& slice) {
|
|
|
|
const char* ptr = slice.data();
|
|
|
|
size_t left = slice.size();
|
|
|
|
|
|
|
|
// Fragment the record if necessary and emit it. Note that if slice
|
|
|
|
// is empty, we still want to iterate once to emit a single
|
|
|
|
// zero-length record
|
|
|
|
Status s;
|
2011-05-21 02:17:43 +00:00
|
|
|
bool begin = true;
|
2011-03-18 22:37:00 +00:00
|
|
|
do {
|
|
|
|
const int leftover = kBlockSize - block_offset_;
|
|
|
|
assert(leftover >= 0);
|
2011-03-28 20:43:44 +00:00
|
|
|
if (leftover < kHeaderSize) {
|
2011-03-18 22:37:00 +00:00
|
|
|
// Switch to a new block
|
|
|
|
if (leftover > 0) {
|
2011-03-28 20:43:44 +00:00
|
|
|
// Fill the trailer (literal below relies on kHeaderSize being 7)
|
2019-05-02 11:01:00 -07:00
|
|
|
static_assert(kHeaderSize == 7, "");
|
2011-03-28 20:43:44 +00:00
|
|
|
dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
block_offset_ = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-28 20:43:44 +00:00
|
|
|
// Invariant: we never leave < kHeaderSize bytes in a block.
|
2011-04-20 22:48:11 +00:00
|
|
|
assert(kBlockSize - block_offset_ - kHeaderSize >= 0);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-04-20 22:48:11 +00:00
|
|
|
const size_t avail = kBlockSize - block_offset_ - kHeaderSize;
|
2011-03-18 22:37:00 +00:00
|
|
|
const size_t fragment_length = (left < avail) ? left : avail;
|
|
|
|
|
|
|
|
RecordType type;
|
|
|
|
const bool end = (left == fragment_length);
|
|
|
|
if (begin && end) {
|
|
|
|
type = kFullType;
|
|
|
|
} else if (begin) {
|
|
|
|
type = kFirstType;
|
|
|
|
} else if (end) {
|
|
|
|
type = kLastType;
|
|
|
|
} else {
|
|
|
|
type = kMiddleType;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = EmitPhysicalRecord(type, ptr, fragment_length);
|
|
|
|
ptr += fragment_length;
|
|
|
|
left -= fragment_length;
|
2011-05-21 02:17:43 +00:00
|
|
|
begin = false;
|
2011-03-18 22:37:00 +00:00
|
|
|
} while (s.ok() && left > 0);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr,
|
|
|
|
size_t length) {
|
|
|
|
assert(length <= 0xffff); // Must fit in two bytes
|
|
|
|
assert(block_offset_ + kHeaderSize + length <= kBlockSize);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Format the header
|
|
|
|
char buf[kHeaderSize];
|
2019-05-02 11:01:00 -07:00
|
|
|
buf[4] = static_cast<char>(length & 0xff);
|
|
|
|
buf[5] = static_cast<char>(length >> 8);
|
2011-03-18 22:37:00 +00:00
|
|
|
buf[6] = static_cast<char>(t);
|
|
|
|
|
|
|
|
// Compute the crc of the record type and the payload.
|
2019-05-02 11:01:00 -07:00
|
|
|
uint32_t crc = crc32c::Extend(type_crc_[t], ptr, length);
|
|
|
|
crc = crc32c::Mask(crc); // Adjust for storage
|
2011-03-18 22:37:00 +00:00
|
|
|
EncodeFixed32(buf, crc);
|
|
|
|
|
|
|
|
// Write the header and the payload
|
|
|
|
Status s = dest_->Append(Slice(buf, kHeaderSize));
|
|
|
|
if (s.ok()) {
|
2019-05-02 11:01:00 -07:00
|
|
|
s = dest_->Append(Slice(ptr, length));
|
2011-03-18 22:37:00 +00:00
|
|
|
if (s.ok()) {
|
|
|
|
s = dest_->Flush();
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 11:01:00 -07:00
|
|
|
block_offset_ += kHeaderSize + length;
|
2011-03-18 22:37:00 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace log
|
|
|
|
} // namespace leveldb
|