2017-06-16 21:12:52 +00:00
|
|
|
// Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The encoding of Cassandra Row Value.
|
|
|
|
*
|
|
|
|
* A Cassandra Row Value could either be a row tombstone,
|
|
|
|
* or contains multiple columns, it has following fields:
|
|
|
|
*
|
|
|
|
* struct row_value {
|
|
|
|
* int32_t local_deletion_time; // Time in second when the row is deleted,
|
|
|
|
* // only used for Cassandra tombstone gc.
|
|
|
|
* int64_t marked_for_delete_at; // Ms that marked this row is deleted.
|
|
|
|
* struct column_base columns[]; // For non tombstone row, all columns
|
|
|
|
* // are stored here.
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* If the local_deletion_time and marked_for_delete_at is set, then this is
|
|
|
|
* a tombstone, otherwise it contains multiple columns.
|
|
|
|
*
|
|
|
|
* There are three type of Columns: Normal Column, Expiring Column and Column
|
|
|
|
* Tombstone, which have following fields:
|
|
|
|
*
|
|
|
|
* // Identify the type of the column.
|
|
|
|
* enum mask {
|
|
|
|
* DELETION_MASK = 0x01,
|
|
|
|
* EXPIRATION_MASK = 0x02,
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* struct column {
|
|
|
|
* int8_t mask = 0;
|
|
|
|
* int8_t index;
|
|
|
|
* int64_t timestamp;
|
|
|
|
* int32_t value_length;
|
|
|
|
* char value[value_length];
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* struct expiring_column {
|
|
|
|
* int8_t mask = mask.EXPIRATION_MASK;
|
|
|
|
* int8_t index;
|
|
|
|
* int64_t timestamp;
|
|
|
|
* int32_t value_length;
|
|
|
|
* char value[value_length];
|
|
|
|
* int32_t ttl;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* struct tombstone_column {
|
|
|
|
* int8_t mask = mask.DELETION_MASK;
|
|
|
|
* int8_t index;
|
|
|
|
* int32_t local_deletion_time; // Similar to row_value's field.
|
|
|
|
* int64_t marked_for_delete_at;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2017-07-21 21:42:32 +00:00
|
|
|
#include <chrono>
|
2017-06-16 21:12:52 +00:00
|
|
|
#include <memory>
|
2019-05-31 00:39:43 +00:00
|
|
|
#include <vector>
|
2022-10-24 23:38:09 +00:00
|
|
|
|
2017-06-16 21:12:52 +00:00
|
|
|
#include "rocksdb/merge_operator.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-06-16 21:12:52 +00:00
|
|
|
namespace cassandra {
|
|
|
|
|
|
|
|
// Identify the type of the column.
|
|
|
|
enum ColumnTypeMask {
|
|
|
|
DELETION_MASK = 0x01,
|
|
|
|
EXPIRATION_MASK = 0x02,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ColumnBase {
|
2022-10-24 23:38:09 +00:00
|
|
|
public:
|
2017-06-16 21:12:52 +00:00
|
|
|
ColumnBase(int8_t mask, int8_t index);
|
|
|
|
virtual ~ColumnBase() = default;
|
|
|
|
|
|
|
|
virtual int64_t Timestamp() const = 0;
|
|
|
|
virtual int8_t Mask() const;
|
|
|
|
virtual int8_t Index() const;
|
|
|
|
virtual std::size_t Size() const;
|
|
|
|
virtual void Serialize(std::string* dest) const;
|
2017-07-21 21:42:32 +00:00
|
|
|
static std::shared_ptr<ColumnBase> Deserialize(const char* src,
|
2017-06-16 21:12:52 +00:00
|
|
|
std::size_t offset);
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
private:
|
2017-06-16 21:12:52 +00:00
|
|
|
int8_t mask_;
|
|
|
|
int8_t index_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Column : public ColumnBase {
|
2022-10-24 23:38:09 +00:00
|
|
|
public:
|
|
|
|
Column(int8_t mask, int8_t index, int64_t timestamp, int32_t value_size,
|
|
|
|
const char* value);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
virtual int64_t Timestamp() const override;
|
|
|
|
virtual std::size_t Size() const override;
|
|
|
|
virtual void Serialize(std::string* dest) const override;
|
2017-07-21 21:42:32 +00:00
|
|
|
static std::shared_ptr<Column> Deserialize(const char* src,
|
2017-06-16 21:12:52 +00:00
|
|
|
std::size_t offset);
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
private:
|
2017-06-16 21:12:52 +00:00
|
|
|
int64_t timestamp_;
|
|
|
|
int32_t value_size_;
|
|
|
|
const char* value_;
|
|
|
|
};
|
|
|
|
|
2017-07-21 21:42:32 +00:00
|
|
|
class Tombstone : public ColumnBase {
|
2022-10-24 23:38:09 +00:00
|
|
|
public:
|
|
|
|
Tombstone(int8_t mask, int8_t index, int32_t local_deletion_time,
|
|
|
|
int64_t marked_for_delete_at);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2017-07-21 21:42:32 +00:00
|
|
|
virtual int64_t Timestamp() const override;
|
2017-06-16 21:12:52 +00:00
|
|
|
virtual std::size_t Size() const override;
|
|
|
|
virtual void Serialize(std::string* dest) const override;
|
2017-08-31 17:10:47 +00:00
|
|
|
bool Collectable(int32_t gc_grace_period) const;
|
2017-07-21 21:42:32 +00:00
|
|
|
static std::shared_ptr<Tombstone> Deserialize(const char* src,
|
|
|
|
std::size_t offset);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
private:
|
2017-07-21 21:42:32 +00:00
|
|
|
int32_t local_deletion_time_;
|
|
|
|
int64_t marked_for_delete_at_;
|
2017-06-16 21:12:52 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 21:42:32 +00:00
|
|
|
class ExpiringColumn : public Column {
|
2022-10-24 23:38:09 +00:00
|
|
|
public:
|
2017-07-21 21:42:32 +00:00
|
|
|
ExpiringColumn(int8_t mask, int8_t index, int64_t timestamp,
|
2022-10-24 23:38:09 +00:00
|
|
|
int32_t value_size, const char* value, int32_t ttl);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
virtual std::size_t Size() const override;
|
|
|
|
virtual void Serialize(std::string* dest) const override;
|
2017-07-21 21:42:32 +00:00
|
|
|
bool Expired() const;
|
|
|
|
std::shared_ptr<Tombstone> ToTombstone() const;
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2017-07-21 21:42:32 +00:00
|
|
|
static std::shared_ptr<ExpiringColumn> Deserialize(const char* src,
|
|
|
|
std::size_t offset);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
private:
|
2017-07-21 21:42:32 +00:00
|
|
|
int32_t ttl_;
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> TimePoint() const;
|
|
|
|
std::chrono::seconds Ttl() const;
|
2017-06-16 21:12:52 +00:00
|
|
|
};
|
|
|
|
|
2021-09-07 18:31:12 +00:00
|
|
|
using Columns = std::vector<std::shared_ptr<ColumnBase>>;
|
2017-07-21 21:42:32 +00:00
|
|
|
|
2017-06-16 21:12:52 +00:00
|
|
|
class RowValue {
|
2022-10-24 23:38:09 +00:00
|
|
|
public:
|
2017-06-16 21:12:52 +00:00
|
|
|
// Create a Row Tombstone.
|
|
|
|
RowValue(int32_t local_deletion_time, int64_t marked_for_delete_at);
|
|
|
|
// Create a Row containing columns.
|
2022-10-24 23:38:09 +00:00
|
|
|
RowValue(Columns columns, int64_t last_modified_time);
|
2018-04-13 00:55:14 +00:00
|
|
|
RowValue(const RowValue& /*that*/) = delete;
|
|
|
|
RowValue(RowValue&& /*that*/) noexcept = default;
|
|
|
|
RowValue& operator=(const RowValue& /*that*/) = delete;
|
|
|
|
RowValue& operator=(RowValue&& /*that*/) = default;
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2020-05-22 18:16:43 +00:00
|
|
|
std::size_t Size() const;
|
2017-06-16 21:12:52 +00:00
|
|
|
bool IsTombstone() const;
|
|
|
|
// For Tombstone this returns the marked_for_delete_at_,
|
|
|
|
// otherwise it returns the max timestamp of containing columns.
|
|
|
|
int64_t LastModifiedTime() const;
|
|
|
|
void Serialize(std::string* dest) const;
|
2017-08-31 17:10:47 +00:00
|
|
|
RowValue RemoveExpiredColumns(bool* changed) const;
|
|
|
|
RowValue ConvertExpiredColumnsToTombstones(bool* changed) const;
|
|
|
|
RowValue RemoveTombstones(int32_t gc_grace_period) const;
|
2017-07-21 21:42:32 +00:00
|
|
|
bool Empty() const;
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
static RowValue Deserialize(const char* src, std::size_t size);
|
|
|
|
// Merge multiple rows according to their timestamp.
|
|
|
|
static RowValue Merge(std::vector<RowValue>&& values);
|
|
|
|
|
2020-06-02 20:54:03 +00:00
|
|
|
const Columns& get_columns() { return columns_; }
|
|
|
|
|
|
|
|
private:
|
2017-06-16 21:12:52 +00:00
|
|
|
int32_t local_deletion_time_;
|
|
|
|
int64_t marked_for_delete_at_;
|
2017-07-21 21:42:32 +00:00
|
|
|
Columns columns_;
|
2017-06-16 21:12:52 +00:00
|
|
|
int64_t last_modified_time_;
|
|
|
|
};
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
} // namespace cassandra
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|