1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "distributeddb_nb_cursor_testcase.h"
16
17 #include <gtest/gtest.h>
18 #include <ctime>
19 #include <cmath>
20 #include <random>
21 #include <chrono>
22 #include <thread>
23 #include <mutex>
24 #include <string>
25 #include <condition_variable>
26 #include "types_export.h"
27 #include "kv_store_delegate.h"
28 #include "kv_store_nb_delegate.h"
29 #include "kv_store_delegate_manager.h"
30 #include "distributed_test_tools.h"
31 #include "distributeddb_nb_test_tools.h"
32 #include "distributeddb_data_generator.h"
33
34 using namespace std;
35 using namespace std::chrono;
36 using namespace std::placeholders;
37 using namespace DistributedDB;
38 using namespace DistributedDBDataGenerator;
39
40 namespace {
41 const unsigned int FIFTEEN_RECORDS = 15;
42 const unsigned int CURSOR_DIFFERENT_RECORDS = 102;
43 const unsigned long ONE_TENTH_M_LONG_STRING = 104858;
44 const unsigned int FOUR_BYTE_KEY = 4;
45 const unsigned int FIVE_BYTE_KEY = 5;
46 const unsigned int THREAD_NUM_START = 1;
47 const unsigned int THREAD_NUM_END = 4;
48 const unsigned int OPEN_DB_TIMES = 3;
49 const unsigned int DELEGATE_NUM = 3;
50 const unsigned int WAIT_FOR_OBSERVER_REKEY = 75000;
51 const int CURSOR_ORIGINAL_POSITION_NEGATIVE10 = -10;
52 const int CURSOR_ORIGINAL_POSITION_NEGATIVE1 = -1;
53 const int CURSOR_ORIGINAL_POSITION_0 = 0;
54 const int CURSOR_ORIGINAL_POSITION_1 = 1;
55 const int CURSOR_ORIGINAL_POSITION_2 = 2;
56 const int CURSOR_ORIGINAL_POSITION_40 = 40;
57 const int CURSOR_ORIGINAL_POSITION_50 = 50;
58 const int CURSOR_ORIGINAL_POSITION_60 = 60;
59 const int CURSOR_ORIGINAL_POSITION_99 = 99;
60 const int CURSOR_ORIGINAL_POSITION_100 = 100;
61 const int CURSOR_ORIGINAL_POSITION_120 = 120;
62
63 const int CURSOR_POSITION_NEGATIVE5 = -5;
64 const int CURSOR_POSITION_NEGATIVE1 = -1;
65 const int CURSOR_POSITION_0 = 0;
66 const int CURSOR_POSITION_1 = 1;
67 const int CURSOR_POSITION_2 = 2;
68 const int CURSOR_POSITION_40 = 40;
69 const int CURSOR_POSITION_44 = 44;
70 const int CURSOR_POSITION_49 = 49;
71 const int CURSOR_POSITION_50 = 50;
72 const int CURSOR_POSITION_55 = 55;
73 const int CURSOR_POSITION_60 = 60;
74 const int CURSOR_POSITION_99 = 99;
75 const int CURSOR_POSITION_100 = 100;
76
77 const int CURSOR_OFFSET_NEGATIVE65 = -65;
78 const int CURSOR_OFFSET_NEGATIVE60 = -60;
79 const int CURSOR_OFFSET_NEGATIVE50 = -50;
80 const int CURSOR_OFFSET_NEGATIVE45 = -45;
81 const int CURSOR_OFFSET_NEGATIVE2 = -2;
82 const int CURSOR_OFFSET_NEGATIVE1 = -1;
83 const int CURSOR_OFFSET_0 = 0;
84 const int CURSOR_OFFSET_1 = 1;
85 const int CURSOR_OFFSET_2 = 2;
86 const int CURSOR_OFFSET_5 = 5;
87 const int CURSOR_OFFSET_45 = 45;
88 const int CURSOR_OFFSET_50 = 50;
89 const int CURSOR_OFFSET_55 = 55;
90 const int CURSOR_OFFSET_60 = 60;
91 const int CURSOR_OFFSET_65 = 65;
92 DistributedDB::CipherPassword g_passwd1;
93
94 struct PositionStatus01 {
95 DBStatus isGetSuccess = OK;
96 bool isFirst = true;
97 bool isLast = false;
98 bool isBeforeFirst = false;
99 bool isAfterLast = false;
100 int position = 0;
101 };
102
SetResultSetCacheMode(KvStoreNbDelegate * delegate,bool isRowIdMode)103 void SetResultSetCacheMode(KvStoreNbDelegate *delegate, bool isRowIdMode)
104 {
105 int cacheMode = 0;
106 if (isRowIdMode) {
107 cacheMode = static_cast<int>(ResultSetCacheMode::CACHE_ENTRY_ID_ONLY);
108 } else {
109 cacheMode = static_cast<int>(ResultSetCacheMode::CACHE_FULL_ENTRY);
110 }
111 PragmaData data = static_cast<PragmaData>(&cacheMode);
112 EXPECT_EQ(delegate->Pragma(RESULT_SET_CACHE_MODE, data), OK);
113 }
114
JudgePosition(KvStoreResultSet * & resultSet,const PositionStatus01 & currentStatus)115 bool JudgePosition(KvStoreResultSet *&resultSet, const PositionStatus01 ¤tStatus)
116 {
117 Entry entry;
118 bool bRes = true;
119 DBStatus status = resultSet->GetEntry(entry);
120 bRes = bRes && (status == currentStatus.isGetSuccess);
121 bool result = resultSet->IsFirst();
122 bRes = bRes && (result == currentStatus.isFirst);
123 result = resultSet->IsLast();
124 bRes = bRes && (result == currentStatus.isLast);
125 result = resultSet->IsBeforeFirst();
126 bRes = bRes && (result == currentStatus.isBeforeFirst);
127 result = resultSet->IsAfterLast();
128 bRes = bRes && (result == currentStatus.isAfterLast);
129 int position = resultSet->GetPosition();
130 bRes = bRes && (position == currentStatus.position);
131 return bRes;
132 }
133 }
134
ResultSetDb001(KvStoreNbDelegate * delegate,bool isRowIdMode)135 void DistributeddbNbCursorTestcase::ResultSetDb001(KvStoreNbDelegate *delegate, bool isRowIdMode)
136 {
137 ASSERT_TRUE(delegate != nullptr);
138 SetResultSetCacheMode(delegate, isRowIdMode);
139 std::vector<DistributedDB::Entry> entries;
140 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
141 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
142 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
143 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
144 }
145 /**
146 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
147 * @tc.expected: step1. get success.
148 */
149 KvStoreResultSet *resultSet = nullptr;
150 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
151
152 /**
153 * @tc.steps: step2. call GetCount interface.
154 * @tc.expected: step2. call success.
155 */
156 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
157
158 /**
159 * @tc.steps: step3. call GetPosition and MoveToPrevious interface.
160 * @tc.expected: step3. when the Current position is -1, can't do MoveToPrevious, and if do, it can't effect at all.
161 */
162 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
163 EXPECT_TRUE(resultSet->MoveToPrevious() == false);
164
165 /**
166 * @tc.steps: step4. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast and GetPostion interface.
167 * @tc.expected: step4. when the Current position is -1, other position judge interface can return right result.
168 */
169 PositionStatus01 currentStatus1 = {NOT_FOUND, false, false, true, false, CURSOR_POSITION_NEGATIVE1};
170 EXPECT_TRUE(JudgePosition(resultSet, currentStatus1));
171
172 /**
173 * @tc.steps: step5. call MoveToFirst interface.
174 * @tc.expected: step5. Move success.
175 */
176 EXPECT_TRUE(resultSet->MoveToFirst() == true);
177 /**
178 * @tc.steps: step6. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
179 * @tc.expected: step6. when the Current position is 0, other position judge interface can return right result.
180 */
181 PositionStatus01 currentStatus2 = {OK, true, false, false, false, CURSOR_POSITION_0};
182 EXPECT_TRUE(JudgePosition(resultSet, currentStatus2));
183 /**
184 * @tc.steps: step7. call MoveToNext interface.
185 * @tc.expected: step7. Move success.
186 */
187 EXPECT_TRUE(resultSet->MoveToNext() == true);
188 /**
189 * @tc.steps: step8. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
190 * @tc.expected: step8. when the Current position is 1, other position judge interface can return right result.
191 */
192 PositionStatus01 currentStatus3 = {OK, false, false, false, false, CURSOR_POSITION_1};
193 EXPECT_TRUE(JudgePosition(resultSet, currentStatus3));
194 /**
195 * @tc.steps: step9. call MoveToLast interface.
196 * @tc.expected: step9. Move success.
197 */
198 EXPECT_TRUE(resultSet->MoveToLast() == true);
199 /**
200 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
201 * @tc.expected: step10. when the Current position is the last,
202 * other position judge interface can return right result.
203 */
204 PositionStatus01 currentStatus4 = {OK, false, true, false, false, CURSOR_POSITION_99};
205 EXPECT_TRUE(JudgePosition(resultSet, currentStatus4));
206 /**
207 * @tc.steps: step11. call MoveToNext interface.
208 * @tc.expected: step11. Move success.
209 */
210 EXPECT_TRUE(resultSet->MoveToNext() == false);
211 /**
212 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
213 * @tc.expected: step10. if call MoveToNext interface when the Current position is the last,
214 * other position judge interface can also return right result.
215 */
216 PositionStatus01 currentStatus5 = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_100};
217 EXPECT_TRUE(JudgePosition(resultSet, currentStatus5));
218
219 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
220 }
221
ResultSetDb002(KvStoreNbDelegate * delegate,bool isRowIdMode)222 void DistributeddbNbCursorTestcase::ResultSetDb002(KvStoreNbDelegate *delegate, bool isRowIdMode)
223 {
224 ASSERT_TRUE(delegate != nullptr);
225 SetResultSetCacheMode(delegate, isRowIdMode);
226 std::vector<DistributedDB::Entry> entries;
227 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
228 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
229 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
230 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
231 }
232 /**
233 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
234 * @tc.expected: step1. get success.
235 */
236 KvStoreResultSet *resultSet = nullptr;
237 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
238
239 sort(entries.begin(), entries.end(), DistributedTestTools::CompareKey);
240
241 /**
242 * @tc.steps: step2. set the current position is -1, call MoveToNext, GetPostion and GetEntry interface looply.
243 * @tc.expected: step2. return values are all right.
244 */
245 EXPECT_TRUE(DistributedDBNbTestTools::MoveToNextFromBegin(*resultSet, entries, CURSOR_POSITION_100));
246 /**
247 * @tc.steps: step2. set the current position is 100, call MoveToPrevious, GetPostion, GetEntry looply.
248 * @tc.expected: step2. return values are all right.
249 */
250 Entry entry;
251 for (int position = CURSOR_POSITION_100; position > CURSOR_POSITION_NEGATIVE1; --position) {
252 bool result = resultSet->MoveToPrevious();
253 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
254 EXPECT_TRUE(result == true);
255 } else {
256 EXPECT_TRUE(result == false);
257 }
258 int positionGot = resultSet->GetPosition();
259 EXPECT_TRUE(positionGot == (position - CURSOR_POSITION_1));
260 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
261 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
262 EXPECT_TRUE(CompareVector(entry.key, entries[position - 1].key));
263 EXPECT_TRUE(CompareVector(entry.value, entries[position - 1].value));
264 } else {
265 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
266 }
267 }
268 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
269 }
270 namespace {
271 struct MoveStatus {
272 int offSet = 0;
273 bool moveResult = true;
274 int currentPosition = 0;
275 DBStatus status = OK;
276 };
277
MoveAndCheck(KvStoreResultSet * & resultSet,const MoveStatus & status)278 bool MoveAndCheck(KvStoreResultSet *&resultSet, const MoveStatus &status)
279 {
280 bool result = true;
281 Entry entry;
282 bool mRes = resultSet->Move(status.offSet);
283 result = result && (mRes == status.moveResult);
284 int position = resultSet->GetPosition();
285 result = result && (position == status.currentPosition);
286 DBStatus statusGot = resultSet->GetEntry(entry);
287 result = result && (statusGot == status.status);
288 return result;
289 }
290 }
291
ResultSetDb003(KvStoreNbDelegate * delegate,bool isRowIdMode)292 void DistributeddbNbCursorTestcase::ResultSetDb003(KvStoreNbDelegate *delegate, bool isRowIdMode)
293 {
294 ASSERT_TRUE(delegate != nullptr);
295 SetResultSetCacheMode(delegate, isRowIdMode);
296 std::vector<DistributedDB::Entry> entries;
297 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
298 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
299 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
300 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
301 }
302 /**
303 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
304 * @tc.expected: step1. get success.
305 */
306 KvStoreResultSet *resultSet = nullptr;
307 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
308 /**
309 * @tc.steps: step2. call Move interface move to offset 50 and check the result.
310 * @tc.expected: step2. move ok, and the position is 50, and can get entry.
311 */
312 MoveStatus status1 = {CURSOR_OFFSET_50, true, CURSOR_POSITION_49, OK};
313 EXPECT_TRUE(MoveAndCheck(resultSet, status1));
314 /**
315 * @tc.steps: step3. call Move interface move to offset 0 by upstairs and check the result.
316 * @tc.expected: step3. move ok, and the position is 50, and can get entry.
317 */
318 MoveStatus status2 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_49, OK};
319 EXPECT_TRUE(MoveAndCheck(resultSet, status2));
320 /**
321 * @tc.steps: step4. call Move interface move to offset 60 by upstairs and check the result.
322 * @tc.expected: step4. Move failed, and the position is 100, and can't get entry.
323 */
324 MoveStatus status3 = {CURSOR_OFFSET_60, false, CURSOR_POSITION_100, NOT_FOUND};
325 EXPECT_TRUE(MoveAndCheck(resultSet, status3));
326 /**
327 * @tc.steps: step5. call Move interface move to offset -50 by upstairs and check the result.
328 * @tc.expected: step5. move ok, and the position is 50, and can get entry.
329 */
330 MoveStatus status4 = {CURSOR_OFFSET_NEGATIVE50, true, CURSOR_POSITION_50, OK};
331 EXPECT_TRUE(MoveAndCheck(resultSet, status4));
332 /**
333 * @tc.steps: step6. call Move interface move to offset 0 by upstairs and check the result.
334 * @tc.expected: step6. move ok, and the position is 50, and can get entry.
335 */
336 MoveStatus status5 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_50, OK};
337 EXPECT_TRUE(MoveAndCheck(resultSet, status5));
338 /**
339 * @tc.steps: step7. call Move interface move to offset -60 by upstairs and check the result.
340 * @tc.expected: step7. Move failed, and the position is -1, and can't get entry.
341 */
342 MoveStatus status6 = {CURSOR_OFFSET_NEGATIVE60, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
343 EXPECT_TRUE(MoveAndCheck(resultSet, status6));
344
345 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
346 }
347 namespace {
348 struct PositionStatus02 {
349 int originalPosition = 0;
350 bool moveResult = true;
351 int currentPosition = 0;
352 DBStatus status = OK;
353 };
354
MoveToPositionAndCheck(KvStoreResultSet * & resultSet,const PositionStatus02 & status)355 bool MoveToPositionAndCheck(KvStoreResultSet *&resultSet, const PositionStatus02 &status)
356 {
357 bool result = true;
358 Entry entry;
359 bool mRes = resultSet->MoveToPosition(status.originalPosition);
360 result = result && (mRes == status.moveResult);
361 int position = resultSet->GetPosition();
362 result = result && (position == status.currentPosition);
363 DBStatus statusGot = resultSet->GetEntry(entry);
364 result = result && (statusGot == status.status);
365 return result;
366 }
367 }
ResultSetDb004(KvStoreNbDelegate * delegate,bool isRowIdMode)368 void DistributeddbNbCursorTestcase::ResultSetDb004(KvStoreNbDelegate *delegate, bool isRowIdMode)
369 {
370 ASSERT_TRUE(delegate != nullptr);
371 SetResultSetCacheMode(delegate, isRowIdMode);
372 std::vector<DistributedDB::Entry> entries;
373 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_ONE_K_BYTE};
374 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
375 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
376 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
377 }
378 /**
379 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
380 * @tc.expected: step1. get success.
381 */
382 KvStoreResultSet *resultSet = nullptr;
383 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
384 /**
385 * @tc.steps: step2. call MoveToPostion interface move to position 50 and check the result.
386 * @tc.expected: step2. MoveToPostion ok, and the position is 50, and can get entry.
387 */
388 PositionStatus02 status1 = {CURSOR_ORIGINAL_POSITION_50, true, CURSOR_POSITION_50, OK};
389 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status1));
390 /**
391 * @tc.steps: step3. call MoveToPostion interface move to position 60 and check the result.
392 * @tc.expected: step3. MoveToPostion ok, and the position is 60, and can get entry.
393 */
394 PositionStatus02 status2 = {CURSOR_ORIGINAL_POSITION_60, true, CURSOR_POSITION_60, OK};
395 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status2));
396 /**
397 * @tc.steps: step4. call MoveToPostion interface move to position -10 and check the result.
398 * @tc.expected: step4. Move failed, and the position is -1, and can't get entry.
399 */
400 PositionStatus02 status3 = {CURSOR_ORIGINAL_POSITION_NEGATIVE10, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
401 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status3));
402 /**
403 * @tc.steps: step5. call MoveToPostion interface move to position 120 and check the result.
404 * @tc.expected: step5. Move failed, and the position is 100, and can't get entry.
405 */
406 PositionStatus02 status4 = {CURSOR_ORIGINAL_POSITION_120, false, CURSOR_POSITION_100, NOT_FOUND};
407 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status4));
408 /**
409 * @tc.steps: step6. call MoveToPostion interface move to position 100 and check the result.
410 * @tc.expected: step6. Move failed, and the position is 100, and can't get entry.
411 */
412 PositionStatus02 status5 = {CURSOR_ORIGINAL_POSITION_100, false, CURSOR_POSITION_100, NOT_FOUND};
413 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status5));
414 /**
415 * @tc.steps: step7. call MoveToPostion interface move to position 0 and check the result.
416 * @tc.expected: step7. move OK, and the position is 0, and can get entry.
417 */
418 PositionStatus02 status6 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
419 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status6));
420 /**
421 * @tc.steps: step8. call MoveToPostion interface move to position 99 and check the result.
422 * @tc.expected: step8. move OK, and the position is 99, and can get entry.
423 */
424 PositionStatus02 status7 = {CURSOR_ORIGINAL_POSITION_99, true, CURSOR_POSITION_99, OK};
425 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status7));
426 /**
427 * @tc.steps: step9. call MoveToPostion interface move to position 0 and check the result.
428 * @tc.expected: step9. move OK, and the position is 0, and can get entry.
429 */
430 PositionStatus02 status8 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
431 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status8));
432 /**
433 * @tc.steps: step10. call MoveToPostion interface move to position -1 and check the result.
434 * @tc.expected: step10. Move failed, and the position is -1, and can't get entry.
435 */
436 PositionStatus02 status9 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
437 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status9));
438
439 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
440 }
441
ResultSetDb005(KvStoreNbDelegate * delegate,bool isRowIdMode)442 void DistributeddbNbCursorTestcase::ResultSetDb005(KvStoreNbDelegate *delegate, bool isRowIdMode)
443 {
444 ASSERT_TRUE(delegate != nullptr);
445 SetResultSetCacheMode(delegate, isRowIdMode);
446 std::vector<DistributedDB::Entry> entries;
447 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
448 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
449 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
450
451 /**
452 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
453 * @tc.expected: step1. get success.
454 */
455 KvStoreResultSet *resultSet = nullptr;
456 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
457 /**
458 * @tc.steps: step2. call GetCount interface.
459 * @tc.expected: step2. call success and returned 1 records.
460 */
461 EXPECT_TRUE(resultSet->GetCount() == ONE_RECORD);
462 /**
463 * @tc.steps: step3. call GetPosition, MoveToPrevious and GetPosition interface and check the result.
464 * @tc.expected: step3. GetPosition returned -1, MoveToPrevious returned false, and GetPosition still returned -1.
465 */
466 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
467 EXPECT_TRUE(resultSet->MoveToPrevious() == false);
468 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
469 /**
470 * @tc.steps: step4. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast and GetPostion interface.
471 * @tc.expected: step4. when the Current position is -1, other position judge interface can return right result.
472 */
473 PositionStatus01 positionStatus = {NOT_FOUND, false, false, true, false, CURSOR_POSITION_NEGATIVE1};
474 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
475 /**
476 * @tc.steps: step5. call MoveToFirst interface.
477 * @tc.expected: step5. Move success.
478 */
479 EXPECT_TRUE(resultSet->MoveToFirst() == true);
480 /**
481 * @tc.steps: step6. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
482 * @tc.expected: step6. when the Current position is 0, other position judge interface can return right result.
483 */
484 positionStatus = {OK, true, true, false, false, CURSOR_POSITION_0};
485 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
486 /**
487 * @tc.steps: step7. call MoveToNext interface.
488 * @tc.expected: step7. Move success.
489 */
490 EXPECT_TRUE(resultSet->MoveToNext() == false);
491 /**
492 * @tc.steps: step8. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
493 * @tc.expected: step8. when the Current position is 1, other position judge interface can return right result.
494 */
495 positionStatus = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_1};
496 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
497 /**
498 * @tc.steps: step9. call MoveToLast interface.
499 * @tc.expected: step9. Move success.
500 */
501 EXPECT_TRUE(resultSet->MoveToLast() == true);
502 /**
503 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
504 * @tc.expected: step10. when the Current position is 1, other position judge interface can return right result.
505 */
506 positionStatus = {OK, true, true, false, false, CURSOR_POSITION_0};
507 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
508 /**
509 * @tc.steps: step11. call MoveToNext interface.
510 * @tc.expected: step11. Move success.
511 */
512 EXPECT_TRUE(resultSet->MoveToNext() == false);
513 /**
514 * @tc.steps: step12. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
515 * @tc.expected: step12. when the Current position is 1, other position judge interface can return right result.
516 */
517 positionStatus = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_1};
518 EXPECT_TRUE(JudgePosition(resultSet, positionStatus));
519
520 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
521 }
522
ResultSetDb006(KvStoreNbDelegate * delegate,bool isRowIdMode)523 void DistributeddbNbCursorTestcase::ResultSetDb006(KvStoreNbDelegate *delegate, bool isRowIdMode)
524 {
525 ASSERT_TRUE(delegate != nullptr);
526 SetResultSetCacheMode(delegate, isRowIdMode);
527 std::vector<DistributedDB::Entry> entries;
528 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
529 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
530 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
531 /**
532 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
533 * @tc.expected: step1. get success.
534 */
535 KvStoreResultSet *resultSet = nullptr;
536 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
537
538 /**
539 * @tc.steps: step2. set the current position is -1, call MoveToNext, GetPostion and GetEntry interface looply.
540 * @tc.expected: step2. return values are all right.
541 */
542 Entry entry;
543 for (int position = CURSOR_POSITION_NEGATIVE1; position < ONE_RECORD; ++position) {
544 bool result = resultSet->MoveToNext();
545 if (position < (ONE_RECORD - CURSOR_POSITION_1)) {
546 EXPECT_TRUE(result == true);
547 } else {
548 EXPECT_TRUE(result == false);
549 }
550 int currentPosition = resultSet->GetPosition();
551 EXPECT_TRUE(currentPosition == (position + CURSOR_POSITION_1));
552 if (position < (ONE_RECORD - CURSOR_POSITION_1)) {
553 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
554 EXPECT_TRUE(CompareVector(entry.key, entries[position + CURSOR_POSITION_1].key));
555 EXPECT_TRUE(CompareVector(entry.value, entries[position + CURSOR_POSITION_1].value));
556 } else {
557 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
558 }
559 }
560 /**
561 * @tc.steps: step2. set the current position is 100, call MoveToPrevious, GetPostion, GetEntry looply.
562 * @tc.expected: step2. return values are all right.
563 */
564 for (int position = ONE_RECORD; position > CURSOR_POSITION_NEGATIVE1; --position) {
565 bool result = resultSet->MoveToPrevious();
566 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
567 EXPECT_TRUE(result == true);
568 } else {
569 EXPECT_TRUE(result == false);
570 }
571 int currentPosition = resultSet->GetPosition();
572 EXPECT_TRUE(currentPosition == (position - CURSOR_POSITION_1));
573 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
574 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
575 EXPECT_TRUE(CompareVector(entry.key, entries[position - CURSOR_POSITION_1].key));
576 EXPECT_TRUE(CompareVector(entry.value, entries[position - CURSOR_POSITION_1].value));
577 } else {
578 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
579 }
580 }
581 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
582 }
583
ResultSetDb007(KvStoreNbDelegate * delegate,bool isRowIdMode)584 void DistributeddbNbCursorTestcase::ResultSetDb007(KvStoreNbDelegate *delegate, bool isRowIdMode)
585 {
586 ASSERT_TRUE(delegate != nullptr);
587 SetResultSetCacheMode(delegate, isRowIdMode);
588 std::vector<DistributedDB::Entry> entries;
589 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
590 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
591 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
592 /**
593 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
594 * @tc.expected: step1. get success.
595 */
596 KvStoreResultSet *resultSet = nullptr;
597 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
598 /**
599 * @tc.steps: step2. call Move interface move to offset 50 and check the result.
600 * @tc.expected: step2. move ok, and the position is 50, and can get entry.
601 */
602 MoveStatus status1 = {CURSOR_OFFSET_1, true, CURSOR_POSITION_0, OK};
603 EXPECT_TRUE(MoveAndCheck(resultSet, status1));
604 /**
605 * @tc.steps: step3. call Move interface move to offset 0 by upstairs and check the result.
606 * @tc.expected: step3. move ok, and the position is 50, and can get entry.
607 */
608 MoveStatus status2 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_0, OK};
609 EXPECT_TRUE(MoveAndCheck(resultSet, status2));
610 /**
611 * @tc.steps: step4. call Move interface move to offset 60 by upstairs and check the result.
612 * @tc.expected: step4. Move failed, and the position is 100, and can't get entry.
613 */
614 MoveStatus status3 = {CURSOR_OFFSET_2, false, CURSOR_POSITION_1, NOT_FOUND};
615 EXPECT_TRUE(MoveAndCheck(resultSet, status3));
616 /**
617 * @tc.steps: step5. call Move interface move to offset -50 by upstairs and check the result.
618 * @tc.expected: step5. move ok, and the position is 50, and can get entry.
619 */
620 MoveStatus status4 = {CURSOR_OFFSET_NEGATIVE1, true, CURSOR_POSITION_0, OK};
621 EXPECT_TRUE(MoveAndCheck(resultSet, status4));
622 /**
623 * @tc.steps: step6. call Move interface move to offset 0 by upstairs and check the result.
624 * @tc.expected: step6. move ok, and the position is 50, and can get entry.
625 */
626 MoveStatus status5 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_0, OK};
627 EXPECT_TRUE(MoveAndCheck(resultSet, status5));
628 /**
629 * @tc.steps: step7. call Move interface move to offset -60 by upstairs and check the result.
630 * @tc.expected: step7. Move failed, and the position is -1, and can't get entry.
631 */
632 MoveStatus status6 = {CURSOR_OFFSET_NEGATIVE2, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
633 EXPECT_TRUE(MoveAndCheck(resultSet, status6));
634
635 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
636 }
637
ResultSetDb008(KvStoreNbDelegate * delegate,bool isRowIdMode)638 void DistributeddbNbCursorTestcase::ResultSetDb008(KvStoreNbDelegate *delegate, bool isRowIdMode)
639 {
640 ASSERT_TRUE(delegate != nullptr);
641 SetResultSetCacheMode(delegate, isRowIdMode);
642 std::vector<DistributedDB::Entry> entries;
643 EntrySize entrySize = {KEY_SIX_BYTE, FOUR_M_LONG_STRING};
644 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_RECORD);
645 EXPECT_TRUE(delegate->Put(entries[0].key, entries[0].value) == OK);
646 /**
647 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
648 * @tc.expected: step1. get success.
649 */
650 KvStoreResultSet *resultSet = nullptr;
651 EXPECT_TRUE(delegate->GetEntries(KEY_K, resultSet) == OK);
652 /**
653 * @tc.steps: step2. call MoveToPostion interface move to position 0 and check the result.
654 * @tc.expected: step2. MoveToPostion ok, and the position is 50, and can get entry.
655 */
656 PositionStatus02 status1 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
657 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status1));
658 /**
659 * @tc.steps: step3. call MoveToPostion interface move to position 1 and check the result.
660 * @tc.expected: step3. MoveToPostion false, and the position is 1, and can't get entry.
661 */
662 PositionStatus02 status2 = {CURSOR_ORIGINAL_POSITION_1, false, CURSOR_POSITION_1, NOT_FOUND};
663 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status2));
664 /**
665 * @tc.steps: step4. call MoveToPostion interface move to position -1 and check the result.
666 * @tc.expected: step4. Move failed, and the position is -1, and can't get entry.
667 */
668 PositionStatus02 status3 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
669 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status3));
670 /**
671 * @tc.steps: step5. call MoveToPostion interface move to position 2 and check the result.
672 * @tc.expected: step5. Move failed, and the position is 1, and can't get entry.
673 */
674 PositionStatus02 status4 = {CURSOR_ORIGINAL_POSITION_2, false, CURSOR_POSITION_1, NOT_FOUND};
675 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status4));
676 /**
677 * @tc.steps: step6. call MoveToPostion interface move to position 1 and check the result.
678 * @tc.expected: step6. Move failed, and the position is 1, and can't get entry.
679 */
680 PositionStatus02 status5 = {CURSOR_ORIGINAL_POSITION_1, false, CURSOR_POSITION_1, NOT_FOUND};
681 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status5));
682 /**
683 * @tc.steps: step7. call MoveToPostion interface move to position 0 and check the result.
684 * @tc.expected: step7. move OK, and the position is 0, and can get entry.
685 */
686 PositionStatus02 status6 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
687 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status6));
688 /**
689 * @tc.steps: step8. call MoveToPostion interface move to position 1 and check the result.
690 * @tc.expected: step8. Move failed, and the position is 1, and can't get entry.
691 */
692 PositionStatus02 status7 = {CURSOR_ORIGINAL_POSITION_1, false, CURSOR_POSITION_1, NOT_FOUND};
693 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status7));
694 /**
695 * @tc.steps: step9. call MoveToPostion interface move to position -1 and check the result.
696 * @tc.expected: step9. Move failed, and the position is -1, and can't get entry.
697 */
698 PositionStatus02 status8 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
699 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status8));
700
701 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
702 }
703
ResultSetDb009(KvStoreNbDelegate * delegate,bool isRowIdMode)704 void DistributeddbNbCursorTestcase::ResultSetDb009(KvStoreNbDelegate *delegate, bool isRowIdMode)
705 {
706 ASSERT_TRUE(delegate != nullptr);
707 SetResultSetCacheMode(delegate, isRowIdMode);
708 std::vector<DistributedDB::Entry> entries;
709 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
710 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
711 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
712 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
713 }
714
715 /**
716 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
717 * @tc.expected: step1. get success.
718 */
719 KvStoreResultSet *resultSet = nullptr;
720 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
721 /**
722 * @tc.steps: step2. call GetCount interface.
723 * @tc.expected: step2. call success and returned 10 records.
724 */
725 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
726 /**
727 * @tc.steps: step3. call GetPosition, MoveToPrevious and GetPosition interface and check the result.
728 * @tc.expected: step3. GetPosition returned -1, MoveToPrevious returned false, and GetPosition still returned -1.
729 */
730 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
731 EXPECT_TRUE(resultSet->MoveToPrevious() == false);
732 EXPECT_TRUE(resultSet->GetPosition() == CURSOR_POSITION_NEGATIVE1);
733 /**
734 * @tc.steps: step4. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast and GetPostion interface.
735 * @tc.expected: step4. when the Current position is -1, other position judge interface can return right result.
736 */
737 PositionStatus01 position = {NOT_FOUND, false, false, true, false, CURSOR_POSITION_NEGATIVE1};
738 EXPECT_TRUE(JudgePosition(resultSet, position));
739 /**
740 * @tc.steps: step5. call MoveToFirst interface.
741 * @tc.expected: step5. Move success.
742 */
743 EXPECT_TRUE(resultSet->MoveToFirst() == true);
744 /**
745 * @tc.steps: step6. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
746 * @tc.expected: step6. when the Current position is 0, other position judge interface can return right result.
747 */
748 position = {OK, true, false, false, false, CURSOR_POSITION_0};
749 EXPECT_TRUE(JudgePosition(resultSet, position));
750 /**
751 * @tc.steps: step7. call MoveToNext interface.
752 * @tc.expected: step7. Move success.
753 */
754 EXPECT_TRUE(resultSet->MoveToNext() == true);
755 /**
756 * @tc.steps: step8. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
757 * @tc.expected: step8. when the Current position is 1, other position judge interface can return right result.
758 */
759 position = {OK, false, false, false, false, CURSOR_POSITION_1};
760 EXPECT_TRUE(JudgePosition(resultSet, position));
761 /**
762 * @tc.steps: step9. call MoveToLast interface.
763 * @tc.expected: step9. Move success.
764 */
765 EXPECT_TRUE(resultSet->MoveToLast() == true);
766 /**
767 * @tc.steps: step10. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
768 * @tc.expected: step10. when the Current position is 9, other position judge interface can return right result.
769 */
770 position = {OK, false, true, false, false, CURSOR_POSITION_99};
771 EXPECT_TRUE(JudgePosition(resultSet, position));
772 /**
773 * @tc.steps: step11. call MoveToNext interface.
774 * @tc.expected: step11. Move success.
775 */
776 EXPECT_TRUE(resultSet->MoveToNext() == false);
777 /**
778 * @tc.steps: step12. call GetEntry, IsFirst, IsLast, IsBeforeFirst, IsAfterLast, GetPostion interface.
779 * @tc.expected: step12. when the Current position is 10, other position judge interface can return right result.
780 */
781 position = {NOT_FOUND, false, false, false, true, CURSOR_POSITION_100};
782 EXPECT_TRUE(JudgePosition(resultSet, position));
783
784 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
785 }
786
ResultSetDb010(KvStoreNbDelegate * delegate,bool isRowIdMode)787 void DistributeddbNbCursorTestcase::ResultSetDb010(KvStoreNbDelegate *delegate, bool isRowIdMode)
788 {
789 ASSERT_TRUE(delegate != nullptr);
790 SetResultSetCacheMode(delegate, isRowIdMode);
791 std::vector<DistributedDB::Entry> entries;
792 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
793 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
794 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
795 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
796 }
797
798 /**
799 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
800 * @tc.expected: step1. get success.
801 */
802 KvStoreResultSet *resultSet = nullptr;
803 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
804 sort(entries.begin(), entries.end(), DistributedTestTools::CompareKey);
805 /**
806 * @tc.steps: step2. set the current position is -1, call MoveToNext, GetPostion and GetEntry interface looply.
807 * @tc.expected: step2. return values are all right.
808 */
809 EXPECT_TRUE(DistributedDBNbTestTools::MoveToNextFromBegin(*resultSet, entries, CURSOR_POSITION_100));
810 /**
811 * @tc.steps: step3. set the current position is 100, call MoveToPrevious, GetPostion, GetEntry looply.
812 * @tc.expected: step3. return values are all right.
813 */
814 int currentPosition = CURSOR_POSITION_NEGATIVE1;
815 Entry entry;
816 for (int position = ONE_HUNDRED_RECORDS; position > CURSOR_POSITION_NEGATIVE1; --position) {
817 bool result = resultSet->MoveToPrevious();
818 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
819 EXPECT_TRUE(result == true);
820 } else {
821 EXPECT_TRUE(result == false);
822 }
823 currentPosition = resultSet->GetPosition();
824 EXPECT_TRUE(currentPosition == (position - CURSOR_POSITION_1));
825 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
826 EXPECT_TRUE(resultSet->GetEntry(entry) == OK);
827 EXPECT_TRUE(CompareVector(entry.key, entries[position - CURSOR_POSITION_1].key));
828 EXPECT_TRUE(CompareVector(entry.value, entries[position - CURSOR_POSITION_1].value));
829 } else {
830 EXPECT_TRUE(resultSet->GetEntry(entry) == NOT_FOUND);
831 }
832 }
833 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
834 }
835
ResultSetDb011(KvStoreNbDelegate * delegate,bool isRowIdMode)836 void DistributeddbNbCursorTestcase::ResultSetDb011(KvStoreNbDelegate *delegate, bool isRowIdMode)
837 {
838 ASSERT_TRUE(delegate != nullptr);
839 SetResultSetCacheMode(delegate, isRowIdMode);
840 std::vector<DistributedDB::Entry> entries;
841 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
842 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
843 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
844 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
845 }
846 /**
847 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
848 * @tc.expected: step1. get success.
849 */
850 KvStoreResultSet *resultSet = nullptr;
851 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
852 /**
853 * @tc.steps: step2. call Move interface move to offset 45 and check the result.
854 * @tc.expected: step2. move ok, and the position is 44, and can get entry.
855 */
856 MoveStatus status1 = {CURSOR_OFFSET_45, true, CURSOR_POSITION_44, OK};
857 EXPECT_TRUE(MoveAndCheck(resultSet, status1));
858 /**
859 * @tc.steps: step3. call Move interface move to offset 0 by upstairs and check the result.
860 * @tc.expected: step3. move ok, and the position is 44, and can get entry.
861 */
862 MoveStatus status2 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_44, OK};
863 EXPECT_TRUE(MoveAndCheck(resultSet, status2));
864 /**
865 * @tc.steps: step4. call Move interface move to offset 65 by upstairs and check the result.
866 * @tc.expected: step4. Move failed, and the position is 100, and can't get entry.
867 */
868 MoveStatus status3 = {CURSOR_OFFSET_55, true, CURSOR_POSITION_99, OK};
869 EXPECT_TRUE(MoveAndCheck(resultSet, status3));
870 MoveStatus status4 = {CURSOR_OFFSET_65, false, CURSOR_POSITION_100, NOT_FOUND};
871 EXPECT_TRUE(MoveAndCheck(resultSet, status4));
872 /**
873 * @tc.steps: step5. call Move interface move to offset -45 by upstairs and check the result.
874 * @tc.expected: step5. move ok, and the position is 55, and can get entry.
875 */
876 MoveStatus status5 = {CURSOR_OFFSET_NEGATIVE45, true, CURSOR_POSITION_55, OK};
877 EXPECT_TRUE(MoveAndCheck(resultSet, status5));
878 /**
879 * @tc.steps: step6. call Move interface move to offset 0 by upstairs and check the result.
880 * @tc.expected: step6. move ok, and the position is 50, and can get entry.
881 */
882 MoveStatus status6 = {CURSOR_OFFSET_0, true, CURSOR_POSITION_55, OK};
883 EXPECT_TRUE(MoveAndCheck(resultSet, status6));
884 /**
885 * @tc.steps: step7. call Move interface move to offset -65 by upstairs and check the result.
886 * @tc.expected: step7. Move failed, and the position is -1, and can't get entry.
887 */
888 MoveStatus status7 = {CURSOR_OFFSET_NEGATIVE65, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
889 EXPECT_TRUE(MoveAndCheck(resultSet, status7));
890
891 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
892 }
893
ResultSetDb012(KvStoreNbDelegate * delegate,bool isRowIdMode)894 void DistributeddbNbCursorTestcase::ResultSetDb012(KvStoreNbDelegate *delegate, bool isRowIdMode)
895 {
896 ASSERT_TRUE(delegate != nullptr);
897 SetResultSetCacheMode(delegate, isRowIdMode);
898 std::vector<DistributedDB::Entry> entries;
899 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
900 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
901 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
902 EXPECT_TRUE(delegate->Put(entries[index].key, entries[index].value) == OK);
903 }
904 /**
905 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet.
906 * @tc.expected: step1. get success.
907 */
908 KvStoreResultSet *resultSet = nullptr;
909 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSet) == OK);
910 /**
911 * @tc.steps: step2. call MoveToPostion interface move to position 40 and check the result.
912 * @tc.expected: step2. Move OK, and the position is 40, and can get entry.
913 */
914 PositionStatus02 status1 = {CURSOR_ORIGINAL_POSITION_40, true, CURSOR_POSITION_40, OK};
915 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status1));
916 /**
917 * @tc.steps: step3. call MoveToPostion interface move to position 60 and check the result.
918 * @tc.expected: step3. Move failed, and the position is 60, and can't get entry.
919 */
920 PositionStatus02 status2 = {CURSOR_ORIGINAL_POSITION_60, true, CURSOR_POSITION_60, OK};
921 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status2));
922 /**
923 * @tc.steps: step4. call MoveToPostion interface move to position -1 and check the result.
924 * @tc.expected: step4. Move failed, and the position is -1, and can't get entry.
925 */
926 PositionStatus02 status3 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
927 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status3));
928 /**
929 * @tc.steps: step5. call MoveToPostion interface move to position 120 and check the result.
930 * @tc.expected: step5. Move failed, and the position is 100, and can't get entry.
931 */
932 PositionStatus02 status4 = {CURSOR_ORIGINAL_POSITION_120, false, CURSOR_POSITION_100, NOT_FOUND};
933 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status4));
934 /**
935 * @tc.steps: step6. call MoveToPostion interface move to position 100 and check the result.
936 * @tc.expected: step6. Move failed, and the position is 100, and can't get entry.
937 */
938 PositionStatus02 status5 = {CURSOR_ORIGINAL_POSITION_100, false, CURSOR_POSITION_100, NOT_FOUND};
939 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status5));
940 /**
941 * @tc.steps: step7. call MoveToPostion interface move to position 0 and check the result.
942 * @tc.expected: step7. move OK, and the position is 0, and can get entry.
943 */
944 PositionStatus02 status6 = {CURSOR_ORIGINAL_POSITION_0, true, CURSOR_POSITION_0, OK};
945 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status6));
946 /**
947 * @tc.steps: step8. call MoveToPostion interface move to position 99 and check the result.
948 * @tc.expected: step8. move OK, and the position is 99, and can get entry.
949 */
950 PositionStatus02 status7 = {CURSOR_ORIGINAL_POSITION_99, true, CURSOR_POSITION_99, OK};
951 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status7));
952 /**
953 * @tc.steps: step9. call MoveToPostion interface move to position -1 and check the result.
954 * @tc.expected: step9. Move failed, and the position is -1, and can't get entry.
955 */
956 PositionStatus02 status8 = {CURSOR_ORIGINAL_POSITION_NEGATIVE1, false, CURSOR_POSITION_NEGATIVE1, NOT_FOUND};
957 EXPECT_TRUE(MoveToPositionAndCheck(resultSet, status8));
958
959 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
960 }
961
ResultSetDb013(KvStoreNbDelegate * delegate,bool isRowIdMode)962 void DistributeddbNbCursorTestcase::ResultSetDb013(KvStoreNbDelegate *delegate, bool isRowIdMode)
963 {
964 ASSERT_TRUE(delegate != nullptr);
965 SetResultSetCacheMode(delegate, isRowIdMode);
966 std::vector<DistributedDB::Entry> entries;
967 EntrySize entrySize = {KEY_SIX_BYTE, VALUE_HUNDRED_K_BYTE};
968 GenerateAppointPrefixAndSizeRecords(entries, entrySize, ONE_HUNDRED_RECORDS);
969 Entry entry4M, 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
ResultSetDb020(KvStoreNbDelegate * delegate,bool isRowIdMode)1366 void DistributeddbNbCursorTestcase::ResultSetDb020(KvStoreNbDelegate *delegate, bool isRowIdMode)
1367 {
1368 ASSERT_TRUE(delegate != nullptr);
1369 SetResultSetCacheMode(delegate, isRowIdMode);
1370 vector<Entry> entries;
1371 vector<Key> allKey;
1372 GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1373 for (const auto &iter : entries) {
1374 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1375 }
1376 /**
1377 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1378 * @tc.expected: step1. get success.
1379 */
1380 KvStoreResultSet *resultSetAll = nullptr;
1381 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1382 /**
1383 * @tc.steps: step2. call GetEntries interface with "ka" parameter to get KvStoreResultSet.
1384 * @tc.expected: step2. get success.
1385 */
1386 KvStoreResultSet *resultSetKA = nullptr;
1387 std::vector<uint8_t> ka = { 'k', 'a' };
1388 Key keyPrefixKA = ka;
1389 EXPECT_EQ(delegate->GetEntries(keyPrefixKA, resultSetKA), OK);
1390 /**
1391 * @tc.steps: step3. call GetEntries interface with "kb" parameter to get KvStoreResultSet.
1392 * @tc.expected: step3. get success.
1393 */
1394 KvStoreResultSet *resultSetKB = nullptr;
1395 std::vector<uint8_t> kb = { 'k', 'b' };
1396 Key keyPrefixKB = kb;
1397 EXPECT_EQ(delegate->GetEntries(keyPrefixKB, resultSetKB), OK);
1398 /**
1399 * @tc.steps: step3. call GetEntries interface with "kc" parameter to get KvStoreResultSet.
1400 * @tc.expected: step3. get success.
1401 */
1402 KvStoreResultSet *resultSetKC = nullptr;
1403 std::vector<uint8_t> kc = { 'k', 'c' };
1404 Key keyPrefixKC = kc;
1405 EXPECT_EQ(delegate->GetEntries(keyPrefixKC, resultSetKC), OK);
1406 /**
1407 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1408 * @tc.expected: step1. get success.
1409 */
1410 KvStoreResultSet *resultSetAll2 = nullptr;
1411 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll2), OVER_MAX_LIMITS);
1412
1413 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1414 EXPECT_EQ(delegate->CloseResultSet(resultSetKA), OK);
1415 EXPECT_EQ(delegate->CloseResultSet(resultSetKB), OK);
1416 EXPECT_EQ(delegate->CloseResultSet(resultSetKC), OK);
1417 }
1418
ResultSetDb021(KvStoreNbDelegate * delegate,KvStoreDelegateManager * manager,bool isRowIdMode)1419 void DistributeddbNbCursorTestcase::ResultSetDb021(KvStoreNbDelegate *delegate,
1420 KvStoreDelegateManager *manager, bool isRowIdMode)
1421 {
1422 ASSERT_TRUE(delegate != nullptr && manager != nullptr);
1423 SetResultSetCacheMode(delegate, isRowIdMode);
1424 vector<Entry> entries;
1425 vector<Key> allKey;
1426 GenerateRecords(ONE_HUNDRED_RECORDS, DEFAULT_START, allKey, entries);
1427 for (const auto &iter : entries) {
1428 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1429 }
1430 /**
1431 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1432 * @tc.expected: step1. get success.
1433 */
1434 KvStoreResultSet *resultSetAll = nullptr;
1435 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1436 /**
1437 * @tc.steps: step2. call GetCount interface of resultSetAll.
1438 * @tc.expected: step2. call success.
1439 */
1440 EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1441 /**
1442 * @tc.steps: step3. closeKvStore returns BUSY because resultSet was not closed.
1443 * @tc.expected: step3. call success.
1444 */
1445 EXPECT_EQ(manager->CloseKvStore(delegate), BUSY);
1446
1447 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1448 }
1449
ResultSetDb022(bool isRowIdMode)1450 void DistributeddbNbCursorTestcase::ResultSetDb022(bool isRowIdMode)
1451 {
1452 KvStoreDelegateManager *manager = nullptr;
1453 KvStoreNbDelegate *delegate = nullptr;
1454 Option option;
1455 option.isEncryptedDb = false;
1456 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1457 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1458 SetResultSetCacheMode(delegate, isRowIdMode);
1459
1460 vector<Entry> entriesBatch;
1461 vector<Key> allKeys;
1462 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1463 for (const auto &iter : entriesBatch) {
1464 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1465 }
1466 /**
1467 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1468 * @tc.expected: step1. get success.
1469 */
1470 KvStoreResultSet *resultSetAll = nullptr;
1471 EXPECT_TRUE(delegate->GetEntries(KEY_EMPTY, resultSetAll) == OK);
1472 /**
1473 * @tc.steps: step2. call GetCount interface of resultSetAll.
1474 * @tc.expected: step2. call success.
1475 */
1476 EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1477 /**
1478 * @tc.steps: step3. Rekey with g_passwd1.
1479 * @tc.expected: step3. call success.
1480 */
1481 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1482 EXPECT_EQ(delegate->Rekey(g_passwd1), BUSY);
1483
1484 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1485 EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1486 delegate = nullptr;
1487 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), OK);
1488 delete manager;
1489 manager = nullptr;
1490 }
1491
ResultSetDb023(bool isRowIdMode)1492 void DistributeddbNbCursorTestcase::ResultSetDb023(bool isRowIdMode)
1493 {
1494 KvStoreDelegateManager *manager = nullptr;
1495 KvStoreNbDelegate *delegate = nullptr;
1496 Option option;
1497 option.isEncryptedDb = false;
1498 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1499 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1500 SetResultSetCacheMode(delegate, isRowIdMode);
1501
1502 vector<Entry> entriesBatch;
1503 vector<Key> allKeys;
1504 GenerateFixedRecords(entriesBatch, allKeys, FOUR_RECORDS, FOUR_BYTE_KEY, ONE_M_LONG_STRING);
1505 for (const auto &iter : entriesBatch) {
1506 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1507 }
1508 /**
1509 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1510 * @tc.expected: step1. get success.
1511 */
1512 KvStoreResultSet *resultSetAll = nullptr;
1513 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1514 /**
1515 * @tc.steps: step2. call GetCount interface of resultSetAll.
1516 * @tc.expected: step2. call success.
1517 */
1518 EXPECT_TRUE(resultSetAll->GetCount() == FOUR_RECORDS);
1519 /**
1520 * @tc.steps: step3. Rekey with g_passwd1.
1521 * @tc.expected: step3. return BUSY.
1522 */
1523 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1524 EXPECT_EQ(delegate->Rekey(g_passwd1), BUSY);
1525
1526 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1527 EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1528 delegate = nullptr;
1529 delete manager;
1530 manager = nullptr;
1531
1532 Option option2;
1533 option2.isEncryptedDb = true;
1534 option2.passwd = PASSWD_VECTOR_1;
1535 option2.createIfNecessary = IS_NOT_NEED_CREATE;
1536 KvStoreNbDelegate *delegate2 = nullptr;
1537 KvStoreDelegateManager *manager2 = nullptr;
1538 delegate2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager2, g_dbParameter2, option2);
1539 ASSERT_TRUE(manager2 == nullptr && delegate2 == nullptr);
1540
1541 option2.isEncryptedDb = false;
1542 delegate2 = DistributedDBNbTestTools::GetNbDelegateSuccess(manager2, g_dbParameter2, option2);
1543 ASSERT_TRUE(manager2 != nullptr && delegate2 != nullptr);
1544 EXPECT_EQ(manager2->CloseKvStore(delegate2), OK);
1545 delegate2 = nullptr;
1546 EXPECT_EQ(manager2->DeleteKvStore(STORE_ID_2), OK);
1547 delete manager2;
1548 manager2 = nullptr;
1549 }
1550
ResultSetDb024(bool isRowIdMode)1551 void DistributeddbNbCursorTestcase::ResultSetDb024(bool isRowIdMode)
1552 {
1553 KvStoreDelegateManager *manager = nullptr;
1554 KvStoreNbDelegate *delegate = nullptr;
1555 Option option;
1556 option.isEncryptedDb = false;
1557 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1558 ASSERT_TRUE(manager != nullptr && delegate != nullptr);
1559 SetResultSetCacheMode(delegate, isRowIdMode);
1560
1561 vector<Entry> entriesBatch;
1562 vector<Key> allKeys;
1563 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS, FOUR_BYTE_KEY, FOUR_M_LONG_STRING);
1564 for (const auto &iter : entriesBatch) {
1565 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1566 }
1567 /**
1568 * @tc.steps: step1. Rekey STORE_ID_SYNC_2 with g_passwd1.
1569 * @tc.expected: step1. operate successfully or BUSY(if the rekey is later executed).
1570 */
1571 std::mutex mtx;
1572 std::condition_variable conditionRekeyVar;
1573 bool rekeyFlag = false;
1574 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
1575 thread subThread([&]() {
1576 auto status = delegate->Rekey(g_passwd1);
1577 EXPECT_EQ(((status == OK) || (status == BUSY)), true);
1578 std::unique_lock<std::mutex> lck(mtx);
1579 conditionRekeyVar.notify_all();
1580 rekeyFlag = true;
1581 });
1582 subThread.detach();
1583 /**
1584 * @tc.steps: step2. call GetEntries interface to get KvStoreResultSet.
1585 * @tc.expected: step2. return BUSY or OK(if the rekey is later executed).
1586 */
1587 KvStoreResultSet *resultSetK = nullptr;
1588 Key keyPrefix = { 'k' };
1589 std::this_thread::sleep_for(std::chrono::microseconds(WAIT_FOR_OBSERVER_REKEY)); // wait the rekey operation.
1590 DBStatus status = delegate->GetEntries(keyPrefix, resultSetK);
1591 EXPECT_EQ(((status == OK) || (status == BUSY)), true);
1592
1593 std::unique_lock<std::mutex> lck(mtx);
1594 conditionRekeyVar.wait(lck, [&] { return rekeyFlag; });
1595 if (resultSetK != nullptr) {
1596 delegate->CloseResultSet(resultSetK);
1597 }
1598 EXPECT_EQ(manager->CloseKvStore(delegate), OK);
1599 delegate = nullptr;
1600 EXPECT_EQ(manager->DeleteKvStore(STORE_ID_2), OK);
1601 delete manager;
1602 manager = nullptr;
1603 }
1604 namespace {
CursorOperThread(KvStoreNbDelegate * & nbCursorDelegate)1605 void CursorOperThread(KvStoreNbDelegate *&nbCursorDelegate)
1606 {
1607 ASSERT_TRUE(nbCursorDelegate != nullptr);
1608 /**
1609 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1610 * @tc.expected: step1. get success.
1611 */
1612 KvStoreResultSet *resultSetAll = nullptr;
1613 EXPECT_EQ(nbCursorDelegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1614 /**
1615 * @tc.steps: step2. call GetCount interface.
1616 * @tc.expected: step2. call success.
1617 */
1618 EXPECT_TRUE(resultSetAll->GetCount() == ONE_HUNDRED_RECORDS);
1619 /**
1620 * @tc.steps: step3. call IsFirst interface.
1621 * @tc.expected: step3. call success.
1622 */
1623 EXPECT_TRUE(resultSetAll->IsFirst() == false);
1624 /**
1625 * @tc.steps: step4. call IsLast interface.
1626 * @tc.expected: step4. call success.
1627 */
1628 EXPECT_TRUE(resultSetAll->IsLast() == false);
1629 /**
1630 * @tc.steps: step5. call MoveToFirst interface.
1631 * @tc.expected: step5. call success.
1632 */
1633 EXPECT_TRUE(resultSetAll->MoveToFirst() == true);
1634 /**
1635 * @tc.steps: step6. call MoveToLast interface.
1636 * @tc.expected: step6. call success.
1637 */
1638 EXPECT_TRUE(resultSetAll->MoveToLast() == true);
1639 /**
1640 * @tc.steps: step7. call IsBeforeFirst interface.
1641 * @tc.expected: step7. call success.
1642 */
1643 EXPECT_TRUE(resultSetAll->IsBeforeFirst() == false);
1644 /**
1645 * @tc.steps: step8. call MoveToNext interface.
1646 * @tc.expected: step8. call success.
1647 */
1648 EXPECT_TRUE(resultSetAll->MoveToNext() == false);
1649 /**
1650 * @tc.steps: step9. call IsAfterLast interface.
1651 * @tc.expected: step9. call success.
1652 */
1653 EXPECT_TRUE(resultSetAll->IsAfterLast() == true);
1654 /**
1655 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1656 * @tc.expected: step10. call success.
1657 */
1658 EXPECT_TRUE(resultSetAll->MoveToPrevious() == true);
1659 EXPECT_TRUE(resultSetAll->IsAfterLast() == false);
1660 /**
1661 * @tc.steps: step11. close recordset.
1662 * @tc.expected: step11. call success.
1663 */
1664 EXPECT_EQ(nbCursorDelegate->CloseResultSet(resultSetAll), OK);
1665 }
1666 }
ResultSetDb025(KvStoreNbDelegate * delegate,bool isRowIdMode)1667 void DistributeddbNbCursorTestcase::ResultSetDb025(KvStoreNbDelegate *delegate, bool isRowIdMode)
1668 {
1669 ASSERT_TRUE(delegate != nullptr);
1670 SetResultSetCacheMode(delegate, isRowIdMode);
1671 vector<Entry> entriesBatch;
1672 vector<Key> allKeys;
1673 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS,
1674 FOUR_BYTE_KEY, ONE_TENTH_M_LONG_STRING);
1675 for (const auto &iter : entriesBatch) {
1676 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1677 }
1678
1679 /**
1680 * @tc.steps: step1. Call resultSet interfaces.
1681 * @tc.expected: step1. operate successfully.
1682 */
1683 std::vector<std::thread> threads;
1684 for (unsigned int threadId = THREAD_NUM_START; threadId <= THREAD_NUM_END; ++threadId) {
1685 threads.push_back(std::thread(CursorOperThread, std::ref(delegate)));
1686 }
1687 for (auto& th : threads) {
1688 th.join();
1689 }
1690 }
1691 namespace {
CursorRandOperThread1(KvStoreResultSet * & resultSet)1692 void CursorRandOperThread1(KvStoreResultSet *&resultSet)
1693 {
1694 /**
1695 * @tc.steps: step2. call GetCount interface.
1696 * @tc.expected: step2. call success.
1697 */
1698 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1699 /**
1700 * @tc.steps: step3. call IsFirst interface.
1701 * @tc.expected: step3. no crash.
1702 */
1703 resultSet->IsFirst();
1704 /**
1705 * @tc.steps: step4. call IsLast interface.
1706 * @tc.expected: step4. call success.
1707 */
1708 resultSet->IsLast();
1709 /**
1710 * @tc.steps: step5. call MoveToFirst interface.
1711 * @tc.expected: step5. call success.
1712 */
1713 resultSet->MoveToFirst();
1714 /**
1715 * @tc.steps: step6. call MoveToLast interface.
1716 * @tc.expected: step6. call success.
1717 */
1718 resultSet->MoveToLast();
1719 /**
1720 * @tc.steps: step7. call IsBeforeFirst interface.
1721 * @tc.expected: step7. call success.
1722 */
1723 resultSet->IsBeforeFirst();
1724 /**
1725 * @tc.steps: step8. call MoveToNext interface.
1726 * @tc.expected: step8. call success.
1727 */
1728 resultSet->MoveToNext();
1729 /**
1730 * @tc.steps: step9. call IsAfterLast interface.
1731 * @tc.expected: step9. call success.
1732 */
1733 resultSet->IsAfterLast();
1734 /**
1735 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1736 * @tc.expected: step10. call success.
1737 */
1738 resultSet->MoveToPrevious();
1739 resultSet->IsAfterLast();
1740 }
1741
CursorRandOperThread2(KvStoreResultSet * & resultSet)1742 void CursorRandOperThread2(KvStoreResultSet *&resultSet)
1743 {
1744 /**
1745 * @tc.steps: step2. call GetCount interface.
1746 * @tc.expected: step2. call success.
1747 */
1748 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1749 /**
1750 * @tc.steps: step3. call IsFirst interface.
1751 * @tc.expected: step3. call success.
1752 */
1753 resultSet->IsFirst();
1754 /**
1755 * @tc.steps: step7. call IsBeforeFirst interface.
1756 * @tc.expected: step7. call success.
1757 */
1758 resultSet->IsBeforeFirst();
1759 /**
1760 * @tc.steps: step8. call MoveToNext interface.
1761 * @tc.expected: step8. call success.
1762 */
1763 resultSet->MoveToNext();
1764 /**
1765 * @tc.steps: step9. call IsAfterLast interface.
1766 * @tc.expected: step9. call success.
1767 */
1768 resultSet->IsAfterLast();
1769 /**
1770 * @tc.steps: step4. call IsLast interface.
1771 * @tc.expected: step4. call success.
1772 */
1773 resultSet->IsLast();
1774 /**
1775 * @tc.steps: step5. call MoveToFirst interface.
1776 * @tc.expected: step5. call success.
1777 */
1778 resultSet->MoveToFirst();
1779 /**
1780 * @tc.steps: step6. call MoveToLast interface.
1781 * @tc.expected: step6. call success.
1782 */
1783 resultSet->MoveToLast();
1784 /**
1785 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1786 * @tc.expected: step10. call success.
1787 */
1788 resultSet->MoveToPrevious();
1789 resultSet->IsAfterLast();
1790 }
1791
CursorRandOperThread3(KvStoreResultSet * & resultSet)1792 void CursorRandOperThread3(KvStoreResultSet *&resultSet)
1793 {
1794 /**
1795 * @tc.steps: step2. call GetCount interface.
1796 * @tc.expected: step2. call success.
1797 */
1798 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1799 /**
1800 * @tc.steps: step3. call IsFirst interface.
1801 * @tc.expected: step3. call success.
1802 */
1803 resultSet->IsFirst();
1804 /**
1805 * @tc.steps: step6. call MoveToLast interface.
1806 * @tc.expected: step6. call success.
1807 */
1808 resultSet->MoveToLast();
1809 /**
1810 * @tc.steps: step7. call IsBeforeFirst interface.
1811 * @tc.expected: step7. call success.
1812 */
1813 resultSet->IsBeforeFirst();
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: step8. call MoveToNext interface.
1826 * @tc.expected: step8. call success.
1827 */
1828 resultSet->MoveToNext();
1829 /**
1830 * @tc.steps: step9. call IsAfterLast interface.
1831 * @tc.expected: step9. call success.
1832 */
1833 resultSet->IsAfterLast();
1834 /**
1835 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1836 * @tc.expected: step10. call success.
1837 */
1838 resultSet->MoveToPrevious();
1839 resultSet->IsAfterLast();
1840 }
1841
CursorRandOperThread4(KvStoreResultSet * & resultSet)1842 void CursorRandOperThread4(KvStoreResultSet *&resultSet)
1843 {
1844 /**
1845 * @tc.steps: step10. call MoveToPrevious and then call IsAfterLast interface.
1846 * @tc.expected: step10. call success.
1847 */
1848 resultSet->IsAfterLast();
1849 resultSet->MoveToPrevious();
1850 /**
1851 * @tc.steps: step9. call IsAfterLast interface.
1852 * @tc.expected: step9. call success.
1853 */
1854 resultSet->IsAfterLast();
1855 /**
1856 * @tc.steps: step8. call MoveToNext interface.
1857 * @tc.expected: step8. call success.
1858 */
1859 resultSet->MoveToNext();
1860 /**
1861 * @tc.steps: step7. call IsBeforeFirst interface.
1862 * @tc.expected: step7. call success.
1863 */
1864 resultSet->IsBeforeFirst();
1865 /**
1866 * @tc.steps: step6. call MoveToLast interface.
1867 * @tc.expected: step6. call success.
1868 */
1869 resultSet->MoveToLast();
1870 /**
1871 * @tc.steps: step5. call MoveToFirst interface.
1872 * @tc.expected: step5. call success.
1873 */
1874 resultSet->MoveToFirst();
1875 /**
1876 * @tc.steps: step4. call IsLast interface.
1877 * @tc.expected: step4. call success.
1878 */
1879 resultSet->IsLast();
1880 /**
1881 * @tc.steps: step3. call IsFirst interface.
1882 * @tc.expected: step3. call success.
1883 */
1884 resultSet->IsFirst();
1885 /**
1886 * @tc.steps: step2. call GetCount interface.
1887 * @tc.expected: step2. call success.
1888 */
1889 EXPECT_TRUE(resultSet->GetCount() == ONE_HUNDRED_RECORDS);
1890 }
1891 }
1892
ResultSetDb026(KvStoreNbDelegate * delegate,bool isRowIdMode)1893 void DistributeddbNbCursorTestcase::ResultSetDb026(KvStoreNbDelegate *delegate, bool isRowIdMode)
1894 {
1895 ASSERT_TRUE(delegate != nullptr);
1896 SetResultSetCacheMode(delegate, isRowIdMode);
1897 vector<Entry> entriesBatch;
1898 vector<Key> allKeys;
1899 GenerateFixedRecords(entriesBatch, allKeys, ONE_HUNDRED_RECORDS,
1900 FOUR_BYTE_KEY, ONE_TENTH_M_LONG_STRING);
1901 for (const auto &iter : entriesBatch) {
1902 EXPECT_EQ(delegate->Put(iter.key, iter.value), OK);
1903 }
1904
1905 /**
1906 * @tc.steps: step1. call GetEntries interface with "" parameter to get KvStoreResultSet.
1907 * @tc.expected: step1. get success.
1908 */
1909 KvStoreResultSet *resultSetAll = nullptr;
1910 EXPECT_EQ(delegate->GetEntries(KEY_EMPTY, resultSetAll), OK);
1911
1912 /**
1913 * @tc.steps: step2. Call resultSet interfaces.
1914 * @tc.expected: step2. operate successfully.
1915 */
1916 std::vector<std::thread> threads;
1917 threads.push_back(std::thread(CursorRandOperThread1, std::ref(resultSetAll)));
1918 threads.push_back(std::thread(CursorRandOperThread2, std::ref(resultSetAll)));
1919 threads.push_back(std::thread(CursorRandOperThread3, std::ref(resultSetAll)));
1920 threads.push_back(std::thread(CursorRandOperThread4, std::ref(resultSetAll)));
1921
1922 for (auto& th : threads) {
1923 th.join();
1924 }
1925
1926 /**
1927 * @tc.steps: step11. close recordset.
1928 * @tc.expected: step11. call success.
1929 */
1930 EXPECT_EQ(delegate->CloseResultSet(resultSetAll), OK);
1931 }
1932 namespace {
VerifyResultSetInterfaces(KvStoreNbDelegate ** delegates,unsigned long delegateCount)1933 void VerifyResultSetInterfaces(KvStoreNbDelegate **delegates, unsigned long delegateCount)
1934 {
1935 ASSERT_TRUE(delegates != nullptr);
1936 Key keyPrefixKA = { 'k', 'a' };
1937 Key keyPrefixKB = { 'k', 'b' };
1938 Key keyPrefixKK = { 'k', 'k' };
1939 KvStoreResultSet *resultSetAlls[OPEN_DB_TIMES] = { nullptr };
1940 KvStoreResultSet *resultSetKAs[OPEN_DB_TIMES] = { nullptr };
1941 KvStoreResultSet *resultSetKBs[OPEN_DB_TIMES] = { nullptr };
1942 KvStoreResultSet *resultSetKKs[OPEN_DB_TIMES] = { nullptr };
1943 unsigned long delegateCnt = 0;
1944 for (delegateCnt = 0; delegateCnt < delegateCount; ++delegateCnt) {
1945 /**
1946 * @tc.steps: step2. check GetEntries of interfaces of every delegate.
1947 * @tc.expected: step2. success.
1948 */
1949 EXPECT_EQ(delegates[delegateCnt]->GetEntries(KEY_EMPTY, resultSetAlls[delegateCnt]), OK);
1950 EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKA, resultSetKAs[delegateCnt]), OK);
1951 EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKB, resultSetKBs[delegateCnt]), OK);
1952 EXPECT_EQ(delegates[delegateCnt]->GetEntries(keyPrefixKK, resultSetKKs[delegateCnt]), OK);
1953 /**
1954 * @tc.steps: step3. check GetCount of interfaces of every delegate.
1955 * @tc.expected: step3. success.
1956 */
1957 EXPECT_TRUE(resultSetAlls[delegateCnt]->GetCount() == TWO_HUNDREDS_RECORDS);
1958 EXPECT_TRUE(resultSetKAs[delegateCnt]->GetCount() == ONE_HUNDRED_RECORDS);
1959 EXPECT_TRUE(resultSetKBs[delegateCnt]->GetCount() == ONE_HUNDRED_RECORDS);
1960 EXPECT_TRUE(resultSetKKs[delegateCnt]->GetCount() == 0);
1961 }
1962
1963 for (delegateCnt = 0; delegateCnt < delegateCount; ++delegateCnt) {
1964 /**
1965 * @tc.steps: step4. check GetCount of interfaces of every delegate.
1966 * @tc.expected: step4. success.
1967 */
1968 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetAlls[delegateCnt]) == OK);
1969 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKAs[delegateCnt]) == OK);
1970 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKBs[delegateCnt]) == OK);
1971 EXPECT_TRUE(delegates[delegateCnt]->CloseResultSet(resultSetKKs[delegateCnt]) == OK);
1972 }
1973 }
1974 }
1975
ResultSetDb027(bool isRowIdMode)1976 void DistributeddbNbCursorTestcase::ResultSetDb027(bool isRowIdMode)
1977 {
1978 KvStoreDelegateManager *managers[OPEN_DB_TIMES] = {nullptr};
1979 KvStoreNbDelegate *delegates[OPEN_DB_TIMES] = {nullptr};
1980 Option option;
1981 unsigned long delegateCnt = 0;
1982 /**
1983 * @tc.steps: step1. open STORE_ID_2 for three times.
1984 * @tc.expected: step1. success.
1985 */
1986 delegates[INDEX_ZEROTH] = DistributedDBNbTestTools::GetNbDelegateSuccess(managers[INDEX_ZEROTH],
1987 g_dbParameter3, option);
1988 ASSERT_TRUE(managers[INDEX_ZEROTH] != nullptr && delegates[INDEX_ZEROTH] != nullptr);
1989 SetResultSetCacheMode(delegates[INDEX_ZEROTH], isRowIdMode);
1990
1991 vector<Entry> entriesKA, entriesKB;
1992 std::vector<uint8_t> ka = { 'k', 'a' };
1993 std::vector<uint8_t> kb = { 'k', 'b' };
1994 EntrySize entrySizeKA = { FIVE_BYTE_KEY, ONE_K_LONG_STRING };
1995 EntrySize entrySizeKB = { FIVE_BYTE_KEY, ONE_TENTH_M_LONG_STRING };
1996 GenerateAppointPrefixAndSizeRecords(entriesKA, entrySizeKA, ONE_HUNDRED_RECORDS, ka, {'v'});
1997 GenerateAppointPrefixAndSizeRecords(entriesKB, entrySizeKB, ONE_HUNDRED_RECORDS, kb, {'v'});
1998 for (int index = 0; index < ONE_HUNDRED_RECORDS; index++) {
1999 EXPECT_TRUE(delegates[INDEX_ZEROTH]->Put(entriesKA[index].key, entriesKA[index].value) == OK);
2000 EXPECT_TRUE(delegates[INDEX_ZEROTH]->Put(entriesKB[index].key, entriesKB[index].value) == OK);
2001 }
2002
2003 option.createIfNecessary = false;
2004 for (delegateCnt = INDEX_FIRST; delegateCnt < DELEGATE_NUM; ++delegateCnt) {
2005 delegates[delegateCnt] = DistributedDBNbTestTools::GetNbDelegateSuccess(managers[delegateCnt],
2006 g_dbParameter3, option);
2007 ASSERT_TRUE(managers[delegateCnt] != nullptr && delegates[delegateCnt] != nullptr);
2008 SetResultSetCacheMode(delegates[delegateCnt], isRowIdMode);
2009 }
2010
2011 VerifyResultSetInterfaces(delegates, DELEGATE_NUM);
2012 for (unsigned long operCnt = INDEX_ZEROTH; operCnt < OPEN_DB_TIMES; ++operCnt) {
2013 EXPECT_EQ(managers[operCnt]->CloseKvStore(delegates[operCnt]), OK);
2014 delegates[operCnt] = nullptr;
2015 if (operCnt == OPEN_DB_TIMES - 1) {
2016 EXPECT_EQ(managers[operCnt]->DeleteKvStore(STORE_ID_3), OK);
2017 }
2018 delete managers[operCnt];
2019 managers[operCnt] = nullptr;
2020 }
2021 }