]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/cassandra/format.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / utilities / cassandra / format.h
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>
59 #include <memory>
60 #include <vector>
61 #include "rocksdb/merge_operator.h"
62 #include "rocksdb/slice.h"
63
64 namespace ROCKSDB_NAMESPACE {
65 namespace cassandra {
66
67 // Identify the type of the column.
68 enum ColumnTypeMask {
69 DELETION_MASK = 0x01,
70 EXPIRATION_MASK = 0x02,
71 };
72
73
74 class ColumnBase {
75 public:
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
87 private:
88 int8_t mask_;
89 int8_t index_;
90 };
91
92 class Column : public ColumnBase {
93 public:
94 Column(int8_t mask, int8_t index, int64_t timestamp,
95 int32_t value_size, const char* value);
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
103 private:
104 int64_t timestamp_;
105 int32_t value_size_;
106 const char* value_;
107 };
108
109 class Tombstone : public ColumnBase {
110 public:
111 Tombstone(int8_t mask, int8_t index,
112 int32_t local_deletion_time, int64_t marked_for_delete_at);
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
121 private:
122 int32_t local_deletion_time_;
123 int64_t marked_for_delete_at_;
124 };
125
126 class ExpiringColumn : public Column {
127 public:
128 ExpiringColumn(int8_t mask, int8_t index, int64_t timestamp,
129 int32_t value_size, const char* value, int32_t ttl);
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
139 private:
140 int32_t ttl_;
141 std::chrono::time_point<std::chrono::system_clock> TimePoint() const;
142 std::chrono::seconds Ttl() const;
143 };
144
145 typedef std::vector<std::shared_ptr<ColumnBase>> Columns;
146
147 class RowValue {
148 public:
149 // Create a Row Tombstone.
150 RowValue(int32_t local_deletion_time, int64_t marked_for_delete_at);
151 // Create a Row containing columns.
152 RowValue(Columns columns,
153 int64_t last_modified_time);
154 RowValue(const RowValue& /*that*/) = delete;
155 RowValue(RowValue&& /*that*/) noexcept = default;
156 RowValue& operator=(const RowValue& /*that*/) = delete;
157 RowValue& operator=(RowValue&& /*that*/) = default;
158
159 std::size_t Size() const;
160 bool IsTombstone() const;
161 // For Tombstone this returns the marked_for_delete_at_,
162 // otherwise it returns the max timestamp of containing columns.
163 int64_t LastModifiedTime() const;
164 void Serialize(std::string* dest) const;
165 RowValue RemoveExpiredColumns(bool* changed) const;
166 RowValue ConvertExpiredColumnsToTombstones(bool* changed) const;
167 RowValue RemoveTombstones(int32_t gc_grace_period) const;
168 bool Empty() const;
169
170 static RowValue Deserialize(const char* src, std::size_t size);
171 // Merge multiple rows according to their timestamp.
172 static RowValue Merge(std::vector<RowValue>&& values);
173
174 const Columns& get_columns() { return columns_; }
175
176 private:
177 int32_t local_deletion_time_;
178 int64_t marked_for_delete_at_;
179 Columns columns_;
180 int64_t last_modified_time_;
181 };
182
183 } // namepsace cassandrda
184 } // namespace ROCKSDB_NAMESPACE