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 16 #ifndef DATASHARESERVICE_DB_DELEGATE_H 17 #define DATASHARESERVICE_DB_DELEGATE_H 18 19 #include <string> 20 21 #include "abs_shared_result_set.h" 22 #include "concurrent_map.h" 23 #include "datashare_predicates.h" 24 #include "datashare_result_set.h" 25 #include "datashare_values_bucket.h" 26 #include "executor_pool.h" 27 #include "hiview_fault_adapter.h" 28 #include "metadata/store_meta_data.h" 29 #include "result_set.h" 30 #include "serializable/serializable.h" 31 #include "value_object.h" 32 33 namespace OHOS::DataShare { 34 class DBDelegate { 35 public: 36 using Time = std::chrono::steady_clock::time_point; 37 using Filter = std::function<bool(const std::string &user)>; 38 static std::shared_ptr<DBDelegate> Create(DistributedData::StoreMetaData &metaData, 39 const std::string &extUri = "", const std::string &backup = ""); 40 static void Close(const Filter &filter); 41 virtual std::pair<int, std::shared_ptr<DataShareResultSet>> Query(const std::string &tableName, 42 const DataSharePredicates &predicates, const std::vector<std::string> &columns, 43 int32_t callingPid, uint32_t callingTokenId) = 0; 44 virtual std::string Query( 45 const std::string &sql, const std::vector<std::string> &selectionArgs = std::vector<std::string>()) = 0; 46 virtual std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql) = 0; 47 virtual std::pair<int, int64_t> UpdateSql(const std::string &sql) = 0; 48 virtual bool IsInvalid() = 0; 49 static void SetExecutorPool(std::shared_ptr<ExecutorPool> executor); 50 static void EraseStoreCache(const int32_t tokenId); 51 virtual std::pair<int64_t, int64_t> InsertEx(const std::string &tableName, 52 const DataShareValuesBucket &valuesBucket) = 0; 53 virtual std::pair<int64_t, int64_t> UpdateEx(const std::string &tableName, 54 const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) = 0; 55 virtual std::pair<int64_t, int64_t> DeleteEx(const std::string &tableName, 56 const DataSharePredicates &predicate) = 0; 57 private: 58 static void GarbageCollect(bool encrypt); 59 static void StartTimer(bool encrypt); 60 struct Entity { 61 explicit Entity(std::shared_ptr<DBDelegate> store, const DistributedData::StoreMetaData &meta); 62 std::shared_ptr<DBDelegate> store_; 63 std::string user; 64 Time time_; 65 }; 66 static constexpr int NO_CHANGE_VERSION = -1; 67 static constexpr int64_t INTERVAL = 20; //seconds 68 // Encrypt store of RDB depends on other components, and other components will use datashare, 69 // causing circular dependencies and deadlocks, the encrypt store is separated from the non-encryption store here. 70 static ConcurrentMap<uint32_t, std::map<std::string, std::shared_ptr<Entity>>> stores_; 71 static ConcurrentMap<uint32_t, std::map<std::string, std::shared_ptr<Entity>>> storesEncrypt_; 72 static std::shared_ptr<ExecutorPool> executor_; 73 static ExecutorPool::TaskId taskId_; 74 static ExecutorPool::TaskId taskIdEncrypt_; 75 }; 76 77 class Id : public DistributedData::Serializable { 78 public: 79 static constexpr int INVALID_USER = -1; 80 Id(const std::string &id, const int32_t userId); 81 ~Id() = default; 82 bool Marshal(json &node) const override; 83 bool Unmarshal(const json &node) override; string()84 operator std::string() 85 { 86 return DistributedData::Serializable::Marshall(*this); 87 } 88 89 private: 90 std::string _id; 91 int32_t userId; 92 }; 93 94 class VersionData : public DistributedData::Serializable { 95 public: 96 explicit VersionData(int version); 97 bool Marshal(json &node) const override; 98 bool Unmarshal(const json &node) override; SetVersion(int ver)99 virtual void SetVersion(int ver) 100 { 101 version = ver; 102 }; GetVersion()103 virtual int GetVersion() const 104 { 105 return version; 106 }; 107 108 private: 109 int version; 110 }; 111 112 class KvData { 113 public: 114 explicit KvData(const Id &id); 115 const std::string &GetId() const; 116 virtual bool HasVersion() const = 0; 117 virtual int GetVersion() const = 0; 118 virtual std::string GetValue() const = 0; 119 120 private: 121 std::string id; 122 }; 123 124 class KvDBDelegate { 125 public: 126 static constexpr const char *TEMPLATE_TABLE = "template_"; 127 static constexpr const char *DATA_TABLE = "data_"; 128 static std::shared_ptr<KvDBDelegate> GetInstance( 129 bool reInit = false, const std::string &dir = "", const std::shared_ptr<ExecutorPool> &executors = nullptr); 130 virtual ~KvDBDelegate() = default; 131 virtual std::pair<int32_t, int32_t> Upsert(const std::string &collectionName, const KvData &value) = 0; 132 virtual std::pair<int32_t, int32_t> Delete(const std::string &collectionName, const std::string &filter) = 0; 133 virtual int32_t Get(const std::string &collectionName, const Id &id, std::string &value) = 0; 134 virtual int32_t Get(const std::string &collectionName, const std::string &filter, const std::string &projection, 135 std::string &result) = 0; 136 virtual int32_t GetBatch(const std::string &collectionName, const std::string &filter, 137 const std::string &projection, std::vector<std::string> &result) = 0; 138 virtual void NotifyBackup() = 0; 139 }; 140 } // namespace OHOS::DataShare 141 #endif // DATASHARESERVICE_DB_DELEGATE_H 142