1 /*
2  * Copyright (c) 2021-2022 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_STORAGE_H
17 #define OHOS_DISTRIBUTED_DATA_STORAGE_H
18 
19 #include <atomic>
20 #include <future>
21 #include <memory>
22 #include <mutex>
23 #include <shared_mutex>
24 #include <set>
25 #include "deviceManager/dms_device_info.h"
26 #include "distributed_data_change_listener.h"
27 #include "distributed_kv_data_manager.h"
28 #include "event_handler.h"
29 #include "kvstore_death_recipient.h"
30 
31 namespace OHOS {
32 namespace DistributedSchedule {
33 class DistributedDataStorage {
34 public:
35     DistributedDataStorage();
36     ~DistributedDataStorage() = default;
37 
38     /**
39      * Init DistributedDataStorage.
40      *
41      * @return Returns true if Init successfully.
42      */
43     bool Init();
44 
45     /**
46      * Stop DistributedDataStorage.
47      *
48      * @return Returns true if Stop successfully.
49      */
50     bool Stop();
51 
52     /**
53      * Insert networkId + missionId to kvStore.
54      *
55      * @param networkId, the networkId to insert
56      * @param missionId, the missionId to insert
57      * @param byteStream, byte stream for file conversion
58      * @param len, length of the byte stream
59      * @return Returns true if Insert successfully.
60      */
61     bool Insert(const std::string& networkId, int32_t missionId, const uint8_t* byteStream, size_t len);
62 
63     /**
64      * Delete networkId + missionId in kvStore.
65      *
66      * @param networkId, the networkId to delete
67      * @param missionId, the missionId to delete
68      * @return Returns true if Delete successfully.
69      */
70     bool Delete(const std::string& networkId, int32_t missionId);
71 
72     /**
73      * FuzzyDelete networkId in kvStore.
74      *
75      * @param networkId, the networkId to delete
76      * @return Returns true if Delete successfully.
77      */
78     bool FuzzyDelete(const std::string& networkId);
79 
80     /**
81      * Query networkId + missionId in kvStore.
82      *
83      * @param networkId, the networkId to query
84      * @param missionId, the missionId to query
85      * @param value, if success return the value
86      * @return Returns true if query successfully.
87      */
88     bool Query(const std::string& networkId, int32_t missionId, DistributedKv::Value& value) const;
89 
90     void NotifyRemoteDied(const wptr<IRemoteObject>& remote);
91 
92 private:
93     bool InitKvDataService();
94     bool WaitKvDataService();
95     void InitDistributedDataStorage();
96     bool TryGetKvStore();
97     DistributedKv::Status GetKvStore();
98     void SubscribeDistributedDataStorage();
99     bool UninitDistributedDataStorage();
100     bool InsertInnerLocked(const std::string& uuid, int32_t missionId, const uint8_t* byteStream, size_t len);
101     bool DeleteInnerLocked(const std::string& uuid, int32_t missionId);
102     bool FuzzyDeleteInnerLocked(const std::string& networkId);
103     bool QueryInnerLocked(const std::string& networkId, int32_t missionId, DistributedKv::Value& value) const;
104     static void GenerateKey(const std::string& uuid, int32_t missionId, DistributedKv::Key& key);
105     static void GenerateValue(const uint8_t* byteStream, size_t len, DistributedKv::Value& value);
106     OHOS::DistributedKv::Status GetResultSatus(std::promise<OHOS::DistributedKv::Status> &resultStatusSignal) const;
107 
108     mutable std::shared_mutex initLock_;
109     std::shared_ptr<AppExecFwk::EventHandler> dmsDataStorageHandler_;
110     DistributedKv::AppId appId_;
111     DistributedKv::StoreId storeId_;
112     DistributedKv::DistributedKvDataManager dataManager_;
113     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_; // protected by initLock_
114     std::unique_ptr<DistributedDataChangeListener> distributedDataChangeListener_;
115     sptr<IRemoteObject::DeathRecipient> kvStoreDeathRecipient_;
116     int32_t waittingTime_ = 180; // 3 s
117 };
118 } // DistributedSchedule
119 } // OHOS
120 
121 #endif // OHOS_DISTRIBUTED_DATA_STORAGE_H