1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Haichao.chc
17 * \date Oct 2020
18 * \brief Format of collection writing requests
19 */
20
21#pragma once
22
23#include <memory>
24#include <vector>
25#include "common/macro_define.h"
26#include "common/types.h"
27#include "constants.h"
28
29namespace proxima {
30namespace be {
31namespace index {
32
33class CollectionDataset;
34using CollectionDatasetPtr = std::shared_ptr<CollectionDataset>;
35
36/*
37 * CollectionDataset represents a batch of insert/update/delete requests.
38 * A RowData represents a record, it consists of several ColumnData.
39 * A ColumnData contains field name and field value.
40 */
41class CollectionDataset {
42 public:
43 //! ColumnData represents index column data
44 struct ColumnData {
45 std::string column_name{};
46 DataTypes data_type{DataTypes::UNDEFINED};
47 uint32_t dimension{0U};
48 std::string data{};
49 };
50
51 //! A RowData contains serveral index columns and forward data
52 struct RowData {
53 OperationTypes operation_type{OperationTypes::UNDEFINED};
54 uint64_t primary_key{0U};
55 uint32_t revision{0U};
56 uint64_t lsn{0U};
57 std::string lsn_context{};
58 bool lsn_check{false};
59 uint64_t timestamp{0U};
60 std::string forward_data{};
61 std::vector<ColumnData> column_datas{};
62
63 RowData() {
64 primary_key = INVALID_KEY;
65 }
66 };
67
68 public:
69 //! Constructor
70 CollectionDataset(uint32_t schema_rev) : schema_revision_(schema_rev) {}
71
72 //! Destructor
73 ~CollectionDataset() = default;
74
75 public:
76 //! Add a row data
77 RowData *add_row_data();
78
79 //! Return a row
80 const RowData &get(size_t i) const;
81
82 //! Return row size
83 size_t size() const {
84 return records_.size();
85 }
86
87 //! Return if empty
88 bool empty() const {
89 return records_.empty();
90 }
91
92 //! Clear rows
93 void clear() {
94 records_.clear();
95 }
96
97 //! Get schema revision
98 uint32_t get_schema_revision() const {
99 return schema_revision_;
100 }
101
102 private:
103 uint32_t schema_revision_{0U};
104 std::vector<RowData> records_{};
105};
106
107using Record = CollectionDataset::RowData;
108using ColumnData = CollectionDataset::ColumnData;
109
110} // end namespace index
111} // namespace be
112} // end namespace proxima
113