1 /*
2  * Copyright (c) 2023 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 #ifndef RD_SINGLE_VER_NATURAL_STORE_H
16 #define RD_SINGLE_VER_NATURAL_STORE_H
17 #include <atomic>
18 #include <mutex>
19 
20 #include "db_common.h"
21 #include "grd_kv_api.h"
22 #include "kvdb_utils.h"
23 #include "rd_single_ver_storage_engine.h"
24 #include "rd_single_ver_storage_executor.h"
25 #include "single_ver_natural_store.h"
26 #include "storage_engine_manager.h"
27 
28 namespace DistributedDB {
29 class RdSingleVerNaturalStore : public SingleVerNaturalStore {
30 public:
31     RdSingleVerNaturalStore();
32     ~RdSingleVerNaturalStore() override;
33 
34     // Delete the copy and assign constructors
35     DISABLE_COPY_ASSIGN_MOVE(RdSingleVerNaturalStore);
36 
37     // Open the database
38     int Open(const KvDBProperties &kvDBProp) override;
39 
40     // Invoked automatically when connection count is zero
41     void Close() override;
42 
43     // Create a connection object.
44     GenericKvDBConnection *NewConnection(int &errCode) override;
45 
46     // Get interface type of this kvdb.
47     int GetInterfaceType() const override;
48 
49     // Get the interface ref-count, in order to access asynchronously.
50     void IncRefCount() override;
51 
52     // Drop the interface ref-count.
53     void DecRefCount() override;
54 
55     // Get the identifier of this kvdb.
56     std::vector<uint8_t> GetIdentifier() const override;
57 
58     // Get interface for syncer.
59     IKvDBSyncInterface *GetSyncInterface() override;
60 
61     int GetMetaData(const Key &key, Value &value) const override;
62 
63     int PutMetaData(const Key &key, const Value &value, bool isInTransaction) override;
64 
65     // Delete multiple meta data records in a transaction.
66     int DeleteMetaData(const std::vector<Key> &keys) override;
67 
68     // Delete multiple meta data records with key prefix in a transaction.
69     int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const override;
70 
71     int GetAllMetaKeys(std::vector<Key> &keys) const override;
72 
73     void GetMaxTimestamp(Timestamp &stamp) const override;
74 
75     void SetMaxTimestamp(Timestamp timestamp);
76 
77     int Rekey(const CipherPassword &passwd) override;
78 
79     int Export(const std::string &filePath, const CipherPassword &passwd) override;
80 
81     int Import(const std::string &filePath, const CipherPassword &passwd) override;
82 
83     RdSingleVerStorageExecutor *GetHandle(bool isWrite, int &errCode,
84         OperatePerm perm = OperatePerm::NORMAL_PERM) const;
85 
86     void AbortHandleAndWaitWriteHandle() const;
87 
88     void ReleaseHandle(RdSingleVerStorageExecutor *&handle) const;
89 
90     int TransObserverTypeToRegisterFunctionType(int observerType, RegisterFuncType &type) const override;
91 
92     SchemaObject GetSchemaInfo() const override;
93 
94     bool CheckCompatible(const std::string &schema, uint8_t type) const override;
95 
96     Timestamp GetCurrentTimestamp();
97 
98     SchemaObject GetSchemaObject() const;
99 
100     const SchemaObject &GetSchemaObjectConstRef() const;
101 
102     const KvDBProperties &GetDbProperties() const override;
103 
104     int GetKvDBSize(const KvDBProperties &properties, uint64_t &size) const override;
105 
106     KvDBProperties &GetDbPropertyForUpdate();
107 
108     int InitDatabaseContext(const KvDBProperties &kvDBProp);
109 
110     int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier);
111 
112     int SetAutoLifeCycleTime(uint32_t time);
113 
114     bool IsDataMigrating() const override;
115 
116     int TriggerToMigrateData() const;
117 
118     int CheckReadDataControlled() const;
119 
120     bool IsCacheDBMode() const;
121 
122     bool IsExtendedCacheDBMode() const;
123 
124     void IncreaseCacheRecordVersion() const;
125 
126     uint64_t GetCacheRecordVersion() const;
127 
128     uint64_t GetAndIncreaseCacheRecordVersion() const;
129 
130     int CheckIntegrity() const override;
131 
132     int GetCompressionAlgo(std::set<CompressAlgorithm> &algorithmSet) const override;
133 
134     void SetSendDataInterceptor(const PushDataInterceptor &interceptor) override;
135 
136     int SetMaxLogSize(uint64_t limit);
137 
138     uint64_t GetMaxLogSize() const;
139 
140     void Dump(int fd) override;
141 
142     void WakeUpSyncer() override;
143 
144     void CommitNotify(int notifyEvent, KvDBCommitNotifyFilterAbleData *data) override;
145 
146     void SetReceiveDataInterceptor(const DataInterceptor &interceptor) override;
147 
148 private:
149     int PreCheckRdImport(std::string &storePath);
150 
151     int GetAndInitStorageEngine(const KvDBProperties &kvDBProp);
152 
153     int RegisterNotification();
154 
155     void ReleaseResources();
156 
157     void InitDataBaseOption(const KvDBProperties &kvDBProp, OpenDbProperties &option);
158     mutable std::shared_mutex engineMutex_;
159     RdSingleVerStorageEngine *storageEngine_;
160     bool notificationEventsRegistered_;
161 };
162 } // namespace DistributedDB
163 #endif // RD_SINGLE_VER_NATURAL_STORE_H
164