1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "kv_operator.h"
17 #include "directory_ex.h"
18 #include "media_log.h"
19
20 namespace {
21 const OHOS::DistributedKv::AppId KVSTORE_APPID = {"com.ohos.sharing.codec"};
22 const OHOS::DistributedKv::StoreId KVSTORE_STOREID = {"media_sharingcodec"};
23 }
24
25 static constexpr int32_t FILE_PERMISSION = 0777;
26
27 namespace OHOS {
28 namespace Sharing {
29
OpenKvStore()30 void KvOperator::OpenKvStore()
31 {
32 DistributedKv::Options options = {
33 .createIfMissing = true,
34 .encrypt = false,
35 .autoSync = false,
36 .area = DistributedKv::Area::EL1,
37 .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
38 .baseDir = "/data/service/el1/public/database/sharingcodec"
39 };
40
41 DistributedKv::AppId appId = { KVSTORE_APPID };
42 DistributedKv::StoreId storeId = { KVSTORE_STOREID };
43 mkdir(options.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
44 DistributedKv::Status status = dataManager_.GetSingleKvStore(options, appId, storeId, kvStorePtr_);
45 if (status != DistributedKv::Status::SUCCESS) {
46 MEDIA_LOGE("KvStore get failed! %{public}d.", status);
47 return;
48 }
49
50 OHOS::ChangeModeDirectory(options.baseDir, FILE_PERMISSION);
51 MEDIA_LOGI("KvStore using for %{public}s init success!", KVSTORE_APPID.appId.c_str());
52 }
53
CloseKvStore()54 void KvOperator::CloseKvStore()
55 {
56 if (kvStorePtr_ != nullptr) {
57 dataManager_.CloseKvStore({ KVSTORE_APPID }, kvStorePtr_);
58 kvStorePtr_ = nullptr;
59 }
60
61 MEDIA_LOGI("Close KvStore for %{public}s init success!", KVSTORE_APPID.appId.c_str());
62 }
63
GetValues(const std::string &key, std::string &val)64 bool KvOperator::GetValues(const std::string &key, std::string &val)
65 {
66 if (kvStorePtr_ == nullptr) {
67 MEDIA_LOGE("kvstore is nullptr.");
68 return false;
69 }
70
71 DistributedKv::Key k(key);
72 DistributedKv::Value v;
73 DistributedKv::Status status = kvStorePtr_->Get(k, v);
74 if (status != DistributedKv::Status::SUCCESS) {
75 MEDIA_LOGE("get kvstore failed %{public}d.", status);
76 val = "";
77 return false;
78 }
79
80 val = v.ToString();
81
82 MEDIA_LOGI("get kvstore success! val %{public}s.", val.c_str());
83 return true;
84 }
85
PutValues(const std::string &key, std::string values)86 bool KvOperator::PutValues(const std::string &key, std::string values)
87 {
88 if (kvStorePtr_ == nullptr) {
89 MEDIA_LOGE("kvstore is nullptr.");
90 return false;
91 }
92
93 DistributedKv::Key k(key);
94 DistributedKv::Value v(values);
95 DistributedKv::Status status = kvStorePtr_->Put(k, v);
96 if (status != DistributedKv::Status::SUCCESS) {
97 MEDIA_LOGE("put kvstore failed %{public}d.", status);
98 return false;
99 }
100 MEDIA_LOGI("put kvstore success!, val %{public}s.", values.c_str());
101 return true;
102 }
103
104
SyncKvStore(const std::string &key, const std::string &deviceId)105 void KvOperator::SyncKvStore(const std::string &key, const std::string &deviceId)
106 {
107 if (kvStorePtr_ == nullptr) {
108 MEDIA_LOGE("kvstore is nullptr.");
109 return;
110 }
111
112 DistributedKv::DataQuery dataQuery;
113 dataQuery.KeyPrefix(key);
114 std::vector<std::string> deviceIds = { deviceId };
115 DistributedKv::Status status = kvStorePtr_->Sync(deviceIds, DistributedKv::SyncMode::PULL,
116 dataQuery, shared_from_this());
117 MEDIA_LOGE("kvstore sync end, status %{public}d.", status);
118 }
119
SyncCompleted(const std::map<std::string, DistributedKv::Status> &results)120 void KvOperator::SyncCompleted(const std::map<std::string, DistributedKv::Status> &results)
121 {
122 MEDIA_LOGI("kvstore sync completed.");
123 }
124
StringToJson(const std::string &str, nlohmann::json &jsonObj)125 bool KvOperator::StringToJson(const std::string &str, nlohmann::json &jsonObj)
126 {
127 jsonObj = nlohmann::json::parse(str);
128 if (jsonObj.is_discarded()) {
129 MEDIA_LOGE("parse json failed.");
130 return false;
131 }
132
133 return true;
134 }
135
JsonToString(const nlohmann::json &jsonObj, std::string &str)136 bool KvOperator::JsonToString(const nlohmann::json &jsonObj, std::string &str)
137 {
138 str = jsonObj.dump();
139 return true;
140 }
141
QueryFromJson(const nlohmann::json &jsonObj, const std::string &key, std::string &val)142 bool KvOperator::QueryFromJson(const nlohmann::json &jsonObj, const std::string &key, std::string &val)
143 {
144 val = jsonObj.at(key.c_str());
145 return true;
146 }
147
148 } // namespace Media
149 } // namespace OHOS
150