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 #include "value_object.h"
17 #include "db_errno.h"
18 #include "log_print.h"
19 
20 namespace DistributedDB {
ValueObject(const ValueObject & other)21 ValueObject::ValueObject(const ValueObject &other)
22 {
23     isValid_ = other.isValid_;
24     value_ = other.value_;
25     dataBeforeOffset_ = other.dataBeforeOffset_;
26 }
27 
operator =(const ValueObject & other)28 ValueObject& ValueObject::operator=(const ValueObject &other)
29 {
30     if (&other != this) { // LCOV_EXCL_BR_LINE
31         isValid_ = other.isValid_;
32         value_ = other.value_;
33         dataBeforeOffset_ = other.dataBeforeOffset_;
34     }
35     return *this;
36 }
37 
Parse(const std::string & inString)38 int ValueObject::Parse(const std::string &inString)
39 {
40     if (isValid_) {
41         return -E_NOT_PERMIT;
42     }
43     int errCode = value_.Parse(inString);
44     isValid_ = (errCode == E_OK);
45     return errCode;
46 }
47 
Parse(const std::vector<uint8_t> & inData)48 int ValueObject::Parse(const std::vector<uint8_t> &inData)
49 {
50     if (isValid_) {
51         return -E_NOT_PERMIT;
52     }
53     int errCode = value_.Parse(inData);
54     isValid_ = (errCode == E_OK);
55     return errCode;
56 }
57 
Parse(const uint8_t * dataBegin,const uint8_t * dataEnd,uint32_t offset)58 int ValueObject::Parse(const uint8_t *dataBegin, const uint8_t *dataEnd, uint32_t offset)
59 {
60     if (isValid_) {
61         return -E_NOT_PERMIT;
62     }
63     if (dataBegin == nullptr || dataBegin >= dataEnd || offset >= static_cast<uint64_t>(dataEnd - dataBegin)) {
64         LOGE("[Value][Parse] Data range invalid: dataEnd - dataBegin=%" PRId64 ", offset=%" PRIu32,
65             static_cast<int64_t>(dataEnd - dataBegin), offset);
66         return -E_INVALID_ARGS;
67     }
68     int errCode = value_.Parse(dataBegin + offset, dataEnd);
69     if (errCode != E_OK) {
70         return errCode;
71     }
72     dataBeforeOffset_.assign(dataBegin, dataBegin + offset);
73     isValid_ = true;
74     return E_OK;
75 }
76 
IsValid() const77 bool ValueObject::IsValid() const
78 {
79     return isValid_;
80 }
81 
ToString() const82 std::string ValueObject::ToString() const
83 {
84     if (dataBeforeOffset_.empty()) {
85         return value_.ToString();
86     }
87     // It is OK if '\0' exist in dataBeforeOffset_, when call string.size, '\0' will not disturb
88     std::string outString(dataBeforeOffset_.begin(), dataBeforeOffset_.end());
89     outString += value_.ToString();
90     return outString;
91 }
92 
WriteIntoVector(std::vector<uint8_t> & outData) const93 void ValueObject::WriteIntoVector(std::vector<uint8_t> &outData) const
94 {
95     // If not valid, valueStr and dataBeforeOffset_ will be empty
96     std::string valueStr = value_.ToString();
97     // If valid, dataBeforeOffset_ may be empty
98     outData.insert(outData.end(), dataBeforeOffset_.begin(), dataBeforeOffset_.end());
99     outData.insert(outData.end(), valueStr.begin(), valueStr.end());
100 }
101 
IsFieldPathExist(const FieldPath & inPath) const102 bool ValueObject::IsFieldPathExist(const FieldPath &inPath) const
103 {
104     return value_.IsFieldPathExist(inPath);
105 }
106 
GetFieldTypeByFieldPath(const FieldPath & inPath,FieldType & outType) const107 int ValueObject::GetFieldTypeByFieldPath(const FieldPath &inPath, FieldType &outType) const
108 {
109     if (!isValid_) { // LCOV_EXCL_BR_LINE
110         return -E_NOT_PERMIT;
111     }
112     return value_.GetFieldTypeByFieldPath(inPath, outType);
113 }
114 
GetFieldValueByFieldPath(const FieldPath & inPath,FieldValue & outValue) const115 int ValueObject::GetFieldValueByFieldPath(const FieldPath &inPath, FieldValue &outValue) const
116 {
117     if (!isValid_) { // LCOV_EXCL_BR_LINE
118         return -E_NOT_PERMIT;
119     }
120     return value_.GetFieldValueByFieldPath(inPath, outValue);
121 }
122 
GetSubFieldPath(const FieldPath & inPath,std::set<FieldPath> & outSubPath) const123 int ValueObject::GetSubFieldPath(const FieldPath &inPath, std::set<FieldPath> &outSubPath) const
124 {
125     if (!isValid_) { // LCOV_EXCL_BR_LINE
126         return -E_NOT_PERMIT;
127     }
128     return value_.GetSubFieldPath(inPath, outSubPath);
129 }
130 
GetSubFieldPath(const std::set<FieldPath> & inPath,std::set<FieldPath> & outSubPath) const131 int ValueObject::GetSubFieldPath(const std::set<FieldPath> &inPath, std::set<FieldPath> &outSubPath) const
132 {
133     if (!isValid_) { // LCOV_EXCL_BR_LINE
134         return -E_NOT_PERMIT;
135     }
136     return value_.GetSubFieldPath(inPath, outSubPath);
137 }
138 
GetSubFieldPathAndType(const FieldPath & inPath,std::map<FieldPath,FieldType> & outSubPathType) const139 int ValueObject::GetSubFieldPathAndType(const FieldPath &inPath, std::map<FieldPath, FieldType> &outSubPathType) const
140 {
141     if (!isValid_) { // LCOV_EXCL_BR_LINE
142         return -E_NOT_PERMIT;
143     }
144     return value_.GetSubFieldPathAndType(inPath, outSubPathType);
145 }
146 
GetSubFieldPathAndType(const std::set<FieldPath> & inPath,std::map<FieldPath,FieldType> & outSubPathType) const147 int ValueObject::GetSubFieldPathAndType(const std::set<FieldPath> &inPath,
148     std::map<FieldPath, FieldType> &outSubPathType) const
149 {
150     if (!isValid_) { // LCOV_EXCL_BR_LINE
151         return -E_NOT_PERMIT;
152     }
153     return value_.GetSubFieldPathAndType(inPath, outSubPathType);
154 }
155 
InsertField(const FieldPath & inPath,FieldType inType,const FieldValue & inValue)156 int ValueObject::InsertField(const FieldPath &inPath, FieldType inType, const FieldValue &inValue)
157 {
158     int errCode = value_.InsertField(inPath, inType, inValue);
159     if (errCode == E_OK) {
160         isValid_ = true;
161     }
162     return errCode;
163 }
164 
DeleteField(const FieldPath & inPath)165 int ValueObject::DeleteField(const FieldPath &inPath)
166 {
167     if (!isValid_) { // LCOV_EXCL_BR_LINE
168         return -E_NOT_PERMIT;
169     }
170     return value_.DeleteField(inPath);
171 }
172 } // namespace DistributedDB
173