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 CLOUD_META_DATA_H
17 #define CLOUD_META_DATA_H
18 
19 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "cloud/cloud_store_types.h"
24 #include "cloud/cloud_db_types.h"
25 #include "db_types.h"
26 #include "icloud_sync_storage_interface.h"
27 #include "types_export.h"
28 
29 namespace DistributedDB {
30 
31 class CloudMetaData {
32 public:
33     explicit CloudMetaData(ICloudSyncStorageInterface *store);
34     ~CloudMetaData() = default;
35 
36     int GetLocalWaterMark(const TableName &tableName, Timestamp &localMark);
37     int GetLocalWaterMarkByType(const TableName &tableName, CloudWaterType type, Timestamp &localMark);
38     int GetCloudWaterMark(const TableName &tableName, std::string &cloudMark);
39 
40     int SetLocalWaterMark(const TableName &tableName, Timestamp localMark);
41     int SetLocalWaterMarkByType(const TableName &tableName, CloudWaterType type, Timestamp localMark);
42     int SetCloudWaterMark(const TableName &tableName, std::string &cloudMark);
43 
44     int CleanWaterMark(const TableName &tableName);
45 
46     void CleanAllWaterMark();
47 
48     void CleanWaterMarkInMemory(const TableName &tableName);
49 
50 private:
51     typedef struct CloudMetaValue {
52         Timestamp localMark = 0u;
53         Timestamp insertLocalMark = 0u;
54         Timestamp updateLocalMark = 0u;
55         Timestamp deleteLocalMark = 0u;
56         std::string cloudMark;
57     } CloudMetaValue;
58 
59     int ReadMarkFromMeta(const TableName &tableName);
60     int WriteMarkToMeta(const TableName &tableName, Timestamp localmark, std::string &cloudMark);
61     int WriteTypeMarkToMeta(const TableName &tableName, CloudMetaValue &cloudMetaValue);
62     int SerializeWaterMark(CloudMetaValue &cloudMetaValue, Value &blobMetaVal);
63     int DeserializeMark(Value &blobMark, CloudMetaValue &cloudMetaValue);
64     uint64_t GetParcelCurrentLength(CloudMetaValue &cloudMetaValue);
65 
66     mutable std::mutex cloudMetaMutex_;
67     std::unordered_map<TableName, CloudMetaValue> cloudMetaVals_;
68     ICloudSyncStorageInterface *store_ = nullptr;
69 };
70 
71 } // namespace DistributedDB
72 #endif // CLOUD_META_DATA_H
73