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), dataSet_(), colNames_(), cacheDataSet_(), mutex_() 32 {} 33 34 ~RelationalResultSetImpl() = default; 35 36 // Returns the count of rows in the result set. 37 int GetCount() const override; 38 39 // Returns the current read position of the result set. 40 int GetPosition() const override; 41 42 // Move the read position to the first row, return false if the result set is empty. 43 bool MoveToFirst() override; 44 45 // Move the read position to the last row, return false if the result set is empty. 46 bool MoveToLast() override; 47 48 // Move the read position to the next row, return false if the result set is empty 49 // or the read position is already past the last entry in the result set. 50 bool MoveToNext() override; 51 52 // Move the read position to the previous row, return false if the result set is empty 53 // or the read position is already before the first entry in the result set. 54 bool MoveToPrevious() override; 55 56 // Move the read position by a relative amount from the current position. 57 bool Move(int offset) override; 58 59 // Move the read position to an absolute position value. 60 bool MoveToPosition(int position) override; 61 62 // Returns whether the read position is pointing to the first row. 63 bool IsFirst() const override; 64 65 // Returns whether the read position is pointing to the last row. 66 bool IsLast() const override; 67 68 // Returns whether the read position is before the first row. 69 bool IsBeforeFirst() const override; 70 71 // Returns whether the read position is after the last row 72 bool IsAfterLast() const override; 73 74 // Returns whether the result set is empty. 75 bool IsClosed() const override; 76 77 // Clear the result set. Set the position -1. 78 void Close() override; 79 80 // Get a key-value entry. Just for kv delegate. Returns OK or NOT_SUPPORT. 81 DBStatus GetEntry(Entry &entry) const override; 82 83 // Get column names. 84 void GetColumnNames(std::vector<std::string> &columnNames) const override; 85 86 // Get the column name by column index. Returns OK, NOT_FOUND or NONEXISTENT. 87 DBStatus GetColumnType(int columnIndex, ColumnType &columnType) const override; 88 89 // Get the column index by column name. Returns OK, NOT_FOUND or NONEXISTENT. 90 DBStatus GetColumnIndex(const std::string &columnName, int &columnIndex) const override; 91 92 // Get the column name by column index. Returns OK, NOT_FOUND or NONEXISTENT. 93 DBStatus GetColumnName(int columnIndex, std::string &columnName) const override; 94 95 // Get blob. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH. 96 DBStatus Get(int columnIndex, std::vector<uint8_t> &value) const override; 97 98 // Get string. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH. 99 DBStatus Get(int columnIndex, std::string &value) const override; 100 101 // Get int64. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH. 102 DBStatus Get(int columnIndex, int64_t &value) const override; 103 104 // Get double. Returns OK, NOT_FOUND, NONEXISTENT or TYPE_MISMATCH. 105 DBStatus Get(int columnIndex, double &value) const override; 106 107 // Get whether the column value is null. Returns OK, NOT_FOUND or NONEXISTENT. 108 DBStatus IsColumnNull(int columnIndex, bool &isNull) const override; 109 110 // Get the row record. Returns OK, NOT_FOUND or NOT_SUPPORT. 111 DBStatus GetRow(std::map<std::string, VariantData> &data) const override; 112 113 // sequenceId start from 1. 114 int Put(const DeviceID &deviceName, uint32_t sequenceId, RelationalRowDataSet &&data); 115 116 private: 117 inline bool IsValid() const; 118 inline bool IsValid(int64_t columnIndex) const; 119 VariantData GetData(int64_t columnIndex) const; 120 121 bool isClosed_; 122 int dataSetSize_; 123 int64_t index_; 124 RelationalRowDataSet dataSet_; 125 mutable std::unordered_map<std::string, int> colNames_; 126 std::map<uint32_t, RelationalRowDataSet> cacheDataSet_; 127 mutable std::shared_mutex mutex_; 128 }; 129 } // namespace DistributedDB 130 #endif // RELATIONAL_RESULT_SET_IMPL_H