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 I_SYNC_ENGINE_H
17 #define I_SYNC_ENGINE_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "ikvdb_sync_interface.h"
23 #include "meta_data.h"
24 #include "ref_object.h"
25 #include "sync_operation.h"
26 
27 namespace DistributedDB {
28 class ISyncEngine : public virtual RefObject {
29 public:
30     struct InitCallbackParam {
31         std::function<void(std::string)> onRemoteDataChanged;
32         std::function<void(std::string)> offlineChanged;
33         std::function<void(const InternalSyncParma &param)> queryAutoSyncCallback;
34     };
35     // Do some init things
36     virtual int Initialize(ISyncInterface *syncInterface, const std::shared_ptr<Metadata> &metadata,
37         const InitCallbackParam &callbackParam) = 0;
38 
39     // Do some things, when db close.
40     virtual int Close() = 0;
41 
42     // Alloc and Add sync SyncTarget
43     // return E_OK if operator success.
44     virtual int AddSyncOperation(SyncOperation *operation) = 0;
45 
46     // Clear the SyncTarget matched the syncId.
47     virtual void RemoveSyncOperation(int syncId) = 0;
48 
49 #ifndef OMIT_MULTI_VER
50     // notify other devices data has changed
51     virtual void BroadCastDataChanged() const = 0;
52 #endif
53 
54     // Get Online devices
55     virtual void GetOnlineDevices(std::vector<std::string> &devices) const = 0;
56 
57     // Register the device connect callback and active communicator, this function must be called after Engine initted
58     virtual void StartCommunicator() = 0;
59 
60     // Get the database identifier
61     virtual std::string GetLabel() const = 0;
62 
63     // Set Manual Sync retry config
64     virtual void SetSyncRetry(bool isRetry) = 0;
65 
66     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
67     virtual int SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets) = 0;
68 
69     // Set record device equal identifier when called in import/rekey scene when restart syncer
70     virtual void SetEqualIdentifier() = 0;
71 
72     virtual void SetEqualIdentifierMap(const std::string &identifier, const std::vector<std::string> &targets) = 0;
73 
74     // Add auto subscribe timer when start sync engine, used for auto subscribe failed subscribe task when db online
75     virtual int StartAutoSubscribeTimer(const ISyncInterface &syncInterface) = 0;
76 
77     // Stop auto subscribe timer when start sync engine
78     virtual void StopAutoSubscribeTimer() = 0;
79 
80     // Check if number of subscriptions out of limit
81     virtual int SubscribeLimitCheck(const std::vector<std::string> &devices, QuerySyncObject &query) const = 0;
82 
83     // Check if the Sync Engine is active, some times synchronization is not allowed
84     virtual bool IsEngineActive() const = 0;
85 
86     virtual void SchemaChange() = 0;
87 
88     virtual void Dump(int fd) = 0;
89 
90     virtual int RemoteQuery(const std::string &device, const RemoteCondition &condition,
91         uint64_t timeout, uint64_t connectionId, std::shared_ptr<ResultSet> &result) = 0;
92 
93     virtual void NotifyConnectionClosed(uint64_t connectionId) = 0;
94 
95     virtual void NotifyUserChange() = 0;
96 
97     virtual void AbortMachineIfNeed(uint32_t syncId) = 0;
98 
99     virtual void AddSubscribe(SyncGenericInterface *storage,
100         const std::map<std::string, std::vector<QuerySyncObject>> &subscribeQuery) = 0;
101 
102     virtual void TimeChange() = 0;
103 
104     virtual int32_t GetResponseTaskCount() = 0;
105 protected:
~ISyncEngine()106     ~ISyncEngine() override {};
107 };
108 } // namespace DistributedDB
109 
110 #endif // I_SYNC_ENGINE_H