format.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) 2017-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. * The encoding of Cassandra Row Value.
  7. *
  8. * A Cassandra Row Value could either be a row tombstone,
  9. * or contains multiple columns, it has following fields:
  10. *
  11. * struct row_value {
  12. * int32_t local_deletion_time; // Time in second when the row is deleted,
  13. * // only used for Cassandra tombstone gc.
  14. * int64_t marked_for_delete_at; // Ms that marked this row is deleted.
  15. * struct column_base columns[]; // For non tombstone row, all columns
  16. * // are stored here.
  17. * }
  18. *
  19. * If the local_deletion_time and marked_for_delete_at is set, then this is
  20. * a tombstone, otherwise it contains multiple columns.
  21. *
  22. * There are three type of Columns: Normal Column, Expiring Column and Column
  23. * Tombstone, which have following fields:
  24. *
  25. * // Identify the type of the column.
  26. * enum mask {
  27. * DELETION_MASK = 0x01,
  28. * EXPIRATION_MASK = 0x02,
  29. * };
  30. *
  31. * struct column {
  32. * int8_t mask = 0;
  33. * int8_t index;
  34. * int64_t timestamp;
  35. * int32_t value_length;
  36. * char value[value_length];
  37. * }
  38. *
  39. * struct expiring_column {
  40. * int8_t mask = mask.EXPIRATION_MASK;
  41. * int8_t index;
  42. * int64_t timestamp;
  43. * int32_t value_length;
  44. * char value[value_length];
  45. * int32_t ttl;
  46. * }
  47. *
  48. * struct tombstone_column {
  49. * int8_t mask = mask.DELETION_MASK;
  50. * int8_t index;
  51. * int32_t local_deletion_time; // Similar to row_value's field.
  52. * int64_t marked_for_delete_at;
  53. * }
  54. */
  55. #pragma once
  56. #include <chrono>
  57. #include <memory>
  58. #include <vector>
  59. #include "rocksdb/merge_operator.h"
  60. #include "rocksdb/slice.h"
  61. namespace ROCKSDB_NAMESPACE {
  62. namespace cassandra {
  63. // Identify the type of the column.
  64. enum ColumnTypeMask {
  65. DELETION_MASK = 0x01,
  66. EXPIRATION_MASK = 0x02,
  67. };
  68. class ColumnBase {
  69. public:
  70. ColumnBase(int8_t mask, int8_t index);
  71. virtual ~ColumnBase() = default;
  72. virtual int64_t Timestamp() const = 0;
  73. virtual int8_t Mask() const;
  74. virtual int8_t Index() const;
  75. virtual std::size_t Size() const;
  76. virtual void Serialize(std::string* dest) const;
  77. static std::shared_ptr<ColumnBase> Deserialize(const char* src,
  78. std::size_t offset);
  79. private:
  80. int8_t mask_;
  81. int8_t index_;
  82. };
  83. class Column : public ColumnBase {
  84. public:
  85. Column(int8_t mask, int8_t index, int64_t timestamp, int32_t value_size,
  86. const char* value);
  87. int64_t Timestamp() const override;
  88. std::size_t Size() const override;
  89. void Serialize(std::string* dest) const override;
  90. static std::shared_ptr<Column> Deserialize(const char* src,
  91. std::size_t offset);
  92. private:
  93. int64_t timestamp_;
  94. int32_t value_size_;
  95. const char* value_;
  96. };
  97. class Tombstone : public ColumnBase {
  98. public:
  99. Tombstone(int8_t mask, int8_t index, int32_t local_deletion_time,
  100. int64_t marked_for_delete_at);
  101. int64_t Timestamp() const override;
  102. std::size_t Size() const override;
  103. void Serialize(std::string* dest) const override;
  104. bool Collectable(int32_t gc_grace_period) const;
  105. static std::shared_ptr<Tombstone> Deserialize(const char* src,
  106. std::size_t offset);
  107. private:
  108. int32_t local_deletion_time_;
  109. int64_t marked_for_delete_at_;
  110. };
  111. class ExpiringColumn : public Column {
  112. public:
  113. ExpiringColumn(int8_t mask, int8_t index, int64_t timestamp,
  114. int32_t value_size, const char* value, int32_t ttl);
  115. std::size_t Size() const override;
  116. void Serialize(std::string* dest) const override;
  117. bool Expired() const;
  118. std::shared_ptr<Tombstone> ToTombstone() const;
  119. static std::shared_ptr<ExpiringColumn> Deserialize(const char* src,
  120. std::size_t offset);
  121. private:
  122. int32_t ttl_;
  123. std::chrono::time_point<std::chrono::system_clock> TimePoint() const;
  124. std::chrono::seconds Ttl() const;
  125. };
  126. using Columns = std::vector<std::shared_ptr<ColumnBase>>;
  127. class RowValue {
  128. public:
  129. // Create a Row Tombstone.
  130. RowValue(int32_t local_deletion_time, int64_t marked_for_delete_at);
  131. // Create a Row containing columns.
  132. RowValue(Columns columns, int64_t last_modified_time);
  133. RowValue(const RowValue& /*that*/) = delete;
  134. RowValue(RowValue&& /*that*/) noexcept = default;
  135. RowValue& operator=(const RowValue& /*that*/) = delete;
  136. RowValue& operator=(RowValue&& /*that*/) = default;
  137. std::size_t Size() const;
  138. bool IsTombstone() const;
  139. // For Tombstone this returns the marked_for_delete_at_,
  140. // otherwise it returns the max timestamp of containing columns.
  141. int64_t LastModifiedTime() const;
  142. void Serialize(std::string* dest) const;
  143. RowValue RemoveExpiredColumns(bool* changed) const;
  144. RowValue ConvertExpiredColumnsToTombstones(bool* changed) const;
  145. RowValue RemoveTombstones(int32_t gc_grace_period) const;
  146. bool Empty() const;
  147. static RowValue Deserialize(const char* src, std::size_t size);
  148. // Merge multiple rows according to their timestamp.
  149. static RowValue Merge(std::vector<RowValue>&& values);
  150. const Columns& get_columns() { return columns_; }
  151. private:
  152. int32_t local_deletion_time_;
  153. int64_t marked_for_delete_at_;
  154. Columns columns_;
  155. int64_t last_modified_time_;
  156. };
  157. } // namespace cassandra
  158. } // namespace ROCKSDB_NAMESPACE