1e0e9324cSopenharmony_ci/*
2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License.
5e0e9324cSopenharmony_ci * You may obtain a copy of the License at
6e0e9324cSopenharmony_ci *
7e0e9324cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0e9324cSopenharmony_ci *
9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and
13e0e9324cSopenharmony_ci * limitations under the License.
14e0e9324cSopenharmony_ci */
15e0e9324cSopenharmony_ci
16e0e9324cSopenharmony_ci#include "kv_operator.h"
17e0e9324cSopenharmony_ci#include "directory_ex.h"
18e0e9324cSopenharmony_ci#include "media_log.h"
19e0e9324cSopenharmony_ci
20e0e9324cSopenharmony_cinamespace {
21e0e9324cSopenharmony_ci    const OHOS::DistributedKv::AppId KVSTORE_APPID = {"com.ohos.sharing.codec"};
22e0e9324cSopenharmony_ci    const OHOS::DistributedKv::StoreId KVSTORE_STOREID = {"media_sharingcodec"};
23e0e9324cSopenharmony_ci}
24e0e9324cSopenharmony_ci
25e0e9324cSopenharmony_cistatic constexpr int32_t FILE_PERMISSION = 0777;
26e0e9324cSopenharmony_ci
27e0e9324cSopenharmony_cinamespace OHOS {
28e0e9324cSopenharmony_cinamespace Sharing {
29e0e9324cSopenharmony_ci
30e0e9324cSopenharmony_civoid KvOperator::OpenKvStore()
31e0e9324cSopenharmony_ci{
32e0e9324cSopenharmony_ci    DistributedKv::Options options = {
33e0e9324cSopenharmony_ci        .createIfMissing = true,
34e0e9324cSopenharmony_ci        .encrypt = false,
35e0e9324cSopenharmony_ci        .autoSync = false,
36e0e9324cSopenharmony_ci        .area = DistributedKv::Area::EL1,
37e0e9324cSopenharmony_ci        .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
38e0e9324cSopenharmony_ci        .baseDir = "/data/service/el1/public/database/sharingcodec"
39e0e9324cSopenharmony_ci    };
40e0e9324cSopenharmony_ci
41e0e9324cSopenharmony_ci    DistributedKv::AppId appId = { KVSTORE_APPID };
42e0e9324cSopenharmony_ci    DistributedKv::StoreId storeId = { KVSTORE_STOREID };
43e0e9324cSopenharmony_ci    mkdir(options.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
44e0e9324cSopenharmony_ci    DistributedKv::Status status = dataManager_.GetSingleKvStore(options, appId, storeId, kvStorePtr_);
45e0e9324cSopenharmony_ci    if (status != DistributedKv::Status::SUCCESS) {
46e0e9324cSopenharmony_ci        MEDIA_LOGE("KvStore get failed! %{public}d.", status);
47e0e9324cSopenharmony_ci        return;
48e0e9324cSopenharmony_ci    }
49e0e9324cSopenharmony_ci
50e0e9324cSopenharmony_ci    OHOS::ChangeModeDirectory(options.baseDir, FILE_PERMISSION);
51e0e9324cSopenharmony_ci    MEDIA_LOGI("KvStore using for %{public}s init success!", KVSTORE_APPID.appId.c_str());
52e0e9324cSopenharmony_ci}
53e0e9324cSopenharmony_ci
54e0e9324cSopenharmony_civoid KvOperator::CloseKvStore()
55e0e9324cSopenharmony_ci{
56e0e9324cSopenharmony_ci    if (kvStorePtr_ != nullptr) {
57e0e9324cSopenharmony_ci        dataManager_.CloseKvStore({ KVSTORE_APPID }, kvStorePtr_);
58e0e9324cSopenharmony_ci        kvStorePtr_ = nullptr;
59e0e9324cSopenharmony_ci    }
60e0e9324cSopenharmony_ci
61e0e9324cSopenharmony_ci    MEDIA_LOGI("Close KvStore for %{public}s init success!", KVSTORE_APPID.appId.c_str());
62e0e9324cSopenharmony_ci}
63e0e9324cSopenharmony_ci
64e0e9324cSopenharmony_cibool KvOperator::GetValues(const std::string &key,  std::string &val)
65e0e9324cSopenharmony_ci{
66e0e9324cSopenharmony_ci    if (kvStorePtr_ == nullptr) {
67e0e9324cSopenharmony_ci        MEDIA_LOGE("kvstore is nullptr.");
68e0e9324cSopenharmony_ci        return false;
69e0e9324cSopenharmony_ci    }
70e0e9324cSopenharmony_ci
71e0e9324cSopenharmony_ci    DistributedKv::Key k(key);
72e0e9324cSopenharmony_ci    DistributedKv::Value v;
73e0e9324cSopenharmony_ci    DistributedKv::Status status = kvStorePtr_->Get(k, v);
74e0e9324cSopenharmony_ci    if (status != DistributedKv::Status::SUCCESS) {
75e0e9324cSopenharmony_ci        MEDIA_LOGE("get kvstore failed %{public}d.", status);
76e0e9324cSopenharmony_ci        val = "";
77e0e9324cSopenharmony_ci        return false;
78e0e9324cSopenharmony_ci    }
79e0e9324cSopenharmony_ci
80e0e9324cSopenharmony_ci    val = v.ToString();
81e0e9324cSopenharmony_ci
82e0e9324cSopenharmony_ci    MEDIA_LOGI("get kvstore success!  val %{public}s.", val.c_str());
83e0e9324cSopenharmony_ci    return true;
84e0e9324cSopenharmony_ci}
85e0e9324cSopenharmony_ci
86e0e9324cSopenharmony_cibool KvOperator::PutValues(const std::string &key, std::string values)
87e0e9324cSopenharmony_ci{
88e0e9324cSopenharmony_ci    if (kvStorePtr_ == nullptr) {
89e0e9324cSopenharmony_ci        MEDIA_LOGE("kvstore is nullptr.");
90e0e9324cSopenharmony_ci        return false;
91e0e9324cSopenharmony_ci    }
92e0e9324cSopenharmony_ci
93e0e9324cSopenharmony_ci    DistributedKv::Key k(key);
94e0e9324cSopenharmony_ci    DistributedKv::Value v(values);
95e0e9324cSopenharmony_ci    DistributedKv::Status status = kvStorePtr_->Put(k, v);
96e0e9324cSopenharmony_ci    if (status != DistributedKv::Status::SUCCESS) {
97e0e9324cSopenharmony_ci        MEDIA_LOGE("put kvstore failed %{public}d.", status);
98e0e9324cSopenharmony_ci        return false;
99e0e9324cSopenharmony_ci    }
100e0e9324cSopenharmony_ci    MEDIA_LOGI("put kvstore success!, val %{public}s.", values.c_str());
101e0e9324cSopenharmony_ci    return true;
102e0e9324cSopenharmony_ci}
103e0e9324cSopenharmony_ci
104e0e9324cSopenharmony_ci
105e0e9324cSopenharmony_civoid KvOperator::SyncKvStore(const std::string &key, const std::string &deviceId)
106e0e9324cSopenharmony_ci{
107e0e9324cSopenharmony_ci    if (kvStorePtr_ == nullptr) {
108e0e9324cSopenharmony_ci        MEDIA_LOGE("kvstore is nullptr.");
109e0e9324cSopenharmony_ci        return;
110e0e9324cSopenharmony_ci    }
111e0e9324cSopenharmony_ci
112e0e9324cSopenharmony_ci    DistributedKv::DataQuery dataQuery;
113e0e9324cSopenharmony_ci    dataQuery.KeyPrefix(key);
114e0e9324cSopenharmony_ci    std::vector<std::string> deviceIds = { deviceId };
115e0e9324cSopenharmony_ci    DistributedKv::Status status = kvStorePtr_->Sync(deviceIds, DistributedKv::SyncMode::PULL,
116e0e9324cSopenharmony_ci        dataQuery, shared_from_this());
117e0e9324cSopenharmony_ci    MEDIA_LOGE("kvstore sync end, status %{public}d.", status);
118e0e9324cSopenharmony_ci}
119e0e9324cSopenharmony_ci
120e0e9324cSopenharmony_civoid KvOperator::SyncCompleted(const std::map<std::string, DistributedKv::Status> &results)
121e0e9324cSopenharmony_ci{
122e0e9324cSopenharmony_ci    MEDIA_LOGI("kvstore sync completed.");
123e0e9324cSopenharmony_ci}
124e0e9324cSopenharmony_ci
125e0e9324cSopenharmony_cibool KvOperator::StringToJson(const std::string &str, nlohmann::json &jsonObj)
126e0e9324cSopenharmony_ci{
127e0e9324cSopenharmony_ci    jsonObj = nlohmann::json::parse(str);
128e0e9324cSopenharmony_ci    if (jsonObj.is_discarded()) {
129e0e9324cSopenharmony_ci        MEDIA_LOGE("parse json failed.");
130e0e9324cSopenharmony_ci        return false;
131e0e9324cSopenharmony_ci    }
132e0e9324cSopenharmony_ci
133e0e9324cSopenharmony_ci    return true;
134e0e9324cSopenharmony_ci}
135e0e9324cSopenharmony_ci
136e0e9324cSopenharmony_cibool KvOperator::JsonToString(const nlohmann::json &jsonObj, std::string &str)
137e0e9324cSopenharmony_ci{
138e0e9324cSopenharmony_ci    str = jsonObj.dump();
139e0e9324cSopenharmony_ci    return true;
140e0e9324cSopenharmony_ci}
141e0e9324cSopenharmony_ci
142e0e9324cSopenharmony_cibool KvOperator::QueryFromJson(const nlohmann::json &jsonObj, const std::string &key, std::string &val)
143e0e9324cSopenharmony_ci{
144e0e9324cSopenharmony_ci    val = jsonObj.at(key.c_str());
145e0e9324cSopenharmony_ci    return true;
146e0e9324cSopenharmony_ci}
147e0e9324cSopenharmony_ci
148e0e9324cSopenharmony_ci} // namespace Media
149e0e9324cSopenharmony_ci} // namespace OHOS
150