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 #ifndef OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_AUTO_SYNC_MATRIX_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_AUTO_SYNC_MATRIX_H
18 
19 #include <bitset>
20 #include <map>
21 #include <mutex>
22 #include "metadata/store_meta_data.h"
23 
24 namespace OHOS::DistributedData {
25 class API_EXPORT AutoSyncMatrix {
26 public:
27     static AutoSyncMatrix &GetInstance();
28     void Initialize();
29     void Online(const std::string &device);
30     void Offline(const std::string &device);
31     void OnChanged(const StoreMetaData &metaData);
32     void OnExchanged(const std::string &device, const StoreMetaData &metaData);
33     std::vector<StoreMetaData> GetChangedStore(const std::string &device);
34 
35 private:
36     static constexpr uint32_t MAX_SIZE = 128;
37     struct Mask {
38         std::bitset<MAX_SIZE> data;
39         void Delete(size_t pos);
40         void Set(size_t pos);
41         void Reset(size_t pos);
42         void Init(size_t size);
43     };
44     enum DataType {
45         STATICS = 0,
46         DYNAMICAL,
47     };
48 
49     AutoSyncMatrix();
50     ~AutoSyncMatrix();
51     AutoSyncMatrix(const AutoSyncMatrix &) = delete;
52     AutoSyncMatrix(AutoSyncMatrix &&) noexcept = delete;
53     AutoSyncMatrix &operator=(const AutoSyncMatrix &) = delete;
54     AutoSyncMatrix &operator=(AutoSyncMatrix &&) noexcept = delete;
55     bool IsAutoSync(const StoreMetaData &meta);
56     void AddStore(const StoreMetaData &meta);
57     void DelStore(const StoreMetaData &meta);
58     void UpdateStore(const StoreMetaData &meta);
59 
60     std::mutex mutex_;
61     std::map<std::string, Mask> onlines_;
62     std::map<std::string, Mask> offlines_;
63     std::vector<StoreMetaData> metas_;
64 };
65 } // namespace OHOS::DistributedData
66 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_AUTO_SYNC_MATRIX_H