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