1 /* 2 * Copyright (c) 2021 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_DB_DATA_VALUE_H 17 #define DISTRIBUTED_DB_DATA_VALUE_H 18 19 #include <cstdint> 20 #include <macro_utils.h> 21 #include <map> 22 #include <string> 23 #include <vector> 24 #include "db_types.h" 25 namespace DistributedDB { 26 class Blob { 27 public: 28 Blob(); 29 ~Blob(); 30 31 Blob(Blob &&); 32 Blob(const Blob &) = delete; 33 Blob &operator=(Blob &&) noexcept; 34 Blob &operator=(const Blob &) = delete; 35 36 const uint8_t* GetData() const; 37 uint32_t GetSize() const; 38 39 int WriteBlob(const uint8_t *ptrArray, const uint32_t &size); 40 std::vector<uint8_t> ToVector() const; 41 42 private: 43 uint8_t* ptr_; 44 uint32_t size_; 45 }; 46 47 class DataValue { 48 public: 49 DataValue(); 50 ~DataValue(); 51 52 // copy constructor 53 DataValue(const DataValue &dataValue); 54 DataValue &operator=(const DataValue &dataValue); 55 // move constructor 56 DataValue(DataValue &&dataValue) noexcept; 57 DataValue &operator=(DataValue &&dataValue) noexcept; 58 DataValue &operator=(int64_t intVal); 59 DataValue &operator=(double doubleVal); 60 DataValue &operator=(const Blob &blob); 61 DataValue &operator=(const std::string &string); 62 int Set(Blob *&blob); 63 64 // equals 65 bool operator==(const DataValue &dataValue) const; 66 bool operator!=(const DataValue &dataValue) const; 67 68 StorageType GetType() const; 69 int GetInt64(int64_t &outVal) const; 70 int GetDouble(double &outVal) const; 71 int GetBlob(Blob *&outVal) const; 72 int SetBlob(const Blob &val); 73 int GetBlob(Blob &outVal) const; 74 int SetText(const std::string &val); 75 int SetText(const uint8_t *val, uint32_t length); 76 int GetText(std::string &outVal) const; 77 void ResetValue(); 78 std::string ToString() const; 79 80 private: 81 StorageType type_ = StorageType::STORAGE_TYPE_NULL; 82 union { 83 void* zeroMem; 84 Blob* blobPtr; 85 double dValue; 86 int64_t iValue; 87 } value_{}; 88 }; 89 } 90 #endif // DISTRIBUTED_DB_DATA_VALUE_H 91