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 #include "cloud/cloud_db.h" 19 #include "cloud/cloud_store_types.h" 20 #include "cloud/icloud_db.h" 21 #include "error/general_error.h" 22 #include "snapshot/snapshot.h" 23 24 namespace OHOS::DistributedRdb { 25 class RdbCloud : public DistributedDB::ICloudDb { 26 public: 27 using DBStatus = DistributedDB::DBStatus; 28 using DBVBucket = DistributedDB::VBucket; 29 using DBQueryNodes = std::vector<DistributedDB::QueryNode>; 30 using DataBucket = DistributedData::VBucket; 31 using BindAssets = DistributedData::BindAssets; 32 33 explicit RdbCloud(std::shared_ptr<DistributedData::CloudDB> cloudDB, BindAssets* bindAssets); 34 virtual ~RdbCloud() = default; 35 DBStatus BatchInsert(const std::string &tableName, std::vector<DBVBucket> &&record, 36 std::vector<DBVBucket> &extend) override; 37 DBStatus BatchUpdate(const std::string &tableName, std::vector<DBVBucket> &&record, 38 std::vector<DBVBucket> &extend) override; 39 DBStatus BatchDelete(const std::string &tableName, std::vector<DBVBucket> &extend) override; 40 DBStatus Query(const std::string &tableName, DBVBucket &extend, std::vector<DBVBucket> &data) override; 41 DistributedData::GeneralError PreSharing(const std::string &tableName, DistributedData::VBuckets &extend); 42 std::pair<DBStatus, uint32_t> Lock() override; 43 DBStatus UnLock() override; 44 DBStatus HeartBeat() override; 45 DBStatus Close() override; 46 std::pair<DBStatus, std::string> GetEmptyCursor(const std::string &tableName) override; 47 static DBStatus ConvertStatus(DistributedData::GeneralError error); 48 49 private: 50 static constexpr const char *TYPE_FIELD = "#_type"; 51 static constexpr const char *QUERY_FIELD = "#_query"; 52 using QueryNodes = std::vector<DistributedData::QueryNode>; 53 static std::pair<QueryNodes, DistributedData::GeneralError> ConvertQuery(DBVBucket& extend); 54 static QueryNodes ConvertQuery(DBQueryNodes&& nodes); 55 static constexpr int32_t TO_MS = 1000; // s > ms 56 std::shared_ptr<DistributedData::CloudDB> cloudDB_; 57 BindAssets* snapshots_; 58 59 void PostEvent(DistributedData::VBuckets& records, std::set<std::string>& skipAssets, 60 DistributedData::VBuckets& extend, DistributedData::AssetEvent eventId); 61 void PostEvent(DistributedData::Value& value, DataBucket& extend, std::set<std::string>& skipAssets, 62 DistributedData::AssetEvent eventId); 63 void PostEventAsset(DistributedData::Asset& asset, DataBucket& extend, std::set<std::string>& skipAssets, 64 DistributedData::AssetEvent eventId); 65 }; 66 } // namespace OHOS::DistributedRdb 67 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H 68