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 OBJECT_STORAGE_ENGINE_H
17 #define OBJECT_STORAGE_ENGINE_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <vector>
22 
23 #include "kv_store_observer.h"
24 #include "watcher.h"
25 
26 namespace OHOS::ObjectStore {
27 using Key = std::vector<uint8_t>;
28 using Value = std::vector<uint8_t>;
29 using Field = std::vector<uint8_t>;
30 
31 class TableWatcher : public Watcher {
32 public:
TableWatcher(const std::string & sessionId)33     TableWatcher(const std::string &sessionId) : Watcher(sessionId)
34     {
35     }
36     virtual void OnChanged(
37         const std::string &sessionId, const std::vector<std::string> &changedData, bool enableTransfer) = 0;
38 };
39 
40 class StatusWatcher {
41 public:
42     virtual void OnChanged(
43         const std::string &sessionId, const std::string &networkId, const std::string &onlineStatus) = 0;
44 };
45 
46 class ObjectStorageEngine {
47 public:
48     ObjectStorageEngine(const ObjectStorageEngine &) = delete;
49     ObjectStorageEngine &operator=(const ObjectStorageEngine &) = delete;
50     ObjectStorageEngine(ObjectStorageEngine &&) = delete;
51     ObjectStorageEngine &operator=(ObjectStorageEngine &&) = delete;
52     ObjectStorageEngine() = default;
53     virtual ~ObjectStorageEngine() = default;
54     virtual uint32_t Open(const std::string &bundleName) = 0;
55     virtual uint32_t Close() = 0;
56     virtual uint32_t DeleteTable(const std::string &key) = 0;
57     virtual uint32_t CreateTable(const std::string &key) = 0;
58     virtual uint32_t GetTable(const std::string &key, std::map<std::string, Value> &result) = 0;
59     virtual uint32_t UpdateItem(const std::string &key, const std::string &itemKey, Value &value) = 0;
60     virtual uint32_t UpdateItems(const std::string &key, const std::map<std::string, std::vector<uint8_t>> &data) = 0;
61     virtual uint32_t GetItem(const std::string &key, const std::string &itemKey, Value &value) = 0;
62     virtual uint32_t GetItems(const std::string &key, std::map<std::string, std::vector<uint8_t>> &data) = 0;
63     virtual uint32_t RegisterObserver(const std::string &key, std::shared_ptr<TableWatcher> watcher) = 0;
64     virtual uint32_t UnRegisterObserver(const std::string &key) = 0;
65     virtual uint32_t SetStatusNotifier(std::shared_ptr<StatusWatcher> watcher) = 0;
66 };
67 } // namespace OHOS::ObjectStore
68 #endif