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