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