1 /* 2 * Copyright (c) 2021 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 SQLITE_SINGLE_VER_RESULT_SET_H 17 #define SQLITE_SINGLE_VER_RESULT_SET_H 18 19 #include <mutex> 20 #include <vector> 21 22 #include "ikvdb_raw_cursor.h" 23 #include "kvdb_windowed_result_set.h" 24 #include "query_object.h" 25 26 namespace DistributedDB { 27 constexpr int DEFAULT_RESULT_SET_CACHE_MAX_SIZE = 1; // Unit MB, default 1 MB 28 constexpr int RESULT_SET_CACHE_MAX_SIZE_MIN = 1; 29 constexpr int RESULT_SET_CACHE_MAX_SIZE_MAX = 16; 30 31 // Forward declaration 32 class SQLiteSingleVerNaturalStore; 33 class SQLiteSingleVerStorageExecutor; 34 35 class SQLiteSingleVerResultSet : public KvDBWindowedResultSet { 36 public: 37 struct Option { 38 ResultSetCacheMode cacheMode = ResultSetCacheMode::CACHE_FULL_ENTRY; 39 int cacheMaxSize = DEFAULT_RESULT_SET_CACHE_MAX_SIZE; 40 }; 41 42 SQLiteSingleVerResultSet(SQLiteSingleVerNaturalStore *kvDB, const Key &keyPrefix, const Option& option); 43 SQLiteSingleVerResultSet(SQLiteSingleVerNaturalStore *kvDB, const QueryObject &queryObj, const Option& option); 44 ~SQLiteSingleVerResultSet() override; 45 46 // Delete the copy and assign constructors 47 DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerResultSet); 48 49 // Initialize logic 50 int Open(bool isMemDb) override; 51 52 // Get total entries count. 53 // >= 0: count, < 0: errCode. 54 int GetCount() const override; 55 56 // Get current read position. 57 // >= 0: position, < 0: errCode 58 int GetPosition() const override; 59 60 int Move(int offset) const override; 61 62 // Move the read position to an absolute position value. 63 int MoveTo(int position) const override; 64 65 int MoveToFirst() override; 66 67 int MoveToLast() override; 68 69 bool IsFirst() const override; 70 71 bool IsLast() const override; 72 73 bool IsBeforeFirst() const override; 74 75 bool IsAfterLast() const override; 76 77 // Get the entry of current position. 78 int GetEntry(Entry &entry) const override; 79 80 // Finalize logic 81 int Close() override; 82 private: 83 int OpenForCacheFullEntryMode(bool isMemDb); 84 int OpenForCacheEntryIdMode(); 85 86 int MoveToForCacheFullEntryMode(int position) const; 87 int MoveToForCacheEntryIdMode(int position) const; 88 89 void CloseForCacheFullEntryMode(); 90 void CloseForCacheEntryIdMode(); 91 92 const Option option_; 93 94 // Common Part Of Two ResultSet Mode. 95 bool isOpen_ = false; 96 int count_ = 0; 97 mutable int position_ = INIT_POSITION; // The position in the overall result 98 mutable std::mutex mutex_; 99 100 // For KeyPrefix Type Or Query Type. 101 const ResultSetType type_ = ResultSetType::KEYPREFIX; 102 Key keyPrefix_; 103 mutable QueryObject queryObj_; // Some QueryObject member function need to call is not a const function(BAD...) 104 // Common Pointer For Use, Not Own it, Not Responsible To Release It. 105 SQLiteSingleVerNaturalStore *kvDB_ = nullptr; 106 107 // Cache Full Entry Mode Using ResultEntriesWindow and IKvDBRawCursor, Own It, Responsible To Release It. 108 ResultEntriesWindow *window_ = nullptr; 109 IKvDBRawCursor *rawCursor_ = nullptr; 110 111 // Cache EntryId Mode Using StorageExecutor, Own It, Responsible To Release It. 112 SQLiteSingleVerStorageExecutor *handle_ = nullptr; 113 mutable std::vector<int64_t> cachedRowIds_; 114 mutable int cacheStartPosition_ = INIT_POSITION; // The offset of the first cached rowid in all result rowids 115 }; 116 } // namespace DistributedDB 117 118 #endif // SQLITE_SINGLE_VER_RESULT_SET_H 119