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 SYNC_ABLE_KVDB_H
17 #define SYNC_ABLE_KVDB_H
18 
19 #include <shared_mutex>
20 
21 #include "cloud/cloud_syncer.h"
22 #include "generic_kvdb.h"
23 #include "icloud_sync_storage_interface.h"
24 #include "ikvdb_sync_interface.h"
25 #include "intercepted_data.h"
26 #include "sync_able_kvdb_connection.h"
27 #include "syncer_proxy.h"
28 
29 namespace DistributedDB {
30 class SyncAbleKvDB : public GenericKvDB {
31 public:
32     SyncAbleKvDB();
33     ~SyncAbleKvDB() override;
34     DISABLE_COPY_ASSIGN_MOVE(SyncAbleKvDB);
35 
36     // Delete a connection object.
37     void DelConnection(GenericKvDBConnection *connection) override;
38 
39     // Used to notify Syncer and other listeners data has changed
40     void CommitNotify(int notifyEvent, KvDBCommitNotifyFilterAbleData *data) override;
41 
42     // Invoked automatically when connection count is zero
43     void Close() override;
44 
45     // Start a sync action.
46     int Sync(const ISyncer::SyncParma &parma, uint64_t connectionId);
47 
48     // Enable auto sync
49     void EnableAutoSync(bool enable);
50 
51     // Stop a sync action in progress.
52     void StopSync(uint64_t connectionId);
53 
54     // Get The current virtual timestamp from db
55     virtual uint64_t GetTimestampFromDB();
56 
57     // Get The current virtual timestamp
58     uint64_t GetTimestamp(bool needStartSync = true);
59 
60     void WakeUpSyncer() override;
61 
62     // Get manual sync queue size
63     int GetQueuedSyncSize(int *queuedSyncSize) const;
64 
65     // Set manual sync queue limit
66     int SetQueuedSyncLimit(const int *queuedSyncLimit);
67 
68     // Get manual sync queue limit
69     int GetQueuedSyncLimit(int *queuedSyncLimit) const;
70 
71     // Disable add new manual sync , for rekey
72     int DisableManualSync(void);
73 
74     // Enable add new manual sync , for rekey
75     int EnableManualSync(void);
76 
77     int SetStaleDataWipePolicy(WipePolicy policy);
78 
79     int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash);
80 
81     NotificationChain::Listener *AddRemotePushFinishedNotify(const RemotePushFinishedNotifier &notifier, int &errCode);
82 
83     void NotifyRemotePushFinishedInner(const std::string &targetId) const;
84 
85     int SetSyncRetry(bool isRetry);
86     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
87     int SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets);
88 
89     virtual void SetSendDataInterceptor(const PushDataInterceptor &interceptor) = 0;
90 
91     void Dump(int fd) override;
92 
93     int GetSyncDataSize(const std::string &device, size_t &size) const;
94 
95     int GetHashDeviceId(const std::string &clientId, std::string &hashDevId);
96 
97     int GetWatermarkInfo(const std::string &device, WatermarkInfo &info);
98 
99     int UpgradeSchemaVerInMeta();
100 
101     void ResetSyncStatus() override;
102 
103     TimeOffset GetLocalTimeOffset();
104 
105     int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess);
106 
107     int SetCloudDB(const std::map<std::string, std::shared_ptr<ICloudDb>> &cloudDBs);
108 
109     int32_t GetTaskCount();
110 
111     void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback);
112 
113     virtual void SetReceiveDataInterceptor(const DataInterceptor &interceptor) = 0;
114 protected:
115     virtual IKvDBSyncInterface *GetSyncInterface() = 0;
116 
117     void SetSyncModuleActive();
118 
119     bool GetSyncModuleActive();
120 
121     void ReSetSyncModuleActive();
122     // Start syncer
123     int StartSyncer(bool isCheckSyncActive = false, bool isNeedActive = true);
124 
125     int StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive);
126 
127     // Stop syncer
128     void StopSyncer(bool isClosedOperation = false, bool isStopTaskOnly = false);
129 
130     void StopSyncerWithNoLock(bool isClosedOperation = false);
131 
132     void UserChangeHandle();
133 
134     void ChangeUserListener();
135 
136     // Get the dataItem's append length, the append length = after-serialized-len - original-dataItem-len
137     uint32_t GetAppendedLen() const;
138 
139     int GetLocalIdentity(std::string &outTarget) const;
140 
141     void TriggerSync(int notifyEvent);
142 
143     virtual ICloudSyncStorageInterface *GetICloudSyncInterface() const;
144 
145     int CleanAllWaterMark();
146 protected:
147     virtual std::map<std::string, DataBaseSchema> GetDataBaseSchemas();
148 
149     virtual bool CheckSchemaSupportForCloudSync() const;
150 private:
151     int RegisterEventType(EventType type);
152 
153     bool NeedStartSyncer() const;
154 
155     void StartCloudSyncer();
156 
157     void FillSyncInfo(const CloudSyncOption &option, const SyncProcessCallback &onProcess,
158         CloudSyncer::CloudTaskInfo &info);
159 
160     int CheckSyncOption(const CloudSyncOption &option, const CloudSyncer &syncer);
161 
162     CloudSyncer *GetAndIncCloudSyncer();
163 
164     SyncerProxy syncer_;
165     std::atomic<bool> started_;
166     std::atomic<bool> closed_;
167     std::atomic<bool> isSyncModuleActiveCheck_;
168     std::atomic<bool> isSyncNeedActive_;
169     mutable std::shared_mutex notifyChainLock_;
170     NotificationChain *notifyChain_;
171 
172     mutable std::mutex syncerOperateLock_;
173     NotificationChain::Listener *userChangeListener_;
174 
175     mutable std::mutex cloudSyncerLock_;
176     CloudSyncer *cloudSyncer_;
177 
178     static const EventType REMOTE_PUSH_FINISHED;
179 };
180 }
181 
182 #endif // SYNC_ABLE_KVDB_H
183