1 /*
2  * Copyright (c) 2023 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 RD_SINGLE_VER_RESULT_SET_H
17 #define RD_SINGLE_VER_RESULT_SET_H
18 #include "ikvdb_result_set.h"
19 #include "grd_resultset_api.h"
20 #include "rd_single_ver_natural_store.h"
21 #include "rd_single_ver_storage_executor.h"
22 
23 namespace DistributedDB {
24 class RdSingleVerResultSet : public IKvDBResultSet {
25 public:
26     RdSingleVerResultSet(RdSingleVerNaturalStore *kvDB, const Key &key);
27     RdSingleVerResultSet(RdSingleVerNaturalStore *kvDB, const Key &beginKey,
28         const Key &endKey, GRD_KvScanModeE kvScanMode);
29     ~RdSingleVerResultSet() override;
30 
31     // Initialize logic
32     int Open(bool isMemDb) override;
33 
34     // Finalize logic
35     int Close() override;
36 
37     // Get total entries count.
38     // >= 0: count, < 0: errCode.
39     int GetCount() const override;
40 
41     // Get current read position.
42     // >= 0: position, < 0: errCode
43     int GetPosition() const override;
44 
45     int Move(int offset) const override;
46 
47     // Move the read position to an absolute position value.
48     int MoveTo(int position) const override;
49 
50     int MoveToFirst() override;
51 
52     int MoveToLast() override;
53 
54     bool IsFirst() const override;
55 
56     bool IsLast() const override;
57 
58     bool IsBeforeFirst() const override;
59 
60     bool IsAfterLast() const override;
61 
62     // Get the entry of current position.
63     int GetEntry(Entry &entry) const override;
64 private:
65     int PreCheckResultSet() const;
66 
67     int MoveToNext() const;
68 
69     int MoveToPrev() const;
70 
71     mutable std::mutex mutex_;
72 
73     mutable bool isOpen_ = false;
74 
75     mutable int position_ = INIT_POSITION; // The position in the overall result
76 
77     mutable int endPosition_ = INIT_POSITION;   // the position after the last position, if find
78 
79     mutable bool isMovedBefore_ = false;
80 
81     Key key_;
82 
83     Key beginKey_;
84 
85     Key endKey_;
86 
87     GRD_KvScanModeE kvScanMode_ = KV_SCAN_PREFIX;
88 
89     mutable Entry entry_;
90 
91     // Common Pointer For Use, Not Own it, Not Responsible To Release It.
92     RdSingleVerNaturalStore *kvDB_ = nullptr;
93 
94     // Cache EntryId Mode Using StorageExecutor, Own It, Responsible To Release It.
95     RdSingleVerStorageExecutor *handle_ = nullptr;
96 
97     GRD_ResultSet *resultSet_ = nullptr;
98 };
99 } // namespace DistributedDB
100 #endif // RD_SINGLE_VER_RESULT_SET_H