• 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_H
17 #define SYNC_ABLE_KVDB_H
18 
19 #include <shared_mutex>
20 
21 #include "generic_kvdb.h"
22 #include "ikvdb_sync_interface.h"
23 #include "intercepted_data.h"
24 #include "sync_able_kvdb_connection.h"
25 #include "syncer_proxy.h"
26 
27 namespace DistributedDB {
28 class SyncAbleKvDB : public GenericKvDB {
29 public:
30     SyncAbleKvDB();
31     ~SyncAbleKvDB() override;
32     DISABLE_COPY_ASSIGN_MOVE(SyncAbleKvDB);
33 
34     // Delete a connection object.
35     void DelConnection(GenericKvDBConnection *connection) override;
36 
37     // Used to notify Syncer and other listeners data has changed
38     void CommitNotify(int notifyEvent, KvDBCommitNotifyFilterAbleData *data) override;
39 
40     // Invoked automatically when connection count is zero
41     void Close() override;
42 
43     // Start a sync action.
44     int Sync(const ISyncer::SyncParma &parma, uint64_t connectionId);
45 
46     // Enable auto sync
47     void EnableAutoSync(bool enable);
48 
49     // Stop a sync action in progress.
50     void StopSync(uint64_t connectionId);
51 
52     // Get The current virtual timestamp
53     uint64_t GetTimestamp();
54 
55     void WakeUpSyncer() override;
56 
57     // Get manual sync queue size
58     int GetQueuedSyncSize(int *queuedSyncSize) const;
59 
60     // Set manual sync queue limit
61     int SetQueuedSyncLimit(const int *queuedSyncLimit);
62 
63     // Get manual sync queue limit
64     int GetQueuedSyncLimit(int *queuedSyncLimit) const;
65 
66     // Disable add new manual sync , for rekey
67     int DisableManualSync(void);
68 
69     // Enable add new manual sync , for rekey
70     int EnableManualSync(void);
71 
72     int SetStaleDataWipePolicy(WipePolicy policy);
73 
74     int EraseDeviceWaterMark(const std::string &deviceId, bool isNeedHash);
75 
76     NotificationChain::Listener *AddRemotePushFinishedNotify(const RemotePushFinishedNotifier &notifier, int &errCode);
77 
78     void NotifyRemotePushFinishedInner(const std::string &targetId) const;
79 
80     int SetSyncRetry(bool isRetry);
81     // Set an equal identifier for this database, After this called, send msg to the target will use this identifier
82     int SetEqualIdentifier(const std::string &identifier, const std::vector<std::string> &targets);
83 
84     virtual void SetDataInterceptor(const PushDataInterceptor &interceptor) = 0;
85 
86     void Dump(int fd) override;
87 
88     int GetSyncDataSize(const std::string &device, size_t &size) const;
89 
90     int GetHashDeviceId(const std::string &clientId, std::string &hashDevId);
91 
92 protected:
93     virtual IKvDBSyncInterface *GetSyncInterface() = 0;
94 
95     void SetSyncModuleActive();
96 
97     bool GetSyncModuleActive();
98 
99     void ReSetSyncModuleActive();
100     // Start syncer
101     int StartSyncer(bool isCheckSyncActive = false, bool isNeedActive = true);
102 
103     int StartSyncerWithNoLock(bool isCheckSyncActive, bool isNeedActive);
104 
105     // Stop syncer
106     void StopSyncer(bool isClosedOperation = false);
107 
108     void StopSyncerWithNoLock(bool isClosedOperation = false);
109 
110     void UserChangeHandle();
111 
112     void ChangeUserListener();
113 
114     // Get the dataItem's append length, the append length = after-serialized-len - original-dataItem-len
115     uint32_t GetAppendedLen() const;
116 
117     int GetLocalIdentity(std::string &outTarget) const;
118 
119     void TriggerSync(int notifyEvent);
120 
121 private:
122     int RegisterEventType(EventType type);
123 
124     bool NeedStartSyncer() const;
125 
126     SyncerProxy syncer_;
127     std::atomic<bool> started_;
128     std::atomic<bool> closed_;
129     std::atomic<bool> isSyncModuleActiveCheck_;
130     std::atomic<bool> isSyncNeedActive_;
131     mutable std::shared_mutex notifyChainLock_;
132     NotificationChain *notifyChain_;
133 
134     mutable std::mutex syncerOperateLock_;
135     NotificationChain::Listener *userChangeListener_;
136 
137     static const EventType REMOTE_PUSH_FINISHED;
138 };
139 }
140 
141 #endif // SYNC_ABLE_KVDB_H
142