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