• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int CancelDeviceSync(uint32_t syncId);
61     void InitPragmaFunc();
62 
63     // If enable is true, it will enable auto sync
64     int EnableAutoSync(bool enable);
65 
66     void OnSyncComplete(const std::map<std::string, int> &statuses,
67         const std::function<void(const std::map<std::string, int> &devicesMap)> &onComplete, bool wait);
68 
69     void OnDeviceSyncProcess(const std::map<std::string, DeviceSyncProcess> &syncRecordMap,
70         const DeviceSyncProcessCallback &onProcess);
71 
72     int GetQueuedSyncSize(int *queuedSyncSize) const;
73 
74     int SetQueuedSyncLimit(const int *queuedSyncLimit);
75 
76     int GetQueuedSyncLimit(int *queuedSyncLimit) const;
77 
78     int SetStaleDataWipePolicy(const WipePolicy *policy);
79 
80     int SetRemotePushFinishedNotify(PragmaRemotePushNotify *notifyParma);
81 
82     int SetSyncRetry(bool isRetry);
83     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
84     int SetEqualIdentifier(const PragmaSetEqualIdentifier *param);
85 
86     int SetPushDataInterceptor(const PushDataInterceptor &interceptor);
87 
88     std::mutex remotePushFinishedListenerLock_;
89     NotificationChain::Listener *remotePushFinishedListener_;
90     std::map<int, std::function<void(void *, int &errCode)>> pragmaFunc_{};
91 };
92 }
93 #endif // SYNC_ABLE_KVDB_CONNECTION_H
94