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