• 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 CLOUD_DB_PROXY_H
17 #define CLOUD_DB_PROXY_H
18 #include <atomic>
19 #include <condition_variable>
20 #include <mutex>
21 #include <shared_mutex>
22 #include "cloud/icloud_db.h"
23 #include "cloud/iAssetLoader.h"
24 
25 namespace DistributedDB {
26 class CloudDBProxy {
27 public:
28     CloudDBProxy();
29     ~CloudDBProxy() = default;
30 
31     void SetCloudDB(const std::shared_ptr<ICloudDb> &cloudDB);
32 
33     void SetIAssetLoader(const std::shared_ptr<IAssetLoader> &loader);
34 
35     int BatchInsert(const std::string &tableName, std::vector<VBucket> &record,
36         std::vector<VBucket> &extend, Info &uploadInfo);
37 
38     int BatchUpdate(const std::string &tableName, std::vector<VBucket> &record, std::vector<VBucket> &extend,
39         Info &uploadInfo);
40 
41     int BatchDelete(const std::string &tableName, std::vector<VBucket> &record, std::vector<VBucket> &extend,
42         Info &uploadInfo);
43 
44     int Query(const std::string &tableName, VBucket &extend, std::vector<VBucket> &data);
45 
46     std::pair<int, uint64_t> Lock();
47 
48     int UnLock();
49 
50     int Close();
51 
52     int HeartBeat();
53 
54     bool IsNotExistCloudDB() const;
55 
56     int Download(const std::string &tableName, const std::string &gid, const Type &prefix,
57         std::map<std::string, Assets> &assets);
58 
59     int RemoveLocalAssets(const std::vector<Asset> &assets);
60 
61 protected:
62     class CloudActionContext {
63     public:
64         CloudActionContext();
65         ~CloudActionContext() = default;
66 
67         void MoveInRecordAndExtend(std::vector<VBucket> &record, std::vector<VBucket> &extend);
68 
69         void MoveInExtend(std::vector<VBucket> &extend);
70 
71         void MoveOutRecordAndExtend(std::vector<VBucket> &record, std::vector<VBucket> &extend);
72 
73         void MoveInQueryExtendAndData(VBucket &extend, std::vector<VBucket> &data);
74 
75         void MoveOutQueryExtendAndData(VBucket &extend, std::vector<VBucket> &data);
76 
77         void MoveInLockStatus(std::pair<int, uint64_t> &lockStatus);
78 
79         void MoveOutLockStatus(std::pair<int, uint64_t> &lockStatus);
80 
81         bool WaitForRes(int64_t timeout);
82 
83         void SetActionRes(int res);
84 
85         int GetActionRes();
86 
87         void FinishAndNotify();
88 
89         Info GetInfo();
90 
91         void SetInfo(const uint32_t &totalCount, const uint32_t &successCount, const uint32_t &failedCount);
92 
93         void SetTableName(const std::string &tableName);
94 
95         std::string GetTableName();
96     private:
97         std::mutex actionMutex_;
98         std::condition_variable actionCv_;
99         bool actionFinished_;
100         int actionRes_;
101         uint32_t totalCount_;
102         uint32_t successCount_;
103         uint32_t failedCount_;
104 
105         std::string tableName_;
106         std::vector<VBucket> record_;
107         std::vector<VBucket> extend_;
108         VBucket queryExtend_;
109         std::vector<VBucket> data_;
110         std::pair<int, uint64_t> lockStatus_;
111     };
112     enum InnerActionCode : uint8_t {
113         INSERT = 0,
114         UPDATE,
115         DELETE,
116         QUERY,
117         LOCK,
118         UNLOCK,
119         HEARTBEAT,
120         // add action code before INVALID_ACTION
121         INVALID_ACTION
122     };
123     int InnerAction(const std::shared_ptr<CloudActionContext> &context,
124         const std::shared_ptr<ICloudDb> &cloudDb, InnerActionCode action);
125 
126     static DBStatus DMLActionTask(const std::shared_ptr<CloudActionContext> &context,
127         const std::shared_ptr<ICloudDb> &cloudDb, InnerActionCode action);
128 
129     void InnerActionTask(const std::shared_ptr<CloudActionContext> &context,
130         const std::shared_ptr<ICloudDb> &cloudDb, InnerActionCode action);
131 
132     static DBStatus InnerActionLock(const std::shared_ptr<CloudActionContext> &context,
133         const std::shared_ptr<ICloudDb> &cloudDb);
134 
135     static int GetInnerErrorCode(DBStatus status);
136 
137     mutable std::shared_mutex cloudMutex_;
138     mutable std::shared_mutex assetLoaderMutex_;
139     std::shared_ptr<ICloudDb> iCloudDb_;
140     std::shared_ptr<IAssetLoader> iAssetLoader_;
141     std::atomic<int64_t> timeout_;
142 
143     std::mutex asyncTaskMutex_;
144     std::condition_variable asyncTaskCv_;
145     int32_t asyncTaskCount_;
146 };
147 }
148 #endif // CLOUD_DB_PROXY_H
149