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 &currentStatus)
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, entry2M;
970     entry4M.key.assign(KEY_SIX_BYTE, 'k');
971     entry4M.value.assign(FOUR_M_LONG_STRING, 'v');
972     entry2M.key.assign(KEY_THIRTYTWO_BYTE, 'k');
973     entry2M.value.assign(TWO_M_LONG_STRING, 'v');
974     entries.push_back(entry4M);
975     entries.push_back(entry2M);
976     for (unsigned int index = 0; index < CURSOR_DIFFERENT_RECORDS; index++) {
977         EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
978     }
979     /**
980      * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
981      * @tc.expected: step1. get success.
982      */
983     KvStoreResultSet *resultSet = nullptr;
984     EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
985     /**
986      * @tc.steps: step2. call GetCount interface get number of records of cursor.
987      * @tc.expected: step2. the number is 102.
988      */
989     EXPECT_TRUE(resultSet->GetCount() == CURSOR_DIFFERENT_RECORDS);
990     unsigned int position = 0;
991     while (position < CURSOR_DIFFERENT_RECORDS) {
992         /**
993          * @tc.steps: step3. call MoveToNext interface and check the result.
994          * @tc.expected: step3. move ok.
995          */
996         if (position != (CURSOR_DIFFERENT_RECORDS - CURSOR_POSITION_1)) {
997             EXPECT_TRUE(resultSet->MoveToNext());
998             position = resultSet->GetPosition();
999             /**
1000              * @tc.steps: step4. call Move interface by 1 offset and check the result.
1001              * @tc.expected: step4. move ok.
1002              */
1003             EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_1));
1004             /**
1005              * @tc.steps: step5. call GetPosition interface by 1 offset and check the result.
1006              * @tc.expected: step5. GetPosition ok and the position increased by 1 each loop.
1007              */
1008             position = resultSet->GetPosition();
1009             EXPECT_TRUE(resultSet->MoveToPosition(++position));
1010         } else {
1011             EXPECT_FALSE(resultSet->MoveToNext());
1012             /**
1013              * @tc.steps: step4. call Move interface by 1 offset and check the result.
1014              * @tc.expected: step4. move ok.
1015              */
1016             EXPECT_FALSE(resultSet->Move(CURSOR_OFFSET_1));
1017             /**
1018              * @tc.steps: step5. call GetPosition interface by 1 offset and check the result.
1019              * @tc.expected: step5. GetPosition ok and the position increased by 1 each loop.
1020              */
1021             position = resultSet->GetPosition();
1022             EXPECT_FALSE(resultSet->MoveToPosition(++position));
1023         }
1024     }
1025 
1026     EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
1027 }
1028 
ResultSetDb014(KvStoreNbDelegate * delegate,bool isRowIdMode)1029 void DistributeddbNbCursorTestcase::ResultSetDb014(KvStoreNbDelegate *delegate, bool isRowIdMode)
1030 {
1031     ASSERT_TRUE(delegate != nullptr);
1032     SetResultSetCacheMode(delegate, isRowIdMode);
1033     std::vector<DistributedDB::Entry> entries;
1034     EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
1035     GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
1036     for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1037         EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
1038     }
1039     /**
1040      * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet with the prefix = { 'a' }.
1041      * @tc.expected: step1. get KvStoreResultSet success.
1042      */
1043     KvStoreResultSet *resultSet = nullptr;
1044     EXPECT_EQ(delegate->GetEntries(KEY_A, resultSet), OK);
1045     /**
1046      * @tc.steps: step2. call GetCount interface get number of records of cursor.
1047      * @tc.expected: step2. the number is 0.
1048      */
1049     EXPECT_TRUE(resultSet->GetCount() == NO_RECORD);
1050     /**
1051      * @tc.steps: step3. call IsFirst interface check whether the current position is first.
1052      * @tc.expected: step3. return false.
1053      */
1054     EXPECT_TRUE(resultSet->IsFirst() == false);
1055     /**
1056      * @tc.steps: step4. call IsLast interface check whether the current position is last.
1057      * @tc.expected: step4. return false.
1058      */
1059     EXPECT_TRUE(resultSet->IsLast() == false);
1060     /**
1061      * @tc.steps: step5. call MoveToFirst interface check whether it can MoveToFirst.
1062      * @tc.expected: step5. return false.
1063      */
1064     EXPECT_TRUE(resultSet->MoveToFirst() == false);
1065     /**
1066      * @tc.steps: step6. call MoveToLast interface check whether it can MoveToLast.
1067      * @tc.expected: step6. return false.
1068      */
1069     EXPECT_TRUE(resultSet->MoveToLast() == false);
1070     /**
1071      * @tc.steps: step7. call IsBeforeFirst interface check whether it can MoveToLast.
1072      * @tc.expected: step7. return false.
1073      */
1074     EXPECT_TRUE(resultSet->IsBeforeFirst() == true);
1075     /**
1076      * @tc.steps: step8. call MoveToNext first and then call IsBeforeFirst interface check the result.
1077      * @tc.expected: step8. MoveToNext can returns ok, but IsBeforeFirst still returns false.
1078      */
1079     EXPECT_TRUE(resultSet->MoveToNext() == false);
1080     EXPECT_TRUE(resultSet->IsBeforeFirst() == true);
1081     /**
1082      * @tc.steps: step9. call IsAfterLast interface check whether it can MoveToLast.
1083      * @tc.expected: step9. return false.
1084      */
1085     EXPECT_TRUE(resultSet->IsAfterLast() == true);
1086     /**
1087      * @tc.steps: step10. call MoveToPrevious first and then call IsAfterLast interface check the result.
1088      * @tc.expected: step10. MoveToPrevious can returns ok, but IsAfterLast still returns false.
1089      */
1090     EXPECT_TRUE(resultSet->MoveToPrevious() == false);
1091     EXPECT_TRUE(resultSet->IsAfterLast() == true);
1092 
1093     /**
1094      * @tc.steps: step11. close KvStoreResultSet.
1095      * @tc.expected: step11. close success.
1096      */
1097     EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
1098 }
1099 
ResultSetDb015(KvStoreNbDelegate * delegate,bool isRowIdMode)1100 void DistributeddbNbCursorTestcase::ResultSetDb015(KvStoreNbDelegate *delegate, bool isRowIdMode)
1101 {
1102     ASSERT_TRUE(delegate != nullptr);
1103     SetResultSetCacheMode(delegate, isRowIdMode);
1104     std::vector<DistributedDB::Entry> entries;
1105     EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
1106     GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
1107     for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1108         EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
1109     }
1110     /**
1111      * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet with the prefix = { 'a' }.
1112      * @tc.expected: step1. get KvStoreResultSet success.
1113      */
1114     Entry entry;
1115     KvStoreResultSet *resultSet = nullptr;
1116     EXPECT_EQ(delegate->GetEntries(KEY_A, resultSet), OK);
1117     /**
1118      * @tc.steps: step2. call GetCount interface get number of records of cursor.
1119      * @tc.expected: step2. the number is 0.
1120      */
1121     EXPECT_TRUE(resultSet->GetCount() == NO_RECORD);
1122     /**
1123      * @tc.steps: step3. call Move interface with the offset is 0.
1124      * @tc.expected: step3. return false.
1125      */
1126     EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_0) == false);
1127     /**
1128      * @tc.steps: step4. call GetPosition interface to check the current position and GetEntry to check the Entry.
1129      * @tc.expected: step4. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1130      */
1131     EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
1132     EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
1133     /**
1134      * @tc.steps: step5. call Move interface with the offset is -1.
1135      * @tc.expected: step5. return false.
1136      */
1137     EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_NEGATIVE1) == false);
1138     /**
1139      * @tc.steps: step6. call GetPosition interface to check the current position and GetEntry to check the Entry.
1140      * @tc.expected: step6. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1141      */
1142     EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
1143     EXPECT_EQ(resultSet->GetEntry(entry), NOT_FOUND);
1144     /**
1145      * @tc.steps: step7. call Move interface with the offset is 5.
1146      * @tc.expected: step7. return false.
1147      */
1148     EXPECT_TRUE(resultSet->Move(CURSOR_OFFSET_5) == false);
1149     /**
1150      * @tc.steps: step8. call GetPosition interface to check the current position and GetEntry to check the Entry.
1151      * @tc.expected: step8. GetPosition returns 0, and GetEntry returns NOT_FOUND.
1152      */
1153     EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_0);
1154     EXPECT_EQ(resultSet->GetEntry(entry), NOT_FOUND);
1155 
1156     /**
1157      * @tc.steps: step11. close KvStoreResultSet.
1158      * @tc.expected: step11. close success.
1159      */
1160     EXPECT_EQ(delegate->CloseResultSet(resultSet), OK);
1161 }
1162 
ResultSetDb016(KvStoreNbDelegate * delegate,bool isRowIdMode)1163 void DistributeddbNbCursorTestcase::ResultSetDb016(KvStoreNbDelegate *delegate, bool isRowIdMode)
1164 {
1165     ASSERT_TRUE(delegate != nullptr);
1166     SetResultSetCacheMode(delegate, isRowIdMode);
1167     std::vector<DistributedDB::Entry> entries;
1168     EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
1169     GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
1170     for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1171         EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
1172     }
1173     /**
1174      * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet with the prefix = { 'a' }.
1175      * @tc.expected: step1. get KvStoreResultSet success.
1176      */
1177     Entry entryGot;
1178     KvStoreResultSet *resultSet = nullptr;
1179     EXPECT_EQ(delegate->GetEntries(KEY_A, resultSet), OK);
1180     /**
1181      * @tc.steps: step2. call GetCount interface get number of records of cursor.
1182      * @tc.expected: step2. the number is 0.
1183      */
1184     EXPECT_TRUE(resultSet->GetCount() == NO_RECORD);
1185     /**
1186      * @tc.steps: step3. call MoveToPosition interface to move to position 0.
1187      * @tc.expected: step3. return false.
1188      */
1189     EXPECT_TRUE(resultSet->MoveToPosition(CURSOR_POSITION_0) == false);
1190     /**
1191      * @tc.steps: step4. call GetPosition interface to check the current position and GetEntry to check the Entry.
1192      * @tc.expected: step4. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1193      */
1194     EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_0);
1195     EXPECT_EQ(resultSet->GetEntry(entryGot), NOT_FOUND);
1196     /**
1197      * @tc.steps: step5. call MoveToPosition interface to move to position 2.
1198      * @tc.expected: step5. return false.
1199      */
1200     EXPECT_TRUE(resultSet->MoveToPosition(CURSOR_POSITION_2) == false);
1201     /**
1202      * @tc.steps: step6. call GetPosition interface to check the current position and GetEntry to check the Entry.
1203      * @tc.expected: step6. GetPosition returns 0, and GetEntry returns NOT_FOUND.
1204      */
1205     EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_0);
1206     EXPECT_EQ(resultSet->GetEntry(entryGot), NOT_FOUND);
1207     /**
1208      * @tc.steps: step7. call MoveToPosition interface to move to position -5.
1209      * @tc.expected: step7. return false.
1210      */
1211     EXPECT_TRUE(resultSet->MoveToPosition(CURSOR_POSITION_NEGATIVE5) == false);
1212     /**
1213      * @tc.steps: step8. call GetPosition interface to check the current position and GetEntry to check the Entry.
1214      * @tc.expected: step8. GetPosition returns -1, and GetEntry returns NOT_FOUND.
1215      */
1216     EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
1217     EXPECT_EQ(resultSet->GetEntry(entryGot), NOT_FOUND);
1218 
1219     /**
1220      * @tc.steps: step11. close KvStoreResultSet.
1221      * @tc.expected: step11. close success.
1222      */
1223     EXPECT_EQ(delegate->CloseResultSet(resultSet), OK);
1224 }
1225 
ResultSetDb017(KvStoreNbDelegate * delegate,bool isRowIdMode)1226 void DistributeddbNbCursorTestcase::ResultSetDb017(KvStoreNbDelegate *delegate, bool isRowIdMode)
1227 {
1228     ASSERT_TRUE(delegate != nullptr);
1229     SetResultSetCacheMode(delegate, isRowIdMode);
1230     vector<Entry> entries;
1231     vector<Key> allKey;
1232     GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1233     for (const auto &iter : entries) {
1234         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1235     }
1236     /**
1237      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1238      * @tc.expected: step1. get success.
1239      */
1240     KvStoreResultSet *resultSetAll = nullptr;
1241     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1242     /**
1243      * @tc.steps: step2. call CloseResultSet interface with nullptr parameter.
1244      * @tc.expected: step2. return INVALID_ARGS.
1245      */
1246     KvStoreResultSet *resultSetAllptr = nullptr;
1247     EXPECT_EQ(delegate->CloseResultSet(resultSetAllptr), INVALID_ARGS);
1248     /**
1249      * @tc.steps: step3. call CloseResultSet interface with resultSetAll.
1250      * @tc.expected: step3. return OK.
1251      */
1252     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1253 }
1254 
ResultSetDb018(KvStoreNbDelegate * delegate,bool isRowIdMode)1255 void DistributeddbNbCursorTestcase::ResultSetDb018(KvStoreNbDelegate *delegate, bool isRowIdMode)
1256 {
1257     ASSERT_TRUE(delegate != nullptr);
1258     SetResultSetCacheMode(delegate, isRowIdMode);
1259     vector<Entry> entriesKA, entriesKB;
1260     vector<Key> allKeysKA, allKeysKB;
1261     std::vector<uint8_t> ka = { 'k', 'a' };
1262     std::vector<uint8_t> kb = { 'k', 'b' };
1263     GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKeysKA, entriesKA, ka);
1264     GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKeysKB, entriesKB, kb);
1265     for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1266         EXPECT_EQ(delegate->Put(entriesKA[index].key, entriesKA[index].value), OK);
1267         EXPECT_EQ(delegate->Put(entriesKB[index].key, entriesKB[index].value), OK);
1268     }
1269     /**
1270      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1271      * @tc.expected: step1. get success.
1272      */
1273     KvStoreResultSet *resultSetAll = nullptr;
1274     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1275     /**
1276      * @tc.steps: step2. call GetEntries interface with "ka" parameter to get KvStoreResultSet.
1277      * @tc.expected: step2. get success.
1278      */
1279     KvStoreResultSet *resultSetKA = nullptr;
1280     Key keyPrefixKA = ka;
1281     EXPECT_EQ(delegate->GetEntries(keyPrefixKA, resultSetKA), OK);
1282     /**
1283      * @tc.steps: step3. call GetEntries interface with "kb" parameter to get KvStoreResultSet.
1284      * @tc.expected: step3. get success.
1285      */
1286     KvStoreResultSet *resultSetKB = nullptr;
1287     Key keyPrefixKB = kb;
1288     EXPECT_EQ(delegate->GetEntries(keyPrefixKB, resultSetKB), OK);
1289     /**
1290      * @tc.steps: step4. call GetCount interface of all recordsets.
1291      * @tc.expected: step4. call success.
1292      */
1293     EXPECT_TRUE(resultSetAll->GetCount() == TWO_HUNDREDS_RECORDS);
1294     EXPECT_TRUE(resultSetKA->GetCount() == ONE_HUNDRED_RECORDS);
1295     EXPECT_TRUE(resultSetKB->GetCount() == ONE_HUNDRED_RECORDS);
1296     /**
1297      * @tc.steps: step5. close resultSetAll.
1298      * @tc.expected: step5. call success.
1299      */
1300     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1301     /**
1302      * @tc.steps: step6. close resultSetAll.
1303      * @tc.expected: step6. call success.
1304      */
1305     EXPECT_TRUE(resultSetKA->GetCount() == ONE_HUNDRED_RECORDS);
1306     EXPECT_TRUE(resultSetKB->GetCount() == ONE_HUNDRED_RECORDS);
1307 
1308     EXPECT_EQ(delegate->CloseResultSet(resultSetKA), OK);
1309     EXPECT_EQ(delegate->CloseResultSet(resultSetKB), OK);
1310 }
1311 
ResultSetDb019(KvStoreNbDelegate * delegate,bool isRowIdMode)1312 void DistributeddbNbCursorTestcase::ResultSetDb019(KvStoreNbDelegate *delegate, bool isRowIdMode)
1313 {
1314     ASSERT_TRUE(delegate != nullptr);
1315     SetResultSetCacheMode(delegate, isRowIdMode);
1316     vector<Entry> entriesBatch;
1317     vector<Key> allKeys;
1318     GenerateFixedRecords(entriesBatch, allKeys, TEN_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1319     for (const auto &iter : entriesBatch) {
1320         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1321     }
1322     /**
1323      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1324      * @tc.expected: step1. get success.
1325      */
1326     KvStoreResultSet *resultSetAll = nullptr;
1327     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1328     /**
1329      * @tc.steps: step2. call GetCount interface of resultSetAll and delete k1~k5.
1330      * @tc.expected: step2. call success.
1331      */
1332     EXPECT_TRUE(resultSetAll->GetCount() == TEN_RECORDS);
1333     for (unsigned int delCnt = 0; delCnt < FIVE_RECORDS; ++delCnt) {
1334         EXPECT_EQ(DistributedDBNbTestTools::Delete(*delegate, entriesBatch[0].key), OK);
1335         entriesBatch.erase(entriesBatch.begin());
1336     }
1337     /**
1338      * @tc.steps: step3. update k6 and insert another 10 * 1M records.
1339      * @tc.expected: step3. call success.
1340      */
1341     entriesBatch[0].value.push_back('a');
1342     EXPECT_EQ(delegate->Put(entriesBatch[0].key, entriesBatch[0].value), OK);
1343     vector<Entry> entriesBatch2;
1344     vector<Key> allKeys2;
1345     GenerateFixedRecords(entriesBatch2, allKeys2, TEN_RECORDS, KEY_SIX_BYTE, ONE_M_LONG_STRING);
1346     for (const auto &iter : entriesBatch2) {
1347         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1348     }
1349     /**
1350      * @tc.steps: step4. call GetEntries interface with "" parameter to get KvStoreResultSet.
1351      * @tc.expected: step4. get success.
1352      */
1353     KvStoreResultSet *resultSetAll2 = nullptr;
1354     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll2), OK);
1355     /**
1356      * @tc.steps: step5. close resultSetAll.
1357      * @tc.expected: step5. call success.
1358      */
1359     EXPECT_TRUE(resultSetAll->GetCount() == TEN_RECORDS);
1360     EXPECT_TRUE(resultSetAll2->GetCount() == FIFTEEN_RECORDS);
1361 
1362     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1363     EXPECT_EQ(delegate->CloseResultSet(resultSetAll2), OK);
1364 }
1365 namespace ResultSetDbNS {
ExecuteResultSetDb020(KvStoreNbDelegate * & delegate)1366 void ExecuteResultSetDb020(KvStoreNbDelegate *&delegate)
1367 {
1368     /**
1369      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1370      * @tc.expected: step1. get success.
1371      */
1372     KvStoreResultSet *resultSetAll = nullptr;
1373     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1374     /**
1375      * @tc.steps: step2. call GetEntries interface with "ka" parameter to get KvStoreResultSet.
1376      * @tc.expected: step2. get success.
1377      */
1378     KvStoreResultSet *resultSetKA = nullptr;
1379     std::vector<uint8_t> ka = { 'k', 'a' };
1380     Key keyPrefixKA = ka;
1381     EXPECT_EQ(delegate->GetEntries(keyPrefixKA, resultSetKA), OK);
1382     /**
1383      * @tc.steps: step3. call GetEntries interface with "kb" parameter to get KvStoreResultSet.
1384      * @tc.expected: step3. get success.
1385      */
1386     KvStoreResultSet *resultSetKB = nullptr;
1387     std::vector<uint8_t> kb = { 'k', 'b' };
1388     Key keyPrefixKB = kb;
1389     EXPECT_EQ(delegate->GetEntries(keyPrefixKB, resultSetKB), OK);
1390     /**
1391      * @tc.steps: step3. call GetEntries interface with "kc" parameter to get KvStoreResultSet.
1392      * @tc.expected: step3. get success.
1393      */
1394     KvStoreResultSet *resultSetKC = nullptr;
1395     std::vector<uint8_t> kc = { 'k', 'c' };
1396     Key keyPrefixKC = kc;
1397     EXPECT_EQ(delegate->GetEntries(keyPrefixKC, resultSetKC), OK);
1398     /**
1399      * @tc.steps: step4. call GetEntries interface with "kd" parameter to get KvStoreResultSet.
1400      * @tc.expected: step4. get success.
1401      */
1402     KvStoreResultSet *resultSetKD = nullptr;
1403     std::vector<uint8_t> kd = { 'k', 'd' };
1404     Key keyPrefixKD = kd;
1405     EXPECT_EQ(delegate->GetEntries(keyPrefixKD, resultSetKD), OK);
1406     /**
1407      * @tc.steps: step5. call GetEntries interface with "ke" parameter to get KvStoreResultSet.
1408      * @tc.expected: step5. get success.
1409      */
1410     KvStoreResultSet *resultSetKE = nullptr;
1411     std::vector<uint8_t> ke = { 'k', 'e' };
1412     Key keyPrefixKE = ke;
1413     EXPECT_EQ(delegate->GetEntries(keyPrefixKE, resultSetKE), OK);
1414     /**
1415      * @tc.steps: step6. call GetEntries interface with "kf" parameter to get KvStoreResultSet.
1416      * @tc.expected: step6. get success.
1417      */
1418     KvStoreResultSet *resultSetKF = nullptr;
1419     std::vector<uint8_t> kf = { 'k', 'f' };
1420     Key keyPrefixKF = kf;
1421     EXPECT_EQ(delegate->GetEntries(keyPrefixKF, resultSetKF), OK);
1422 
1423     /**
1424      * @tc.steps: step7. call GetEntries interface with "kg" parameter to get KvStoreResultSet.
1425      * @tc.expected: step7. get success.
1426      */
1427     KvStoreResultSet *resultSetKG = nullptr;
1428     std::vector<uint8_t> kg = { 'k', 'g' };
1429     Key keyPrefixKG = kg;
1430     EXPECT_EQ(delegate->GetEntries(keyPrefixKG, resultSetKG), OK);
1431     /**
1432      * @tc.steps: step8. call GetEntries interface with "" parameter to get KvStoreResultSet.
1433      * @tc.expected: step8. get success.
1434      */
1435     KvStoreResultSet *resultSetAll2 = nullptr;
1436     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll2), OVER_MAX_LIMITS);
1437 
1438     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1439     EXPECT_EQ(delegate->CloseResultSet(resultSetKA), OK);
1440     EXPECT_EQ(delegate->CloseResultSet(resultSetKB), OK);
1441     EXPECT_EQ(delegate->CloseResultSet(resultSetKC), OK);
1442     EXPECT_EQ(delegate->CloseResultSet(resultSetKD), OK);
1443     EXPECT_EQ(delegate->CloseResultSet(resultSetKE), OK);
1444     EXPECT_EQ(delegate->CloseResultSet(resultSetKF), OK);
1445     EXPECT_EQ(delegate->CloseResultSet(resultSetKG), OK);
1446     if (resultSetAll2 != nullptr) {
1447         EXPECT_EQ(delegate->CloseResultSet(resultSetAll2), OK);
1448     }
1449 }
1450 }
ResultSetDb020(KvStoreNbDelegate * delegate,bool isRowIdMode)1451 void DistributeddbNbCursorTestcase::ResultSetDb020(KvStoreNbDelegate *delegate, bool isRowIdMode)
1452 {
1453     ASSERT_TRUE(delegate != nullptr);
1454     SetResultSetCacheMode(delegate, isRowIdMode);
1455     vector<Entry> entries;
1456     vector<Key> allKey;
1457     GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1458     for (const auto &iter : entries) {
1459         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1460     }
1461     ResultSetDbNS::ExecuteResultSetDb020(delegate);
1462 }
1463 
ResultSetDb021(KvStoreNbDelegate * delegate,KvStoreDelegateManager * manager,bool isRowIdMode)1464 void DistributeddbNbCursorTestcase::ResultSetDb021(KvStoreNbDelegate *delegate,
1465     KvStoreDelegateManager *manager, bool isRowIdMode)
1466 {
1467     ASSERT_TRUE(delegate != nullptr && manager != nullptr);
1468     SetResultSetCacheMode(delegate, isRowIdMode);
1469     vector<Entry> entries;
1470     vector<Key> allKey;
1471     GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1472     for (const auto &iter : entries) {
1473         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1474     }
1475     /**
1476      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1477      * @tc.expected: step1. get success.
1478      */
1479     KvStoreResultSet *resultSetAll = nullptr;
1480     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1481     /**
1482      * @tc.steps: step2. call GetCount interface of resultSetAll.
1483      * @tc.expected: step2. call success.
1484      */
1485     EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1486     /**
1487      * @tc.steps: step3. closeKvStore returns BUSY because resultSet was not closed.
1488      * @tc.expected: step3. call success.
1489      */
1490     EXPECT_EQ(manager->CloseKvStore(delegate), BUSY);
1491 
1492     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1493 }
1494 
ResultSetDb022(bool isRowIdMode)1495 void DistributeddbNbCursorTestcase::ResultSetDb022(bool isRowIdMode)
1496 {
1497     KvStoreDelegateManager *manager = nullptr;
1498     KvStoreNbDelegate *delegate = nullptr;
1499     Option option;
1500     option.isEncryptedDb = false;
1501     delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1502     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1503     SetResultSetCacheMode(delegate, isRowIdMode);
1504 
1505     vector<Entry> entriesBatch;
1506     vector<Key> allKeys;
1507     GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1508     for (const auto &iter : entriesBatch) {
1509         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1510     }
1511     /**
1512      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1513      * @tc.expected: step1. get success.
1514      */
1515     KvStoreResultSet *resultSetAll = nullptr;
1516     EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSetAll) == OK);
1517     /**
1518      * @tc.steps: step2. call GetCount interface of resultSetAll.
1519      * @tc.expected: step2. call success.
1520      */
1521     EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1522     /**
1523      * @tc.steps: step3. Rekey with g_passwd1.
1524      * @tc.expected: step3. call success.
1525      */
1526     (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1527     EXPECT_EQ(delegate->Rekey(g_passwd1), BUSY);
1528 
1529     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1530     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1531     delegate = nullptr;
1532     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), OK);
1533     delete manager;
1534     manager = nullptr;
1535 }
1536 
ResultSetDb023(bool isRowIdMode)1537 void DistributeddbNbCursorTestcase::ResultSetDb023(bool isRowIdMode)
1538 {
1539     KvStoreDelegateManager *manager = nullptr;
1540     KvStoreNbDelegate *delegate = nullptr;
1541     Option option;
1542     option.isEncryptedDb = false;
1543     delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1544     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1545     SetResultSetCacheMode(delegate, isRowIdMode);
1546 
1547     vector<Entry> entriesBatch;
1548     vector<Key> allKeys;
1549     GenerateFixedRecords(entriesBatch, allKeys, FOUR_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1550     for (const auto &iter : entriesBatch) {
1551         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1552     }
1553     /**
1554      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1555      * @tc.expected: step1. get success.
1556      */
1557     KvStoreResultSet *resultSetAll = nullptr;
1558     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1559     /**
1560      * @tc.steps: step2. call GetCount interface of resultSetAll.
1561      * @tc.expected: step2. call success.
1562      */
1563     EXPECT_TRUE(resultSetAll->GetCount() == FOUR_RECORDS);
1564     /**
1565      * @tc.steps: step3. Rekey with g_passwd1.
1566      * @tc.expected: step3. return BUSY.
1567      */
1568     (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1569     EXPECT_EQ(delegate->Rekey(g_passwd1), BUSY);
1570 
1571     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1572     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1573     delegate = nullptr;
1574     delete manager;
1575     manager = nullptr;
1576 
1577     Option option2;
1578     option2.isEncryptedDb = true;
1579     option2.passwd = PASSWD_VECTOR_1;
1580     option2.createIfNecessary = IS_NOT_NEED_CREATE;
1581     KvStoreNbDelegate *delegate2 = nullptr;
1582     KvStoreDelegateManager *manager2 = nullptr;
1583     delegate2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager2, g_dbParameter2, option2);
1584     ASSERT_TRUE(manager2 == nullptr && delegate2 == nullptr);
1585 
1586     option2.isEncryptedDb = false;
1587     delegate2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager2, g_dbParameter2, option2);
1588     ASSERT_TRUE(manager2 != nullptr && delegate2 != nullptr);
1589     EXPECT_EQ(manager2->CloseKvStore(delegate2), OK);
1590     delegate2 = nullptr;
1591     EXPECT_EQ(manager2->DeleteKvStore(STORE_ID_2), OK);
1592     delete manager2;
1593     manager2 = nullptr;
1594 }
1595 
ResultSetDb024(bool isRowIdMode)1596 void DistributeddbNbCursorTestcase::ResultSetDb024(bool isRowIdMode)
1597 {
1598     KvStoreDelegateManager *manager = nullptr;
1599     KvStoreNbDelegate *delegate = nullptr;
1600     Option option;
1601     option.isEncryptedDb = false;
1602     delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1603     ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1604     SetResultSetCacheMode(delegate, isRowIdMode);
1605 
1606     vector<Entry> entriesBatch;
1607     vector<Key> allKeys;
1608     GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS, FOUR_BYTE_KEY, FOUR_M_LONG_STRING);
1609     for (const auto &iter : entriesBatch) {
1610         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1611     }
1612     /**
1613      * @tc.steps: step1. Rekey STORE_ID_SYNC_2 with g_passwd1.
1614      * @tc.expected: step1. operate successfully or BUSY(if the rekey is later executed).
1615      */
1616     std::mutex mtx;
1617     std::condition_variable conditionRekeyVar;
1618     bool rekeyFlag = false;
1619     (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1620     thread subThread([&delegate, &conditionRekeyVar, &rekeyFlag, &mtx]() {
1621         auto status = delegate->Rekey(g_passwd1);
1622         EXPECT_EQ(((status == OK) || (status == BUSY)), true);
1623         std::unique_lock<std::mutex> lck(mtx);
1624         conditionRekeyVar.notify_all();
1625         rekeyFlag = true;
1626     });
1627     subThread.detach();
1628     /**
1629      * @tc.steps: step2. call GetEntries interface to get KvStoreResultSet.
1630      * @tc.expected: step2. return BUSY or OK(if the rekey is later executed).
1631      */
1632     KvStoreResultSet *resultSetK = nullptr;
1633     Key keyPrefix = { 'k' };
1634     std::this_thread::sleep_for(std::chrono::microseconds(WAIT_FOR_OBSERVER_REKEY)); // wait the rekey operation.
1635     DBStatus status = delegate->GetEntries(keyPrefix, resultSetK);
1636     EXPECT_EQ(((status == OK) || (status == BUSY)), true);
1637 
1638     std::unique_lock<std::mutex> lck(mtx);
1639     conditionRekeyVar.wait(lck, [&] { return rekeyFlag; });
1640     if (resultSetK != nullptr) {
1641         delegate->CloseResultSet(resultSetK);
1642     }
1643     EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1644     delegate = nullptr;
1645     EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), OK);
1646     delete manager;
1647     manager = nullptr;
1648 }
1649 namespace {
CursorOperThread(KvStoreNbDelegate * & nbCursorDelegate)1650 void CursorOperThread(KvStoreNbDelegate *&nbCursorDelegate)
1651 {
1652     ASSERT_TRUE(nbCursorDelegate != nullptr);
1653     /**
1654      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1655      * @tc.expected: step1. get success.
1656      */
1657     KvStoreResultSet *resultSetAll = nullptr;
1658     EXPECT_EQ(nbCursorDelegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1659     /**
1660      * @tc.steps: step2. call GetCount interface.
1661      * @tc.expected: step2. call success.
1662      */
1663     EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1664     /**
1665      * @tc.steps: step3. call IsFirst interface.
1666      * @tc.expected: step3. call success.
1667      */
1668     EXPECT_TRUE(resultSetAll->IsFirst() == false);
1669     /**
1670      * @tc.steps: step4. call IsLast interface.
1671      * @tc.expected: step4. call success.
1672      */
1673     EXPECT_TRUE(resultSetAll->IsLast() == false);
1674     /**
1675      * @tc.steps: step5. call MoveToFirst interface.
1676      * @tc.expected: step5. call success.
1677      */
1678     EXPECT_TRUE(resultSetAll->MoveToFirst() == true);
1679     /**
1680      * @tc.steps: step6. call MoveToLast interface.
1681      * @tc.expected: step6. call success.
1682      */
1683     EXPECT_TRUE(resultSetAll->MoveToLast() == true);
1684     /**
1685      * @tc.steps: step7. call IsBeforeFirst interface.
1686      * @tc.expected: step7. call success.
1687      */
1688     EXPECT_TRUE(resultSetAll->IsBeforeFirst() == false);
1689     /**
1690      * @tc.steps: step8. call MoveToNext interface.
1691      * @tc.expected: step8. call success.
1692      */
1693     EXPECT_TRUE(resultSetAll->MoveToNext() == false);
1694     /**
1695      * @tc.steps: step9. call IsAfterLast interface.
1696      * @tc.expected: step9. call success.
1697      */
1698     EXPECT_TRUE(resultSetAll->IsAfterLast() == true);
1699     /**
1700      * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1701      * @tc.expected: step10. call success.
1702      */
1703     EXPECT_TRUE(resultSetAll->MoveToPrevious() == true);
1704     EXPECT_TRUE(resultSetAll->IsAfterLast() == false);
1705     /**
1706      * @tc.steps: step11. close recordset.
1707      * @tc.expected: step11. call success.
1708      */
1709     EXPECT_EQ(nbCursorDelegate->CloseResultSet(resultSetAll), OK);
1710 }
1711 }
ResultSetDb025(KvStoreNbDelegate * delegate,bool isRowIdMode)1712 void DistributeddbNbCursorTestcase::ResultSetDb025(KvStoreNbDelegate *delegate, bool isRowIdMode)
1713 {
1714     ASSERT_TRUE(delegate != nullptr);
1715     SetResultSetCacheMode(delegate, isRowIdMode);
1716     vector<Entry> entriesBatch;
1717     vector<Key> allKeys;
1718     GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS,
1719         FOUR_BYTE_KEY, ONE_TENTH_M_LONG_STRING);
1720     for (const auto &iter : entriesBatch) {
1721         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1722     }
1723 
1724     /**
1725      * @tc.steps: step1. Call resultSet interfaces.
1726      * @tc.expected: step1. operate successfully.
1727      */
1728     std::vector<std::thread> threads;
1729     for (unsigned int threadId = THREAD_NUM_START; threadId <= THREAD_NUM_END; ++threadId) {
1730         threads.push_back(std::thread(CursorOperThread, std::ref(delegate)));
1731     }
1732     for (auto& th : threads) {
1733         th.join();
1734     }
1735 }
1736 namespace {
CursorRandOperThread1(KvStoreResultSet * & resultSet)1737 void CursorRandOperThread1(KvStoreResultSet *&resultSet)
1738 {
1739     /**
1740      * @tc.steps: step2. call GetCount interface.
1741      * @tc.expected: step2. call success.
1742      */
1743     EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1744     /**
1745      * @tc.steps: step3. call IsFirst interface.
1746      * @tc.expected: step3. no crash.
1747      */
1748     resultSet->IsFirst();
1749     /**
1750      * @tc.steps: step4. call IsLast interface.
1751      * @tc.expected: step4. call success.
1752      */
1753     resultSet->IsLast();
1754     /**
1755      * @tc.steps: step5. call MoveToFirst interface.
1756      * @tc.expected: step5. call success.
1757      */
1758     resultSet->MoveToFirst();
1759     /**
1760      * @tc.steps: step6. call MoveToLast interface.
1761      * @tc.expected: step6. call success.
1762      */
1763     resultSet->MoveToLast();
1764     /**
1765      * @tc.steps: step7. call IsBeforeFirst interface.
1766      * @tc.expected: step7. call success.
1767      */
1768     resultSet->IsBeforeFirst();
1769     /**
1770      * @tc.steps: step8. call MoveToNext interface.
1771      * @tc.expected: step8. call success.
1772      */
1773     resultSet->MoveToNext();
1774     /**
1775      * @tc.steps: step9. call IsAfterLast interface.
1776      * @tc.expected: step9. call success.
1777      */
1778     resultSet->IsAfterLast();
1779     /**
1780      * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1781      * @tc.expected: step10. call success.
1782      */
1783     resultSet->MoveToPrevious();
1784     resultSet->IsAfterLast();
1785 }
1786 
CursorRandOperThread2(KvStoreResultSet * & resultSet)1787 void CursorRandOperThread2(KvStoreResultSet *&resultSet)
1788 {
1789     /**
1790      * @tc.steps: step2. call GetCount interface.
1791      * @tc.expected: step2. call success.
1792      */
1793     EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1794     /**
1795      * @tc.steps: step3. call IsFirst interface.
1796      * @tc.expected: step3. call success.
1797      */
1798     resultSet->IsFirst();
1799     /**
1800      * @tc.steps: step7. call IsBeforeFirst interface.
1801      * @tc.expected: step7. call success.
1802      */
1803     resultSet->IsBeforeFirst();
1804     /**
1805      * @tc.steps: step8. call MoveToNext interface.
1806      * @tc.expected: step8. call success.
1807      */
1808     resultSet->MoveToNext();
1809     /**
1810      * @tc.steps: step9. call IsAfterLast interface.
1811      * @tc.expected: step9. call success.
1812      */
1813     resultSet->IsAfterLast();
1814     /**
1815      * @tc.steps: step4. call IsLast interface.
1816      * @tc.expected: step4. call success.
1817      */
1818     resultSet->IsLast();
1819     /**
1820      * @tc.steps: step5. call MoveToFirst interface.
1821      * @tc.expected: step5. call success.
1822      */
1823     resultSet->MoveToFirst();
1824     /**
1825      * @tc.steps: step6. call MoveToLast interface.
1826      * @tc.expected: step6. call success.
1827      */
1828     resultSet->MoveToLast();
1829     /**
1830      * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1831      * @tc.expected: step10. call success.
1832      */
1833     resultSet->MoveToPrevious();
1834     resultSet->IsAfterLast();
1835 }
1836 
CursorRandOperThread3(KvStoreResultSet * & resultSet)1837 void CursorRandOperThread3(KvStoreResultSet *&resultSet)
1838 {
1839     /**
1840      * @tc.steps: step2. call GetCount interface.
1841      * @tc.expected: step2. call success.
1842      */
1843     EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1844     /**
1845      * @tc.steps: step3. call IsFirst interface.
1846      * @tc.expected: step3. call success.
1847      */
1848     resultSet->IsFirst();
1849     /**
1850      * @tc.steps: step6. call MoveToLast interface.
1851      * @tc.expected: step6. call success.
1852      */
1853     resultSet->MoveToLast();
1854     /**
1855      * @tc.steps: step7. call IsBeforeFirst interface.
1856      * @tc.expected: step7. call success.
1857      */
1858     resultSet->IsBeforeFirst();
1859     /**
1860      * @tc.steps: step4. call IsLast interface.
1861      * @tc.expected: step4. call success.
1862      */
1863     resultSet->IsLast();
1864     /**
1865      * @tc.steps: step5. call MoveToFirst interface.
1866      * @tc.expected: step5. call success.
1867      */
1868     resultSet->MoveToFirst();
1869     /**
1870      * @tc.steps: step8. call MoveToNext interface.
1871      * @tc.expected: step8. call success.
1872      */
1873     resultSet->MoveToNext();
1874     /**
1875      * @tc.steps: step9. call IsAfterLast interface.
1876      * @tc.expected: step9. call success.
1877      */
1878     resultSet->IsAfterLast();
1879     /**
1880      * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1881      * @tc.expected: step10. call success.
1882      */
1883     resultSet->MoveToPrevious();
1884     resultSet->IsAfterLast();
1885 }
1886 
CursorRandOperThread4(KvStoreResultSet * & resultSet)1887 void CursorRandOperThread4(KvStoreResultSet *&resultSet)
1888 {
1889     /**
1890      * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1891      * @tc.expected: step10. call success.
1892      */
1893     resultSet->IsAfterLast();
1894     resultSet->MoveToPrevious();
1895     /**
1896      * @tc.steps: step9. call IsAfterLast interface.
1897      * @tc.expected: step9. call success.
1898      */
1899     resultSet->IsAfterLast();
1900     /**
1901      * @tc.steps: step8. call MoveToNext interface.
1902      * @tc.expected: step8. call success.
1903      */
1904     resultSet->MoveToNext();
1905     /**
1906      * @tc.steps: step7. call IsBeforeFirst interface.
1907      * @tc.expected: step7. call success.
1908      */
1909     resultSet->IsBeforeFirst();
1910     /**
1911      * @tc.steps: step6. call MoveToLast interface.
1912      * @tc.expected: step6. call success.
1913      */
1914     resultSet->MoveToLast();
1915     /**
1916      * @tc.steps: step5. call MoveToFirst interface.
1917      * @tc.expected: step5. call success.
1918      */
1919     resultSet->MoveToFirst();
1920     /**
1921      * @tc.steps: step4. call IsLast interface.
1922      * @tc.expected: step4. call success.
1923      */
1924     resultSet->IsLast();
1925     /**
1926      * @tc.steps: step3. call IsFirst interface.
1927      * @tc.expected: step3. call success.
1928      */
1929     resultSet->IsFirst();
1930     /**
1931      * @tc.steps: step2. call GetCount interface.
1932      * @tc.expected: step2. call success.
1933      */
1934     EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1935 }
1936 }
1937 
ResultSetDb026(KvStoreNbDelegate * delegate,bool isRowIdMode)1938 void DistributeddbNbCursorTestcase::ResultSetDb026(KvStoreNbDelegate *delegate, bool isRowIdMode)
1939 {
1940     ASSERT_TRUE(delegate != nullptr);
1941     SetResultSetCacheMode(delegate, isRowIdMode);
1942     vector<Entry> entriesBatch;
1943     vector<Key> allKeys;
1944     GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS,
1945         FOUR_BYTE_KEY, ONE_TENTH_M_LONG_STRING);
1946     for (const auto &iter : entriesBatch) {
1947         EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1948     }
1949 
1950     /**
1951      * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1952      * @tc.expected: step1. get success.
1953      */
1954     KvStoreResultSet *resultSetAll = nullptr;
1955     EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1956 
1957     /**
1958      * @tc.steps: step2. Call resultSet interfaces.
1959      * @tc.expected: step2. operate successfully.
1960      */
1961     std::vector<std::thread> threads;
1962     threads.push_back(std::thread(CursorRandOperThread1, std::ref(resultSetAll)));
1963     threads.push_back(std::thread(CursorRandOperThread2, std::ref(resultSetAll)));
1964     threads.push_back(std::thread(CursorRandOperThread3, std::ref(resultSetAll)));
1965     threads.push_back(std::thread(CursorRandOperThread4, std::ref(resultSetAll)));
1966 
1967     for (auto& th : threads) {
1968         th.join();
1969     }
1970 
1971     /**
1972      * @tc.steps: step11. close recordset.
1973      * @tc.expected: step11. call success.
1974      */
1975     EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1976 }
1977 namespace {
VerifyResultSetInterfaces(KvStoreNbDelegate ** delegates,unsigned long delegateCount)1978 void VerifyResultSetInterfaces(KvStoreNbDelegate **delegates, unsigned long delegateCount)
1979 {
1980     ASSERT_TRUE(delegates != nullptr);
1981     Key keyPrefixKA = { 'k', 'a' };
1982     Key keyPrefixKB = { 'k', 'b' };
1983     Key keyPrefixKK = { 'k', 'k' };
1984     KvStoreResultSet *resultSetAlls[OPEN_DB_TIMES] = { nullptr };
1985     KvStoreResultSet *resultSetKAs[OPEN_DB_TIMES] = { nullptr };
1986     KvStoreResultSet *resultSetKBs[OPEN_DB_TIMES] = { nullptr };
1987     KvStoreResultSet *resultSetKKs[OPEN_DB_TIMES] = { nullptr };
1988     unsigned long delegateCnt = 0;
1989     for (delegateCnt = 0; delegateCnt < delegateCount; ++delegateCnt) {
1990         /**
1991          * @tc.steps: step2. check GetEntries of interfaces of every delegate.
1992          * @tc.expected: step2. success.
1993          */
1994         EXPECT_EQ(delegates[delegateCnt]->GetEntries(KEY_EMPTY, resultSetAlls[delegateCnt]), OK);
1995         EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKA, resultSetKAs[delegateCnt]), OK);
1996         EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKB, resultSetKBs[delegateCnt]), OK);
1997         EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKK, resultSetKKs[delegateCnt]), OK);
1998         /**
1999          * @tc.steps: step3. check GetCount of interfaces of every delegate.
2000          * @tc.expected: step3. success.
2001          */
2002         EXPECT_TRUE(resultSetAlls[delegateCnt]->GetCount() == TWO_HUNDREDS_RECORDS);
2003         EXPECT_TRUE(resultSetKAs[delegateCnt]->GetCount() == ONE_HUNDRED_RECORDS);
2004         EXPECT_TRUE(resultSetKBs[delegateCnt]->GetCount() == ONE_HUNDRED_RECORDS);
2005         EXPECT_TRUE(resultSetKKs[delegateCnt]->GetCount() == 0);
2006     }
2007 
2008     for (delegateCnt = 0; delegateCnt < delegateCount; ++delegateCnt) {
2009         /**
2010          * @tc.steps: step4. check GetCount of interfaces of every delegate.
2011          * @tc.expected: step4. success.
2012          */
2013         EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetAlls[delegateCnt]) == OK);
2014         EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKAs[delegateCnt]) == OK);
2015         EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKBs[delegateCnt]) == OK);
2016         EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKKs[delegateCnt]) == OK);
2017     }
2018 }
2019 }
2020 
ResultSetDb027(bool isRowIdMode)2021 void DistributeddbNbCursorTestcase::ResultSetDb027(bool isRowIdMode)
2022 {
2023     KvStoreDelegateManager *managers[OPEN_DB_TIMES] = {nullptr};
2024     KvStoreNbDelegate *delegates[OPEN_DB_TIMES] = {nullptr};
2025     Option option;
2026     unsigned long delegateCnt = 0;
2027     /**
2028      * @tc.steps: step1. open STORE_ID_2 for three times.
2029      * @tc.expected: step1. success.
2030      */
2031     delegates[INDEX_ZEROTH] = DistributedDBNbTestTools::GetNbDelegateSuccess(managers[INDEX_ZEROTH],
2032         g_dbParameter3, option);
2033     ASSERT_TRUE(managers[INDEX_ZEROTH] != nullptr && delegates[INDEX_ZEROTH] != nullptr);
2034     SetResultSetCacheMode(delegates[INDEX_ZEROTH], isRowIdMode);
2035 
2036     vector<Entry> entriesKA, entriesKB;
2037     std::vector<uint8_t> ka = { 'k', 'a' };
2038     std::vector<uint8_t> kb = { 'k', 'b' };
2039     EntrySize entrySizeKA = { FIVE_BYTE_KEY, ONE_K_LONG_STRING };
2040     EntrySize entrySizeKB = { FIVE_BYTE_KEY, ONE_TENTH_M_LONG_STRING };
2041     GenerateAppointPrefixAndSizeRecords(entriesKA, entrySizeKA, ONE_HUNDRED_RECORDS, ka, {'v'});
2042     GenerateAppointPrefixAndSizeRecords(entriesKB, entrySizeKB, ONE_HUNDRED_RECORDS, kb, {'v'});
2043     for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
2044         EXPECT_TRUE(delegates[INDEX_ZEROTH]->Put(entriesKA[index].key, entriesKA[index].value) == OK);
2045         EXPECT_TRUE(delegates[INDEX_ZEROTH]->Put(entriesKB[index].key, entriesKB[index].value) == OK);
2046     }
2047 
2048     option.createIfNecessary = false;
2049     for (delegateCnt = INDEX_FIRST; delegateCnt < DELEGATE_NUM; ++delegateCnt) {
2050         delegates[delegateCnt] = DistributedDBNbTestTools::GetNbDelegateSuccess(managers[delegateCnt],
2051             g_dbParameter3, option);
2052         ASSERT_TRUE(managers[delegateCnt] != nullptr && delegates[delegateCnt] != nullptr);
2053         SetResultSetCacheMode(delegates[delegateCnt], isRowIdMode);
2054     }
2055 
2056     VerifyResultSetInterfaces(delegates, DELEGATE_NUM);
2057     for (unsigned long operCnt = INDEX_ZEROTH; operCnt < OPEN_DB_TIMES; ++operCnt) {
2058         EXPECT_EQ(managers[operCnt]->CloseKvStore(delegates[operCnt]), OK);
2059         delegates[operCnt] = nullptr;
2060         if (operCnt == OPEN_DB_TIMES - 1) {
2061             EXPECT_EQ(managers[operCnt]->DeleteKvStore(STORE_ID_3), OK);
2062         }
2063         delete managers[operCnt];
2064         managers[operCnt] = nullptr;
2065     }
2066 }