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_OBJECT_H
17 #define DISTRIBUTED_OBJECT_H
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 #include "object_types.h"
23 
24 namespace OHOS::ObjectStore {
25 enum Type : uint8_t {
26     TYPE_STRING = 0,
27     TYPE_BOOLEAN,
28     TYPE_DOUBLE,
29     TYPE_COMPLEX,
30 };
31 class DistributedObject {
32 public:
~DistributedObject()33     virtual ~DistributedObject(){};
34 
35     /**
36      * @brief Put or update the data whose value type is double into the database, which means that the data of
37      * objects in the same sessionId is put or updated.
38      *
39      * @param key Indicates the key of key-value data to put or update.
40      * @param value Indicates the value of key-value data to put or update.
41      *
42      * @return Returns 0 for success, others for failure.
43      */
44     virtual uint32_t PutDouble(const std::string &key, double value) = 0;
45 
46     /**
47      * @brief Put or update the data whose value type is bool into the database, which means that the data of
48      * objects in the same sessionId is put or updated.
49      *
50      * @param key Indicates the key of key-value data to put or update.
51      * @param value Indicates the value of key-value data to put or update.
52      *
53      * @return Returns 0 for success, others for failure.
54      */
55     virtual uint32_t PutBoolean(const std::string &key, bool value) = 0;
56 
57     /**
58      * @brief Put or update the data whose value type is string into the database, which means that the data of
59      * objects in the same sessionId is put or updated.
60      *
61      * @param key Indicates the key of key-value data to put or update.
62      * @param value Indicates the value of key-value data to put or update.
63      *
64      * @return Returns 0 for success, others for failure.
65      */
66     virtual uint32_t PutString(const std::string &key, const std::string &value) = 0;
67 
68     /**
69      * @brief Put or update the data whose value type is bytes stream into the database, which means that the data of
70      * objects in the same sessionId is put or updated.
71      *
72      * @param key Indicates the key of key-value data to put or update.
73      * @param value Indicates the value of key-value data to put or update.
74      *
75      * @return Returns 0 for success, others for failure.
76      */
77     virtual uint32_t PutComplex(const std::string &key, const std::vector<uint8_t> &value) = 0;
78 
79     /**
80      * @brief Get the data whose value type is double from the database according to the key,
81      * which means that the data of objects in the same sessionId is get.
82      *
83      * @param key Indicates the key of key-value data to put or update.
84      * @param value Indicates the value of key-value data to put or update.
85      *
86      * @return Returns 0 for success, others for failure.
87      */
88     virtual uint32_t GetDouble(const std::string &key, double &value) = 0;
89 
90     /**
91      * @brief Get the data whose value type is bool from the database according to the key,
92      * which means that the data of objects in the same sessionId is get.
93      *
94      * @param key Indicates the key of key-value data to get.
95      * @param value Indicates the value of key-value data to get.
96      *
97      * @return Returns 0 for success, others for failure.
98      */
99     virtual uint32_t GetBoolean(const std::string &key, bool &value) = 0;
100 
101     /**
102      * @brief Get the data whose value type is string from the database according to the key,
103      * which means that the data of objects in the same sessionId is get.
104      *
105      * @param key Indicates the key of key-value data to put or update.
106      * @param value Indicates the value of key-value data to put or update.
107      *
108      * @return Returns 0 for success, others for failure.
109      */
110     virtual uint32_t GetString(const std::string &key, std::string &value) = 0;
111 
112     /**
113      * @brief Get the data whose value type is complex from the database according to the key,
114      * which means that the data of objects in the same sessionId is get.
115      *
116      * @param key Indicates the key of key-value data to put or update.
117      * @param value Indicates the value of key-value data to put or update.
118      *
119      * @return Returns 0 for success, others for failure.
120      */
121     virtual uint32_t GetComplex(const std::string &key, std::vector<uint8_t> &value) = 0;
122 
123     /**
124      * @brief Get the value type of key-value data by the key
125      *
126      * @param key Indicates the key of key-value data.
127      * @param value Indicates the value of key-value data.
128      *
129      * @return Returns 0 for success, others for failure.
130      */
131     virtual uint32_t GetType(const std::string &key, Type &type) = 0;
132 
133     /**
134      * @brief Save the data to local device.
135      *
136      * @param deviceId Indicates the device Id.
137      *
138      * @return Returns 0 for success, others for failure.
139      */
140     virtual uint32_t Save(const std::string &deviceId) = 0;
141 
142     /**
143      * @brief Revoke save data.
144      *
145      * @return Returns 0 for success, others for failure.
146      */
147     virtual uint32_t RevokeSave() = 0;
148 
149     /**
150      * @brief Get the sessionId of the object.
151      *
152      * @return Returns sessionId of the object.
153      */
154     virtual std::string &GetSessionId() = 0;
155 
156     /**
157      * @brief Bind Asset.
158      *
159      * @param assetKey Indicates the assetKey key.
160      * @param bindInfo Indicates asset info.
161      *
162      * @return Returns 0 for success, others for failure.
163      */
164     virtual uint32_t BindAssetStore(const std::string &assetKey, AssetBindInfo &bindInfo) = 0;
165 };
166 
167 class ObjectWatcher {
168 public:
169     virtual void OnChanged(const std::string &sessionid, const std::vector<std::string> &changedData) = 0;
170 };
171 } // namespace OHOS::ObjectStore
172 #endif // DISTRIBUTED_OBJECT_H
173