• 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 <bytes.h>
20 
21 #include <shared_mutex>
22 
23 #include "distributed_objectstore.h"
24 
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 };
54 class StatusNotifierProxy : public StatusWatcher {
55 public:
56     virtual ~StatusNotifierProxy();
57     StatusNotifierProxy(const std::shared_ptr<StatusNotifier> &notifier);
58     void OnChanged(
59         const std::string &sessionId, const std::string &networkId, const std::string &onlineStatus) override;
60 
61 private:
62     std::shared_ptr<StatusNotifier> notifier;
63 };
64 class WatcherProxy : public FlatObjectWatcher {
65 public:
66     WatcherProxy(const std::shared_ptr<ObjectWatcher> objectWatcher, const std::string &sessionId);
67     void OnChanged(const std::string &sessionid, const std::vector<std::string> &changedData) override;
68 
69 private:
70     std::shared_ptr<ObjectWatcher> objectWatcher_;
71 };
72 } // namespace OHOS::ObjectStore
73 
74 #endif // DISTRIBUTED_OBJECTSTORE_H
75