• 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 GENERIC_KVDB_H
17 #define GENERIC_KVDB_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "store_types.h"
25 #include "version.h"
26 #include "ikvdb.h"
27 #include "generic_kvdb_connection.h"
28 #include "performance_analysis.h"
29 #include "kvdb_conflict_entry.h"
30 #include "db_types.h"
31 
32 namespace DistributedDB {
33 class KvDBCommitNotifyFilterAbleData;
34 
35 struct ImportFileInfo {
36     std::string backupDir; // the directory of the current database backup
37     std::string unpackedDir; // the directory of the unpacked import file
38     std::string currentDir; // the directory of the current database
39     std::string curValidFile; // the file imply that the current directory is valid
40     std::string backValidFile; // the file imply that the backup directory is valid
41 };
42 
43 enum RegisterFuncType {
44     OBSERVER_SINGLE_VERSION_NS_PUT_EVENT = 0,
45     OBSERVER_SINGLE_VERSION_NS_SYNC_EVENT,
46     OBSERVER_SINGLE_VERSION_NS_LOCAL_EVENT,
47     OBSERVER_SINGLE_VERSION_NS_CONFLICT_EVENT,
48     OBSERVER_MULTI_VERSION_NS_COMMIT_EVENT,
49     CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ONLY,
50     CONFLICT_SINGLE_VERSION_NS_FOREIGN_KEY_ORIG,
51     CONFLICT_SINGLE_VERSION_NS_NATIVE_ALL,
52     REGISTER_FUNC_TYPE_MAX
53 };
54 
55 class GenericKvDB : public IKvDB {
56 public:
57     GenericKvDB();
58     ~GenericKvDB() override;
59 
60     DISABLE_COPY_ASSIGN_MOVE(GenericKvDB);
61 
62     // Get properties of this database.
63     const KvDBProperties &GetMyProperties() const override;
64 
65     // Create a db connection.
66     IKvDBConnection *GetDBConnection(int &errCode) final;
67 
68     // Called when all connections of this database closed.
69     void OnClose(const std::function<void(void)> &notifier) final;
70 
71     // Publish event when a commit action happened.
72     virtual void CommitNotify(int notifyEvent, KvDBCommitNotifyFilterAbleData *data);
73 
74     // Invoked automatically when connection count is zero
75     virtual void Close() = 0;
76 
77     virtual int TryToDisableConnection(OperatePerm perm);
78 
79     virtual void ReEnableConnection(OperatePerm perm);
80 
81     virtual int Rekey(const CipherPassword &passwd) = 0;
82 
83     // Empty passwords represent non-encrypted files.
84     // Export existing database files to a specified database file in the specified directory.
85     virtual int Export(const std::string &filePath, const CipherPassword &passwd) = 0;
86 
87     // Import the existing database files to the specified database file in the specified directory.
88     virtual int Import(const std::string &filePath, const CipherPassword &passwd) = 0;
89 
90     // Release a db connection.
91     void ReleaseDBConnection(GenericKvDBConnection *connection);
92 
93     // Register an event listener.
94     NotificationChain::Listener *RegisterEventListener(EventType type,
95         const NotificationChain::Listener::OnEvent &onEvent,
96         const NotificationChain::Listener::OnFinalize &onFinalize, int &errCode);
97 
98     // Get event notify counter.
99     uint64_t GetEventNotifyCounter() const;
100 
101     void OpenPerformanceAnalysis() override;
102 
103     void ClosePerformanceAnalysis() override;
104 
WakeUpSyncer()105     void WakeUpSyncer() override {};
106 
EnableAutonomicUpgrade()107     void EnableAutonomicUpgrade() override {};
108 
109     void SetCorruptHandler(const DatabaseCorruptHandler &handler) override;
110 
111     int RegisterFunction(RegisterFuncType type);
112 
113     int UnregisterFunction(RegisterFuncType type);
114 
115     uint32_t GetRegisterFunctionCount(RegisterFuncType type) const;
116 
117     virtual int TransObserverTypeToRegisterFunctionType(int observerType, RegisterFuncType &type) const;
118 
119     virtual int TransConflictTypeToRegisterFunctionType(int conflictType, RegisterFuncType &type) const;
120 
121     virtual int CheckDataStatus(const Key &key, const Value &value, bool isDeleted) const;
122 
123     virtual bool CheckWritePermission() const;
124 
125     virtual bool IsDataMigrating() const;
126 
127     virtual void SetConnectionFlag(bool isExisted) const;
128 
129     int CheckIntegrity() const override;
130 
131     std::string GetStorePath() const override;
132 
133     void Dump(int fd) override;
134 
135 protected:
136     // Create a connection object, no DB ref increased.
137     virtual GenericKvDBConnection *NewConnection(int &errCode) = 0;
138 
139     // Delete a connection object.
140     virtual void DelConnection(GenericKvDBConnection *connection);
141 
142     // Called when a new connection created.
143     void IncreaseConnectionCounter();
144 
145     // Called when a connection released.
146     void DecreaseConnectionCounter();
147 
148     // Register a new notification event type.
149     int RegisterNotificationEventType(int eventType);
150 
151     // Unregister a notification event type.
152     void UnRegisterNotificationEventType(int eventType);
153 
154     // Access 'properties_' for derived class.
155     const KvDBProperties &MyProp() const;
156     KvDBProperties &MyProp();
157 
158     static int GetWorkDir(const KvDBProperties &kvDBProp, std::string &workDir);
159 
160     void CorruptNotify() const;
161 
162     std::string GetStoreIdOnlyIdentifier(const KvDBProperties &properties) const;
163 
164     void GetStoreDirectory(const KvDBProperties &properties, int dbType,
165         std::string &storeDir, std::string &storeOnlyDir) const;
166 
167     PerformanceAnalysis *performance_;
168     DatabaseCorruptHandler corruptHandler_;
169     DeviceID devId_;
170 
171 private:
172     // Do commit notify in task pool.
173     void CommitNotifyAsync(int notifyEvent, KvDBCommitNotifyFilterAbleData *data);
174 
175     void CorruptNotifyAsync() const;
176 
177     // Get the ID of this kvdb.
178     std::string GetStoreId() const;
179 
180     DECLARE_OBJECT_TAG(GenericKvDB);
181 
182     // Databasse  event notify counter.
183     std::atomic<uint64_t> eventNotifyCounter_;
184 
185     // Fields for tracking the connection count and invoking callbacks.
186     std::atomic<int> connectionCount_;
187     std::vector<std::function<void(void)>> closeNotifiers_;
188     NotificationChain *notificationChain_;
189     KvDBProperties properties_;
190     std::mutex connectMutex_;
191     mutable std::mutex corruptMutex_;
192     OperatePerm operatePerm_;
193     mutable std::mutex regFuncCountMutex_;
194     std::vector<uint32_t> registerFunctionCount_;
195 };
196 } // namespace DistributedDB
197 
198 #endif // GENERIC_KVDB_H
199