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