• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 DISTRIBUTED_OBJECTSTORE_IMPL_H
17 #define DISTRIBUTED_OBJECTSTORE_IMPL_H
18 
19 #include <shared_mutex>
20 
21 #include "distributed_objectstore.h"
22 namespace OHOS::ObjectStore {
23 class WatcherProxy;
24 enum SyncStatus : int8_t {
25     SYNC_START,
26     SYNCING,
27     SYNC_SUCCESS,
28     SYNC_FAIL,
29 };
30 class DistributedObjectStoreImpl : public DistributedObjectStore {
31 public:
32     DistributedObjectStoreImpl(FlatObjectStore *flatObjectStore);
33     ~DistributedObjectStoreImpl() override;
34     uint32_t Get(const std::string &sessionId, DistributedObject **object) override;
35     DistributedObject *CreateObject(const std::string &sessionId) override;
36     DistributedObject *CreateObject(const std::string &sessionId, uint32_t &status) override;
37     uint32_t DeleteObject(const std::string &sessionId) override;
38     uint32_t Watch(DistributedObject *object, std::shared_ptr<ObjectWatcher> watcher) override;
39     uint32_t UnWatch(DistributedObject *object) override;
40     uint32_t SetStatusNotifier(std::shared_ptr<StatusNotifier> notifier) override;
41     void NotifyCachedStatus(const std::string &sessionId) override;
42 
43 private:
44     DistributedObject *CacheObject(const std::string &sessionId, FlatObjectStore *flatObjectStore);
45     void RemoveCacheObject(const std::string &sessionId);
46     FlatObjectStore *flatObjectStore_ = nullptr;
47     std::map<DistributedObject *, std::shared_ptr<WatcherProxy>> watchers_;
48     std::shared_mutex dataMutex_ {};
49     std::vector<DistributedObject *> objects_ {};
50     std::mutex watchersLock_;
51 };
52 class StatusNotifierProxy : public StatusWatcher {
53 public:
54     virtual ~StatusNotifierProxy();
55     StatusNotifierProxy(const std::shared_ptr<StatusNotifier> &notifier);
56     void OnChanged(
57         const std::string &sessionId, const std::string &networkId, const std::string &onlineStatus) override;
58 
59 private:
60     std::shared_ptr<StatusNotifier> notifier;
61 };
62 class WatcherProxy : public FlatObjectWatcher {
63 public:
64     using AssetChangeCallback = std::function<void(const std::string& sessionId,
65         const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher)>;
66 
67     WatcherProxy(const std::shared_ptr<ObjectWatcher> objectWatcher, const std::string &sessionId);
68     void OnChanged(
69         const std::string &sessionId, const std::vector<std::string> &changedData, bool enableTransfer) override;
70     void SetAssetChangeCallBack(const AssetChangeCallback &assetChangeCallback);
71 private:
72     bool FindChangedAssetKey(const std::string &changedKey, std::string &assetKey);
73     std::shared_ptr<ObjectWatcher> objectWatcher_;
74     AssetChangeCallback assetChangeCallback_;
75 };
76 } // namespace OHOS::ObjectStore
77 #endif // DISTRIBUTED_OBJECTSTORE_IMPL_H
78