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 FLAT_OBJECT_STORAGE_ENGINE_H
17 #define FLAT_OBJECT_STORAGE_ENGINE_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <mutex>
22 #include <vector>
23 
24 #include "kv_store_delegate_manager.h"
25 #include "object_storage_engine.h"
26 
27 namespace OHOS::ObjectStore {
28 class FlatObjectStorageEngine : public ObjectStorageEngine {
29 public:
30     FlatObjectStorageEngine() = default;
31     ~FlatObjectStorageEngine() override;
32     uint32_t Open(const std::string &bundleName) override;
33     uint32_t Close() override;
34     uint32_t DeleteTable(const std::string &key) override;
35     uint32_t CreateTable(const std::string &key) override;
36     uint32_t GetTable(const std::string &key, std::map<std::string, Value> &result) override;
37     uint32_t UpdateItem(const std::string &key, const std::string &itemKey, Value &value) override;
38     uint32_t UpdateItems(const std::string &key, const std::map<std::string, std::vector<uint8_t>> &data) override;
39     uint32_t GetItem(const std::string &key, const std::string &itemKey, Value &value) override;
40     uint32_t GetItems(const std::string &key, std::map<std::string, std::vector<uint8_t>> &data) override;
41     uint32_t RegisterObserver(const std::string &key, std::shared_ptr<TableWatcher> watcher) override;
42     uint32_t UnRegisterObserver(const std::string &key) override;
43     uint32_t SetStatusNotifier(std::shared_ptr<StatusWatcher> watcher) override;
44     uint32_t SyncAllData(const std::string &sessionId, const std::vector<std::string> &deviceIds,
45         const std::function<void(const std::map<std::string, DistributedDB::DBStatus> &)> &onComplete);
46     void OnComplete(const std::string &key, const std::map<std::string, DistributedDB::DBStatus> &devices,
47         std::shared_ptr<StatusWatcher> statusWatcher);
48     bool isOpened_ = false;
49     void NotifyStatus(const std::string &sessionId, const std::string &deviceId, const std::string &status);
50     void NotifyChange(const std::string &sessionId, const std::map<std::string, std::vector<uint8_t>> &changedData);
51 private:
52     constexpr static const char *DISTRIBUTED_DATASYNC = "ohos.permission.DISTRIBUTED_DATASYNC";
53     std::mutex operationMutex_{};
54     std::mutex watcherMutex_{};
55     std::shared_ptr<DistributedDB::KvStoreDelegateManager> storeManager_;
56     std::map<std::string, DistributedDB::KvStoreNbDelegate *> delegates_;
57     std::map<std::string, std::shared_ptr<TableWatcher>> observerMap_;
58     std::shared_ptr<StatusWatcher> statusWatcher_ = nullptr;
59 };
60 } // namespace OHOS::ObjectStore
61 #endif
62