|
14 | 14 | // KIND, either express or implied. See the License for the |
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | | - |
18 | | -#ifndef DORIS_BE_RUNTIME_JSON_VALUE_H |
19 | | -#define DORIS_BE_RUNTIME_JSON_VALUE_H |
20 | | - |
21 | | -#include <glog/logging.h> |
22 | | - |
| 17 | +#pragma once |
23 | 18 | #include <cstddef> |
24 | | -#include <ostream> |
25 | 19 | #include <string> |
26 | 20 |
|
27 | 21 | #include "common/status.h" |
28 | | -#include "util/hash_util.hpp" |
29 | 22 | #include "util/jsonb_parser_simd.h" |
| 23 | +#include "util/jsonb_utils.h" |
30 | 24 |
|
31 | 25 | namespace doris { |
| 26 | +#include "common/compile_check_begin.h" |
| 27 | + |
| 28 | +// JsonBinaryValue wraps a Doris jsonb object. |
| 29 | +// The jsonb object is written using JsonbWriter. |
| 30 | +// JsonBinaryValue is non-movable and non-copyable; it is only a simple wrapper. |
| 31 | +// To parse a string to a jsonb object, use it like this: |
| 32 | +// JsonBinaryValue jsonb_value; |
| 33 | +// RETURN_IF_ERROR(jsonb_value.from_json_string(slice.data, slice.size)); |
| 34 | +// insert_data(jsonb_value.value(), jsonb_value.size()); |
| 35 | +// insert_data should use copy semantics. |
| 36 | +// |
| 37 | +// from_json_string can be called multiple times. |
| 38 | +// Example: |
| 39 | +// JsonBinaryValue jsonb_value; |
| 40 | +// for (;;) { |
| 41 | +// RETURN_IF_ERROR(jsonb_value.from_json_string(slice.data, slice.size)); |
| 42 | +// insert_data(jsonb_value.value(), jsonb_value.size()); |
| 43 | +// } |
32 | 44 |
|
33 | | -struct JsonBinaryValue { |
34 | | - static const int MAX_LENGTH = (1 << 30); |
35 | | - |
36 | | - // default nullprt and size 0 for invalid or NULL value |
37 | | - const char* ptr = nullptr; |
38 | | - size_t len = 0; |
39 | | - JsonbParser parser; |
| 45 | +struct JsonBinaryValue final { |
| 46 | + static constexpr int MAX_LENGTH = (1 << 30); |
40 | 47 |
|
41 | 48 | JsonBinaryValue() = default; |
| 49 | + JsonBinaryValue(const JsonBinaryValue&) = delete; |
| 50 | + JsonBinaryValue& operator=(const JsonBinaryValue&) = delete; |
| 51 | + JsonBinaryValue(JsonBinaryValue&&) = delete; |
| 52 | + JsonBinaryValue& operator=(JsonBinaryValue&&) = delete; |
42 | 53 |
|
43 | 54 | const char* value() const { return ptr; } |
44 | 55 |
|
45 | 56 | size_t size() const { return len; } |
46 | 57 |
|
47 | | - void replace(const char* ptr, int len) { |
48 | | - this->ptr = ptr; |
49 | | - this->len = len; |
50 | | - } |
51 | | - |
52 | | - bool operator==(const JsonBinaryValue& other) const { |
53 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
54 | | - } |
55 | | - // != |
56 | | - bool ne(const JsonBinaryValue& other) const { |
57 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
58 | | - } |
59 | | - // <= |
60 | | - bool le(const JsonBinaryValue& other) const { |
61 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
62 | | - } |
63 | | - // >= |
64 | | - bool ge(const JsonBinaryValue& other) const { |
65 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
66 | | - } |
67 | | - // < |
68 | | - bool lt(const JsonBinaryValue& other) const { |
69 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
70 | | - } |
71 | | - // > |
72 | | - bool gt(const JsonBinaryValue& other) const { |
73 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
74 | | - } |
75 | | - |
76 | | - bool operator!=(const JsonBinaryValue& other) const { |
77 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
78 | | - } |
79 | | - |
80 | | - bool operator<=(const JsonBinaryValue& other) const { |
81 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
82 | | - } |
83 | | - |
84 | | - bool operator>=(const JsonBinaryValue& other) const { |
85 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
86 | | - } |
87 | | - |
88 | | - bool operator<(const JsonBinaryValue& other) const { |
89 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
90 | | - } |
91 | | - |
92 | | - bool operator>(const JsonBinaryValue& other) const { |
93 | | - throw Exception(Status::FatalError("comparing between JsonBinaryValue is not supported")); |
94 | | - } |
95 | | - |
96 | | - Status init_from_json_string(const char* s, size_t len); |
97 | | - |
98 | | - Status init_from_json_string(const std::string& s) { |
99 | | - return init_from_json_string(s.c_str(), s.size()); |
| 58 | + Status from_json_string(const std::string& s) { return from_json_string(s.data(), s.length()); } |
| 59 | + Status from_json_string(const char* s, size_t length) { |
| 60 | + // reset all fields |
| 61 | + ptr = nullptr; |
| 62 | + len = 0; |
| 63 | + writer.reset(); |
| 64 | + RETURN_IF_ERROR(JsonbParser::parse(s, length, writer)); |
| 65 | + ptr = writer.getOutput()->getBuffer(); |
| 66 | + len = writer.getOutput()->getSize(); |
| 67 | + if (len > MAX_LENGTH) { |
| 68 | + return Status::InternalError( |
| 69 | + "Jsonb value length {} exceeds maximum allowed length of {} bytes", len, |
| 70 | + MAX_LENGTH); |
| 71 | + } |
| 72 | + return Status::OK(); |
100 | 73 | } |
101 | 74 |
|
102 | | - std::string to_json_string() const; |
| 75 | + std::string to_json_string() const { return JsonbToJson::jsonb_to_json_string(ptr, len); } |
103 | 76 |
|
104 | | - struct HashOfJsonBinaryValue { |
105 | | - size_t operator()(const JsonBinaryValue& v) const { |
106 | | - return HashUtil::hash(v.ptr, v.len, 0); |
107 | | - } |
108 | | - }; |
| 77 | +private: |
| 78 | + // default nullprt and size 0 for invalid or NULL value |
| 79 | + const char* ptr = nullptr; |
| 80 | + size_t len = 0; |
| 81 | + JsonbWriter writer; |
109 | 82 | }; |
110 | 83 |
|
111 | | -// This function must be called 'hash_value' to be picked up by boost. |
112 | | -inline std::size_t hash_value(const JsonBinaryValue& v) { |
113 | | - return HashUtil::hash(v.ptr, v.len, 0); |
114 | | -} |
115 | | - |
116 | | -std::ostream& operator<<(std::ostream& os, const JsonBinaryValue& json_value); |
117 | | - |
118 | | -std::size_t operator-(const JsonBinaryValue& v1, const JsonBinaryValue& v2); |
119 | | - |
| 84 | +#include "common/compile_check_end.h" |
120 | 85 | } // namespace doris |
121 | | - |
122 | | -#endif |
0 commit comments