fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
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.
140 lines
4.0 KiB
140 lines
4.0 KiB
8 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
8 years ago
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
5 years ago
|
#include "db/blob/blob_log_writer.h"
|
||
8 years ago
|
|
||
|
#include <cstdint>
|
||
|
#include <string>
|
||
7 years ago
|
|
||
5 years ago
|
#include "db/blob/blob_log_format.h"
|
||
5 years ago
|
#include "file/writable_file_writer.h"
|
||
7 years ago
|
#include "monitoring/statistics.h"
|
||
8 years ago
|
#include "rocksdb/env.h"
|
||
|
#include "util/coding.h"
|
||
7 years ago
|
#include "util/stop_watch.h"
|
||
8 years ago
|
|
||
5 years ago
|
namespace ROCKSDB_NAMESPACE {
|
||
8 years ago
|
namespace blob_db {
|
||
|
|
||
6 years ago
|
Writer::Writer(std::unique_ptr<WritableFileWriter>&& dest, Env* env,
|
||
7 years ago
|
Statistics* statistics, uint64_t log_number, uint64_t bpsync,
|
||
|
bool use_fs, uint64_t boffset)
|
||
8 years ago
|
: dest_(std::move(dest)),
|
||
7 years ago
|
env_(env),
|
||
|
statistics_(statistics),
|
||
8 years ago
|
log_number_(log_number),
|
||
|
block_offset_(boffset),
|
||
|
bytes_per_sync_(bpsync),
|
||
|
next_sync_offset_(0),
|
||
|
use_fsync_(use_fs),
|
||
7 years ago
|
last_elem_type_(kEtNone) {}
|
||
8 years ago
|
|
||
7 years ago
|
Status Writer::Sync() {
|
||
7 years ago
|
StopWatch sync_sw(env_, statistics_, BLOB_DB_BLOB_FILE_SYNC_MICROS);
|
||
7 years ago
|
Status s = dest_->Sync(use_fsync_);
|
||
7 years ago
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_SYNCED);
|
||
7 years ago
|
return s;
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
Status Writer::WriteHeader(BlobLogHeader& header) {
|
||
8 years ago
|
assert(block_offset_ == 0);
|
||
|
assert(last_elem_type_ == kEtNone);
|
||
|
std::string str;
|
||
|
header.EncodeTo(&str);
|
||
|
|
||
|
Status s = dest_->Append(Slice(str));
|
||
|
if (s.ok()) {
|
||
|
block_offset_ += str.size();
|
||
|
s = dest_->Flush();
|
||
|
}
|
||
|
last_elem_type_ = kEtFileHdr;
|
||
7 years ago
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_WRITTEN,
|
||
|
BlobLogHeader::kSize);
|
||
8 years ago
|
return s;
|
||
|
}
|
||
|
|
||
7 years ago
|
Status Writer::AppendFooter(BlobLogFooter& footer) {
|
||
8 years ago
|
assert(block_offset_ != 0);
|
||
7 years ago
|
assert(last_elem_type_ == kEtFileHdr || last_elem_type_ == kEtRecord);
|
||
8 years ago
|
|
||
|
std::string str;
|
||
|
footer.EncodeTo(&str);
|
||
|
|
||
|
Status s = dest_->Append(Slice(str));
|
||
|
if (s.ok()) {
|
||
|
block_offset_ += str.size();
|
||
|
s = dest_->Close();
|
||
|
dest_.reset();
|
||
|
}
|
||
|
|
||
|
last_elem_type_ = kEtFileFooter;
|
||
7 years ago
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_WRITTEN,
|
||
|
BlobLogFooter::kSize);
|
||
8 years ago
|
return s;
|
||
|
}
|
||
|
|
||
|
Status Writer::AddRecord(const Slice& key, const Slice& val,
|
||
7 years ago
|
uint64_t expiration, uint64_t* key_offset,
|
||
|
uint64_t* blob_offset) {
|
||
8 years ago
|
assert(block_offset_ != 0);
|
||
7 years ago
|
assert(last_elem_type_ == kEtFileHdr || last_elem_type_ == kEtRecord);
|
||
8 years ago
|
|
||
|
std::string buf;
|
||
7 years ago
|
ConstructBlobHeader(&buf, key, val, expiration);
|
||
8 years ago
|
|
||
|
Status s = EmitPhysicalRecord(buf, key, val, key_offset, blob_offset);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
Status Writer::AddRecord(const Slice& key, const Slice& val,
|
||
|
uint64_t* key_offset, uint64_t* blob_offset) {
|
||
|
assert(block_offset_ != 0);
|
||
7 years ago
|
assert(last_elem_type_ == kEtFileHdr || last_elem_type_ == kEtRecord);
|
||
8 years ago
|
|
||
|
std::string buf;
|
||
7 years ago
|
ConstructBlobHeader(&buf, key, val, 0);
|
||
8 years ago
|
|
||
|
Status s = EmitPhysicalRecord(buf, key, val, key_offset, blob_offset);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
7 years ago
|
void Writer::ConstructBlobHeader(std::string* buf, const Slice& key,
|
||
|
const Slice& val, uint64_t expiration) {
|
||
|
BlobLogRecord record;
|
||
|
record.key = key;
|
||
|
record.value = val;
|
||
|
record.expiration = expiration;
|
||
|
record.EncodeHeaderTo(buf);
|
||
8 years ago
|
}
|
||
|
|
||
|
Status Writer::EmitPhysicalRecord(const std::string& headerbuf,
|
||
|
const Slice& key, const Slice& val,
|
||
|
uint64_t* key_offset, uint64_t* blob_offset) {
|
||
7 years ago
|
StopWatch write_sw(env_, statistics_, BLOB_DB_BLOB_FILE_WRITE_MICROS);
|
||
8 years ago
|
Status s = dest_->Append(Slice(headerbuf));
|
||
|
if (s.ok()) {
|
||
|
s = dest_->Append(key);
|
||
7 years ago
|
}
|
||
|
if (s.ok()) {
|
||
|
s = dest_->Append(val);
|
||
|
}
|
||
|
if (s.ok()) {
|
||
|
s = dest_->Flush();
|
||
8 years ago
|
}
|
||
|
|
||
|
*key_offset = block_offset_ + BlobLogRecord::kHeaderSize;
|
||
|
*blob_offset = *key_offset + key.size();
|
||
|
block_offset_ = *blob_offset + val.size();
|
||
|
last_elem_type_ = kEtRecord;
|
||
7 years ago
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_WRITTEN,
|
||
|
BlobLogRecord::kHeaderSize + key.size() + val.size());
|
||
8 years ago
|
return s;
|
||
|
}
|
||
|
|
||
|
} // namespace blob_db
|
||
5 years ago
|
} // namespace ROCKSDB_NAMESPACE
|
||
8 years ago
|
#endif // ROCKSDB_LITE
|