1 /*
2  * Copyright (c) 2024 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 #include "cloud/cloud_upload_recorder.h"
17 
18 namespace DistributedDB {
RecordUploadRecord(const std::string & table,const Bytes & hashKey,const CloudWaterType & type,int64_t modifyTime)19 void CloudUploadRecorder::RecordUploadRecord(const std::string &table, const Bytes &hashKey, const CloudWaterType &type,
20     int64_t modifyTime)
21 {
22     std::lock_guard<std::mutex> autoLock(recordMutex_);
23     uploadRecord_[currentUser_][table][type][hashKey] = modifyTime;
24 }
25 
IsIgnoreUploadRecord(const std::string & table,const Bytes & hashKey,const CloudWaterType & type,int64_t modifyTime) const26 bool CloudUploadRecorder::IsIgnoreUploadRecord(const std::string &table, const Bytes &hashKey,
27     const CloudWaterType &type, int64_t modifyTime) const
28 {
29     std::lock_guard<std::mutex> autoLock(recordMutex_);
30     auto userRecord = uploadRecord_.find(currentUser_);
31     if (userRecord == uploadRecord_.end()) {
32         return false;
33     }
34     auto tableRecord = userRecord->second.find(table);
35     if (tableRecord == userRecord->second.end()) {
36         return false;
37     }
38     auto typeRecord = tableRecord->second.find(type);
39     if (typeRecord == tableRecord->second.end()) {
40         return false;
41     }
42     auto it = typeRecord->second.find(hashKey);
43     return it != typeRecord->second.end() && it->second == modifyTime;
44 }
45 
ReleaseUploadRecord(const std::string & table,const CloudWaterType & type,Timestamp localWaterMark)46 void CloudUploadRecorder::ReleaseUploadRecord(const std::string &table, const CloudWaterType &type,
47     Timestamp localWaterMark)
48 {
49     std::lock_guard<std::mutex> autoLock(recordMutex_);
50     auto &records = uploadRecord_[currentUser_][table][type];
51     for (auto it = records.begin(); it != records.end();) {
52         if (it->second <= static_cast<int64_t>(localWaterMark)) {
53             it = records.erase(it);
54         } else {
55             it++;
56         }
57     }
58 }
59 
SetUser(const std::string & user)60 void CloudUploadRecorder::SetUser(const std::string &user)
61 {
62     std::lock_guard<std::mutex> autoLock(recordMutex_);
63     currentUser_ = user;
64 }
65 }