1 /*
2 * Copyright (c) 2022 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 #include "distributed_object_impl.h"
17
18 #include "hitrace.h"
19 #include "objectstore_errors.h"
20 #include "string_utils.h"
21
22 namespace OHOS::ObjectStore {
~DistributedObjectImpl()23 DistributedObjectImpl::~DistributedObjectImpl()
24 {
25 }
26
PutNum(void * val,uint32_t offset,uint32_t valLen,Bytes & data)27 void PutNum(void *val, uint32_t offset, uint32_t valLen, Bytes &data)
28 {
29 uint32_t len = valLen + offset;
30 if (len > sizeof(data.front()) * data.size()) {
31 data.resize(len);
32 }
33
34 for (uint32_t i = 0; i < valLen; i++) {
35 // 8 bit = 1 byte
36 data[offset + i] = *(static_cast<uint64_t *>(val)) >> ((valLen - i - 1) * 8);
37 }
38 }
39
GetNum(Bytes & data,uint32_t offset,void * val,uint32_t valLen)40 uint32_t GetNum(Bytes &data, uint32_t offset, void *val, uint32_t valLen)
41 {
42 uint8_t *value = static_cast<uint8_t *>(val);
43 uint32_t len = offset + valLen;
44 uint32_t dataLen = data.size();
45 if (dataLen < len) {
46 LOG_ERROR("DistributedObjectImpl:GetNum data.size() %{public}d, offset %{public}d, valLen %{public}d", dataLen,
47 offset, valLen);
48 return ERR_DATA_LEN;
49 }
50 for (uint32_t i = 0; i < valLen; i++) {
51 value[i] = data[len - 1 - i];
52 }
53 return SUCCESS;
54 }
55
PutDouble(const std::string & key,double value)56 uint32_t DistributedObjectImpl::PutDouble(const std::string &key, double value)
57 {
58 DataObjectHiTrace trace("DistributedObjectImpl::PutDouble");
59 Bytes data;
60 Type type = Type::TYPE_DOUBLE;
61 PutNum(&type, 0, sizeof(type), data);
62 PutNum(&value, sizeof(type), sizeof(value), data);
63 return flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
64 }
65
PutBoolean(const std::string & key,bool value)66 uint32_t DistributedObjectImpl::PutBoolean(const std::string &key, bool value)
67 {
68 DataObjectHiTrace trace("DistributedObjectImpl::PutBoolean");
69 Bytes data;
70 Type type = Type::TYPE_BOOLEAN;
71 PutNum(&type, 0, sizeof(type), data);
72 PutNum(&value, sizeof(type), sizeof(value), data);
73 return flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
74 }
75
PutString(const std::string & key,const std::string & value)76 uint32_t DistributedObjectImpl::PutString(const std::string &key, const std::string &value)
77 {
78 DataObjectHiTrace trace("DistributedObjectImpl::PutString");
79 Bytes data;
80 Type type = Type::TYPE_STRING;
81 PutNum(&type, 0, sizeof(type), data);
82 Bytes dst = StringUtils::StrToBytes(value);
83 data.insert(data.end(), dst.begin(), dst.end());
84 return flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
85 }
86
GetDouble(const std::string & key,double & value)87 uint32_t DistributedObjectImpl::GetDouble(const std::string &key, double &value)
88 {
89 Bytes data;
90 Bytes keyBytes = StringUtils::StrToBytes(key);
91 uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
92 if (status != SUCCESS) {
93 LOG_ERROR("DistributedObjectImpl:GetDouble field not exist. %{public}d %{public}s", status, key.c_str());
94 return status;
95 }
96 status = GetNum(data, sizeof(Type), &value, sizeof(value));
97 if (status != SUCCESS) {
98 LOG_ERROR("DistributedObjectImpl::GetDouble getNum err. %{public}d", status);
99 }
100 return status;
101 }
102
GetBoolean(const std::string & key,bool & value)103 uint32_t DistributedObjectImpl::GetBoolean(const std::string &key, bool &value)
104 {
105 Bytes data;
106 Bytes keyBytes = StringUtils::StrToBytes(key);
107 uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
108 if (status != SUCCESS) {
109 LOG_ERROR("DistributedObjectImpl:GetBoolean field not exist. %{public}d %{public}s", status, key.c_str());
110 return status;
111 }
112 status = GetNum(data, sizeof(Type), &value, sizeof(value));
113 if (status != SUCCESS) {
114 LOG_ERROR("DistributedObjectImpl::GetBoolean getNum err. %{public}d", status);
115 return status;
116 }
117 return SUCCESS;
118 }
119
GetString(const std::string & key,std::string & value)120 uint32_t DistributedObjectImpl::GetString(const std::string &key, std::string &value)
121 {
122 Bytes data;
123 uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
124 if (status != SUCCESS) {
125 LOG_ERROR("DistributedObjectImpl:GetString field not exist. %{public}d %{public}s", status, key.c_str());
126 return status;
127 }
128 status = StringUtils::BytesToStrWithType(data, value);
129 if (status != SUCCESS) {
130 LOG_ERROR("DistributedObjectImpl::GetString dataToVal err. %{public}d", status);
131 }
132 return status;
133 }
134
GetType(const std::string & key,Type & type)135 uint32_t DistributedObjectImpl::GetType(const std::string &key, Type &type)
136 {
137 Bytes data;
138 uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
139 if (status != SUCCESS) {
140 LOG_ERROR("DistributedObjectImpl:GetString field not exist. %{public}d %{public}s", status, key.c_str());
141 return status;
142 }
143 status = GetNum(data, 0, &type, sizeof(type));
144 if (status != SUCCESS) {
145 LOG_ERROR("DistributedObjectImpl::GetBoolean getNum err. %{public}d", status);
146 return status;
147 }
148 return SUCCESS;
149 }
150
GetSessionId()151 std::string &DistributedObjectImpl::GetSessionId()
152 {
153 return sessionId_;
154 }
155
DistributedObjectImpl(const std::string & sessionId,FlatObjectStore * flatObjectStore)156 DistributedObjectImpl::DistributedObjectImpl(const std::string &sessionId, FlatObjectStore *flatObjectStore)
157 : sessionId_(sessionId), flatObjectStore_(flatObjectStore)
158 {
159 }
160
PutComplex(const std::string & key,const std::vector<uint8_t> & value)161 uint32_t DistributedObjectImpl::PutComplex(const std::string &key, const std::vector<uint8_t> &value)
162 {
163 DataObjectHiTrace trace("DistributedObjectImpl::PutComplex");
164 Bytes data;
165 Type type = Type::TYPE_COMPLEX;
166 PutNum(&type, 0, sizeof(type), data);
167 data.insert(data.end(), value.begin(), value.end());
168 uint32_t status = flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
169 if (status != SUCCESS) {
170 LOG_ERROR("DistributedObjectImpl::PutBoolean setField err %{public}d", status);
171 }
172 return status;
173 }
174
GetComplex(const std::string & key,std::vector<uint8_t> & value)175 uint32_t DistributedObjectImpl::GetComplex(const std::string &key, std::vector<uint8_t> &value)
176 {
177 uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, value);
178 if (status != SUCCESS) {
179 LOG_ERROR("DistributedObjectImpl:GetString field not exist. %{public}d %{public}s", status, key.c_str());
180 return status;
181 }
182 value.erase(value.begin(), value.begin() + sizeof(Type));
183 return status;
184 }
185
Save(const std::string & deviceId)186 uint32_t DistributedObjectImpl::Save(const std::string &deviceId)
187 {
188 uint32_t status = flatObjectStore_->Save(sessionId_, deviceId);
189 if (status != SUCCESS) {
190 LOG_ERROR("DistributedObjectImpl:Save failed. status = %{public}d", status);
191 return status;
192 }
193 return status;
194 }
195
RevokeSave()196 uint32_t DistributedObjectImpl::RevokeSave()
197 {
198 uint32_t status = flatObjectStore_->RevokeSave(sessionId_);
199 if (status != SUCCESS) {
200 LOG_ERROR("DistributedObjectImpl:RevokeSave failed. status = %{public}d", status);
201 return status;
202 }
203 return status;
204 }
205 } // namespace OHOS::ObjectStore