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_CONNECTION_H
17 #define SYNC_ABLE_KVDB_CONNECTION_H
18 
19 #include "generic_kvdb_connection.h"
20 #include "intercepted_data.h"
21 #include "ref_object.h"
22 
23 namespace DistributedDB {
24 class SyncAbleKvDB;
25 struct PragmaSync;
26 struct PragmaRemotePushNotify;
27 struct PragmaSetEqualIdentifier;
28 
29 class SyncAbleKvDBConnection : public GenericKvDBConnection, public virtual RefObject {
30 public:
31     explicit SyncAbleKvDBConnection(SyncAbleKvDB *kvDB);
32     ~SyncAbleKvDBConnection() override;
33     DISABLE_COPY_ASSIGN_MOVE(SyncAbleKvDBConnection);
34 
35     // Pragma interface.
36     int Pragma(int cmd, void *parameter) override;
37 
38     int GetSyncDataSize(const std::string &device, size_t &size) const override;
39 
40     int GetWatermarkInfo(const std::string &device, WatermarkInfo &info) override;
41 
42     int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess) override;
43 
44     int SetCloudDB(const std::map<std::string, std::shared_ptr<ICloudDb>> &cloudDBs) override;
45 
46     int32_t GetTaskCount() override;
47 
48     void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) override;
49 
50     int SetReceiveDataInterceptor(const DataInterceptor &interceptor) override;
51 protected:
52     int DisableManualSync();
53 
54     int EnableManualSync();
55 
56 private:
57     // Do pragma-sync action.
58     int PragmaParamCheck(int cmd, const void *parameter);
59     int PragmaSyncAction(const PragmaSync *syncParameter);
60     void InitPragmaFunc();
61 
62     // If enable is true, it will enable auto sync
63     int EnableAutoSync(bool enable);
64 
65     void OnSyncComplete(const std::map<std::string, int> &statuses,
66         const std::function<void(const std::map<std::string, int> &devicesMap)> &onComplete, bool wait);
67 
68     int GetQueuedSyncSize(int *queuedSyncSize) const;
69 
70     int SetQueuedSyncLimit(const int *queuedSyncLimit);
71 
72     int GetQueuedSyncLimit(int *queuedSyncLimit) const;
73 
74     int SetStaleDataWipePolicy(const WipePolicy *policy);
75 
76     int SetRemotePushFinishedNotify(PragmaRemotePushNotify *notifyParma);
77 
78     int SetSyncRetry(bool isRetry);
79     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
80     int SetEqualIdentifier(const PragmaSetEqualIdentifier *param);
81 
82     int SetPushDataInterceptor(const PushDataInterceptor &interceptor);
83 
84     std::mutex remotePushFinishedListenerLock_;
85     NotificationChain::Listener *remotePushFinishedListener_;
86     std::map<int, std::function<void(void *, int &errCode)>> pragmaFunc_{};
87 };
88 }
89 #endif // SYNC_ABLE_KVDB_CONNECTION_H
90