1 /*
2  * Copyright (c) 2024 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 OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H
17 #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <shared_mutex>
22 
23 #include "kv_store_changed_data.h"
24 #include "kv_store_delegate_manager.h"
25 #include "kv_store_nb_delegate.h"
26 #include "kvstore_sync_callback.h"
27 #include "kvstore_sync_manager.h"
28 #include "metadata/store_meta_data.h"
29 #include "store/general_store.h"
30 #include "store/general_value.h"
31 #include "store_observer.h"
32 
33 namespace OHOS::DistributedKv {
34 using namespace DistributedData;
35 class KVDBGeneralStore : public DistributedData::GeneralStore {
36 public:
37     using Value = DistributedData::Value;
38     using GenErr = DistributedData::GeneralError;
39     using DBStatus = DistributedDB::DBStatus;
40     using DBOption = DistributedDB::KvStoreNbDelegate::Option;
41     using DBSecurity = DistributedDB::SecurityOption;
42     using DBPassword = DistributedDB::CipherPassword;
43 
44     explicit KVDBGeneralStore(const StoreMetaData &meta);
45     ~KVDBGeneralStore();
46     int32_t Bind(Database &database, const std::map<uint32_t, BindInfo> &bindInfos,
47         const CloudConfig &config) override;
48     bool IsBound() override;
49     bool IsValid();
50     int32_t Execute(const std::string &table, const std::string &sql) override;
51     int32_t SetDistributedTables(
52         const std::vector<std::string> &tables, int32_t type, const std::vector<Reference> &references) override;
53     int32_t SetTrackerTable(const std::string &tableName, const std::set<std::string> &trackerColNames,
54         const std::string &extendColName, bool isForceUpgrade = false) override;
55     int32_t Insert(const std::string &table, VBuckets &&values) override;
56     int32_t Update(const std::string &table, const std::string &setSql, Values &&values, const std::string &whereSql,
57         Values &&conditions) override;
58     int32_t Replace(const std::string &table, VBucket &&value) override;
59     int32_t Delete(const std::string &table, const std::string &sql, Values &&args) override;
60     std::pair<int32_t, std::shared_ptr<Cursor>> Query(const std::string &table, const std::string &sql,
61         Values &&args) override;
62     std::pair<int32_t, std::shared_ptr<Cursor>> Query(const std::string &table, GenQuery &query) override;
63     std::pair<int32_t, int32_t> Sync(const Devices &devices, GenQuery &query, DetailAsync async,
64         const SyncParam &syncParm) override;
65     std::pair<int32_t, std::shared_ptr<Cursor>> PreSharing(GenQuery &query) override;
66     int32_t Clean(const std::vector<std::string> &devices, int32_t mode, const std::string &tableName) override;
67     int32_t Watch(int32_t origin, Watcher &watcher) override;
68     int32_t Unwatch(int32_t origin, Watcher &watcher) override;
69     int32_t RegisterDetailProgressObserver(DetailAsync async) override;
70     int32_t UnregisterDetailProgressObserver() override;
71     int32_t Close(bool isForce = false) override;
72     int32_t AddRef() override;
73     int32_t Release() override;
74     int32_t BindSnapshots(std::shared_ptr<std::map<std::string, std::shared_ptr<Snapshot>>> bindAssets) override;
75     int32_t MergeMigratedData(const std::string &tableName, VBuckets &&values) override;
76     int32_t CleanTrackerData(const std::string &tableName, int64_t cursor) override;
77     std::vector<std::string> GetWaterVersion(const std::string &deviceId) override;
78     void SetEqualIdentifier(const std::string &appId, const std::string &storeId, std::string account = "") override;
79     void SetConfig(const StoreConfig &storeConfig) override;
80     void SetExecutor(std::shared_ptr<Executor> executor) override;
81     static DBPassword GetDBPassword(const StoreMetaData &data);
82     static DBOption GetDBOption(const StoreMetaData &data, const DBPassword &password);
83     static DBSecurity GetDBSecurity(int32_t secLevel);
84     std::pair<int32_t, uint32_t> LockCloudDB() override;
85     int32_t UnLockCloudDB() override;
86 
87 private:
88     using KvDelegate = DistributedDB::KvStoreNbDelegate;
89     using KvManager = DistributedDB::KvStoreDelegateManager;
90     using SyncProcess = DistributedDB::SyncProcess;
91     using DBSyncCallback = std::function<void(const std::map<std::string, DBStatus> &status)>;
92     using DBProcessCB = std::function<void(const std::map<std::string, SyncProcess> &processes)>;
93     static GenErr ConvertStatus(DBStatus status);
94     void SetDBPushDataInterceptor(int32_t storeType);
95     void SetDBReceiveDataInterceptor(int32_t storeType);
96     std::vector<uint8_t> GetNewKey(std::vector<uint8_t> &key, const std::string &uuid);
97     DBSyncCallback GetDBSyncCompleteCB(DetailAsync async);
98     DBProcessCB GetDBProcessCB(DetailAsync async);
99     DBStatus CloudSync(const Devices &devices, DistributedDB::SyncMode cloudSyncMode, DetailAsync async, int64_t wait,
100         const std::string &prepareTraceId);
101     void GetIdentifierParams(std::vector<std::string> &devices,
102         const std::vector<std::string> &uuids, int32_t authType);
103     class ObserverProxy : public DistributedDB::KvStoreObserver {
104     public:
105         using DBOrigin = DistributedDB::Origin;
106         using DBChangeData = DistributedDB::ChangedData;
107         using DBEntry = DistributedDB::Entry;
108         using GenOrigin = Watcher::Origin;
109         ~ObserverProxy() = default;
110         void OnChange(DBOrigin origin, const std::string &originalId, DBChangeData &&data) override;
111         void OnChange(const DistributedDB::KvStoreChangedData &data) override;
112         void ConvertChangeData(const std::list<DBEntry> &entries, std::vector<Values> &values);
HasWatcher()113         bool HasWatcher() const
114         {
115             return watcher_ != nullptr;
116         }
117 
118     private:
119         friend KVDBGeneralStore;
120         Watcher *watcher_ = nullptr;
121         std::string storeId_;
122     };
123 
124     static constexpr uint8_t META_COMPRESS_RATE = 10;
125     ObserverProxy observer_;
126     KvManager manager_;
127     KvDelegate *delegate_ = nullptr;
128     std::map<std::string, std::shared_ptr<DistributedDB::ICloudDb>> dbClouds_{};
129     std::set<BindInfo> bindInfos_;
130     std::atomic<bool> isBound_ = false;
131     std::mutex mutex_;
132     int32_t ref_ = 1;
133     mutable std::shared_timed_mutex rwMutex_;
134     StoreInfo storeInfo_;
135 
136     static constexpr int32_t NO_ACCOUNT = 0;
137     static constexpr int32_t IDENTICAL_ACCOUNT = 1;
138     static constexpr const char *defaultAccountId = "ohosAnonymousUid";
139     bool enableCloud_ = false;
140     bool isPublic_ = false;
141     static const std::map<DBStatus, GenErr> dbStatusMap_;
142 };
143 } // namespace OHOS::DistributedKv
144 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_KVDB_GENERAL_STORE_H