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