• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(std::string(__FUNCTION__));
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     uint32_t status = flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
64     if (status != SUCCESS) {
65         LOG_ERROR("DistributedObjectImpl::PutDouble setField err %{public}d", status);
66     }
67     return status;
68 }
69 
PutBoolean(const std::string & key,bool value)70 uint32_t DistributedObjectImpl::PutBoolean(const std::string &key, bool value)
71 {
72     DataObjectHiTrace trace(std::string(__FUNCTION__));
73     Bytes data;
74     Type type = Type::TYPE_BOOLEAN;
75     PutNum(&type, 0, sizeof(type), data);
76     PutNum(&value, sizeof(type), sizeof(value), data);
77     uint32_t status = flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
78     if (status != SUCCESS) {
79         LOG_ERROR("DistributedObjectImpl::PutBoolean setField err %{public}d", status);
80     }
81     return status;
82 }
83 
PutString(const std::string & key,const std::string & value)84 uint32_t DistributedObjectImpl::PutString(const std::string &key, const std::string &value)
85 {
86     DataObjectHiTrace trace(std::string(__FUNCTION__));
87     Bytes data;
88     Type type = Type::TYPE_STRING;
89     PutNum(&type, 0, sizeof(type), data);
90     Bytes dst = StringUtils::StrToBytes(value);
91     data.insert(data.end(), dst.begin(), dst.end());
92     uint32_t status = flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
93     if (status != SUCCESS) {
94         LOG_ERROR("DistributedObjectImpl::PutString setField err %{public}d", status);
95     }
96     return status;
97 }
98 
GetDouble(const std::string & key,double & value)99 uint32_t DistributedObjectImpl::GetDouble(const std::string &key, double &value)
100 {
101     Bytes data;
102     Bytes keyBytes = StringUtils::StrToBytes(key);
103     uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
104     if (status != SUCCESS) {
105         LOG_ERROR("DistributedObjectImpl:GetDouble field not exist. %{public}d %{public}s", status, key.c_str());
106         return status;
107     }
108     status = GetNum(data, sizeof(Type), &value, sizeof(value));
109     if (status != SUCCESS) {
110         LOG_ERROR("DistributedObjectImpl::GetDouble getNum err. %{public}d", status);
111     }
112     return status;
113 }
114 
GetBoolean(const std::string & key,bool & value)115 uint32_t DistributedObjectImpl::GetBoolean(const std::string &key, bool &value)
116 {
117     Bytes data;
118     Bytes keyBytes = StringUtils::StrToBytes(key);
119     uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
120     if (status != SUCCESS) {
121         LOG_ERROR("DistributedObjectImpl:GetBoolean field not exist. %{public}d %{public}s", status, key.c_str());
122         return status;
123     }
124     status = GetNum(data, sizeof(Type), &value, sizeof(value));
125     if (status != SUCCESS) {
126         LOG_ERROR("DistributedObjectImpl::GetBoolean getNum err. %{public}d", status);
127         return status;
128     }
129     return SUCCESS;
130 }
131 
GetString(const std::string & key,std::string & value)132 uint32_t DistributedObjectImpl::GetString(const std::string &key, std::string &value)
133 {
134     Bytes data;
135     uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
136     if (status != SUCCESS) {
137         LOG_ERROR("DistributedObjectImpl:GetString field not exist. %{public}d %{public}s", status, key.c_str());
138         return status;
139     }
140     status = StringUtils::BytesToStrWithType(data, value);
141     if (status != SUCCESS) {
142         LOG_ERROR("DistributedObjectImpl::GetString dataToVal err. %{public}d", status);
143     }
144     return status;
145 }
146 
GetType(const std::string & key,Type & type)147 uint32_t DistributedObjectImpl::GetType(const std::string &key, Type &type)
148 {
149     Bytes data;
150     uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, data);
151     if (status != SUCCESS) {
152         LOG_ERROR("DistributedObjectImpl:GetString field not exist. %{public}d %{public}s", status, key.c_str());
153         return status;
154     }
155     status = GetNum(data, 0, &type, sizeof(type));
156     if (status != SUCCESS) {
157         LOG_ERROR("DistributedObjectImpl::GetBoolean getNum err. %{public}d", status);
158         return status;
159     }
160     return SUCCESS;
161 }
162 
GetSessionId()163 std::string &DistributedObjectImpl::GetSessionId()
164 {
165     return sessionId_;
166 }
167 
DistributedObjectImpl(const std::string & sessionId,FlatObjectStore * flatObjectStore)168 DistributedObjectImpl::DistributedObjectImpl(const std::string &sessionId, FlatObjectStore *flatObjectStore)
169     : sessionId_(sessionId), flatObjectStore_(flatObjectStore)
170 {
171 }
172 
PutComplex(const std::string & key,const std::vector<uint8_t> & value)173 uint32_t DistributedObjectImpl::PutComplex(const std::string &key, const std::vector<uint8_t> &value)
174 {
175     DataObjectHiTrace trace(std::string(__FUNCTION__));
176     Bytes data;
177     Type type = Type::TYPE_COMPLEX;
178     PutNum(&type, 0, sizeof(type), data);
179     data.insert(data.end(), value.begin(), value.end());
180     uint32_t status = flatObjectStore_->Put(sessionId_, FIELDS_PREFIX + key, data);
181     if (status != SUCCESS) {
182         LOG_ERROR("DistributedObjectImpl::PutBoolean setField err %{public}d", status);
183     }
184     return status;
185 }
186 
GetComplex(const std::string & key,std::vector<uint8_t> & value)187 uint32_t DistributedObjectImpl::GetComplex(const std::string &key, std::vector<uint8_t> &value)
188 {
189     uint32_t status = flatObjectStore_->Get(sessionId_, FIELDS_PREFIX + key, value);
190     if (status != SUCCESS) {
191         LOG_ERROR("DistributedObjectImpl:GetString field not exist. %{public}d %{public}s", status, key.c_str());
192         return status;
193     }
194     value.erase(value.begin(), value.begin() + sizeof(Type));
195     return status;
196 }
197 
Save(const std::string & deviceId)198 uint32_t DistributedObjectImpl::Save(const std::string &deviceId)
199 {
200     uint32_t status = flatObjectStore_->Save(sessionId_, deviceId);
201     if (status != SUCCESS) {
202         LOG_ERROR("DistributedObjectImpl:Save failed. status = %{public}d", status);
203         return status;
204     }
205     return status;
206 }
207 
RevokeSave()208 uint32_t DistributedObjectImpl::RevokeSave()
209 {
210     uint32_t status = flatObjectStore_->RevokeSave(sessionId_);
211     if (status != SUCCESS) {
212         LOG_ERROR("DistributedObjectImpl:RevokeSave failed. status = %{public}d", status);
213         return status;
214     }
215     return status;
216 }
217 } // namespace OHOS::ObjectStore