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 #include "sqlite_single_ver_forward_cursor.h"
17
18 #include "db_errno.h"
19 #include "log_print.h"
20
21 namespace DistributedDB {
SQLiteSingleVerForwardCursor(SQLiteSingleVerNaturalStore * kvDB,const Key & keyPrefix)22 SQLiteSingleVerForwardCursor::SQLiteSingleVerForwardCursor(SQLiteSingleVerNaturalStore *kvDB, const Key &keyPrefix)
23 : kvDB_(kvDB),
24 keyPrefix_(keyPrefix),
25 handle_(nullptr),
26 count_(0),
27 isOpen_(false),
28 isQueryMode_(false)
29 {}
30
SQLiteSingleVerForwardCursor(SQLiteSingleVerNaturalStore * kvDB,const QueryObject & queryObj)31 SQLiteSingleVerForwardCursor::SQLiteSingleVerForwardCursor(SQLiteSingleVerNaturalStore *kvDB,
32 const QueryObject &queryObj)
33 : kvDB_(kvDB),
34 queryObj_(queryObj),
35 handle_(nullptr),
36 count_(0),
37 isOpen_(false),
38 isQueryMode_(true)
39 {}
40
~SQLiteSingleVerForwardCursor()41 SQLiteSingleVerForwardCursor::~SQLiteSingleVerForwardCursor()
42 {
43 kvDB_ = nullptr;
44 keyPrefix_.clear();
45 handle_ = nullptr;
46 count_ = 0;
47 }
48
Open()49 int SQLiteSingleVerForwardCursor::Open()
50 {
51 std::lock_guard<std::mutex> lock(isOpenMutex_);
52 if (isOpen_) {
53 return E_OK;
54 }
55 int errCode = E_OK;
56 handle_ = kvDB_->GetHandle(false, errCode);
57 if (handle_ == nullptr) {
58 LOGE("Get handle failed.");
59 return errCode;
60 }
61
62 if (isQueryMode_) {
63 errCode = handle_->OpenResultSet(queryObj_, count_);
64 } else {
65 errCode = handle_->OpenResultSet(keyPrefix_, count_);
66 }
67 if (errCode == E_OK) {
68 if (count_ == 0) {
69 handle_->CloseResultSet();
70 kvDB_->ReleaseHandle(handle_);
71 }
72 isOpen_ = true;
73 } else {
74 handle_->CloseResultSet();
75 kvDB_->ReleaseHandle(handle_);
76 LOGE("Handle open result set failed, errCode: %d", errCode);
77 }
78
79 return errCode;
80 }
81
Close()82 void SQLiteSingleVerForwardCursor::Close()
83 {
84 std::lock_guard<std::mutex> lock(isOpenMutex_);
85 if (!isOpen_) {
86 return;
87 }
88 if (handle_ != nullptr) {
89 handle_->CloseResultSet();
90 kvDB_->ReleaseHandle(handle_);
91 }
92 count_ = 0;
93 isOpen_ = false;
94 }
95
Reload()96 int SQLiteSingleVerForwardCursor::Reload()
97 {
98 std::lock_guard<std::mutex> lock(isOpenMutex_);
99 if (!isOpen_) {
100 return -E_RESULT_SET_STATUS_INVALID;
101 }
102 if (count_ == 0) {
103 return E_OK;
104 }
105 int errCode = E_OK;
106 if (isQueryMode_) {
107 errCode = handle_->ReloadResultSet(queryObj_);
108 } else {
109 errCode = handle_->ReloadResultSet(keyPrefix_);
110 }
111 if (errCode != E_OK) {
112 handle_->CloseResultSet();
113 kvDB_->ReleaseHandle(handle_);
114 isOpen_ = false;
115 }
116 return errCode;
117 }
118
GetCount() const119 int SQLiteSingleVerForwardCursor::GetCount() const
120 {
121 std::lock_guard<std::mutex> lock(isOpenMutex_);
122 if (!isOpen_) {
123 return 0;
124 }
125 return count_;
126 }
127
GetNext(Entry & entry,bool isCopy) const128 int SQLiteSingleVerForwardCursor::GetNext(Entry &entry, bool isCopy) const
129 {
130 std::lock_guard<std::mutex> lock(isOpenMutex_);
131 if (!isOpen_) {
132 return -E_RESULT_SET_STATUS_INVALID;
133 }
134 if (count_ == 0) {
135 return -E_RESULT_SET_EMPTY;
136 }
137 int errCode = handle_->GetNextEntryFromResultSet(entry.key, entry.value, isCopy);
138 if (errCode != E_OK && errCode != -E_FINISHED) {
139 handle_->CloseResultSet();
140 kvDB_->ReleaseHandle(handle_);
141 isOpen_ = false;
142 }
143 return errCode;
144 }
145 } // namespace DistributedDB
146