]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/blob_db/blob_log_writer.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / utilities / blob_db / blob_log_writer.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 #pragma once
6
7 #ifndef ROCKSDB_LITE
8
9 #include <cstdint>
10 #include <memory>
11 #include <string>
12
13 #include "rocksdb/env.h"
14 #include "rocksdb/slice.h"
15 #include "rocksdb/statistics.h"
16 #include "rocksdb/status.h"
17 #include "rocksdb/types.h"
18 #include "utilities/blob_db/blob_log_format.h"
19
20 namespace rocksdb {
21
22 class WritableFileWriter;
23
24 namespace blob_db {
25
26 /**
27 * Writer is the blob log stream writer. It provides an append-only
28 * abstraction for writing blob data.
29 *
30 *
31 * Look at blob_db_format.h to see the details of the record formats.
32 */
33
34 class Writer {
35 public:
36 // Create a writer that will append data to "*dest".
37 // "*dest" must be initially empty.
38 // "*dest" must remain live while this Writer is in use.
39 Writer(std::unique_ptr<WritableFileWriter>&& dest, Env* env,
40 Statistics* statistics, uint64_t log_number, uint64_t bpsync,
41 bool use_fsync, uint64_t boffset = 0);
42
43 ~Writer() = default;
44
45 // No copying allowed
46 Writer(const Writer&) = delete;
47 Writer& operator=(const Writer&) = delete;
48
49 static void ConstructBlobHeader(std::string* buf, const Slice& key,
50 const Slice& val, uint64_t expiration);
51
52 Status AddRecord(const Slice& key, const Slice& val, uint64_t* key_offset,
53 uint64_t* blob_offset);
54
55 Status AddRecord(const Slice& key, const Slice& val, uint64_t expiration,
56 uint64_t* key_offset, uint64_t* blob_offset);
57
58 Status EmitPhysicalRecord(const std::string& headerbuf, const Slice& key,
59 const Slice& val, uint64_t* key_offset,
60 uint64_t* blob_offset);
61
62 Status AppendFooter(BlobLogFooter& footer);
63
64 Status WriteHeader(BlobLogHeader& header);
65
66 WritableFileWriter* file() { return dest_.get(); }
67
68 const WritableFileWriter* file() const { return dest_.get(); }
69
70 uint64_t get_log_number() const { return log_number_; }
71
72 bool ShouldSync() const { return block_offset_ > next_sync_offset_; }
73
74 Status Sync();
75
76 void ResetSyncPointer() { next_sync_offset_ += bytes_per_sync_; }
77
78 private:
79 std::unique_ptr<WritableFileWriter> dest_;
80 Env* env_;
81 Statistics* statistics_;
82 uint64_t log_number_;
83 uint64_t block_offset_; // Current offset in block
84 uint64_t bytes_per_sync_;
85 uint64_t next_sync_offset_;
86 bool use_fsync_;
87
88 public:
89 enum ElemType { kEtNone, kEtFileHdr, kEtRecord, kEtFileFooter };
90 ElemType last_elem_type_;
91 };
92
93 } // namespace blob_db
94 } // namespace rocksdb
95 #endif // ROCKSDB_LITE