2017-05-10 21:54:35 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// 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).
|
2017-05-10 21:54:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2020-06-09 22:12:59 +00:00
|
|
|
#include "db/blob/blob_log_format.h"
|
2017-11-28 19:42:28 +00:00
|
|
|
#include "rocksdb/env.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "rocksdb/slice.h"
|
2017-11-28 19:42:28 +00:00
|
|
|
#include "rocksdb/statistics.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "rocksdb/types.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
class WritableFileWriter;
|
|
|
|
|
|
|
|
/**
|
2020-07-07 00:10:41 +00:00
|
|
|
* BlobLogWriter is the blob log stream writer. It provides an append-only
|
2017-05-10 21:54:35 +00:00
|
|
|
* abstraction for writing blob data.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Look at blob_db_format.h to see the details of the record formats.
|
|
|
|
*/
|
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
class BlobLogWriter {
|
2017-05-10 21:54:35 +00:00
|
|
|
public:
|
|
|
|
// Create a writer that will append data to "*dest".
|
|
|
|
// "*dest" must be initially empty.
|
2020-07-07 00:10:41 +00:00
|
|
|
// "*dest" must remain live while this BlobLogWriter is in use.
|
|
|
|
BlobLogWriter(std::unique_ptr<WritableFileWriter>&& dest, Env* env,
|
2020-07-14 20:50:26 +00:00
|
|
|
Statistics* statistics, uint64_t log_number, bool use_fsync,
|
|
|
|
uint64_t boffset = 0);
|
2017-10-27 20:14:34 +00:00
|
|
|
// No copying allowed
|
2020-07-07 00:10:41 +00:00
|
|
|
BlobLogWriter(const BlobLogWriter&) = delete;
|
|
|
|
BlobLogWriter& operator=(const BlobLogWriter&) = delete;
|
2017-10-27 20:14:34 +00:00
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
~BlobLogWriter();
|
2019-09-12 01:07:12 +00:00
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
static void ConstructBlobHeader(std::string* buf, const Slice& key,
|
|
|
|
const Slice& val, uint64_t expiration);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Status AddRecord(const Slice& key, const Slice& val, uint64_t* key_offset,
|
|
|
|
uint64_t* blob_offset);
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
Status AddRecord(const Slice& key, const Slice& val, uint64_t expiration,
|
|
|
|
uint64_t* key_offset, uint64_t* blob_offset);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Status EmitPhysicalRecord(const std::string& headerbuf, const Slice& key,
|
|
|
|
const Slice& val, uint64_t* key_offset,
|
|
|
|
uint64_t* blob_offset);
|
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
Status AppendFooter(BlobLogFooter& footer, std::string* checksum_method,
|
|
|
|
std::string* checksum_value);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
Status WriteHeader(BlobLogHeader& header);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
WritableFileWriter* file() { return dest_.get(); }
|
|
|
|
|
|
|
|
const WritableFileWriter* file() const { return dest_.get(); }
|
|
|
|
|
|
|
|
uint64_t get_log_number() const { return log_number_; }
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
Status Sync();
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<WritableFileWriter> dest_;
|
2017-11-28 19:42:28 +00:00
|
|
|
Env* env_;
|
|
|
|
Statistics* statistics_;
|
2017-05-10 21:54:35 +00:00
|
|
|
uint64_t log_number_;
|
|
|
|
uint64_t block_offset_; // Current offset in block
|
|
|
|
bool use_fsync_;
|
|
|
|
|
|
|
|
public:
|
2017-10-17 19:11:52 +00:00
|
|
|
enum ElemType { kEtNone, kEtFileHdr, kEtRecord, kEtFileFooter };
|
2017-05-10 21:54:35 +00:00
|
|
|
ElemType last_elem_type_;
|
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|