1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RELATIONAL_RESULT_SET_IMPL_H
17 #define RELATIONAL_RESULT_SET_IMPL_H
18 
19 #include <shared_mutex>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 #include "db_types.h"
24 #include "distributeddb/result_set.h"
25 #include "relational_row_data_set.h"
26 
27 namespace DistributedDB {
28 class RelationalResultSetImpl : public ResultSet {
29 public:
RelationalResultSetImpl()30     RelationalResultSetImpl()
31         : isClosed_(false), dataSetSize_(0), index_(-1)
32     {}
33 
34     ~RelationalResultSetImpl() = default;
35 
36     DISABLE_COPY_ASSIGN_MOVE(RelationalResultSetImpl);
37 
38     // Returns the count of rows in the result set.
39     int GetCount() const override;
40 
41     // Returns the current read position of the result set.
42     int GetPosition() const override;
43 
44     // Move the read position to the first row, return false if the result set is empty.
45     bool MoveToFirst() override;
46 
47     // Move the read position to the last row, return false if the result set is empty.
48     bool MoveToLast() override;
49 
50     // Move the read position to the next row, return false if the result set is empty
51     // or the read position is already past the last entry in the result set.
52     bool MoveToNext() override;
53 
54     // Move the read position to the previous row, return false if the result set is empty
55     // or the read position is already before the first entry in the result set.
56     bool MoveToPrevious() override;
57 
58     // Move the read position by a relative amount from the current position.
59     bool Move(int offset) override;
60 
61     // Move the read position to an absolute position value.
62     bool MoveToPosition(int position) override;
63 
64     // Returns whether the read position is pointing to the first row.
65     bool IsFirst() const override;
66 
67     // Returns whether the read position is pointing to the last row.
68     bool IsLast() const override;
69 
70     // Returns whether the read position is before the first row.
71     bool IsBeforeFirst() const override;
72 
73     // Returns whether the read position is after the last row
74     bool IsAfterLast() const override;
75 
76     // Returns whether the result set is empty.
77     bool IsClosed() const override;
78 
79     // Clear the result set. Set the position -1.
80     void Close() override;
81 
82     // Get a key-value entry. Just for kv delegate. Returns OK or NOT_SUPPORT.
83     DBStatus GetEntry(Entry &entry) const override;
84 
85     // Get column names.
86     void GetColumnNames(std::vector<std::string> &columnNames) const override;
87 
88     // Get the column name by column index. Returns OK, NOT_FOUND or NONEXISTENT.
89     DBStatus GetColumnType(int columnIndex, ColumnType &columnType) const override;
90 
91     // Get the column index by column name. Returns OK, NOT_FOUND or NONEXISTENT.
92     DBStatus GetColumnIndex(const std::string &columnName, int &columnIndex) const override;
93 
94     // Get the column name by column index. Returns OK, NOT_FOUND or NONEXISTENT.
95     DBStatus GetColumnName(int columnIndex, std::string &columnName) const override;
96 
97     // Get blob. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH.
98     DBStatus Get(int columnIndex, std::vector<uint8_t> &value) const override;
99 
100     // Get string. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH.
101     DBStatus Get(int columnIndex, std::string &value) const override;
102 
103     // Get int64. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH.
104     DBStatus Get(int columnIndex, int64_t &value) const override;
105 
106     // Get double. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH.
107     DBStatus Get(int columnIndex, double &value) const override;
108 
109     // Get whether the column value is null. Returns OK, NOT_FOUND or NONEXISTENT.
110     DBStatus IsColumnNull(int columnIndex, bool &isNull) const override;
111 
112     // Get the row record. Returns OK, NOT_FOUND or NOT_SUPPORT.
113     DBStatus GetRow(std::map<std::string, VariantData> &data) const override;
114 
115     // sequenceId start from 1.
116     int Put(const DeviceID &deviceName, uint32_t sequenceId, RelationalRowDataSet &&data);
117 
118 private:
119     inline bool IsValid() const;
120     inline bool IsValid(int64_t columnIndex) const;
121     VariantData GetData(int64_t columnIndex) const;
122 
123     bool isClosed_;
124     int dataSetSize_;
125     int64_t index_;
126     RelationalRowDataSet dataSet_;
127     mutable std::unordered_map<std::string, int> colNames_;
128     std::map<uint32_t, RelationalRowDataSet> cacheDataSet_;
129     mutable std::shared_mutex mutex_;
130 };
131 } // namespace DistributedDB
132 #endif  // RELATIONAL_RESULT_SET_IMPL_H