• 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 #ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H
17 #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H
18 
19 #include <mutex>
20 
21 #include "cloud/cloud_db.h"
22 #include "cloud/cloud_store_types.h"
23 #include "cloud/icloud_db.h"
24 #include "error/general_error.h"
25 #include "snapshot/snapshot.h"
26 
27 namespace OHOS::DistributedRdb {
28 using Snapshot = DistributedData::Snapshot;
29 using BindAssets = std::shared_ptr<std::map<std::string, std::shared_ptr<Snapshot>>>;
30 class RdbCloud : public DistributedDB::ICloudDb {
31 public:
32     enum FLAG : uint8_t {
33         SYSTEM_ABILITY = 1,
34         APPLICATION
35     };
36     using DBStatus = DistributedDB::DBStatus;
37     using DBVBucket = DistributedDB::VBucket;
38     using DBQueryNodes = std::vector<DistributedDB::QueryNode>;
39     using DataBucket = DistributedData::VBucket;
40     using GeneralError = DistributedData::GeneralError;
41 
42     explicit RdbCloud(std::shared_ptr<DistributedData::CloudDB> cloudDB, BindAssets bindAssets);
43     virtual ~RdbCloud() = default;
44     DBStatus BatchInsert(const std::string &tableName, std::vector<DBVBucket> &&record,
45         std::vector<DBVBucket> &extend) override;
46     DBStatus BatchUpdate(const std::string &tableName, std::vector<DBVBucket> &&record,
47         std::vector<DBVBucket> &extend) override;
48     DBStatus BatchDelete(const std::string &tableName, std::vector<DBVBucket> &extend) override;
49     DBStatus Query(const std::string &tableName, DBVBucket &extend, std::vector<DBVBucket> &data) override;
50     DistributedData::GeneralError PreSharing(const std::string &tableName, DistributedData::VBuckets &extend);
51     std::pair<DBStatus, uint32_t> Lock() override;
52     DBStatus UnLock() override;
53     DBStatus HeartBeat() override;
54     DBStatus Close() override;
55     std::pair<DBStatus, std::string> GetEmptyCursor(const std::string &tableName) override;
56     static DBStatus ConvertStatus(DistributedData::GeneralError error);
57     uint8_t GetLockFlag() const;
58     std::pair<GeneralError, uint32_t> LockCloudDB(FLAG flag);
59     GeneralError UnLockCloudDB(FLAG flag);
60     void SetPrepareTraceId(const std::string &traceId) override;
61 
62 private:
63     static constexpr const char *TYPE_FIELD = "#_type";
64     static constexpr const char *QUERY_FIELD = "#_query";
65     using QueryNodes = std::vector<DistributedData::QueryNode>;
66     static std::pair<QueryNodes, DistributedData::GeneralError> ConvertQuery(DBVBucket& extend);
67     static QueryNodes ConvertQuery(DBQueryNodes&& nodes);
68     static void ConvertErrorField(DistributedData::VBuckets& extends);
69     static constexpr int32_t TO_MS = 1000; // s > ms
70     std::shared_ptr<DistributedData::CloudDB> cloudDB_;
71     const BindAssets snapshots_;
72     uint8_t flag_ = 0;
73     std::mutex mutex_;
74 
75     void PostEvent(DistributedData::VBuckets& records, std::set<std::string>& skipAssets,
76         DistributedData::VBuckets& extend, DistributedData::AssetEvent eventId);
77     void PostEvent(DistributedData::Value& value, DataBucket& extend, std::set<std::string>& skipAssets,
78         DistributedData::AssetEvent eventId);
79     void PostEventAsset(DistributedData::Asset& asset, DataBucket& extend, std::set<std::string>& skipAssets,
80         DistributedData::AssetEvent eventId);
81     std::pair<GeneralError, uint32_t> InnerLock(FLAG flag);
82     GeneralError InnerUnLock(FLAG flag);
83 };
84 } // namespace OHOS::DistributedRdb
85 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H