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