/root/doris/be/src/runtime/jsonb_value.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | #pragma once |
18 | | #include <cstddef> |
19 | | #include <string> |
20 | | |
21 | | #include "common/status.h" |
22 | | #include "util/jsonb_parser_simd.h" |
23 | | #include "util/jsonb_utils.h" |
24 | | |
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 | | // } |
44 | | |
45 | | struct JsonBinaryValue final { |
46 | | static constexpr int MAX_LENGTH = (1 << 30); |
47 | | |
48 | 1.33k | JsonBinaryValue() = default; |
49 | | JsonBinaryValue(const JsonBinaryValue&) = delete; |
50 | | JsonBinaryValue& operator=(const JsonBinaryValue&) = delete; |
51 | | JsonBinaryValue(JsonBinaryValue&&) = delete; |
52 | | JsonBinaryValue& operator=(JsonBinaryValue&&) = delete; |
53 | | |
54 | 1.42k | const char* value() const { return ptr; } |
55 | | |
56 | 1.42k | size_t size() const { return len; } |
57 | | |
58 | 1.20k | Status from_json_string(const std::string& s) { return from_json_string(s.data(), s.length()); } |
59 | 1.53k | Status from_json_string(const char* s, size_t length) { |
60 | | // reset all fields |
61 | 1.53k | ptr = nullptr; |
62 | 1.53k | len = 0; |
63 | 1.53k | writer.reset(); |
64 | 1.53k | RETURN_IF_ERROR(JsonbParser::parse(s, length, writer)); |
65 | 1.42k | ptr = writer.getOutput()->getBuffer(); |
66 | 1.42k | len = writer.getOutput()->getSize(); |
67 | 1.42k | if (len > MAX_LENGTH) { |
68 | 0 | return Status::InternalError( |
69 | 0 | "Jsonb value length {} exceeds maximum allowed length of {} bytes", len, |
70 | 0 | MAX_LENGTH); |
71 | 0 | } |
72 | 1.42k | return Status::OK(); |
73 | 1.42k | } |
74 | | |
75 | 0 | std::string to_json_string() const { return JsonbToJson::jsonb_to_json_string(ptr, len); } |
76 | | |
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; |
82 | | }; |
83 | | |
84 | | #include "common/compile_check_end.h" |
85 | | } // namespace doris |