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