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