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 DATASHARE_TEMPLATE_H 17 #define DATASHARE_TEMPLATE_H 18 19 #include <string> 20 #include <variant> 21 #include <vector> 22 #include "iremote_object.h" 23 24 namespace OHOS { 25 namespace DataShare { 26 /** 27 * Specifies the upper limit of size of data that RdbChangeNode will transfer by IPC. Currently it's 200k. 28 */ 29 constexpr int32_t DATA_SIZE_IPC_TRANSFER_LIMIT = 200 << 10; 30 /** 31 * Specifies the upper limit of size of data that RdbChangeNode will transfer by the shared memory. Currently it's 10M. 32 */ 33 constexpr int32_t DATA_SIZE_ASHMEM_TRANSFER_LIMIT = (10 << 10) << 10; 34 /** 35 * Specifies the name of the shared memory that RdbChangeNode will transfer. 36 */ 37 constexpr const char* ASHMEM_NAME = "DataShareRdbChangeNode"; 38 /** 39 * Specifies the default wait time for connecting extension 40 */ 41 static constexpr int DEFAULT_WAITTIME = 2; 42 43 /** 44 * Specifies the predicates structure of the template. 45 */ 46 struct PredicateTemplateNode { 47 PredicateTemplateNode() = default; PredicateTemplateNodePredicateTemplateNode48 PredicateTemplateNode(const std::string &key, const std::string &selectSql) : key_(key), selectSql_(selectSql) {} 49 /** Specifies the key of the sql. */ 50 std::string key_; 51 /** Specifies the sql of the template. */ 52 std::string selectSql_; 53 }; 54 55 /** 56 * Specifies the template structure in subscribe. 57 */ 58 struct Template { 59 Template() = default; TemplateTemplate60 Template(const std::vector<PredicateTemplateNode> &predicates, 61 const std::string &scheduler) : predicates_(predicates), scheduler_(scheduler) {} TemplateTemplate62 Template(const std::string &update, 63 const std::vector<PredicateTemplateNode> &predicates, 64 const std::string &scheduler) : update_(update), predicates_(predicates), scheduler_(scheduler) {} 65 /** Specifies the update sql of the template. */ 66 std::string update_; 67 /** Specifies the predicates of the template. {@link #PredicateTemplateNode} */ 68 std::vector<PredicateTemplateNode> predicates_; 69 /** Specifies the scheduler sql of the template. */ 70 std::string scheduler_; 71 }; 72 73 /** 74 * Specifies the {@link Template} id structure, template is marked by the template id. 75 */ 76 struct TemplateId { 77 /** Specifies the id of subscriber. */ 78 int64_t subscriberId_; 79 /** Specifies the bundleName of template owner. */ 80 std::string bundleName_; 81 bool operator==(const TemplateId &tplId) const 82 { 83 return subscriberId_ == tplId.subscriberId_ && bundleName_ == tplId.bundleName_; 84 } 85 bool operator!=(const TemplateId &tplId) const 86 { 87 return !(tplId == *this); 88 } 89 bool operator<(const TemplateId &tplId) const 90 { 91 if (subscriberId_ != tplId.subscriberId_) { 92 return subscriberId_ < tplId.subscriberId_; 93 } 94 return bundleName_ < tplId.bundleName_; 95 } 96 }; 97 98 /** 99 * Manages create datashare helper options. 100 */ 101 struct CreateOptions { 102 /** Specifies whether the {@link DataShareHelper} in proxy mode. */ 103 bool isProxy_ = true; 104 /** Specifies the System token. */ 105 sptr<IRemoteObject> token_; 106 /** Specifies whether use options to create DataShareHelper. */ 107 bool enabled_ = false; 108 /** Specifies the time to wait for connecting extension. */ 109 int waitTime_ = DEFAULT_WAITTIME; 110 }; 111 112 struct AshmemNode { 113 sptr<Ashmem> ashmem; 114 bool isManaged; 115 }; 116 117 /** 118 * Specifies the published item structure. 119 */ 120 struct PublishedDataItem { 121 using DataType = std::variant<std::vector<uint8_t>, std::string>; 122 /** The key of the published data. */ 123 std::string key_; 124 /** The subscriber id */ 125 int64_t subscriberId_; 126 /** The published data. If the data is large, use Ashmem. Do not access, only for ipc */ 127 std::variant<AshmemNode, std::string> value_; PublishedDataItemPublishedDataItem128 PublishedDataItem(){}; 129 PublishedDataItem(const PublishedDataItem &) = delete; 130 PublishedDataItem &operator=(const PublishedDataItem &) = delete; 131 virtual ~PublishedDataItem(); 132 PublishedDataItem(const std::string &key, int64_t subscriberId, DataType value); 133 PublishedDataItem(PublishedDataItem &&item); 134 PublishedDataItem &operator=(PublishedDataItem &&item); 135 bool IsAshmem() const; 136 bool IsString() const; 137 sptr<Ashmem> MoveOutAshmem(); 138 void SetAshmem(sptr<Ashmem> ashmem, bool isManaged = false); 139 void Set(DataType &value); 140 DataType GetData() const; 141 142 private: 143 void Clear(); 144 }; 145 146 /** Specifies the published data structure. */ 147 struct Data { 148 /** Indicates the published data. {@link PublishedDataItem} */ 149 std::vector<PublishedDataItem> datas_; 150 /** Indicates the version of data to publish, larger is newer. */ 151 int version_ = 0; 152 }; 153 154 /** 155 * Specifies the change node structure of published data in callback. 156 */ 157 struct PublishedDataChangeNode { 158 /** Specifies the bundleName of the callback. */ 159 std::string ownerBundleName_; 160 /** Specifies the datas of the callback. */ 161 std::vector<PublishedDataItem> datas_; 162 }; 163 164 /** 165 * Specifies the change node structure of rdb store data in callback. 166 */ 167 struct RdbChangeNode { 168 /** Specifies the uri of the callback. */ 169 std::string uri_; 170 /** Specifies the templateId of the callback. */ 171 TemplateId templateId_; 172 /** Specifies the datas of the callback. */ 173 std::vector<std::string> data_; 174 /** Specifies whether to use the shared meomry to transfer data. This will be set to be true when the size of 175 * the data is more than 200k, but no more than 10M. Usually the data will not be as large as 10M. 176 */ 177 bool isSharedMemory_ = false; 178 /** Specifies the address of the shared memory, wrapped by `OHOS::sptr<Ashmem>`. 179 * (De)serialization: [vec_size(int32); str1_len(int32), str1; str2_len(int32), str2; ...] 180 */ 181 OHOS::sptr<Ashmem> memory_; 182 /** Specifies the data size transferred the shared memory */ 183 int32_t size_; 184 }; 185 186 /** 187 * Specifies the operation result structure. 188 */ 189 struct OperationResult { 190 OperationResult() = default; OperationResultOperationResult191 OperationResult(const std::string &key, int errCode) : key_(key), errCode_(errCode) {} 192 /** Specifies the key of the operation result. */ 193 std::string key_; 194 /** Specifies the operation result error code. */ 195 int errCode_; 196 }; 197 } // namespace DataShare 198 } // namespace OHOS 199 #endif //DATASHARE_TEMPLATE_H 200