153c3577eSopenharmony_ci/*
253c3577eSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
353c3577eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
453c3577eSopenharmony_ci * you may not use this file except in compliance with the License.
553c3577eSopenharmony_ci * You may obtain a copy of the License at
653c3577eSopenharmony_ci *
753c3577eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
853c3577eSopenharmony_ci *
953c3577eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1053c3577eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1153c3577eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1253c3577eSopenharmony_ci * See the License for the specific language governing permissions and
1353c3577eSopenharmony_ci * limitations under the License.
1453c3577eSopenharmony_ci */
1553c3577eSopenharmony_ci
1653c3577eSopenharmony_ci#ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H
1753c3577eSopenharmony_ci#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H
1853c3577eSopenharmony_ci
1953c3577eSopenharmony_ci#include <mutex>
2053c3577eSopenharmony_ci
2153c3577eSopenharmony_ci#include "cloud/cloud_db.h"
2253c3577eSopenharmony_ci#include "cloud/cloud_store_types.h"
2353c3577eSopenharmony_ci#include "cloud/icloud_db.h"
2453c3577eSopenharmony_ci#include "error/general_error.h"
2553c3577eSopenharmony_ci#include "snapshot/snapshot.h"
2653c3577eSopenharmony_ci
2753c3577eSopenharmony_cinamespace OHOS::DistributedRdb {
2853c3577eSopenharmony_ciclass RdbCloud : public DistributedDB::ICloudDb {
2953c3577eSopenharmony_cipublic:
3053c3577eSopenharmony_ci    enum FLAG : uint8_t {
3153c3577eSopenharmony_ci        SYSTEM_ABILITY = 1,
3253c3577eSopenharmony_ci        APPLICATION
3353c3577eSopenharmony_ci    };
3453c3577eSopenharmony_ci    using DBStatus = DistributedDB::DBStatus;
3553c3577eSopenharmony_ci    using DBVBucket = DistributedDB::VBucket;
3653c3577eSopenharmony_ci    using DBQueryNodes = std::vector<DistributedDB::QueryNode>;
3753c3577eSopenharmony_ci    using DataBucket = DistributedData::VBucket;
3853c3577eSopenharmony_ci    using BindAssets = DistributedData::BindAssets;
3953c3577eSopenharmony_ci    using GeneralError = DistributedData::GeneralError;
4053c3577eSopenharmony_ci
4153c3577eSopenharmony_ci    explicit RdbCloud(std::shared_ptr<DistributedData::CloudDB> cloudDB, BindAssets* bindAssets);
4253c3577eSopenharmony_ci    virtual ~RdbCloud() = default;
4353c3577eSopenharmony_ci    DBStatus BatchInsert(const std::string &tableName, std::vector<DBVBucket> &&record,
4453c3577eSopenharmony_ci        std::vector<DBVBucket> &extend) override;
4553c3577eSopenharmony_ci    DBStatus BatchUpdate(const std::string &tableName, std::vector<DBVBucket> &&record,
4653c3577eSopenharmony_ci        std::vector<DBVBucket> &extend) override;
4753c3577eSopenharmony_ci    DBStatus BatchDelete(const std::string &tableName, std::vector<DBVBucket> &extend) override;
4853c3577eSopenharmony_ci    DBStatus Query(const std::string &tableName, DBVBucket &extend, std::vector<DBVBucket> &data) override;
4953c3577eSopenharmony_ci    DistributedData::GeneralError PreSharing(const std::string &tableName, DistributedData::VBuckets &extend);
5053c3577eSopenharmony_ci    std::pair<DBStatus, uint32_t> Lock() override;
5153c3577eSopenharmony_ci    DBStatus UnLock() override;
5253c3577eSopenharmony_ci    DBStatus HeartBeat() override;
5353c3577eSopenharmony_ci    DBStatus Close() override;
5453c3577eSopenharmony_ci    std::pair<DBStatus, std::string> GetEmptyCursor(const std::string &tableName) override;
5553c3577eSopenharmony_ci    static DBStatus ConvertStatus(DistributedData::GeneralError error);
5653c3577eSopenharmony_ci    uint8_t GetLockFlag() const;
5753c3577eSopenharmony_ci    std::pair<GeneralError, uint32_t> LockCloudDB(FLAG flag);
5853c3577eSopenharmony_ci    GeneralError UnLockCloudDB(FLAG flag);
5953c3577eSopenharmony_ci    void SetPrepareTraceId(const std::string &traceId) override;
6053c3577eSopenharmony_ci
6153c3577eSopenharmony_ciprivate:
6253c3577eSopenharmony_ci    static constexpr const char *TYPE_FIELD = "#_type";
6353c3577eSopenharmony_ci    static constexpr const char *QUERY_FIELD = "#_query";
6453c3577eSopenharmony_ci    using QueryNodes = std::vector<DistributedData::QueryNode>;
6553c3577eSopenharmony_ci    static std::pair<QueryNodes, DistributedData::GeneralError> ConvertQuery(DBVBucket& extend);
6653c3577eSopenharmony_ci    static QueryNodes ConvertQuery(DBQueryNodes&& nodes);
6753c3577eSopenharmony_ci    static void ConvertErrorField(DistributedData::VBuckets& extends);
6853c3577eSopenharmony_ci    static constexpr int32_t TO_MS = 1000; // s > ms
6953c3577eSopenharmony_ci    std::shared_ptr<DistributedData::CloudDB> cloudDB_;
7053c3577eSopenharmony_ci    BindAssets* snapshots_;
7153c3577eSopenharmony_ci    uint8_t flag_ = 0;
7253c3577eSopenharmony_ci    std::mutex mutex_;
7353c3577eSopenharmony_ci
7453c3577eSopenharmony_ci    void PostEvent(DistributedData::VBuckets& records, std::set<std::string>& skipAssets,
7553c3577eSopenharmony_ci        DistributedData::VBuckets& extend, DistributedData::AssetEvent eventId);
7653c3577eSopenharmony_ci    void PostEvent(DistributedData::Value& value, DataBucket& extend, std::set<std::string>& skipAssets,
7753c3577eSopenharmony_ci        DistributedData::AssetEvent eventId);
7853c3577eSopenharmony_ci    void PostEventAsset(DistributedData::Asset& asset, DataBucket& extend, std::set<std::string>& skipAssets,
7953c3577eSopenharmony_ci        DistributedData::AssetEvent eventId);
8053c3577eSopenharmony_ci    std::pair<GeneralError, uint32_t> InnerLock(FLAG flag);
8153c3577eSopenharmony_ci    GeneralError InnerUnLock(FLAG flag);
8253c3577eSopenharmony_ci};
8353c3577eSopenharmony_ci} // namespace OHOS::DistributedRdb
8453c3577eSopenharmony_ci#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_H