1 /*
2  * Copyright (c) 2023 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 CLOUD_SYNC_STRATEGY_H
17 #define CLOUD_SYNC_STRATEGY_H
18 
19 #include "data_transformer.h"
20 #include "db_errno.h"
21 #include "db_types.h"
22 #include "icloud_sync_storage_interface.h"
23 
24 namespace DistributedDB {
25 class CloudSyncStrategy {
26 public:
27     CloudSyncStrategy();
28     virtual ~CloudSyncStrategy() = default;
29 
30     void SetIsKvScene(bool isKvScene);
31 
32     void SetConflictResolvePolicy(SingleVerConflictResolvePolicy policy);
33 
34     virtual OpType TagSyncDataStatus(bool existInLocal, bool isCloudWin, const LogInfo &localInfo,
35         const LogInfo &cloudInfo);
36 
37     virtual bool JudgeUpdateCursor();
38 
39     virtual bool JudgeUpload();
40 
41     bool JudgeKvScene();
42 
43     static bool IsDelete(const LogInfo &info);
44 
45     static bool IsLogNeedUpdate(const LogInfo &cloudInfo, const LogInfo &localInfo);
46 
47     OpType TagUpdateLocal(const LogInfo &cloudInfo, const LogInfo &localInfo) const;
48 protected:
49     static bool IsSameVersion(const LogInfo &cloudInfo, const LogInfo &localInfo);
50 
51     bool IsIgnoreUpdate(const LogInfo &localInfo) const;
52 
53     bool IsSameRecord(const LogInfo &cloudInfo, const LogInfo &localInfo);
54 
55     SingleVerConflictResolvePolicy policy_;
56 
57     // isKvScene_ is used to distinguish between the KV and RDB in the following scenarios:
58     // 1. Whether upload to the cloud after delete local data that does not have a gid.
59     // 2. Whether the local data need update for different flag when the local time is larger.
60     bool isKvScene_ = false;
61 };
62 }
63 #endif // CLOUD_SYNC_STRATEGY_H
64