blob_log_writer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include <cstdint>
  7. #include <memory>
  8. #include <string>
  9. #include "db/blob/blob_log_format.h"
  10. #include "rocksdb/slice.h"
  11. #include "rocksdb/statistics.h"
  12. #include "rocksdb/status.h"
  13. #include "rocksdb/types.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. class WritableFileWriter;
  16. class SystemClock;
  17. /**
  18. * BlobLogWriter is the blob log stream writer. It provides an append-only
  19. * abstraction for writing blob data.
  20. *
  21. *
  22. * Look at blob_db_format.h to see the details of the record formats.
  23. */
  24. class BlobLogWriter {
  25. public:
  26. // Create a writer that will append data to "*dest".
  27. // "*dest" must be initially empty.
  28. // "*dest" must remain live while this BlobLogWriter is in use.
  29. BlobLogWriter(std::unique_ptr<WritableFileWriter>&& dest, SystemClock* clock,
  30. Statistics* statistics, uint64_t log_number, bool use_fsync,
  31. bool do_flush, uint64_t boffset = 0);
  32. // No copying allowed
  33. BlobLogWriter(const BlobLogWriter&) = delete;
  34. BlobLogWriter& operator=(const BlobLogWriter&) = delete;
  35. ~BlobLogWriter();
  36. static void ConstructBlobHeader(std::string* buf, const Slice& key,
  37. const Slice& val, uint64_t expiration);
  38. Status AddRecord(const WriteOptions& write_options, const Slice& key,
  39. const Slice& val, uint64_t* key_offset,
  40. uint64_t* blob_offset);
  41. Status AddRecord(const WriteOptions& write_options, const Slice& key,
  42. const Slice& val, uint64_t expiration, uint64_t* key_offset,
  43. uint64_t* blob_offset);
  44. Status EmitPhysicalRecord(const WriteOptions& write_options,
  45. const std::string& headerbuf, const Slice& key,
  46. const Slice& val, uint64_t* key_offset,
  47. uint64_t* blob_offset);
  48. Status AppendFooter(const WriteOptions& write_options, BlobLogFooter& footer,
  49. std::string* checksum_method,
  50. std::string* checksum_value);
  51. Status WriteHeader(const WriteOptions& write_options, BlobLogHeader& header);
  52. WritableFileWriter* file() { return dest_.get(); }
  53. const WritableFileWriter* file() const { return dest_.get(); }
  54. uint64_t get_log_number() const { return log_number_; }
  55. Status Sync(const WriteOptions& write_options);
  56. private:
  57. std::unique_ptr<WritableFileWriter> dest_;
  58. SystemClock* clock_;
  59. Statistics* statistics_;
  60. uint64_t log_number_;
  61. uint64_t block_offset_; // Current offset in block
  62. bool use_fsync_;
  63. bool do_flush_;
  64. public:
  65. enum ElemType { kEtNone, kEtFileHdr, kEtRecord, kEtFileFooter };
  66. ElemType last_elem_type_;
  67. };
  68. } // namespace ROCKSDB_NAMESPACE