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 #include "distributeddb_nb_cursor_testcase.h"
16
17 #include <gtest/gtest.h>
18 #include <ctime>
19 #include <cmath>
20 #include <random>
21 #include <chrono>
22 #include <thread>
23 #include <mutex>
24 #include <string>
25 #include <condition_variable>
26 #include "types_export.h"
27 #include "kv_store_delegate.h"
28 #include "kv_store_nb_delegate.h"
29 #include "kv_store_delegate_manager.h"
30 #include "distributed_test_tools.h"
31 #include "distributeddb_nb_test_tools.h"
32 #include "distributeddb_data_generator.h"
33
34 using namespace std;
35 using namespace std::chrono;
36 using namespace std::placeholders;
37 using namespace DistributedDB;
38 using namespace DistributedDBDataGenerator;
39
40 namespace {
41 const unsigned int FIFTEEN_RECORDS = 15;
42 const unsigned int CURSOR_DIFFERENT_RECORDS = 102;
43 const unsigned long ONE_TENTH_M_LONG_STRING = 104858;
44 const unsigned int FOUR_BYTE_KEY = 4;
45 const unsigned int FIVE_BYTE_KEY = 5;
46 const unsigned int THREAD_NUM_START = 1;
47 const unsigned int THREAD_NUM_END = 4;
48 const unsigned int OPEN_DB_TIMES = 3;
49 const unsigned int DELEGATE_NUM = 3;
50 const unsigned int WAIT_FOR_OBSERVER_REKEY = 75000;
51 const int CURSOR_ORIGINAL_POSITION_NEGATIVE10 = -10;
52 const int CURSOR_ORIGINAL_POSITION_NEGATIVE1 = -1;
53 const int CURSOR_ORIGINAL_POSITION_0 = 0;
54 const int CURSOR_ORIGINAL_POSITION_1 = 1;
55 const int CURSOR_ORIGINAL_POSITION_2 = 2;
56 const int CURSOR_ORIGINAL_POSITION_40 = 40;
57 const int CURSOR_ORIGINAL_POSITION_50 = 50;
58 const int CURSOR_ORIGINAL_POSITION_60 = 60;
59 const int CURSOR_ORIGINAL_POSITION_99 = 99;
60 const int CURSOR_ORIGINAL_POSITION_100 = 100;
61 const int CURSOR_ORIGINAL_POSITION_120 = 120;
62
63 const int CURSOR_POSITION_NEGATIVE5 = -5;
64 const int CURSOR_POSITION_NEGATIVE1 = -1;
65 const int CURSOR_POSITION_0 = 0;
66 const int CURSOR_POSITION_1 = 1;
67 const int CURSOR_POSITION_2 = 2;
68 const int CURSOR_POSITION_40 = 40;
69 const int CURSOR_POSITION_44 = 44;
70 const int CURSOR_POSITION_49 = 49;
71 const int CURSOR_POSITION_50 = 50;
72 const int CURSOR_POSITION_55 = 55;
73 const int CURSOR_POSITION_60 = 60;
74 const int CURSOR_POSITION_99 = 99;
75 const int CURSOR_POSITION_100 = 100;
76
77 const int CURSOR_OFFSET_NEGATIVE65 = -65;
78 const int CURSOR_OFFSET_NEGATIVE60 = -60;
79 const int CURSOR_OFFSET_NEGATIVE50 = -50;
80 const int CURSOR_OFFSET_NEGATIVE45 = -45;
81 const int CURSOR_OFFSET_NEGATIVE2 = -2;
82 const int CURSOR_OFFSET_NEGATIVE1 = -1;
83 const int CURSOR_OFFSET_0 = 0;
84 const int CURSOR_OFFSET_1 = 1;
85 const int CURSOR_OFFSET_2 = 2;
86 const int CURSOR_OFFSET_5 = 5;
87 const int CURSOR_OFFSET_45 = 45;
88 const int CURSOR_OFFSET_50 = 50;
89 const int CURSOR_OFFSET_55 = 55;
90 const int CURSOR_OFFSET_60 = 60;
91 const int CURSOR_OFFSET_65 = 65;
92 DistributedDB::CipherPassword g_passwd1;
93
94 struct PositionStatus01 {
95 DBStatus isGetSuccess = OK;
96 bool isFirst = true;
97 bool isLast = false;
98 bool isBeforeFirst = false;
99 bool isAfterLast = false;
100 int position = 0;
101 };
102
SetResultSetCacheMode(KvStoreNbDelegate * delegate,bool isRowIdMode)103 void SetResultSetCacheMode(KvStoreNbDelegate *delegate, bool isRowIdMode)
104 {
105 int cacheMode = 0;
106 if (isRowIdMode) {
107 cacheMode = static_cast<int>(ResultSetCacheMode::CACHE_ENTRY_ID_ONLY);
108 } else {
109 cacheMode = static_cast<int>(ResultSetCacheMode::CACHE_FULL_ENTRY);
110 }
111 PragmaData data = static_cast<PragmaData>(&cacheMode);
112 EXPECT_EQ(delegate->Pragma(RESULT_SET_CACHE_MODE, data), OK);
113 }
114
JudgePosition(KvStoreResultSet * & resultSet,const PositionStatus01 & currentStatus)115 bool JudgePosition(KvStoreResultSet *&resultSet, const PositionStatus01 ¤tStatus)
116 {
117 Entry entry;
118 bool bRes = true;
119 DBStatus status = resultSet->GetEntry(entry);
120 bRes = bRes && (status == currentStatus.isGetSuccess);
121 bool result = resultSet->IsFirst();
122 bRes = bRes && (result == currentStatus.isFirst);
123 result = resultSet->IsLast();
124 bRes = bRes && (result == currentStatus.isLast);
125 result = resultSet->IsBeforeFirst();
126 bRes = bRes && (result == currentStatus.isBeforeFirst);
127 result = resultSet->IsAfterLast();
128 bRes = bRes && (result == currentStatus.isAfterLast);
129 int position = resultSet->GetPosition();
130 bRes = bRes && (position == currentStatus.position);
131 return bRes;
132 }
133 }
134
ResultSetDb001(KvStoreNbDelegate * delegate,bool isRowIdMode)135 void DistributeddbNbCursorTestcase::ResultSetDb001(KvStoreNbDelegate *delegate, bool isRowIdMode)
136 {
137 ASSERT_TRUE(delegate != nullptr);
138 SetResultSetCacheMode(delegate, isRowIdMode);
139 std::vector<DistributedDB::Entry> entries;
140 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
141 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
142 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
143 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
144 }
145 /**
146 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
147 * @tc.expected: step1. get success.
148 */
149 KvStoreResultSet *resultSet = nullptr;
150 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
151
152 /**
153 * @tc.steps: step2. call GetCount interface.
154 * @tc.expected: step2. call success.
155 */
156 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
157
158 /**
159 * @tc.steps: step3. call GetPosition and MoveToPrevious interface.
160 * @tc.expected: step3. when the Current position is -1, can't do MoveToPrevious, and if do, it can't effect at all.
161 */
162 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
163 EXPECT_TRUE(resultSet->MoveToPrevious() == false);
164
165 /**
166 * @tc.steps: step4. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast and GetPostion interface.
167 * @tc.expected: step4. when the Current position is -1, other position judge interface can return right result.
168 */
169 PositionStatus01 currentStatus1 = {NOT_FOUND, false, false, true, false, CURSOR_POSITION_NEGATIVE1};
170 EXPECT_TRUE(JudgePosition(resultSet, currentStatus1));
171
172 /**
173 * @tc.steps: step5. call MoveToFirst interface.
174 * @tc.expected: step5. Move success.
175 */
176 EXPECT_TRUE(resultSet->MoveToFirst() == true);
177 /**
178 * @tc.steps: step6. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
179 * @tc.expected: step6. when the Current position is 0, other position judge interface can return right result.
180 */
181 PositionStatus01 currentStatus2 = {OK, true, false, false, false, CURSOR_POSITION_0};
182 EXPECT_TRUE(JudgePosition(resultSet, currentStatus2));
183 /**
184 * @tc.steps: step7. call MoveToNext interface.
185 * @tc.expected: step7. Move success.
186 */
187 EXPECT_TRUE(resultSet->MoveToNext() == true);
188 /**
189 * @tc.steps: step8. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
190 * @tc.expected: step8. when the Current position is 1, other position judge interface can return right result.
191 */
192 PositionStatus01 currentStatus3 = {OK, false, false, false, false, CURSOR_POSITION_1};
193 EXPECT_TRUE(JudgePosition(resultSet, currentStatus3));
194 /**
195 * @tc.steps: step9. call MoveToLast interface.
196 * @tc.expected: step9. Move success.
197 */
198 EXPECT_TRUE(resultSet->MoveToLast() == true);
199 /**
200 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
201 * @tc.expected: step10. when the Current position is the last,
202 * other position judge interface can return right result.
203 */
204 PositionStatus01 currentStatus4 = {OK, false, true, false, false, CURSOR_POSITION_99};
205 EXPECT_TRUE(JudgePosition(resultSet, currentStatus4));
206 /**
207 * @tc.steps: step11. call MoveToNext interface.
208 * @tc.expected: step11. Move success.
209 */
210 EXPECT_TRUE(resultSet->MoveToNext() == false);
211 /**
212 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
213 * @tc.expected: step10. if call MoveToNext interface when the Current position is the last,
214 * other position judge interface can also return right result.
215 */
216 PositionStatus01 currentStatus5 = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_100};
217 EXPECT_TRUE(JudgePosition(resultSet, currentStatus5));
218
219 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
220 }
221
ResultSetDb002(KvStoreNbDelegate * delegate,bool isRowIdMode)222 void DistributeddbNbCursorTestcase::ResultSetDb002(KvStoreNbDelegate *delegate, bool isRowIdMode)
223 {
224 ASSERT_TRUE(delegate != nullptr);
225 SetResultSetCacheMode(delegate, isRowIdMode);
226 std::vector<DistributedDB::Entry> entries;
227 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
228 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
229 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
230 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
231 }
232 /**
233 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
234 * @tc.expected: step1. get success.
235 */
236 KvStoreResultSet *resultSet = nullptr;
237 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
238
239 sort(entries.begin(), entries.end(), DistributedTestTools::CompareKey);
240
241 /**
242 * @tc.steps: step2. set the current position is -1, call MoveToNext, GetPostion and GetEntry interface looply.
243 * @tc.expected: step2. return values are all right.
244 */
245 EXPECT_TRUE(DistributedDBNbTestTools::MoveToNextFromBegin(*resultSet, entries, CURSOR_POSITION_100));
246 /**
247 * @tc.steps: step2. set the current position is 100, call MoveToPrevious, GetPostion, GetEntry looply.
248 * @tc.expected: step2. return values are all right.
249 */
250 Entry entry;
251 for (int position = CURSOR_POSITION_100; position > CURSOR_POSITION_NEGATIVE1; --position) {
252 bool result = resultSet->MoveToPrevious();
253 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
254 EXPECT_TRUE(result == true);
255 } else {
256 EXPECT_TRUE(result == false);
257 }
258 int positionGot = resultSet->GetPosition();
259 EXPECT_TRUE(positionGot == (position - CURSOR_POSITION_1));
260 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
261 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
262 EXPECT_TRUE(CompareVector(entry.key, entries[position - 1].key));
263 EXPECT_TRUE(CompareVector(entry.value, entries[position - 1].value));
264 } else {
265 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
266 }
267 }
268 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
269 }
270 namespace {
271 struct MoveStatus {
272 int offSet = 0;
273 bool moveResult = true;
274 int currentPosition = 0;
275 DBStatus status = OK;
276 };
277
MoveAndCheck(KvStoreResultSet * & resultSet,const MoveStatus & status)278 bool MoveAndCheck(KvStoreResultSet *&resultSet, const MoveStatus &status)
279 {
280 bool result = true;
281 Entry entry;
282 bool mRes = resultSet->Move(status.offSet);
283 result = result && (mRes == status.moveResult);
284 int position = resultSet->GetPosition();
285 result = result && (position == status.currentPosition);
286 DBStatus statusGot = resultSet->GetEntry(entry);
287 result = result && (statusGot == status.status);
288 return result;
289 }
290 }
291
ResultSetDb003(KvStoreNbDelegate * delegate,bool isRowIdMode)292 void DistributeddbNbCursorTestcase::ResultSetDb003(KvStoreNbDelegate *delegate, bool isRowIdMode)
293 {
294 ASSERT_TRUE(delegate != nullptr);
295 SetResultSetCacheMode(delegate, isRowIdMode);
296 std::vector<DistributedDB::Entry> entries;
297 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
298 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
299 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
300 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
301 }
302 /**
303 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
304 * @tc.expected: step1. get success.
305 */
306 KvStoreResultSet *resultSet = nullptr;
307 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
308 /**
309 * @tc.steps: step2. call Move interface move to offset 50 and check the result.
310 * @tc.expected: step2. move ok, and the position is 50, and can get entry.
311 */
312 MoveStatus status1 = {CURSOR_OFFSET_50, true, CURSOR_POSITION_49, OK};
313 EXPECT_TRUE(MoveAndCheck(resultSet, status1));
314 /**
315 * @tc.steps: step3. call Move interface move to offset 0 by upstairs and check the result.
316 * @tc.expected: step3. move ok, and the position is 50, and can get entry.
317 */
318 MoveStatus status2 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_49, OK};
319 EXPECT_TRUE(MoveAndCheck(resultSet, status2));
320 /**
321 * @tc.steps: step4. call Move interface move to offset 60 by upstairs and check the result.
322 * @tc.expected: step4. Move failed, and the position is 100, and can't get entry.
323 */
324 MoveStatus status3 = {CURSOR_OFFSET_60, false, CURSOR_POSITION_100, NOT_FOUND};
325 EXPECT_TRUE(MoveAndCheck(resultSet, status3));
326 /**
327 * @tc.steps: step5. call Move interface move to offset -50 by upstairs and check the result.
328 * @tc.expected: step5. move ok, and the position is 50, and can get entry.
329 */
330 MoveStatus status4 = {CURSOR_OFFSET_NEGATIVE50, true, CURSOR_POSITION_50, OK};
331 EXPECT_TRUE(MoveAndCheck(resultSet, status4));
332 /**
333 * @tc.steps: step6. call Move interface move to offset 0 by upstairs and check the result.
334 * @tc.expected: step6. move ok, and the position is 50, and can get entry.
335 */
336 MoveStatus status5 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_50, OK};
337 EXPECT_TRUE(MoveAndCheck(resultSet, status5));
338 /**
339 * @tc.steps: step7. call Move interface move to offset -60 by upstairs and check the result.
340 * @tc.expected: step7. Move failed, and the position is -1, and can't get entry.
341 */
342 MoveStatus status6 = {CURSOR_OFFSET_NEGATIVE60, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
343 EXPECT_TRUE(MoveAndCheck(resultSet, status6));
344
345 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
346 }
347 namespace {
348 struct PositionStatus02 {
349 int originalPosition = 0;
350 bool moveResult = true;
351 int currentPosition = 0;
352 DBStatus status = OK;
353 };
354
MoveToPositionAndCheck(KvStoreResultSet * & resultSet,const PositionStatus02 & status)355 bool MoveToPositionAndCheck(KvStoreResultSet *&resultSet, const PositionStatus02 &status)
356 {
357 bool result = true;
358 Entry entry;
359 bool mRes = resultSet->MoveToPosition(status.originalPosition);
360 result = result && (mRes == status.moveResult);
361 int position = resultSet->GetPosition();
362 result = result && (position == status.currentPosition);
363 DBStatus statusGot = resultSet->GetEntry(entry);
364 result = result && (statusGot == status.status);
365 return result;
366 }
367 }
ResultSetDb004(KvStoreNbDelegate * delegate,bool isRowIdMode)368 void DistributeddbNbCursorTestcase::ResultSetDb004(KvStoreNbDelegate *delegate, bool isRowIdMode)
369 {
370 ASSERT_TRUE(delegate != nullptr);
371 SetResultSetCacheMode(delegate, isRowIdMode);
372 std::vector<DistributedDB::Entry> entries;
373 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
374 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
375 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
376 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
377 }
378 /**
379 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
380 * @tc.expected: step1. get success.
381 */
382 KvStoreResultSet *resultSet = nullptr;
383 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
384 /**
385 * @tc.steps: step2. call MoveToPostion interface move to position 50 and check the result.
386 * @tc.expected: step2. MoveToPostion ok, and the position is 50, and can get entry.
387 */
388 PositionStatus02 status1 = {CURSOR_ORIGINAL_POSITION_50, true, CURSOR_POSITION_50, OK};
389 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status1));
390 /**
391 * @tc.steps: step3. call MoveToPostion interface move to position 60 and check the result.
392 * @tc.expected: step3. MoveToPostion ok, and the position is 60, and can get entry.
393 */
394 PositionStatus02 status2 = {CURSOR_ORIGINAL_POSITION_60, true, CURSOR_POSITION_60, OK};
395 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status2));
396 /**
397 * @tc.steps: step4. call MoveToPostion interface move to position -10 and check the result.
398 * @tc.expected: step4. Move failed, and the position is -1, and can't get entry.
399 */
400 PositionStatus02 status3 = {CURSOR_ORIGINAL_POSITION_NEGATIVE10, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
401 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status3));
402 /**
403 * @tc.steps: step5. call MoveToPostion interface move to position 120 and check the result.
404 * @tc.expected: step5. Move failed, and the position is 100, and can't get entry.
405 */
406 PositionStatus02 status4 = {CURSOR_ORIGINAL_POSITION_120, false, CURSOR_POSITION_100, NOT_FOUND};
407 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status4));
408 /**
409 * @tc.steps: step6. call MoveToPostion interface move to position 100 and check the result.
410 * @tc.expected: step6. Move failed, and the position is 100, and can't get entry.
411 */
412 PositionStatus02 status5 = {CURSOR_ORIGINAL_POSITION_100, false, CURSOR_POSITION_100, NOT_FOUND};
413 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status5));
414 /**
415 * @tc.steps: step7. call MoveToPostion interface move to position 0 and check the result.
416 * @tc.expected: step7. move OK, and the position is 0, and can get entry.
417 */
418 PositionStatus02 status6 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
419 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status6));
420 /**
421 * @tc.steps: step8. call MoveToPostion interface move to position 99 and check the result.
422 * @tc.expected: step8. move OK, and the position is 99, and can get entry.
423 */
424 PositionStatus02 status7 = {CURSOR_ORIGINAL_POSITION_99, true, CURSOR_POSITION_99, OK};
425 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status7));
426 /**
427 * @tc.steps: step9. call MoveToPostion interface move to position 0 and check the result.
428 * @tc.expected: step9. move OK, and the position is 0, and can get entry.
429 */
430 PositionStatus02 status8 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
431 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status8));
432 /**
433 * @tc.steps: step10. call MoveToPostion interface move to position -1 and check the result.
434 * @tc.expected: step10. Move failed, and the position is -1, and can't get entry.
435 */
436 PositionStatus02 status9 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
437 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status9));
438
439 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
440 }
441
ResultSetDb005(KvStoreNbDelegate * delegate,bool isRowIdMode)442 void DistributeddbNbCursorTestcase::ResultSetDb005(KvStoreNbDelegate *delegate, bool isRowIdMode)
443 {
444 ASSERT_TRUE(delegate != nullptr);
445 SetResultSetCacheMode(delegate, isRowIdMode);
446 std::vector<DistributedDB::Entry> entries;
447 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
448 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
449 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
450
451 /**
452 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
453 * @tc.expected: step1. get success.
454 */
455 KvStoreResultSet *resultSet = nullptr;
456 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
457 /**
458 * @tc.steps: step2. call GetCount interface.
459 * @tc.expected: step2. call success and returned 1 records.
460 */
461 EXPECT_TRUE(resultSet->GetCount() == ONE_RECORD);
462 /**
463 * @tc.steps: step3. call GetPosition, MoveToPrevious and GetPosition interface and check the result.
464 * @tc.expected: step3. GetPosition returned -1, MoveToPrevious returned false, and GetPosition still returned -1.
465 */
466 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
467 EXPECT_TRUE(resultSet->MoveToPrevious() == false);
468 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
469 /**
470 * @tc.steps: step4. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast and GetPostion interface.
471 * @tc.expected: step4. when the Current position is -1, other position judge interface can return right result.
472 */
473 PositionStatus01 positionStatus = {NOT_FOUND, false, false, true, false, CURSOR_POSITION_NEGATIVE1};
474 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
475 /**
476 * @tc.steps: step5. call MoveToFirst interface.
477 * @tc.expected: step5. Move success.
478 */
479 EXPECT_TRUE(resultSet->MoveToFirst() == true);
480 /**
481 * @tc.steps: step6. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
482 * @tc.expected: step6. when the Current position is 0, other position judge interface can return right result.
483 */
484 positionStatus = {OK, true, true, false, false, CURSOR_POSITION_0};
485 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
486 /**
487 * @tc.steps: step7. call MoveToNext interface.
488 * @tc.expected: step7. Move success.
489 */
490 EXPECT_TRUE(resultSet->MoveToNext() == false);
491 /**
492 * @tc.steps: step8. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
493 * @tc.expected: step8. when the Current position is 1, other position judge interface can return right result.
494 */
495 positionStatus = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_1};
496 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
497 /**
498 * @tc.steps: step9. call MoveToLast interface.
499 * @tc.expected: step9. Move success.
500 */
501 EXPECT_TRUE(resultSet->MoveToLast() == true);
502 /**
503 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
504 * @tc.expected: step10. when the Current position is 1, other position judge interface can return right result.
505 */
506 positionStatus = {OK, true, true, false, false, CURSOR_POSITION_0};
507 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
508 /**
509 * @tc.steps: step11. call MoveToNext interface.
510 * @tc.expected: step11. Move success.
511 */
512 EXPECT_TRUE(resultSet->MoveToNext() == false);
513 /**
514 * @tc.steps: step12. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
515 * @tc.expected: step12. when the Current position is 1, other position judge interface can return right result.
516 */
517 positionStatus = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_1};
518 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
519
520 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
521 }
522
ResultSetDb006(KvStoreNbDelegate * delegate,bool isRowIdMode)523 void DistributeddbNbCursorTestcase::ResultSetDb006(KvStoreNbDelegate *delegate, bool isRowIdMode)
524 {
525 ASSERT_TRUE(delegate != nullptr);
526 SetResultSetCacheMode(delegate, isRowIdMode);
527 std::vector<DistributedDB::Entry> entries;
528 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
529 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
530 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
531 /**
532 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
533 * @tc.expected: step1. get success.
534 */
535 KvStoreResultSet *resultSet = nullptr;
536 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
537
538 /**
539 * @tc.steps: step2. set the current position is -1, call MoveToNext, GetPostion and GetEntry interface looply.
540 * @tc.expected: step2. return values are all right.
541 */
542 Entry entry;
543 for (int position = CURSOR_POSITION_NEGATIVE1; position < ONE_RECORD; ++position) {
544 bool result = resultSet->MoveToNext();
545 if (position < (ONE_RECORD - CURSOR_POSITION_1)) {
546 EXPECT_TRUE(result == true);
547 } else {
548 EXPECT_TRUE(result == false);
549 }
550 int currentPosition = resultSet->GetPosition();
551 EXPECT_TRUE(currentPosition == (position + CURSOR_POSITION_1));
552 if (position < (ONE_RECORD - CURSOR_POSITION_1)) {
553 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
554 EXPECT_TRUE(CompareVector(entry.key, entries[position + CURSOR_POSITION_1].key));
555 EXPECT_TRUE(CompareVector(entry.value, entries[position + CURSOR_POSITION_1].value));
556 } else {
557 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
558 }
559 }
560 /**
561 * @tc.steps: step2. set the current position is 100, call MoveToPrevious, GetPostion, GetEntry looply.
562 * @tc.expected: step2. return values are all right.
563 */
564 for (int position = ONE_RECORD; position > CURSOR_POSITION_NEGATIVE1; --position) {
565 bool result = resultSet->MoveToPrevious();
566 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
567 EXPECT_TRUE(result == true);
568 } else {
569 EXPECT_TRUE(result == false);
570 }
571 int currentPosition = resultSet->GetPosition();
572 EXPECT_TRUE(currentPosition == (position - CURSOR_POSITION_1));
573 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
574 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
575 EXPECT_TRUE(CompareVector(entry.key, entries[position - CURSOR_POSITION_1].key));
576 EXPECT_TRUE(CompareVector(entry.value, entries[position - CURSOR_POSITION_1].value));
577 } else {
578 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
579 }
580 }
581 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
582 }
583
ResultSetDb007(KvStoreNbDelegate * delegate,bool isRowIdMode)584 void DistributeddbNbCursorTestcase::ResultSetDb007(KvStoreNbDelegate *delegate, bool isRowIdMode)
585 {
586 ASSERT_TRUE(delegate != nullptr);
587 SetResultSetCacheMode(delegate, isRowIdMode);
588 std::vector<DistributedDB::Entry> entries;
589 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
590 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
591 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
592 /**
593 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
594 * @tc.expected: step1. get success.
595 */
596 KvStoreResultSet *resultSet = nullptr;
597 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
598 /**
599 * @tc.steps: step2. call Move interface move to offset 50 and check the result.
600 * @tc.expected: step2. move ok, and the position is 50, and can get entry.
601 */
602 MoveStatus status1 = {CURSOR_OFFSET_1, true, CURSOR_POSITION_0, OK};
603 EXPECT_TRUE(MoveAndCheck(resultSet, status1));
604 /**
605 * @tc.steps: step3. call Move interface move to offset 0 by upstairs and check the result.
606 * @tc.expected: step3. move ok, and the position is 50, and can get entry.
607 */
608 MoveStatus status2 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_0, OK};
609 EXPECT_TRUE(MoveAndCheck(resultSet, status2));
610 /**
611 * @tc.steps: step4. call Move interface move to offset 60 by upstairs and check the result.
612 * @tc.expected: step4. Move failed, and the position is 100, and can't get entry.
613 */
614 MoveStatus status3 = {CURSOR_OFFSET_2, false, CURSOR_POSITION_1, NOT_FOUND};
615 EXPECT_TRUE(MoveAndCheck(resultSet, status3));
616 /**
617 * @tc.steps: step5. call Move interface move to offset -50 by upstairs and check the result.
618 * @tc.expected: step5. move ok, and the position is 50, and can get entry.
619 */
620 MoveStatus status4 = {CURSOR_OFFSET_NEGATIVE1, true, CURSOR_POSITION_0, OK};
621 EXPECT_TRUE(MoveAndCheck(resultSet, status4));
622 /**
623 * @tc.steps: step6. call Move interface move to offset 0 by upstairs and check the result.
624 * @tc.expected: step6. move ok, and the position is 50, and can get entry.
625 */
626 MoveStatus status5 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_0, OK};
627 EXPECT_TRUE(MoveAndCheck(resultSet, status5));
628 /**
629 * @tc.steps: step7. call Move interface move to offset -60 by upstairs and check the result.
630 * @tc.expected: step7. Move failed, and the position is -1, and can't get entry.
631 */
632 MoveStatus status6 = {CURSOR_OFFSET_NEGATIVE2, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
633 EXPECT_TRUE(MoveAndCheck(resultSet, status6));
634
635 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
636 }
637
ResultSetDb008(KvStoreNbDelegate * delegate,bool isRowIdMode)638 void DistributeddbNbCursorTestcase::ResultSetDb008(KvStoreNbDelegate *delegate, bool isRowIdMode)
639 {
640 ASSERT_TRUE(delegate != nullptr);
641 SetResultSetCacheMode(delegate, isRowIdMode);
642 std::vector<DistributedDB::Entry> entries;
643 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
644 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
645 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
646 /**
647 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
648 * @tc.expected: step1. get success.
649 */
650 KvStoreResultSet *resultSet = nullptr;
651 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
652 /**
653 * @tc.steps: step2. call MoveToPostion interface move to position 0 and check the result.
654 * @tc.expected: step2. MoveToPostion ok, and the position is 50, and can get entry.
655 */
656 PositionStatus02 status1 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
657 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status1));
658 /**
659 * @tc.steps: step3. call MoveToPostion interface move to position 1 and check the result.
660 * @tc.expected: step3. MoveToPostion false, and the position is 1, and can't get entry.
661 */
662 PositionStatus02 status2 = {CURSOR_ORIGINAL_POSITION_1, false, CURSOR_POSITION_1, NOT_FOUND};
663 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status2));
664 /**
665 * @tc.steps: step4. call MoveToPostion interface move to position -1 and check the result.
666 * @tc.expected: step4. Move failed, and the position is -1, and can't get entry.
667 */
668 PositionStatus02 status3 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
669 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status3));
670 /**
671 * @tc.steps: step5. call MoveToPostion interface move to position 2 and check the result.
672 * @tc.expected: step5. Move failed, and the position is 1, and can't get entry.
673 */
674 PositionStatus02 status4 = {CURSOR_ORIGINAL_POSITION_2, false, CURSOR_POSITION_1, NOT_FOUND};
675 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status4));
676 /**
677 * @tc.steps: step6. call MoveToPostion interface move to position 1 and check the result.
678 * @tc.expected: step6. Move failed, and the position is 1, and can't get entry.
679 */
680 PositionStatus02 status5 = {CURSOR_ORIGINAL_POSITION_1, false, CURSOR_POSITION_1, NOT_FOUND};
681 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status5));
682 /**
683 * @tc.steps: step7. call MoveToPostion interface move to position 0 and check the result.
684 * @tc.expected: step7. move OK, and the position is 0, and can get entry.
685 */
686 PositionStatus02 status6 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
687 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status6));
688 /**
689 * @tc.steps: step8. call MoveToPostion interface move to position 1 and check the result.
690 * @tc.expected: step8. Move failed, and the position is 1, and can't get entry.
691 */
692 PositionStatus02 status7 = {CURSOR_ORIGINAL_POSITION_1, false, CURSOR_POSITION_1, NOT_FOUND};
693 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status7));
694 /**
695 * @tc.steps: step9. call MoveToPostion interface move to position -1 and check the result.
696 * @tc.expected: step9. Move failed, and the position is -1, and can't get entry.
697 */
698 PositionStatus02 status8 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
699 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status8));
700
701 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
702 }
703
ResultSetDb009(KvStoreNbDelegate * delegate,bool isRowIdMode)704 void DistributeddbNbCursorTestcase::ResultSetDb009(KvStoreNbDelegate *delegate, bool isRowIdMode)
705 {
706 ASSERT_TRUE(delegate != nullptr);
707 SetResultSetCacheMode(delegate, isRowIdMode);
708 std::vector<DistributedDB::Entry> entries;
709 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
710 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
711 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
712 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
713 }
714
715 /**
716 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
717 * @tc.expected: step1. get success.
718 */
719 KvStoreResultSet *resultSet = nullptr;
720 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
721 /**
722 * @tc.steps: step2. call GetCount interface.
723 * @tc.expected: step2. call success and returned 10 records.
724 */
725 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
726 /**
727 * @tc.steps: step3. call GetPosition, MoveToPrevious and GetPosition interface and check the result.
728 * @tc.expected: step3. GetPosition returned -1, MoveToPrevious returned false, and GetPosition still returned -1.
729 */
730 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
731 EXPECT_TRUE(resultSet->MoveToPrevious() == false);
732 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
733 /**
734 * @tc.steps: step4. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast and GetPostion interface.
735 * @tc.expected: step4. when the Current position is -1, other position judge interface can return right result.
736 */
737 PositionStatus01 position = {NOT_FOUND, false, false, true, false, CURSOR_POSITION_NEGATIVE1};
738 EXPECT_TRUE(JudgePosition(resultSet, position));
739 /**
740 * @tc.steps: step5. call MoveToFirst interface.
741 * @tc.expected: step5. Move success.
742 */
743 EXPECT_TRUE(resultSet->MoveToFirst() == true);
744 /**
745 * @tc.steps: step6. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
746 * @tc.expected: step6. when the Current position is 0, other position judge interface can return right result.
747 */
748 position = {OK, true, false, false, false, CURSOR_POSITION_0};
749 EXPECT_TRUE(JudgePosition(resultSet, position));
750 /**
751 * @tc.steps: step7. call MoveToNext interface.
752 * @tc.expected: step7. Move success.
753 */
754 EXPECT_TRUE(resultSet->MoveToNext() == true);
755 /**
756 * @tc.steps: step8. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
757 * @tc.expected: step8. when the Current position is 1, other position judge interface can return right result.
758 */
759 position = {OK, false, false, false, false, CURSOR_POSITION_1};
760 EXPECT_TRUE(JudgePosition(resultSet, position));
761 /**
762 * @tc.steps: step9. call MoveToLast interface.
763 * @tc.expected: step9. Move success.
764 */
765 EXPECT_TRUE(resultSet->MoveToLast() == true);
766 /**
767 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
768 * @tc.expected: step10. when the Current position is 9, other position judge interface can return right result.
769 */
770 position = {OK, false, true, false, false, CURSOR_POSITION_99};
771 EXPECT_TRUE(JudgePosition(resultSet, position));
772 /**
773 * @tc.steps: step11. call MoveToNext interface.
774 * @tc.expected: step11. Move success.
775 */
776 EXPECT_TRUE(resultSet->MoveToNext() == false);
777 /**
778 * @tc.steps: step12. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
779 * @tc.expected: step12. when the Current position is 10, other position judge interface can return right result.
780 */
781 position = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_100};
782 EXPECT_TRUE(JudgePosition(resultSet, position));
783
784 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
785 }
786
ResultSetDb010(KvStoreNbDelegate * delegate,bool isRowIdMode)787 void DistributeddbNbCursorTestcase::ResultSetDb010(KvStoreNbDelegate *delegate, bool isRowIdMode)
788 {
789 ASSERT_TRUE(delegate != nullptr);
790 SetResultSetCacheMode(delegate, isRowIdMode);
791 std::vector<DistributedDB::Entry> entries;
792 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
793 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
794 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
795 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
796 }
797
798 /**
799 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
800 * @tc.expected: step1. get success.
801 */
802 KvStoreResultSet *resultSet = nullptr;
803 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
804 sort(entries.begin(), entries.end(), DistributedTestTools::CompareKey);
805 /**
806 * @tc.steps: step2. set the current position is -1, call MoveToNext, GetPostion and GetEntry interface looply.
807 * @tc.expected: step2. return values are all right.
808 */
809 EXPECT_TRUE(DistributedDBNbTestTools::MoveToNextFromBegin(*resultSet, entries, CURSOR_POSITION_100));
810 /**
811 * @tc.steps: step3. set the current position is 100, call MoveToPrevious, GetPostion, GetEntry looply.
812 * @tc.expected: step3. return values are all right.
813 */
814 int currentPosition = CURSOR_POSITION_NEGATIVE1;
815 Entry entry;
816 for (int position = ONE_HUNDRED_RECORDS; position > CURSOR_POSITION_NEGATIVE1; --position) {
817 bool result = resultSet->MoveToPrevious();
818 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
819 EXPECT_TRUE(result == true);
820 } else {
821 EXPECT_TRUE(result == false);
822 }
823 currentPosition = resultSet->GetPosition();
824 EXPECT_TRUE(currentPosition == (position - CURSOR_POSITION_1));
825 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
826 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
827 EXPECT_TRUE(CompareVector(entry.key, entries[position - CURSOR_POSITION_1].key));
828 EXPECT_TRUE(CompareVector(entry.value, entries[position - CURSOR_POSITION_1].value));
829 } else {
830 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
831 }
832 }
833 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
834 }
835
ResultSetDb011(KvStoreNbDelegate * delegate,bool isRowIdMode)836 void DistributeddbNbCursorTestcase::ResultSetDb011(KvStoreNbDelegate *delegate, bool isRowIdMode)
837 {
838 ASSERT_TRUE(delegate != nullptr);
839 SetResultSetCacheMode(delegate, isRowIdMode);
840 std::vector<DistributedDB::Entry> entries;
841 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
842 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
843 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
844 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
845 }
846 /**
847 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
848 * @tc.expected: step1. get success.
849 */
850 KvStoreResultSet *resultSet = nullptr;
851 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
852 /**
853 * @tc.steps: step2. call Move interface move to offset 45 and check the result.
854 * @tc.expected: step2. move ok, and the position is 44, and can get entry.
855 */
856 MoveStatus status1 = {CURSOR_OFFSET_45, true, CURSOR_POSITION_44, OK};
857 EXPECT_TRUE(MoveAndCheck(resultSet, status1));
858 /**
859 * @tc.steps: step3. call Move interface move to offset 0 by upstairs and check the result.
860 * @tc.expected: step3. move ok, and the position is 44, and can get entry.
861 */
862 MoveStatus status2 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_44, OK};
863 EXPECT_TRUE(MoveAndCheck(resultSet, status2));
864 /**
865 * @tc.steps: step4. call Move interface move to offset 65 by upstairs and check the result.
866 * @tc.expected: step4. Move failed, and the position is 100, and can't get entry.
867 */
868 MoveStatus status3 = {CURSOR_OFFSET_55, true, CURSOR_POSITION_99, OK};
869 EXPECT_TRUE(MoveAndCheck(resultSet, status3));
870 MoveStatus status4 = {CURSOR_OFFSET_65, false, CURSOR_POSITION_100, NOT_FOUND};
871 EXPECT_TRUE(MoveAndCheck(resultSet, status4));
872 /**
873 * @tc.steps: step5. call Move interface move to offset -45 by upstairs and check the result.
874 * @tc.expected: step5. move ok, and the position is 55, and can get entry.
875 */
876 MoveStatus status5 = {CURSOR_OFFSET_NEGATIVE45, true, CURSOR_POSITION_55, OK};
877 EXPECT_TRUE(MoveAndCheck(resultSet, status5));
878 /**
879 * @tc.steps: step6. call Move interface move to offset 0 by upstairs and check the result.
880 * @tc.expected: step6. move ok, and the position is 50, and can get entry.
881 */
882 MoveStatus status6 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_55, OK};
883 EXPECT_TRUE(MoveAndCheck(resultSet, status6));
884 /**
885 * @tc.steps: step7. call Move interface move to offset -65 by upstairs and check the result.
886 * @tc.expected: step7. Move failed, and the position is -1, and can't get entry.
887 */
888 MoveStatus status7 = {CURSOR_OFFSET_NEGATIVE65, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
889 EXPECT_TRUE(MoveAndCheck(resultSet, status7));
890
891 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
892 }
893
ResultSetDb012(KvStoreNbDelegate * delegate,bool isRowIdMode)894 void DistributeddbNbCursorTestcase::ResultSetDb012(KvStoreNbDelegate *delegate, bool isRowIdMode)
895 {
896 ASSERT_TRUE(delegate != nullptr);
897 SetResultSetCacheMode(delegate, isRowIdMode);
898 std::vector<DistributedDB::Entry> entries;
899 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
900 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
901 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
902 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
903 }
904 /**
905 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
906 * @tc.expected: step1. get success.
907 */
908 KvStoreResultSet *resultSet = nullptr;
909 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
910 /**
911 * @tc.steps: step2. call MoveToPostion interface move to position 40 and check the result.
912 * @tc.expected: step2. Move OK, and the position is 40, and can get entry.
913 */
914 PositionStatus02 status1 = {CURSOR_ORIGINAL_POSITION_40, true, CURSOR_POSITION_40, OK};
915 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status1));
916 /**
917 * @tc.steps: step3. call MoveToPostion interface move to position 60 and check the result.
918 * @tc.expected: step3. Move failed, and the position is 60, and can't get entry.
919 */
920 PositionStatus02 status2 = {CURSOR_ORIGINAL_POSITION_60, true, CURSOR_POSITION_60, OK};
921 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status2));
922 /**
923 * @tc.steps: step4. call MoveToPostion interface move to position -1 and check the result.
924 * @tc.expected: step4. Move failed, and the position is -1, and can't get entry.
925 */
926 PositionStatus02 status3 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
927 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status3));
928 /**
929 * @tc.steps: step5. call MoveToPostion interface move to position 120 and check the result.
930 * @tc.expected: step5. Move failed, and the position is 100, and can't get entry.
931 */
932 PositionStatus02 status4 = {CURSOR_ORIGINAL_POSITION_120, false, CURSOR_POSITION_100, NOT_FOUND};
933 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status4));
934 /**
935 * @tc.steps: step6. call MoveToPostion interface move to position 100 and check the result.
936 * @tc.expected: step6. Move failed, and the position is 100, and can't get entry.
937 */
938 PositionStatus02 status5 = {CURSOR_ORIGINAL_POSITION_100, false, CURSOR_POSITION_100, NOT_FOUND};
939 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status5));
940 /**
941 * @tc.steps: step7. call MoveToPostion interface move to position 0 and check the result.
942 * @tc.expected: step7. move OK, and the position is 0, and can get entry.
943 */
944 PositionStatus02 status6 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
945 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status6));
946 /**
947 * @tc.steps: step8. call MoveToPostion interface move to position 99 and check the result.
948 * @tc.expected: step8. move OK, and the position is 99, and can get entry.
949 */
950 PositionStatus02 status7 = {CURSOR_ORIGINAL_POSITION_99, true, CURSOR_POSITION_99, OK};
951 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status7));
952 /**
953 * @tc.steps: step9. call MoveToPostion interface move to position -1 and check the result.
954 * @tc.expected: step9. Move failed, and the position is -1, and can't get entry.
955 */
956 PositionStatus02 status8 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
957 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status8));
958
959 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
960 }
961
ResultSetDb013(KvStoreNbDelegate * delegate,bool isRowIdMode)962 void DistributeddbNbCursorTestcase::ResultSetDb013(KvStoreNbDelegate *delegate, bool isRowIdMode)
963 {
964 ASSERT_TRUE(delegate != nullptr);
965 SetResultSetCacheMode(delegate, isRowIdMode);
966 std::vector<DistributedDB::Entry> entries;
967 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
968 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
969 Entry entry4M;
970 Entry entry2M;
971 entry4M.key.assign(KEY_SIX_BYTE, 'k');
972 entry4M.value.assign(FOUR_M_LONG_STRING, 'v');
973 entry2M.key.assign(KEY_THIRTYTWO_BYTE, 'k');
974 entry2M.value.assign(TWO_M_LONG_STRING, 'v');
975 entries.push_back(entry4M);
976 entries.push_back(entry2M);
977 for (unsigned int index = 0; index < CURSOR_DIFFERENT_RECORDS; index++) {
978 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
979 }
980 /**
981 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
982 * @tc.expected: step1. get success.
983 */
984 KvStoreResultSet *resultSet = nullptr;
985 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
986 /**
987 * @tc.steps: step2. call GetCount interface get number of records of cursor.
988 * @tc.expected: step2. the number is 102.
989 */
990 EXPECT_TRUE(resultSet->GetCount() == CURSOR_DIFFERENT_RECORDS);
991 unsigned int position = 0;
992 while (position < CURSOR_DIFFERENT_RECORDS) {
993 /**
994 * @tc.steps: step3. call MoveToNext interface and check the result.
995 * @tc.expected: step3. move ok.
996 */
997 if (position != (CURSOR_DIFFERENT_RECORDS - CURSOR_POSITION_1)) {
998 EXPECT_TRUE(resultSet->MoveToNext());
999 position = resultSet->GetPosition();
1000 /**
1001 * @tc.steps: step4. call Move interface by 1 offset and check the result.
1002 * @tc.expected: step4. move ok.
1003 */
1004 EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_1));
1005 /**
1006 * @tc.steps: step5. call GetPosition interface by 1 offset and check the result.
1007 * @tc.expected: step5. GetPosition ok and the position increased by 1 each loop.
1008 */
1009 position = resultSet->GetPosition();
1010 EXPECT_TRUE(resultSet->MoveToPosition(++position));
1011 } else {
1012 EXPECT_FALSE(resultSet->MoveToNext());
1013 /**
1014 * @tc.steps: step4. call Move interface by 1 offset and check the result.
1015 * @tc.expected: step4. move ok.
1016 */
1017 EXPECT_FALSE(resultSet->Move(CURSOR_OFFSET_1));
1018 /**
1019 * @tc.steps: step5. call GetPosition interface by 1 offset and check the result.
1020 * @tc.expected: step5. GetPosition ok and the position increased by 1 each loop.
1021 */
1022 position = resultSet->GetPosition();
1023 EXPECT_FALSE(resultSet->MoveToPosition(++position));
1024 }
1025 }
1026
1027 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
1028 }
1029
ResultSetDb014(KvStoreNbDelegate * delegate,bool isRowIdMode)1030 void DistributeddbNbCursorTestcase::ResultSetDb014(KvStoreNbDelegate *delegate, bool isRowIdMode)
1031 {
1032 ASSERT_TRUE(delegate != nullptr);
1033 SetResultSetCacheMode(delegate, isRowIdMode);
1034 std::vector<DistributedDB::Entry> entries;
1035 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
1036 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
1037 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1038 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
1039 }
1040 /**
1041 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet with the prefix = { 'a' }.
1042 * @tc.expected: step1. get KvStoreResultSet success.
1043 */
1044 KvStoreResultSet *resultSet = nullptr;
1045 EXPECT_EQ(delegate->GetEntries(KEY_A, resultSet), OK);
1046 /**
1047 * @tc.steps: step2. call GetCount interface get number of records of cursor.
1048 * @tc.expected: step2. the number is 0.
1049 */
1050 EXPECT_TRUE(resultSet->GetCount() == NO_RECORD);
1051 /**
1052 * @tc.steps: step3. call IsFirst interface check whether the current position is first.
1053 * @tc.expected: step3. return false.
1054 */
1055 EXPECT_TRUE(resultSet->IsFirst() == false);
1056 /**
1057 * @tc.steps: step4. call IsLast interface check whether the current position is last.
1058 * @tc.expected: step4. return false.
1059 */
1060 EXPECT_TRUE(resultSet->IsLast() == false);
1061 /**
1062 * @tc.steps: step5. call MoveToFirst interface check whether it can MoveToFirst.
1063 * @tc.expected: step5. return false.
1064 */
1065 EXPECT_TRUE(resultSet->MoveToFirst() == false);
1066 /**
1067 * @tc.steps: step6. call MoveToLast interface check whether it can MoveToLast.
1068 * @tc.expected: step6. return false.
1069 */
1070 EXPECT_TRUE(resultSet->MoveToLast() == false);
1071 /**
1072 * @tc.steps: step7. call IsBeforeFirst interface check whether it can MoveToLast.
1073 * @tc.expected: step7. return false.
1074 */
1075 EXPECT_TRUE(resultSet->IsBeforeFirst() == true);
1076 /**
1077 * @tc.steps: step8. call MoveToNext first and then call IsBeforeFirst interface check the result.
1078 * @tc.expected: step8. MoveToNext can returns ok, but IsBeforeFirst still returns false.
1079 */
1080 EXPECT_TRUE(resultSet->MoveToNext() == false);
1081 EXPECT_TRUE(resultSet->IsBeforeFirst() == true);
1082 /**
1083 * @tc.steps: step9. call IsAfterLast interface check whether it can MoveToLast.
1084 * @tc.expected: step9. return false.
1085 */
1086 EXPECT_TRUE(resultSet->IsAfterLast() == true);
1087 /**
1088 * @tc.steps: step10. call MoveToPrevious first and then call IsAfterLast interface check the result.
1089 * @tc.expected: step10. MoveToPrevious can returns ok, but IsAfterLast still returns false.
1090 */
1091 EXPECT_TRUE(resultSet->MoveToPrevious() == false);
1092 EXPECT_TRUE(resultSet->IsAfterLast() == true);
1093
1094 /**
1095 * @tc.steps: step11. close KvStoreResultSet.
1096 * @tc.expected: step11. close success.
1097 */
1098 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
1099 }
1100
ResultSetDb015(KvStoreNbDelegate * delegate,bool isRowIdMode)1101 void DistributeddbNbCursorTestcase::ResultSetDb015(KvStoreNbDelegate *delegate, bool isRowIdMode)
1102 {
1103 ASSERT_TRUE(delegate != nullptr);
1104 SetResultSetCacheMode(delegate, isRowIdMode);
1105 std::vector<DistributedDB::Entry> entries;
1106 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
1107 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
1108 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1109 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
1110 }
1111 /**
1112 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet with the prefix = { 'a' }.
1113 * @tc.expected: step1. get KvStoreResultSet success.
1114 */
1115 Entry entry;
1116 KvStoreResultSet *resultSet = nullptr;
1117 EXPECT_EQ(delegate->GetEntries(KEY_A, resultSet), OK);
1118 /**
1119 * @tc.steps: step2. call GetCount interface get number of records of cursor.
1120 * @tc.expected: step2. the number is 0.
1121 */
1122 EXPECT_TRUE(resultSet->GetCount() == NO_RECORD);
1123 /**
1124 * @tc.steps: step3. call Move interface with the offset is 0.
1125 * @tc.expected: step3. return false.
1126 */
1127 EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_0) == false);
1128 /**
1129 * @tc.steps: step4. call GetPosition interface to check the current position and GetEntry to check the Entry.
1130 * @tc.expected: step4. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1131 */
1132 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
1133 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
1134 /**
1135 * @tc.steps: step5. call Move interface with the offset is -1.
1136 * @tc.expected: step5. return false.
1137 */
1138 EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_NEGATIVE1) == false);
1139 /**
1140 * @tc.steps: step6. call GetPosition interface to check the current position and GetEntry to check the Entry.
1141 * @tc.expected: step6. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1142 */
1143 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
1144 EXPECT_EQ(resultSet->GetEntry(entry), NOT_FOUND);
1145 /**
1146 * @tc.steps: step7. call Move interface with the offset is 5.
1147 * @tc.expected: step7. return false.
1148 */
1149 EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_5) == false);
1150 /**
1151 * @tc.steps: step8. call GetPosition interface to check the current position and GetEntry to check the Entry.
1152 * @tc.expected: step8. GetPosition returns 0, and GetEntry returns NOT_FOUND.
1153 */
1154 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_0);
1155 EXPECT_EQ(resultSet->GetEntry(entry), NOT_FOUND);
1156
1157 /**
1158 * @tc.steps: step11. close KvStoreResultSet.
1159 * @tc.expected: step11. close success.
1160 */
1161 EXPECT_EQ(delegate->CloseResultSet(resultSet), OK);
1162 }
1163
ResultSetDb016(KvStoreNbDelegate * delegate,bool isRowIdMode)1164 void DistributeddbNbCursorTestcase::ResultSetDb016(KvStoreNbDelegate *delegate, bool isRowIdMode)
1165 {
1166 ASSERT_TRUE(delegate != nullptr);
1167 SetResultSetCacheMode(delegate, isRowIdMode);
1168 std::vector<DistributedDB::Entry> entries;
1169 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
1170 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
1171 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1172 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
1173 }
1174 /**
1175 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet with the prefix = { 'a' }.
1176 * @tc.expected: step1. get KvStoreResultSet success.
1177 */
1178 Entry entryGot;
1179 KvStoreResultSet *resultSet = nullptr;
1180 EXPECT_EQ(delegate->GetEntries(KEY_A, resultSet), OK);
1181 /**
1182 * @tc.steps: step2. call GetCount interface get number of records of cursor.
1183 * @tc.expected: step2. the number is 0.
1184 */
1185 EXPECT_TRUE(resultSet->GetCount() == NO_RECORD);
1186 /**
1187 * @tc.steps: step3. call MoveToPosition interface to move to position 0.
1188 * @tc.expected: step3. return false.
1189 */
1190 EXPECT_TRUE(resultSet->MoveToPosition(CURSOR_POSITION_0) == false);
1191 /**
1192 * @tc.steps: step4. call GetPosition interface to check the current position and GetEntry to check the Entry.
1193 * @tc.expected: step4. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1194 */
1195 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_0);
1196 EXPECT_EQ(resultSet->GetEntry(entryGot), NOT_FOUND);
1197 /**
1198 * @tc.steps: step5. call MoveToPosition interface to move to position 2.
1199 * @tc.expected: step5. return false.
1200 */
1201 EXPECT_TRUE(resultSet->MoveToPosition(CURSOR_POSITION_2) == false);
1202 /**
1203 * @tc.steps: step6. call GetPosition interface to check the current position and GetEntry to check the Entry.
1204 * @tc.expected: step6. GetPosition returns 0, and GetEntry returns NOT_FOUND.
1205 */
1206 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_0);
1207 EXPECT_EQ(resultSet->GetEntry(entryGot), NOT_FOUND);
1208 /**
1209 * @tc.steps: step7. call MoveToPosition interface to move to position -5.
1210 * @tc.expected: step7. return false.
1211 */
1212 EXPECT_TRUE(resultSet->MoveToPosition(CURSOR_POSITION_NEGATIVE5) == false);
1213 /**
1214 * @tc.steps: step8. call GetPosition interface to check the current position and GetEntry to check the Entry.
1215 * @tc.expected: step8. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1216 */
1217 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
1218 EXPECT_EQ(resultSet->GetEntry(entryGot), NOT_FOUND);
1219
1220 /**
1221 * @tc.steps: step11. close KvStoreResultSet.
1222 * @tc.expected: step11. close success.
1223 */
1224 EXPECT_EQ(delegate->CloseResultSet(resultSet), OK);
1225 }
1226
ResultSetDb017(KvStoreNbDelegate * delegate,bool isRowIdMode)1227 void DistributeddbNbCursorTestcase::ResultSetDb017(KvStoreNbDelegate *delegate, bool isRowIdMode)
1228 {
1229 ASSERT_TRUE(delegate != nullptr);
1230 SetResultSetCacheMode(delegate, isRowIdMode);
1231 vector<Entry> entries;
1232 vector<Key> allKey;
1233 GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1234 for (const auto &iter : entries) {
1235 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1236 }
1237 /**
1238 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1239 * @tc.expected: step1. get success.
1240 */
1241 KvStoreResultSet *resultSetAll = nullptr;
1242 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1243 /**
1244 * @tc.steps: step2. call CloseResultSet interface with nullptr parameter.
1245 * @tc.expected: step2. return INVALID_ARGS.
1246 */
1247 KvStoreResultSet *resultSetAllptr = nullptr;
1248 EXPECT_EQ(delegate->CloseResultSet(resultSetAllptr), INVALID_ARGS);
1249 /**
1250 * @tc.steps: step3. call CloseResultSet interface with resultSetAll.
1251 * @tc.expected: step3. return OK.
1252 */
1253 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1254 }
1255
ResultSetDb018(KvStoreNbDelegate * delegate,bool isRowIdMode)1256 void DistributeddbNbCursorTestcase::ResultSetDb018(KvStoreNbDelegate *delegate, bool isRowIdMode)
1257 {
1258 ASSERT_TRUE(delegate != nullptr);
1259 SetResultSetCacheMode(delegate, isRowIdMode);
1260 vector<Entry> entriesKA;
1261 vector<Entry> entriesKB;
1262 vector<Key> allKeysKA;
1263 vector<Key> allKeysKB;
1264 std::vector<uint8_t> ka = { 'k', 'a' };
1265 std::vector<uint8_t> kb = { 'k', 'b' };
1266 GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKeysKA, entriesKA, ka);
1267 GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKeysKB, entriesKB, kb);
1268 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1269 EXPECT_EQ(delegate->Put(entriesKA[index].key, entriesKA[index].value), OK);
1270 EXPECT_EQ(delegate->Put(entriesKB[index].key, entriesKB[index].value), OK);
1271 }
1272 /**
1273 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1274 * @tc.expected: step1. get success.
1275 */
1276 KvStoreResultSet *resultSetAll = nullptr;
1277 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1278 /**
1279 * @tc.steps: step2. call GetEntries interface with "ka" parameter to get KvStoreResultSet.
1280 * @tc.expected: step2. get success.
1281 */
1282 KvStoreResultSet *resultSetKA = nullptr;
1283 Key keyPrefixKA = ka;
1284 EXPECT_EQ(delegate->GetEntries(keyPrefixKA, resultSetKA), OK);
1285 /**
1286 * @tc.steps: step3. call GetEntries interface with "kb" parameter to get KvStoreResultSet.
1287 * @tc.expected: step3. get success.
1288 */
1289 KvStoreResultSet *resultSetKB = nullptr;
1290 Key keyPrefixKB = kb;
1291 EXPECT_EQ(delegate->GetEntries(keyPrefixKB, resultSetKB), OK);
1292 /**
1293 * @tc.steps: step4. call GetCount interface of all recordsets.
1294 * @tc.expected: step4. call success.
1295 */
1296 EXPECT_TRUE(resultSetAll->GetCount() == TWO_HUNDREDS_RECORDS);
1297 EXPECT_TRUE(resultSetKA->GetCount() == ONE_HUNDRED_RECORDS);
1298 EXPECT_TRUE(resultSetKB->GetCount() == ONE_HUNDRED_RECORDS);
1299 /**
1300 * @tc.steps: step5. close resultSetAll.
1301 * @tc.expected: step5. call success.
1302 */
1303 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1304 /**
1305 * @tc.steps: step6. close resultSetAll.
1306 * @tc.expected: step6. call success.
1307 */
1308 EXPECT_TRUE(resultSetKA->GetCount() == ONE_HUNDRED_RECORDS);
1309 EXPECT_TRUE(resultSetKB->GetCount() == ONE_HUNDRED_RECORDS);
1310
1311 EXPECT_EQ(delegate->CloseResultSet(resultSetKA), OK);
1312 EXPECT_EQ(delegate->CloseResultSet(resultSetKB), OK);
1313 }
1314
ResultSetDb019(KvStoreNbDelegate * delegate,bool isRowIdMode)1315 void DistributeddbNbCursorTestcase::ResultSetDb019(KvStoreNbDelegate *delegate, bool isRowIdMode)
1316 {
1317 ASSERT_TRUE(delegate != nullptr);
1318 SetResultSetCacheMode(delegate, isRowIdMode);
1319 vector<Entry> entriesBatch;
1320 vector<Key> allKeys;
1321 GenerateFixedRecords(entriesBatch, allKeys, TEN_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1322 for (const auto &iter : entriesBatch) {
1323 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1324 }
1325 /**
1326 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1327 * @tc.expected: step1. get success.
1328 */
1329 KvStoreResultSet *resultSetAll = nullptr;
1330 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1331 /**
1332 * @tc.steps: step2. call GetCount interface of resultSetAll and delete k1~k5.
1333 * @tc.expected: step2. call success.
1334 */
1335 EXPECT_TRUE(resultSetAll->GetCount() == TEN_RECORDS);
1336 for (unsigned int delCnt = 0; delCnt < FIVE_RECORDS; ++delCnt) {
1337 EXPECT_EQ(DistributedDBNbTestTools::Delete(*delegate, entriesBatch[0].key), OK);
1338 entriesBatch.erase(entriesBatch.begin());
1339 }
1340 /**
1341 * @tc.steps: step3. update k6 and insert another 10 * 1M records.
1342 * @tc.expected: step3. call success.
1343 */
1344 entriesBatch[0].value.push_back('a');
1345 EXPECT_EQ(delegate->Put(entriesBatch[0].key, entriesBatch[0].value), OK);
1346 vector<Entry> entriesBatch2;
1347 vector<Key> allKeys2;
1348 GenerateFixedRecords(entriesBatch2, allKeys2, TEN_RECORDS, KEY_SIX_BYTE, ONE_M_LONG_STRING);
1349 for (const auto &iter : entriesBatch2) {
1350 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1351 }
1352 /**
1353 * @tc.steps: step4. call GetEntries interface with "" parameter to get KvStoreResultSet.
1354 * @tc.expected: step4. get success.
1355 */
1356 KvStoreResultSet *resultSetAll2 = nullptr;
1357 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll2), OK);
1358 /**
1359 * @tc.steps: step5. close resultSetAll.
1360 * @tc.expected: step5. call success.
1361 */
1362 EXPECT_TRUE(resultSetAll->GetCount() == TEN_RECORDS);
1363 EXPECT_TRUE(resultSetAll2->GetCount() == FIFTEEN_RECORDS);
1364
1365 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1366 EXPECT_EQ(delegate->CloseResultSet(resultSetAll2), OK);
1367 }
1368 namespace ResultSetDbNS {
ExecuteResultSetDb020(KvStoreNbDelegate * & delegate)1369 void ExecuteResultSetDb020(KvStoreNbDelegate *&delegate)
1370 {
1371 /**
1372 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1373 * @tc.expected: step1. get success.
1374 */
1375 KvStoreResultSet *resultSetAll = nullptr;
1376 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1377 /**
1378 * @tc.steps: step2. call GetEntries interface with "ka" parameter to get KvStoreResultSet.
1379 * @tc.expected: step2. get success.
1380 */
1381 KvStoreResultSet *resultSetKA = nullptr;
1382 std::vector<uint8_t> ka = { 'k', 'a' };
1383 Key keyPrefixKA = ka;
1384 EXPECT_EQ(delegate->GetEntries(keyPrefixKA, resultSetKA), OK);
1385 /**
1386 * @tc.steps: step3. call GetEntries interface with "kb" parameter to get KvStoreResultSet.
1387 * @tc.expected: step3. get success.
1388 */
1389 KvStoreResultSet *resultSetKB = nullptr;
1390 std::vector<uint8_t> kb = { 'k', 'b' };
1391 Key keyPrefixKB = kb;
1392 EXPECT_EQ(delegate->GetEntries(keyPrefixKB, resultSetKB), OK);
1393 /**
1394 * @tc.steps: step3. call GetEntries interface with "kc" parameter to get KvStoreResultSet.
1395 * @tc.expected: step3. get success.
1396 */
1397 KvStoreResultSet *resultSetKC = nullptr;
1398 std::vector<uint8_t> kc = { 'k', 'c' };
1399 Key keyPrefixKC = kc;
1400 EXPECT_EQ(delegate->GetEntries(keyPrefixKC, resultSetKC), OK);
1401 /**
1402 * @tc.steps: step4. call GetEntries interface with "kd" parameter to get KvStoreResultSet.
1403 * @tc.expected: step4. get success.
1404 */
1405 KvStoreResultSet *resultSetKD = nullptr;
1406 std::vector<uint8_t> kd = { 'k', 'd' };
1407 Key keyPrefixKD = kd;
1408 EXPECT_EQ(delegate->GetEntries(keyPrefixKD, resultSetKD), OK);
1409 /**
1410 * @tc.steps: step5. call GetEntries interface with "ke" parameter to get KvStoreResultSet.
1411 * @tc.expected: step5. get success.
1412 */
1413 KvStoreResultSet *resultSetKE = nullptr;
1414 std::vector<uint8_t> ke = { 'k', 'e' };
1415 Key keyPrefixKE = ke;
1416 EXPECT_EQ(delegate->GetEntries(keyPrefixKE, resultSetKE), OK);
1417 /**
1418 * @tc.steps: step6. call GetEntries interface with "kf" parameter to get KvStoreResultSet.
1419 * @tc.expected: step6. get success.
1420 */
1421 KvStoreResultSet *resultSetKF = nullptr;
1422 std::vector<uint8_t> kf = { 'k', 'f' };
1423 Key keyPrefixKF = kf;
1424 EXPECT_EQ(delegate->GetEntries(keyPrefixKF, resultSetKF), OK);
1425
1426 /**
1427 * @tc.steps: step7. call GetEntries interface with "kg" parameter to get KvStoreResultSet.
1428 * @tc.expected: step7. get success.
1429 */
1430 KvStoreResultSet *resultSetKG = nullptr;
1431 std::vector<uint8_t> kg = { 'k', 'g' };
1432 Key keyPrefixKG = kg;
1433 EXPECT_EQ(delegate->GetEntries(keyPrefixKG, resultSetKG), OK);
1434 /**
1435 * @tc.steps: step8. call GetEntries interface with "" parameter to get KvStoreResultSet.
1436 * @tc.expected: step8. get success.
1437 */
1438 KvStoreResultSet *resultSetAll2 = nullptr;
1439 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll2), OVER_MAX_LIMITS);
1440
1441 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1442 EXPECT_EQ(delegate->CloseResultSet(resultSetKA), OK);
1443 EXPECT_EQ(delegate->CloseResultSet(resultSetKB), OK);
1444 EXPECT_EQ(delegate->CloseResultSet(resultSetKC), OK);
1445 EXPECT_EQ(delegate->CloseResultSet(resultSetKD), OK);
1446 EXPECT_EQ(delegate->CloseResultSet(resultSetKE), OK);
1447 EXPECT_EQ(delegate->CloseResultSet(resultSetKF), OK);
1448 EXPECT_EQ(delegate->CloseResultSet(resultSetKG), OK);
1449 if (resultSetAll2 != nullptr) {
1450 EXPECT_EQ(delegate->CloseResultSet(resultSetAll2), OK);
1451 }
1452 }
1453 }
ResultSetDb020(KvStoreNbDelegate * delegate,bool isRowIdMode)1454 void DistributeddbNbCursorTestcase::ResultSetDb020(KvStoreNbDelegate *delegate, bool isRowIdMode)
1455 {
1456 ASSERT_TRUE(delegate != nullptr);
1457 SetResultSetCacheMode(delegate, isRowIdMode);
1458 vector<Entry> entries;
1459 vector<Key> allKey;
1460 GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1461 for (const auto &iter : entries) {
1462 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1463 }
1464 ResultSetDbNS::ExecuteResultSetDb020(delegate);
1465 }
1466
ResultSetDb021(KvStoreNbDelegate * delegate,KvStoreDelegateManager * manager,bool isRowIdMode)1467 void DistributeddbNbCursorTestcase::ResultSetDb021(KvStoreNbDelegate *delegate,
1468 KvStoreDelegateManager *manager, bool isRowIdMode)
1469 {
1470 ASSERT_TRUE(delegate != nullptr && manager != nullptr);
1471 SetResultSetCacheMode(delegate, isRowIdMode);
1472 vector<Entry> entries;
1473 vector<Key> allKey;
1474 GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1475 for (const auto &iter : entries) {
1476 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1477 }
1478 /**
1479 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1480 * @tc.expected: step1. get success.
1481 */
1482 KvStoreResultSet *resultSetAll = nullptr;
1483 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1484 /**
1485 * @tc.steps: step2. call GetCount interface of resultSetAll.
1486 * @tc.expected: step2. call success.
1487 */
1488 EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1489 /**
1490 * @tc.steps: step3. closeKvStore returns BUSY because resultSet was not closed.
1491 * @tc.expected: step3. call success.
1492 */
1493 EXPECT_EQ(manager->CloseKvStore(delegate), BUSY);
1494
1495 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1496 }
1497
ResultSetDb022(bool isRowIdMode)1498 void DistributeddbNbCursorTestcase::ResultSetDb022(bool isRowIdMode)
1499 {
1500 KvStoreDelegateManager *manager = nullptr;
1501 KvStoreNbDelegate *delegate = nullptr;
1502 Option option;
1503 option.isEncryptedDb = false;
1504 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1505 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1506 SetResultSetCacheMode(delegate, isRowIdMode);
1507
1508 vector<Entry> entriesBatch;
1509 vector<Key> allKeys;
1510 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1511 for (const auto &iter : entriesBatch) {
1512 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1513 }
1514 /**
1515 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1516 * @tc.expected: step1. get success.
1517 */
1518 KvStoreResultSet *resultSetAll = nullptr;
1519 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSetAll) == OK);
1520 /**
1521 * @tc.steps: step2. call GetCount interface of resultSetAll.
1522 * @tc.expected: step2. call success.
1523 */
1524 EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1525 /**
1526 * @tc.steps: step3. Rekey with g_passwd1.
1527 * @tc.expected: step3. call success.
1528 */
1529 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1530 EXPECT_EQ(delegate->Rekey(g_passwd1), BUSY);
1531
1532 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1533 EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1534 delegate = nullptr;
1535 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), OK);
1536 delete manager;
1537 manager = nullptr;
1538 }
1539
ResultSetDb023(bool isRowIdMode)1540 void DistributeddbNbCursorTestcase::ResultSetDb023(bool isRowIdMode)
1541 {
1542 KvStoreDelegateManager *manager = nullptr;
1543 KvStoreNbDelegate *delegate = nullptr;
1544 Option option;
1545 option.isEncryptedDb = false;
1546 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1547 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1548 SetResultSetCacheMode(delegate, isRowIdMode);
1549
1550 vector<Entry> entriesBatch;
1551 vector<Key> allKeys;
1552 GenerateFixedRecords(entriesBatch, allKeys, FOUR_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1553 for (const auto &iter : entriesBatch) {
1554 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1555 }
1556 /**
1557 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1558 * @tc.expected: step1. get success.
1559 */
1560 KvStoreResultSet *resultSetAll = nullptr;
1561 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1562 /**
1563 * @tc.steps: step2. call GetCount interface of resultSetAll.
1564 * @tc.expected: step2. call success.
1565 */
1566 EXPECT_TRUE(resultSetAll->GetCount() == FOUR_RECORDS);
1567 /**
1568 * @tc.steps: step3. Rekey with g_passwd1.
1569 * @tc.expected: step3. return BUSY.
1570 */
1571 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1572 EXPECT_EQ(delegate->Rekey(g_passwd1), BUSY);
1573
1574 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1575 EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1576 delegate = nullptr;
1577 delete manager;
1578 manager = nullptr;
1579
1580 Option option2;
1581 option2.isEncryptedDb = true;
1582 option2.passwd = PASSWD_VECTOR_1;
1583 option2.createIfNecessary = IS_NOT_NEED_CREATE;
1584 KvStoreNbDelegate *delegate2 = nullptr;
1585 KvStoreDelegateManager *manager2 = nullptr;
1586 delegate2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager2, g_dbParameter2, option2);
1587 ASSERT_TRUE(manager2 == nullptr && delegate2 == nullptr);
1588
1589 option2.isEncryptedDb = false;
1590 delegate2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager2, g_dbParameter2, option2);
1591 ASSERT_TRUE(manager2 != nullptr && delegate2 != nullptr);
1592 EXPECT_EQ(manager2->CloseKvStore(delegate2), OK);
1593 delegate2 = nullptr;
1594 EXPECT_EQ(manager2->DeleteKvStore(STORE_ID_2), OK);
1595 delete manager2;
1596 manager2 = nullptr;
1597 }
1598
ResultSetDb024(bool isRowIdMode)1599 void DistributeddbNbCursorTestcase::ResultSetDb024(bool isRowIdMode)
1600 {
1601 KvStoreDelegateManager *manager = nullptr;
1602 KvStoreNbDelegate *delegate = nullptr;
1603 Option option;
1604 option.isEncryptedDb = false;
1605 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1606 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1607 SetResultSetCacheMode(delegate, isRowIdMode);
1608
1609 vector<Entry> entriesBatch;
1610 vector<Key> allKeys;
1611 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS, FOUR_BYTE_KEY, FOUR_M_LONG_STRING);
1612 for (const auto &iter : entriesBatch) {
1613 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1614 }
1615 /**
1616 * @tc.steps: step1. Rekey STORE_ID_SYNC_2 with g_passwd1.
1617 * @tc.expected: step1. operate successfully or BUSY(if the rekey is later executed).
1618 */
1619 std::mutex mtx;
1620 std::condition_variable conditionRekeyVar;
1621 bool rekeyFlag = false;
1622 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1623 thread subThread([&delegate, &conditionRekeyVar, &rekeyFlag, &mtx]() {
1624 auto status = delegate->Rekey(g_passwd1);
1625 EXPECT_EQ(((status == OK) || (status == BUSY)), true);
1626 std::unique_lock<std::mutex> lck(mtx);
1627 conditionRekeyVar.notify_all();
1628 rekeyFlag = true;
1629 });
1630 subThread.detach();
1631 /**
1632 * @tc.steps: step2. call GetEntries interface to get KvStoreResultSet.
1633 * @tc.expected: step2. return BUSY or OK(if the rekey is later executed).
1634 */
1635 KvStoreResultSet *resultSetK = nullptr;
1636 Key keyPrefix = { 'k' };
1637 std::this_thread::sleep_for(std::chrono::microseconds(WAIT_FOR_OBSERVER_REKEY)); // wait the rekey operation.
1638 DBStatus status = delegate->GetEntries(keyPrefix, resultSetK);
1639 EXPECT_EQ(((status == OK) || (status == BUSY)), true);
1640
1641 std::unique_lock<std::mutex> lck(mtx);
1642 conditionRekeyVar.wait(lck, [&] { return rekeyFlag; });
1643 if (resultSetK != nullptr) {
1644 delegate->CloseResultSet(resultSetK);
1645 }
1646 EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1647 delegate = nullptr;
1648 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), OK);
1649 delete manager;
1650 manager = nullptr;
1651 }
1652 namespace {
CursorOperThread(KvStoreNbDelegate * & nbCursorDelegate)1653 void CursorOperThread(KvStoreNbDelegate *&nbCursorDelegate)
1654 {
1655 ASSERT_TRUE(nbCursorDelegate != nullptr);
1656 /**
1657 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1658 * @tc.expected: step1. get success.
1659 */
1660 KvStoreResultSet *resultSetAll = nullptr;
1661 EXPECT_EQ(nbCursorDelegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1662 /**
1663 * @tc.steps: step2. call GetCount interface.
1664 * @tc.expected: step2. call success.
1665 */
1666 EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1667 /**
1668 * @tc.steps: step3. call IsFirst interface.
1669 * @tc.expected: step3. call success.
1670 */
1671 EXPECT_TRUE(resultSetAll->IsFirst() == false);
1672 /**
1673 * @tc.steps: step4. call IsLast interface.
1674 * @tc.expected: step4. call success.
1675 */
1676 EXPECT_TRUE(resultSetAll->IsLast() == false);
1677 /**
1678 * @tc.steps: step5. call MoveToFirst interface.
1679 * @tc.expected: step5. call success.
1680 */
1681 EXPECT_TRUE(resultSetAll->MoveToFirst() == true);
1682 /**
1683 * @tc.steps: step6. call MoveToLast interface.
1684 * @tc.expected: step6. call success.
1685 */
1686 EXPECT_TRUE(resultSetAll->MoveToLast() == true);
1687 /**
1688 * @tc.steps: step7. call IsBeforeFirst interface.
1689 * @tc.expected: step7. call success.
1690 */
1691 EXPECT_TRUE(resultSetAll->IsBeforeFirst() == false);
1692 /**
1693 * @tc.steps: step8. call MoveToNext interface.
1694 * @tc.expected: step8. call success.
1695 */
1696 EXPECT_TRUE(resultSetAll->MoveToNext() == false);
1697 /**
1698 * @tc.steps: step9. call IsAfterLast interface.
1699 * @tc.expected: step9. call success.
1700 */
1701 EXPECT_TRUE(resultSetAll->IsAfterLast() == true);
1702 /**
1703 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1704 * @tc.expected: step10. call success.
1705 */
1706 EXPECT_TRUE(resultSetAll->MoveToPrevious() == true);
1707 EXPECT_TRUE(resultSetAll->IsAfterLast() == false);
1708 /**
1709 * @tc.steps: step11. close recordset.
1710 * @tc.expected: step11. call success.
1711 */
1712 EXPECT_EQ(nbCursorDelegate->CloseResultSet(resultSetAll), OK);
1713 }
1714 }
ResultSetDb025(KvStoreNbDelegate * delegate,bool isRowIdMode)1715 void DistributeddbNbCursorTestcase::ResultSetDb025(KvStoreNbDelegate *delegate, bool isRowIdMode)
1716 {
1717 ASSERT_TRUE(delegate != nullptr);
1718 SetResultSetCacheMode(delegate, isRowIdMode);
1719 vector<Entry> entriesBatch;
1720 vector<Key> allKeys;
1721 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS,
1722 FOUR_BYTE_KEY, ONE_TENTH_M_LONG_STRING);
1723 for (const auto &iter : entriesBatch) {
1724 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1725 }
1726
1727 /**
1728 * @tc.steps: step1. Call resultSet interfaces.
1729 * @tc.expected: step1. operate successfully.
1730 */
1731 std::vector<std::thread> threads;
1732 for (unsigned int threadId = THREAD_NUM_START; threadId <= THREAD_NUM_END; ++threadId) {
1733 threads.push_back(std::thread(CursorOperThread, std::ref(delegate)));
1734 }
1735 for (auto& th : threads) {
1736 th.join();
1737 }
1738 }
1739 namespace {
CursorRandOperThread1(KvStoreResultSet * & resultSet)1740 void CursorRandOperThread1(KvStoreResultSet *&resultSet)
1741 {
1742 /**
1743 * @tc.steps: step2. call GetCount interface.
1744 * @tc.expected: step2. call success.
1745 */
1746 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1747 /**
1748 * @tc.steps: step3. call IsFirst interface.
1749 * @tc.expected: step3. no crash.
1750 */
1751 resultSet->IsFirst();
1752 /**
1753 * @tc.steps: step4. call IsLast interface.
1754 * @tc.expected: step4. call success.
1755 */
1756 resultSet->IsLast();
1757 /**
1758 * @tc.steps: step5. call MoveToFirst interface.
1759 * @tc.expected: step5. call success.
1760 */
1761 resultSet->MoveToFirst();
1762 /**
1763 * @tc.steps: step6. call MoveToLast interface.
1764 * @tc.expected: step6. call success.
1765 */
1766 resultSet->MoveToLast();
1767 /**
1768 * @tc.steps: step7. call IsBeforeFirst interface.
1769 * @tc.expected: step7. call success.
1770 */
1771 resultSet->IsBeforeFirst();
1772 /**
1773 * @tc.steps: step8. call MoveToNext interface.
1774 * @tc.expected: step8. call success.
1775 */
1776 resultSet->MoveToNext();
1777 /**
1778 * @tc.steps: step9. call IsAfterLast interface.
1779 * @tc.expected: step9. call success.
1780 */
1781 resultSet->IsAfterLast();
1782 /**
1783 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1784 * @tc.expected: step10. call success.
1785 */
1786 resultSet->MoveToPrevious();
1787 resultSet->IsAfterLast();
1788 }
1789
CursorRandOperThread2(KvStoreResultSet * & resultSet)1790 void CursorRandOperThread2(KvStoreResultSet *&resultSet)
1791 {
1792 /**
1793 * @tc.steps: step2. call GetCount interface.
1794 * @tc.expected: step2. call success.
1795 */
1796 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1797 /**
1798 * @tc.steps: step3. call IsFirst interface.
1799 * @tc.expected: step3. call success.
1800 */
1801 resultSet->IsFirst();
1802 /**
1803 * @tc.steps: step7. call IsBeforeFirst interface.
1804 * @tc.expected: step7. call success.
1805 */
1806 resultSet->IsBeforeFirst();
1807 /**
1808 * @tc.steps: step8. call MoveToNext interface.
1809 * @tc.expected: step8. call success.
1810 */
1811 resultSet->MoveToNext();
1812 /**
1813 * @tc.steps: step9. call IsAfterLast interface.
1814 * @tc.expected: step9. call success.
1815 */
1816 resultSet->IsAfterLast();
1817 /**
1818 * @tc.steps: step4. call IsLast interface.
1819 * @tc.expected: step4. call success.
1820 */
1821 resultSet->IsLast();
1822 /**
1823 * @tc.steps: step5. call MoveToFirst interface.
1824 * @tc.expected: step5. call success.
1825 */
1826 resultSet->MoveToFirst();
1827 /**
1828 * @tc.steps: step6. call MoveToLast interface.
1829 * @tc.expected: step6. call success.
1830 */
1831 resultSet->MoveToLast();
1832 /**
1833 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1834 * @tc.expected: step10. call success.
1835 */
1836 resultSet->MoveToPrevious();
1837 resultSet->IsAfterLast();
1838 }
1839
CursorRandOperThread3(KvStoreResultSet * & resultSet)1840 void CursorRandOperThread3(KvStoreResultSet *&resultSet)
1841 {
1842 /**
1843 * @tc.steps: step2. call GetCount interface.
1844 * @tc.expected: step2. call success.
1845 */
1846 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1847 /**
1848 * @tc.steps: step3. call IsFirst interface.
1849 * @tc.expected: step3. call success.
1850 */
1851 resultSet->IsFirst();
1852 /**
1853 * @tc.steps: step6. call MoveToLast interface.
1854 * @tc.expected: step6. call success.
1855 */
1856 resultSet->MoveToLast();
1857 /**
1858 * @tc.steps: step7. call IsBeforeFirst interface.
1859 * @tc.expected: step7. call success.
1860 */
1861 resultSet->IsBeforeFirst();
1862 /**
1863 * @tc.steps: step4. call IsLast interface.
1864 * @tc.expected: step4. call success.
1865 */
1866 resultSet->IsLast();
1867 /**
1868 * @tc.steps: step5. call MoveToFirst interface.
1869 * @tc.expected: step5. call success.
1870 */
1871 resultSet->MoveToFirst();
1872 /**
1873 * @tc.steps: step8. call MoveToNext interface.
1874 * @tc.expected: step8. call success.
1875 */
1876 resultSet->MoveToNext();
1877 /**
1878 * @tc.steps: step9. call IsAfterLast interface.
1879 * @tc.expected: step9. call success.
1880 */
1881 resultSet->IsAfterLast();
1882 /**
1883 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1884 * @tc.expected: step10. call success.
1885 */
1886 resultSet->MoveToPrevious();
1887 resultSet->IsAfterLast();
1888 }
1889
CursorRandOperThread4(KvStoreResultSet * & resultSet)1890 void CursorRandOperThread4(KvStoreResultSet *&resultSet)
1891 {
1892 /**
1893 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1894 * @tc.expected: step10. call success.
1895 */
1896 resultSet->IsAfterLast();
1897 resultSet->MoveToPrevious();
1898 /**
1899 * @tc.steps: step9. call IsAfterLast interface.
1900 * @tc.expected: step9. call success.
1901 */
1902 resultSet->IsAfterLast();
1903 /**
1904 * @tc.steps: step8. call MoveToNext interface.
1905 * @tc.expected: step8. call success.
1906 */
1907 resultSet->MoveToNext();
1908 /**
1909 * @tc.steps: step7. call IsBeforeFirst interface.
1910 * @tc.expected: step7. call success.
1911 */
1912 resultSet->IsBeforeFirst();
1913 /**
1914 * @tc.steps: step6. call MoveToLast interface.
1915 * @tc.expected: step6. call success.
1916 */
1917 resultSet->MoveToLast();
1918 /**
1919 * @tc.steps: step5. call MoveToFirst interface.
1920 * @tc.expected: step5. call success.
1921 */
1922 resultSet->MoveToFirst();
1923 /**
1924 * @tc.steps: step4. call IsLast interface.
1925 * @tc.expected: step4. call success.
1926 */
1927 resultSet->IsLast();
1928 /**
1929 * @tc.steps: step3. call IsFirst interface.
1930 * @tc.expected: step3. call success.
1931 */
1932 resultSet->IsFirst();
1933 /**
1934 * @tc.steps: step2. call GetCount interface.
1935 * @tc.expected: step2. call success.
1936 */
1937 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1938 }
1939 }
1940
ResultSetDb026(KvStoreNbDelegate * delegate,bool isRowIdMode)1941 void DistributeddbNbCursorTestcase::ResultSetDb026(KvStoreNbDelegate *delegate, bool isRowIdMode)
1942 {
1943 ASSERT_TRUE(delegate != nullptr);
1944 SetResultSetCacheMode(delegate, isRowIdMode);
1945 vector<Entry> entriesBatch;
1946 vector<Key> allKeys;
1947 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS,
1948 FOUR_BYTE_KEY, ONE_TENTH_M_LONG_STRING);
1949 for (const auto &iter : entriesBatch) {
1950 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1951 }
1952
1953 /**
1954 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1955 * @tc.expected: step1. get success.
1956 */
1957 KvStoreResultSet *resultSetAll = nullptr;
1958 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1959
1960 /**
1961 * @tc.steps: step2. Call resultSet interfaces.
1962 * @tc.expected: step2. operate successfully.
1963 */
1964 std::vector<std::thread> threads;
1965 threads.push_back(std::thread(CursorRandOperThread1, std::ref(resultSetAll)));
1966 threads.push_back(std::thread(CursorRandOperThread2, std::ref(resultSetAll)));
1967 threads.push_back(std::thread(CursorRandOperThread3, std::ref(resultSetAll)));
1968 threads.push_back(std::thread(CursorRandOperThread4, std::ref(resultSetAll)));
1969
1970 for (auto& th : threads) {
1971 th.join();
1972 }
1973
1974 /**
1975 * @tc.steps: step11. close recordset.
1976 * @tc.expected: step11. call success.
1977 */
1978 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1979 }
1980 namespace {
VerifyResultSetInterfaces(KvStoreNbDelegate ** delegates,unsigned long delegateCount)1981 void VerifyResultSetInterfaces(KvStoreNbDelegate **delegates, unsigned long delegateCount)
1982 {
1983 ASSERT_TRUE(delegates != nullptr);
1984 Key keyPrefixKA = { 'k', 'a' };
1985 Key keyPrefixKB = { 'k', 'b' };
1986 Key keyPrefixKK = { 'k', 'k' };
1987 KvStoreResultSet *resultSetAlls[OPEN_DB_TIMES] = { nullptr };
1988 KvStoreResultSet *resultSetKAs[OPEN_DB_TIMES] = { nullptr };
1989 KvStoreResultSet *resultSetKBs[OPEN_DB_TIMES] = { nullptr };
1990 KvStoreResultSet *resultSetKKs[OPEN_DB_TIMES] = { nullptr };
1991 unsigned long delegateCnt = 0;
1992 for (delegateCnt = 0; delegateCnt < delegateCount; ++delegateCnt) {
1993 /**
1994 * @tc.steps: step2. check GetEntries of interfaces of every delegate.
1995 * @tc.expected: step2. success.
1996 */
1997 EXPECT_EQ(delegates[delegateCnt]->GetEntries(KEY_EMPTY, resultSetAlls[delegateCnt]), OK);
1998 EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKA, resultSetKAs[delegateCnt]), OK);
1999 EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKB, resultSetKBs[delegateCnt]), OK);
2000 EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKK, resultSetKKs[delegateCnt]), OK);
2001 /**
2002 * @tc.steps: step3. check GetCount of interfaces of every delegate.
2003 * @tc.expected: step3. success.
2004 */
2005 EXPECT_TRUE(resultSetAlls[delegateCnt]->GetCount() == TWO_HUNDREDS_RECORDS);
2006 EXPECT_TRUE(resultSetKAs[delegateCnt]->GetCount() == ONE_HUNDRED_RECORDS);
2007 EXPECT_TRUE(resultSetKBs[delegateCnt]->GetCount() == ONE_HUNDRED_RECORDS);
2008 EXPECT_TRUE(resultSetKKs[delegateCnt]->GetCount() == 0);
2009 }
2010
2011 for (delegateCnt = 0; delegateCnt < delegateCount; ++delegateCnt) {
2012 /**
2013 * @tc.steps: step4. check GetCount of interfaces of every delegate.
2014 * @tc.expected: step4. success.
2015 */
2016 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetAlls[delegateCnt]) == OK);
2017 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKAs[delegateCnt]) == OK);
2018 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKBs[delegateCnt]) == OK);
2019 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKKs[delegateCnt]) == OK);
2020 }
2021 }
2022 }
2023
ResultSetDb027(bool isRowIdMode)2024 void DistributeddbNbCursorTestcase::ResultSetDb027(bool isRowIdMode)
2025 {
2026 KvStoreDelegateManager *managers[OPEN_DB_TIMES] = {nullptr};
2027 KvStoreNbDelegate *delegates[OPEN_DB_TIMES] = {nullptr};
2028 Option option;
2029 unsigned long delegateCnt = 0;
2030 /**
2031 * @tc.steps: step1. open STORE_ID_2 for three times.
2032 * @tc.expected: step1. success.
2033 */
2034 delegates[INDEX_ZEROTH] = DistributedDBNbTestTools::GetNbDelegateSuccess(managers[INDEX_ZEROTH],
2035 g_dbParameter3, option);
2036 ASSERT_TRUE(managers[INDEX_ZEROTH] != nullptr && delegates[INDEX_ZEROTH] != nullptr);
2037 SetResultSetCacheMode(delegates[INDEX_ZEROTH], isRowIdMode);
2038
2039 vector<Entry> entriesKA;
2040 vector<Entry> entriesKB;
2041 std::vector<uint8_t> ka = { 'k', 'a' };
2042 std::vector<uint8_t> kb = { 'k', 'b' };
2043 EntrySize entrySizeKA = { FIVE_BYTE_KEY, ONE_K_LONG_STRING };
2044 EntrySize entrySizeKB = { FIVE_BYTE_KEY, ONE_TENTH_M_LONG_STRING };
2045 GenerateAppointPrefixAndSizeRecords(entriesKA, entrySizeKA, ONE_HUNDRED_RECORDS, ka, {'v'});
2046 GenerateAppointPrefixAndSizeRecords(entriesKB, entrySizeKB, ONE_HUNDRED_RECORDS, kb, {'v'});
2047 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
2048 EXPECT_TRUE(delegates[INDEX_ZEROTH]->Put(entriesKA[index].key, entriesKA[index].value) == OK);
2049 EXPECT_TRUE(delegates[INDEX_ZEROTH]->Put(entriesKB[index].key, entriesKB[index].value) == OK);
2050 }
2051
2052 option.createIfNecessary = false;
2053 for (delegateCnt = INDEX_FIRST; delegateCnt < DELEGATE_NUM; ++delegateCnt) {
2054 delegates[delegateCnt] = DistributedDBNbTestTools::GetNbDelegateSuccess(managers[delegateCnt],
2055 g_dbParameter3, option);
2056 ASSERT_TRUE(managers[delegateCnt] != nullptr && delegates[delegateCnt] != nullptr);
2057 SetResultSetCacheMode(delegates[delegateCnt], isRowIdMode);
2058 }
2059
2060 VerifyResultSetInterfaces(delegates, DELEGATE_NUM);
2061 for (unsigned long operCnt = INDEX_ZEROTH; operCnt < OPEN_DB_TIMES; ++operCnt) {
2062 EXPECT_EQ(managers[operCnt]->CloseKvStore(delegates[operCnt]), OK);
2063 delegates[operCnt] = nullptr;
2064 if (operCnt == OPEN_DB_TIMES - 1) {
2065 EXPECT_EQ(managers[operCnt]->DeleteKvStore(STORE_ID_3), OK);
2066 }
2067 delete managers[operCnt];
2068 managers[operCnt] = nullptr;
2069 }
2070 }