blob_log_format.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. //
  6. // Log format information shared by reader and writer.
  7. #pragma once
  8. #ifndef ROCKSDB_LITE
  9. #include <limits>
  10. #include <memory>
  11. #include <utility>
  12. #include "rocksdb/options.h"
  13. #include "rocksdb/slice.h"
  14. #include "rocksdb/status.h"
  15. #include "rocksdb/types.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. namespace blob_db {
  18. constexpr uint32_t kMagicNumber = 2395959; // 0x00248f37
  19. constexpr uint32_t kVersion1 = 1;
  20. constexpr uint64_t kNoExpiration = std::numeric_limits<uint64_t>::max();
  21. using ExpirationRange = std::pair<uint64_t, uint64_t>;
  22. // Format of blob log file header (30 bytes):
  23. //
  24. // +--------------+---------+---------+-------+-------------+-------------------+
  25. // | magic number | version | cf id | flags | compression | expiration range |
  26. // +--------------+---------+---------+-------+-------------+-------------------+
  27. // | Fixed32 | Fixed32 | Fixed32 | char | char | Fixed64 Fixed64 |
  28. // +--------------+---------+---------+-------+-------------+-------------------+
  29. //
  30. // List of flags:
  31. // has_ttl: Whether the file contain TTL data.
  32. //
  33. // Expiration range in the header is a rough range based on
  34. // blob_db_options.ttl_range_secs.
  35. struct BlobLogHeader {
  36. static constexpr size_t kSize = 30;
  37. BlobLogHeader() = default;
  38. BlobLogHeader(uint32_t _column_family_id, CompressionType _compression,
  39. bool _has_ttl, const ExpirationRange& _expiration_range)
  40. : column_family_id(_column_family_id),
  41. compression(_compression),
  42. has_ttl(_has_ttl),
  43. expiration_range(_expiration_range) {}
  44. uint32_t version = kVersion1;
  45. uint32_t column_family_id = 0;
  46. CompressionType compression = kNoCompression;
  47. bool has_ttl = false;
  48. ExpirationRange expiration_range;
  49. void EncodeTo(std::string* dst);
  50. Status DecodeFrom(Slice slice);
  51. };
  52. // Format of blob log file footer (32 bytes):
  53. //
  54. // +--------------+------------+-------------------+------------+
  55. // | magic number | blob count | expiration range | footer CRC |
  56. // +--------------+------------+-------------------+------------+
  57. // | Fixed32 | Fixed64 | Fixed64 + Fixed64 | Fixed32 |
  58. // +--------------+------------+-------------------+------------+
  59. //
  60. // The footer will be presented only when the blob file is properly closed.
  61. //
  62. // Unlike the same field in file header, expiration range in the footer is the
  63. // range of smallest and largest expiration of the data in this file.
  64. struct BlobLogFooter {
  65. static constexpr size_t kSize = 32;
  66. uint64_t blob_count = 0;
  67. ExpirationRange expiration_range = std::make_pair(0, 0);
  68. uint32_t crc = 0;
  69. void EncodeTo(std::string* dst);
  70. Status DecodeFrom(Slice slice);
  71. };
  72. // Blob record format (32 bytes header + key + value):
  73. //
  74. // +------------+--------------+------------+------------+----------+---------+-----------+
  75. // | key length | value length | expiration | header CRC | blob CRC | key | value |
  76. // +------------+--------------+------------+------------+----------+---------+-----------+
  77. // | Fixed64 | Fixed64 | Fixed64 | Fixed32 | Fixed32 | key len | value len |
  78. // +------------+--------------+------------+------------+----------+---------+-----------+
  79. //
  80. // If file has has_ttl = false, expiration field is always 0, and the blob
  81. // doesn't has expiration.
  82. //
  83. // Also note that if compression is used, value is compressed value and value
  84. // length is compressed value length.
  85. //
  86. // Header CRC is the checksum of (key_len + val_len + expiration), while
  87. // blob CRC is the checksum of (key + value).
  88. //
  89. // We could use variable length encoding (Varint64) to save more space, but it
  90. // make reader more complicated.
  91. struct BlobLogRecord {
  92. // header include fields up to blob CRC
  93. static constexpr size_t kHeaderSize = 32;
  94. uint64_t key_size = 0;
  95. uint64_t value_size = 0;
  96. uint64_t expiration = 0;
  97. uint32_t header_crc = 0;
  98. uint32_t blob_crc = 0;
  99. Slice key;
  100. Slice value;
  101. std::unique_ptr<char[]> key_buf;
  102. std::unique_ptr<char[]> value_buf;
  103. uint64_t record_size() const { return kHeaderSize + key_size + value_size; }
  104. void EncodeHeaderTo(std::string* dst);
  105. Status DecodeHeaderFrom(Slice src);
  106. Status CheckBlobCRC() const;
  107. };
  108. } // namespace blob_db
  109. } // namespace ROCKSDB_NAMESPACE
  110. #endif // ROCKSDB_LITE