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 STORAGE_PROXY_H 17 #define STORAGE_PROXY_H 18 19 #include <atomic> 20 #include <shared_mutex> 21 22 #include "cloud/cloud_store_types.h" 23 #include "cloud/cloud_meta_data.h" 24 #include "cloud/cloud_db_constant.h" 25 #include "cloud/schema_mgr.h" 26 #include "data_transformer.h" 27 #include "icloud_sync_storage_interface.h" 28 29 30 namespace DistributedDB { 31 class StorageProxy { 32 public: 33 StorageProxy(ICloudSyncStorageInterface *iCloud); ~StorageProxy()34 virtual ~StorageProxy() {}; 35 36 static std::shared_ptr<StorageProxy> GetCloudDb(ICloudSyncStorageInterface *iCloud); 37 38 int Close(); 39 40 int GetLocalWaterMark(const std::string &tableName, Timestamp &localMark); 41 42 int PutLocalWaterMark(const std::string &tableName, Timestamp &localMark); 43 44 int GetCloudWaterMark(const std::string &tableName, std::string &cloudMark); 45 46 int SetCloudWaterMark(const std::string &tableName, std::string &cloudMark); 47 48 int StartTransaction(TransactType type = TransactType::DEFERRED); 49 50 int Commit(); 51 52 int Rollback(); 53 54 int GetUploadCount(const std::string &tableName, const Timestamp ×tamp, const bool isCloudForcePush, 55 int64_t &count); 56 57 int FillCloudGid(const CloudSyncData &data); 58 59 int GetCloudData(const std::string &tableName, const Timestamp &timeRange, 60 ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult); 61 62 int GetCloudDataNext(ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult) const; 63 64 int GetInfoByPrimaryKeyOrGid(const std::string &tableName, const VBucket &vBucket, 65 DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo); 66 67 int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData); 68 69 int CleanCloudData(ClearMode mode, const std::vector<std::string> &tableNameList, 70 const RelationalSchemaObject &localSchema, std::vector<Asset> &assets); 71 72 int CheckSchema(const TableName &tableName) const; 73 74 int CheckSchema(std::vector<std::string> &tables); 75 76 int GetPrimaryColNamesWithAssetsFields(const TableName &tableName, std::vector<std::string> &colNames, 77 std::vector<Field> &assetFields); 78 79 int NotifyChangedData(const std::string deviceName, ChangedData &&changedData); 80 81 int ReleaseContinueToken(ContinueToken &continueStmtToken); 82 83 int FillCloudAssetForDownload(const std::string &tableName, VBucket &asset, bool isDownloadSuccess); 84 85 int SetLogTriggerStatus(bool status); 86 87 int FillCloudGidAndAsset(OpType opType, const CloudSyncData &data); 88 89 std::string GetIdentify() const; 90 91 int CleanWaterMark(const TableName &tableName); 92 93 protected: 94 void Init(); 95 96 static Timestamp EraseNanoTime(Timestamp localTime); 97 private: 98 ICloudSyncStorageInterface *store_; 99 mutable std::shared_mutex storeMutex_; 100 mutable std::shared_mutex cloudDbMutex_; 101 std::atomic<bool> transactionExeFlag_; 102 std::shared_ptr<CloudMetaData> cloudMetaData_; 103 std::atomic<bool> isWrite_; 104 }; 105 } 106 107 #endif //STORAGE_PROXY_H 108