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 #ifndef DISTRIBUTED_DB_MODULE_CRUD_TRANSACTION_TOOLS_H
16 #define DISTRIBUTED_DB_MODULE_CRUD_TRANSACTION_TOOLS_H
17 #ifndef OMIT_MULTI_VER
18 #include "kv_store_delegate.h"
19 #include "kv_store_delegate_manager.h"
20 
21 enum class CrudMode {
22     PUT_BATCH = 0,
23     UPDATE_BATCH = 1,
24     DELETE_BATCH = 2,
25     CLEAR = 3,
26     PUT = 4,
27     DELETE = 5,
28 };
29 class DistributedCrudTransactionTools final {
30 public:
31 
32     DistributedCrudTransactionTools(DistributedDB::KvStoreDelegate &delegate,
33         CrudMode first, CrudMode second, bool preset, bool isLocal);
~DistributedCrudTransactionTools()34     ~DistributedCrudTransactionTools() {}
35     bool testCrudTransaction();
36 
37     // Delete the copy and assign constructors
38     DistributedCrudTransactionTools(const DistributedCrudTransactionTools &distributeDBTools) = delete;
39     DistributedCrudTransactionTools& operator=(const DistributedCrudTransactionTools &distributeDBTools) = delete;
40     DistributedCrudTransactionTools(DistributedCrudTransactionTools &&distributeDBTools) = delete;
41     DistributedCrudTransactionTools& operator=(DistributedCrudTransactionTools &&distributeDBTools) = delete;
42 
43 private:
44     bool PresetValue();
45     bool CheckFirst();
46     bool CheckSecond();
47     void Check();
48     bool Action1(DistributedDB::KvStoreDelegate &delegate);
49     bool Action2(DistributedDB::KvStoreDelegate &delegate);
50     DistributedDB::KvStoreDelegate *storeDelegate_;
51     CrudMode firstMode_;
52     CrudMode secondMode_;
53     bool isLocal_;
54     bool needPresetData_;
55     unsigned long presetCount_ = 128;
56     int presetValue_ = 1;
57     std::vector<DistributedDB::Entry> entriesBatch_;
58     std::vector<DistributedDB::Key> allKeys_;
59     bool firstComplete_ = false;
60     bool secondComplete_ = false;
61     bool success_ = true;
62 };
63 #endif // OMIT_MULTI_VER
64 #endif // DISTRIBUTED_DB_MODULE_CRUD_TRANSACTION_TOOLS_H