• 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 "dev_manager.h"
21 #include "bytes_utils.h"
22 
23 namespace OHOS::ObjectStore {
~DistributedObjectImpl()24 DistributedObjectImpl::~DistributedObjectImpl()
25 {
26 }
27 
PutDouble(const std::string & key,double value)28 uint32_t DistributedObjectImpl::PutDouble(const std::string &key, double value)
29 {
30     DataObjectHiTrace trace("DistributedObjectImpl::PutDouble");
31     return flatObjectStore_->PutDouble(sessionId_, key, value);
32 }
33 
PutBoolean(const std::string & key,bool value)34 uint32_t DistributedObjectImpl::PutBoolean(const std::string &key, bool value)
35 {
36     DataObjectHiTrace trace("DistributedObjectImpl::PutBoolean");
37     return flatObjectStore_->PutBoolean(sessionId_, key, value);
38 }
39 
PutString(const std::string & key,const std::string & value)40 uint32_t DistributedObjectImpl::PutString(const std::string &key, const std::string &value)
41 {
42     DataObjectHiTrace trace("DistributedObjectImpl::PutString");
43     if (key.find(ASSET_DOT) != std::string::npos) {
44         PutDeviceId();
45     }
46     return flatObjectStore_->PutString(sessionId_, key, value);
47 }
48 
GetDouble(const std::string & key,double & value)49 uint32_t DistributedObjectImpl::GetDouble(const std::string &key, double &value)
50 {
51     return flatObjectStore_->GetDouble(sessionId_, key, value);
52 }
53 
GetBoolean(const std::string & key,bool & value)54 uint32_t DistributedObjectImpl::GetBoolean(const std::string &key, bool &value)
55 {
56     return flatObjectStore_->GetBoolean(sessionId_, key, value);
57 }
58 
GetString(const std::string & key,std::string & value)59 uint32_t DistributedObjectImpl::GetString(const std::string &key, std::string &value)
60 {
61     return flatObjectStore_->GetString(sessionId_, key, value);
62 }
63 
GetType(const std::string & key,Type & type)64 uint32_t DistributedObjectImpl::GetType(const std::string &key, Type &type)
65 {
66     return flatObjectStore_->GetType(sessionId_, key, type);
67 }
68 
GetSessionId()69 std::string &DistributedObjectImpl::GetSessionId()
70 {
71     return sessionId_;
72 }
73 
DistributedObjectImpl(const std::string & sessionId,FlatObjectStore * flatObjectStore)74 DistributedObjectImpl::DistributedObjectImpl(const std::string &sessionId, FlatObjectStore *flatObjectStore)
75     : sessionId_(sessionId), flatObjectStore_(flatObjectStore)
76 {
77 }
78 
PutComplex(const std::string & key,const std::vector<uint8_t> & value)79 uint32_t DistributedObjectImpl::PutComplex(const std::string &key, const std::vector<uint8_t> &value)
80 {
81     DataObjectHiTrace trace("DistributedObjectImpl::PutComplex");
82     return flatObjectStore_->PutComplex(sessionId_, key, value);
83 }
84 
GetComplex(const std::string & key,std::vector<uint8_t> & value)85 uint32_t DistributedObjectImpl::GetComplex(const std::string &key, std::vector<uint8_t> &value)
86 {
87     return flatObjectStore_->GetComplex(sessionId_, key, value);
88 }
89 
Save(const std::string & deviceId)90 uint32_t DistributedObjectImpl::Save(const std::string &deviceId)
91 {
92     uint32_t status = flatObjectStore_->Save(sessionId_, deviceId);
93     if (status != SUCCESS) {
94         LOG_ERROR("DistributedObjectImpl:Save failed. status = %{public}d", status);
95         return status;
96     }
97     return status;
98 }
99 
RevokeSave()100 uint32_t DistributedObjectImpl::RevokeSave()
101 {
102     uint32_t status = flatObjectStore_->RevokeSave(sessionId_);
103     if (status != SUCCESS) {
104         LOG_ERROR("DistributedObjectImpl:RevokeSave failed. status = %{public}d", status);
105         return status;
106     }
107     return status;
108 }
109 
PutDeviceId()110 uint32_t DistributedObjectImpl::PutDeviceId()
111 {
112     DevManager::DetailInfo detailInfo = DevManager::GetInstance()->GetLocalDevice();
113     return flatObjectStore_->PutString(sessionId_, DEVICEID_KEY, detailInfo.networkId);
114 }
115 
GetAssetValue(const std::string & assetKey,Asset & assetValue)116 uint32_t DistributedObjectImpl::GetAssetValue(const std::string &assetKey, Asset &assetValue)
117 {
118     double assetStatus = 0.0;
119     auto status = GetDouble(assetKey + STATUS_SUFFIX, assetStatus);
120     if (status == SUCCESS) {
121         assetValue.status = static_cast<uint32_t>(assetStatus);
122     }
123     status = GetString(assetKey + NAME_SUFFIX, assetValue.name);
124     LOG_ERROR_RETURN(status == SUCCESS, "get name failed!", status);
125     status = GetString(assetKey + URI_SUFFIX, assetValue.uri);
126     LOG_ERROR_RETURN(status == SUCCESS, "get uri failed!", status);
127     status = GetString(assetKey + PATH_SUFFIX, assetValue.path);
128     LOG_ERROR_RETURN(status == SUCCESS, "get path failed!", status);
129     status = GetString(assetKey + CREATE_TIME_SUFFIX, assetValue.createTime);
130     LOG_ERROR_RETURN(status == SUCCESS, "get createTime failed!", status);
131     status = GetString(assetKey + MODIFY_TIME_SUFFIX, assetValue.modifyTime);
132     LOG_ERROR_RETURN(status == SUCCESS, "get modifyTime failed!", status);
133     status = GetString(assetKey + SIZE_SUFFIX, assetValue.size);
134     LOG_ERROR_RETURN(status == SUCCESS, "get size failed!", status);
135     RemovePrefix(assetValue);
136     return status;
137 }
138 
RemovePrefix(Asset & assetValue)139 void DistributedObjectImpl::RemovePrefix(Asset &assetValue)
140 {
141     assetValue.name = assetValue.name.substr(STRING_PREFIX_LEN);
142     assetValue.uri = assetValue.uri.substr(STRING_PREFIX_LEN);
143     assetValue.path = assetValue.path.substr(STRING_PREFIX_LEN);
144     assetValue.createTime = assetValue.createTime.substr(STRING_PREFIX_LEN);
145     assetValue.modifyTime = assetValue.modifyTime.substr(STRING_PREFIX_LEN);
146     assetValue.size = assetValue.size.substr(STRING_PREFIX_LEN);
147     assetValue.hash = assetValue.modifyTime + "_" + assetValue.size;
148 }
149 
BindAssetStore(const std::string & assetKey,AssetBindInfo & bindInfo)150 uint32_t DistributedObjectImpl::BindAssetStore(const std::string &assetKey, AssetBindInfo &bindInfo)
151 {
152     Asset assetValue;
153     auto status = GetAssetValue(assetKey, assetValue);
154     if (status != SUCCESS) {
155         LOG_ERROR("DistributedObjectImpl:GetAssetValue failed. status = %{public}d", status);
156         return status;
157     }
158     status = flatObjectStore_->BindAssetStore(sessionId_, bindInfo, assetValue);
159     if (status != SUCCESS) {
160         LOG_ERROR("DistributedObjectImpl:BindAssetStore failed. status = %{public}d", status);
161         return status;
162     }
163     return status;
164 }
165 } // namespace OHOS::ObjectStore