1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "DBAdaptor"
16 #include "db_delegate.h"
17
18 #include "kv_delegate.h"
19 #include "log_print.h"
20 #include "rdb_delegate.h"
21 namespace OHOS::DataShare {
Create(const std::string & dir,int version,bool registerFunction)22 std::shared_ptr<DBDelegate> DBDelegate::Create(const std::string &dir, int version, bool registerFunction)
23 {
24 return std::make_shared<RdbDelegate>(dir, version, registerFunction);
25 }
26
GetInstance(bool reInit,const std::string & dir,const std::shared_ptr<ExecutorPool> & executors)27 std::shared_ptr<KvDBDelegate> KvDBDelegate::GetInstance(
28 bool reInit, const std::string &dir, const std::shared_ptr<ExecutorPool> &executors)
29 {
30 static std::shared_ptr<KvDBDelegate> delegate = nullptr;
31 static std::mutex mutex;
32 std::lock_guard<decltype(mutex)> lock(mutex);
33 if (delegate == nullptr || reInit) {
34 delegate = std::make_shared<KvDelegate>(dir, executors);
35 }
36 return delegate;
37 }
38
Marshal(DistributedData::Serializable::json & node) const39 bool Id::Marshal(DistributedData::Serializable::json &node) const
40 {
41 auto ret = false;
42 if (userId == INVALID_USER) {
43 ret = SetValue(node[GET_NAME(_id)], _id);
44 } else {
45 ret = SetValue(node[GET_NAME(_id)], _id + "_" + std::to_string(userId));
46 }
47 return ret;
48 }
49
Unmarshal(const DistributedData::Serializable::json & node)50 bool Id::Unmarshal(const DistributedData::Serializable::json &node)
51 {
52 return false;
53 }
54
Id(const std::string & id,const int32_t userId)55 Id::Id(const std::string &id, const int32_t userId) : _id(id), userId(userId) {}
56
VersionData(int version)57 VersionData::VersionData(int version) : version(version) {}
58
Unmarshal(const DistributedData::Serializable::json & node)59 bool VersionData::Unmarshal(const DistributedData::Serializable::json &node)
60 {
61 return GetValue(node, GET_NAME(version), version);
62 }
63
Marshal(DistributedData::Serializable::json & node) const64 bool VersionData::Marshal(DistributedData::Serializable::json &node) const
65 {
66 return SetValue(node[GET_NAME(version)], version);
67 }
68
GetId() const69 const std::string &KvData::GetId() const
70 {
71 return id;
72 }
73
KvData(const Id & id)74 KvData::KvData(const Id &id) : id(DistributedData::Serializable::Marshall(id)) {}
75 } // namespace OHOS::DataShare