1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef STORAGE_EXECUTOR_H
17 #define STORAGE_EXECUTOR_H
18 
19 #include "macro_utils.h"
20 #include "single_ver_natural_store_commit_notify_data.h"
21 #include "types_export.h"
22 
23 namespace DistributedDB {
24 enum class EngineState {
25     INVALID = -1, // default value, representative database is not generated
26     CACHEDB,
27     ATTACHING, // main db and cache db attach together
28     MIGRATING, // began to Migrate data
29     MAINDB,
30     ENGINE_BUSY, // In order to change handle during the migration process, it is temporarily unavailable
31 };
32 
33 enum class SingleVerDataType {
34     META_TYPE,
35     LOCAL_TYPE_SQLITE,
36     SYNC_TYPE,
37 };
38 
39 enum class DataStatus {
40     NOEXISTED,
41     DELETED,
42     EXISTED,
43 };
44 
45 enum class ExecutorState {
46     INVALID = -1,
47     MAINDB,
48     CACHEDB,
49     MAIN_ATTACH_CACHE, // After process crash and cacheDb existed
50     CACHE_ATTACH_MAIN, // while cacheDb migrating to mainDb
51 };
52 
53 struct DataOperStatus {
54     DataStatus preStatus = DataStatus::NOEXISTED;
55     bool isDeleted = false;
56     bool isDefeated = false; // whether the put data is defeated.
57 };
58 
59 struct SingleVerRecord {
60     Key key;
61     Value value;
62     Timestamp timestamp = 0;
63     uint64_t flag = 0;
64     std::string device;
65     std::string origDevice;
66     Key hashKey;
67     Timestamp writeTimestamp = 0;
68 };
69 
70 struct DeviceInfo {
71     bool isLocal = false;
72     std::string deviceName;
73 };
74 
75 struct LocalDataItem {
76     Key key;
77     Value value;
78     Timestamp timestamp = 0;
79     Key hashKey;
80     uint64_t flag = 0;
81 };
82 
83 struct NotifyConflictAndObserverData {
84     SingleVerNaturalStoreCommitNotifyData *committedData = nullptr;
85     DataItem getData;
86     Key hashKey;
87     DataOperStatus dataStatus;
88 };
89 
90 struct NotifyMigrateSyncData {
91     bool isRemote = false;
92     bool isRemoveDeviceData = false;
93     bool isPermitForceWrite = true;
94     SingleVerNaturalStoreCommitNotifyData *committedData = nullptr;
95     std::vector<Entry> entries{};
96 };
97 
98 struct SyncDataDevices {
99     std::string origDev;
100     std::string dev;
101 };
102 
103 class StorageExecutor {
104 public:
105     explicit StorageExecutor(bool writable);
106     virtual ~StorageExecutor();
107 
108     // Delete the copy and assign constructors
109     DISABLE_COPY_ASSIGN_MOVE(StorageExecutor);
110 
111     virtual bool GetWritable() const;
112 
113     virtual int CheckCorruptedStatus(int errCode) const;
114 
115     virtual bool GetCorruptedStatus() const;
116 
117     virtual void SetCorruptedStatus() const;
118 
119     virtual int Reset() = 0;
120 
121 protected:
122     bool writable_;
123     mutable bool isCorrupted_;
124 };
125 } // namespace DistributedDB
126 
127 #endif // STORAGE_EXECUTOR_H
128