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