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 GENERIC_KV_DB_CONNECTION_H
17 #define GENERIC_KV_DB_CONNECTION_H
18 
19 #include <atomic>
20 #include <list>
21 #include <mutex>
22 
23 #include "ikvdb_connection.h"
24 #include "notification_chain.h"
25 
26 namespace DistributedDB {
27 class GenericKvDB;
28 
29 class GenericKvDBConnection : public IKvDBConnection {
30 public:
31     explicit GenericKvDBConnection(GenericKvDB *kvDB);
32     ~GenericKvDBConnection() override;
33 
34     DISABLE_COPY_ASSIGN_MOVE(GenericKvDBConnection);
35 
36     // Register observer.
37     KvDBObserverHandle *RegisterObserver(unsigned mode, const Key &key,
38         const KvDBObserverAction &action, int &errCode) override;
39 
40     // Unregister observer.
41     int UnRegisterObserver(const KvDBObserverHandle *observerHandle) override;
42 
43     // Register a conflict notifier.
44     int SetConflictNotifier(int conflictType, const KvDBConflictAction &action) override;
45 
46     // Close and release the connection.
47     int Close() final;
48 
49     std::string GetIdentifier() const override;
50 
51     // Pragma interface.
52     int Pragma(int cmd, void *parameter) override;
53 
54     // Parse event types(from observer mode).
55     virtual int TranslateObserverModeToEventTypes(unsigned mode, std::list<int> &eventTypes) const = 0;
56 
57     // Set it to 'safe' state to delete the connection
58     void SetSafeDeleted();
59 
60     int GetEntries(const IOption &option, const Key &keyPrefix, std::vector<Entry> &entries) const override;
61 
62     int GetEntries(const IOption &option, const Query &query, std::vector<Entry> &entries) const override;
63 
64     int GetResultSet(const IOption &option, const Key &keyPrefix, IKvDBResultSet *&resultSet) const override;
65 
66     int GetResultSet(const IOption &option, const Query &query, IKvDBResultSet *&resultSet) const override;
67 
68     int GetCount(const IOption &option, const Query &query, int &count) const override;
69 
70     void ReleaseResultSet(IKvDBResultSet *&resultSet) override;
71 
72     int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier) override;
73 
74     int GetSecurityOption(int &securityLabel, int &securityFlag) const override;
75 
76     int CheckIntegrity() const override;
77 
78     int GetKeys(const IOption &option, const Key &keyPrefix, std::vector<Key> &keys) const override;
79 
80     int GetSyncDataSize(const std::string &device, size_t &size) const override;
81 
82     int UpdateKey(const UpdateKeyCallback &callback) override;
83 
84     int GetWatermarkInfo(const std::string &device, WatermarkInfo &info) override;
85 
86     int Sync(const CloudSyncOption &option, const SyncProcessCallback &onProcess) override;
87 
88     int SetCloudDB(const std::map<std::string, std::shared_ptr<ICloudDb>> &cloudDBs) override;
89 
90     int SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema) override;
91 
92     int RemoveDeviceData(const std::string &device, ClearMode mode) override;
93 
94     int RemoveDeviceData(const std::string &device, const std::string &user, ClearMode mode) override;
95 
96     int32_t GetTaskCount() override;
97 
98     int RegisterObserverAction(const KvStoreObserver *observer, const ObserverAction &action) override;
99 
100     int UnRegisterObserverAction(const KvStoreObserver *observer) override;
101 
102     void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) override;
103 
104     int GetCloudVersion(const std::string &device, std::map<std::string, std::string> &versionMap) override;
105 
106     int SetReceiveDataInterceptor(const DataInterceptor &interceptor) override;
107 
108     int SetCloudSyncConfig(const CloudSyncConfig &config) override;
109 
110     int GetEntries(const std::string &device, std::vector<Entry> &entries) const override;
111 
112     void MarkRebuild() override;
113 
114     bool IsRebuild() const override;
115 protected:
116     // Get the stashed 'KvDB_ pointer' without ref.
117     template<typename DerivedDBType>
GetDB()118     DerivedDBType *GetDB() const
119     {
120         return static_cast<DerivedDBType *>(kvDB_);
121     }
122 
123     // Register an event listener with observer action data.
124     NotificationChain::Listener *RegisterSpecialListener(int type, const Key &key,
125         const KvDBObserverAction &action, bool conflict, int &errCode);
126 
127     virtual int PreCheckExclusiveStatus();
128 
129     void ResetExclusiveStatus();
130 
131     bool IsObserverEmpty();
132 
133     // Called in Close(), overriding of Close() is forbidden.
134     virtual int PreClose();
135 
136     GenericKvDB *kvDB_;
137     std::atomic<bool> isExclusive_;
138 
139 private:
140     int GetEventType(unsigned mode, std::list<int> &eventTypes) const;
141 
142     int RegisterObserverForOneType(int type, const Key &key, const KvDBObserverAction &action,
143         NotificationChain::Listener *&listener);
144 
145     bool isSafeDeleted_;
146     std::mutex observerListLock_;
147     std::list<KvDBObserverHandle *> observerList_;
148 
149     std::atomic<bool> isRebuild_;
150 };
151 }
152 
153 #endif // GENERIC_KV_DB_CONNECTION_H
154