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 VIRTUAL_CLOUD_DB_H 17 #define VIRTUAL_CLOUD_DB_H 18 #include <atomic> 19 #include <mutex> 20 #include "icloud_db.h" 21 22 namespace DistributedDB { 23 class VirtualCloudDb : public ICloudDb { 24 public: 25 VirtualCloudDb() = default; 26 ~VirtualCloudDb() override = default; 27 DBStatus BatchInsert(const std::string &tableName, std::vector<VBucket> &&record, 28 std::vector<VBucket> &extend) override; 29 30 DBStatus BatchInsertWithGid(const std::string &tableName, std::vector<VBucket> &&record, 31 std::vector<VBucket> &extend); 32 33 DBStatus BatchUpdate(const std::string &tableName, std::vector<VBucket> &&record, 34 std::vector<VBucket> &extend) override; 35 36 DBStatus BatchDelete(const std::string &tableName, std::vector<VBucket> &extend) override; 37 38 DBStatus Query(const std::string &tableName, VBucket &extend, std::vector<VBucket> &data) override; 39 40 DBStatus DeleteByGid(const std::string &tableName, VBucket &extend); 41 42 std::pair<DBStatus, uint32_t> Lock() override; 43 44 DBStatus UnLock() override; 45 46 DBStatus HeartBeat() override; 47 48 DBStatus Close() override; 49 50 void SetCloudError(bool cloudError); 51 52 void SetBlockTime(int32_t blockTime); 53 54 void ClearHeartbeatCount(); 55 56 int32_t GetHeartbeatCount(); 57 58 bool GetLockStatus(); 59 60 void SetHeartbeatError(bool heartbeatError); 61 62 void SetIncrementData(const std::string &tableName, const VBucket &record, const VBucket &extend); 63 64 uint32_t GetQueryTimes(const std::string &tableName); 65 66 void SetActionStatus(DBStatus status); 67 private: 68 struct CloudData { 69 VBucket record; 70 VBucket extend; 71 }; 72 73 DBStatus InnerUpdate(const std::string &tableName, std::vector<VBucket> &&record, 74 std::vector<VBucket> &extend, bool isDelete); 75 76 DBStatus UpdateCloudData(const std::string &tableName, CloudData &&cloudData); 77 78 void GetCloudData(const std::string &cursor, bool isIncreCursor, std::vector<CloudData> allData, 79 std::vector<VBucket> &data); 80 81 std::atomic<bool> cloudError_ = false; 82 std::atomic<bool> heartbeatError_ = false; 83 std::atomic<bool> lockStatus_ = false; 84 std::atomic<int32_t> blockTimeMs_ = 0; 85 std::atomic<int64_t> currentGid_ = 0; 86 std::atomic<int64_t> currentCursor_ = 1; 87 std::atomic<int32_t> queryLimit_ = 100; 88 std::atomic<int32_t> heartbeatCount_ = 0; 89 std::mutex cloudDataMutex_; 90 std::map<std::string, std::vector<CloudData>> cloudData_; 91 std::map<std::string, std::vector<CloudData>> incrementCloudData_; 92 bool isSetCrementCloudData_ = false; 93 std::string increPrefix_ = "increPrefix_"; 94 std::map<std::string, uint32_t> queryTimes_; 95 DBStatus actionStatus_ = OK; 96 }; 97 } 98 #endif // VIRTUAL_CLOUD_DB_H 99