blob_log_writer.h 2.7 KB

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