MySQL 8.0.40
Source Code Documentation
json_diff.h
Go to the documentation of this file.
1#ifndef JSON_DIFF_INCLUDED
2#define JSON_DIFF_INCLUDED
3
4/* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/**
28 @file
29
30 Header file for the Json_diff class.
31
32 The Json_diff class is used to represent a logical change in a JSON column,
33 so that a replication master can send only what has changed, instead of
34 sending the whole new value to the replication slave when a JSON column is
35 updated.
36*/
37
38#include <stddef.h>
39#include <algorithm>
40#include <memory> // std::unique_ptr
41#include <vector>
42
45#include "sql/psi_memory_key.h" // key_memory_JSON
46
47class Field_json;
48class Json_dom;
49class Json_wrapper;
50class String;
51
52/// Enum that describes what kind of operation a Json_diff object represents.
54 /**
55 The JSON value in the given path is replaced with a new value.
56 It has the same effect as `JSON_REPLACE(col, path, value)`.
57 */
58 REPLACE,
59
60 /**
61 Add a new element at the given path.
62
63 If the path specifies an array element, it has the same effect as
64 `JSON_ARRAY_INSERT(col, path, value)`.
65
66 If the path specifies an object member, it has the same effect as
67 `JSON_INSERT(col, path, value)`.
68 */
69 INSERT,
70
71 /**
72 The JSON value at the given path is removed from an array or object.
73 It has the same effect as `JSON_REMOVE(col, path)`.
74 */
75 REMOVE,
76};
77/// The number of elements of the enumeration above.
78static const int JSON_DIFF_OPERATION_COUNT = 3;
79
80/**
81 A class that represents a logical change to a JSON document. It is used by
82 row-based replication to send information about changes in JSON documents
83 without sending the whole updated document.
84*/
85class Json_diff final {
86 /// The path that is changed.
88 /// The operation to perform on the changed path.
90 /// The new value to add to the changed path.
91 std::unique_ptr<Json_dom> m_value;
92
93 /// The length of the operation when encoded in binary format.
94 static const size_t ENCODED_OPERATION_BYTES = 1;
95
96 public:
97 /**
98 Construct a Json_diff object.
99
100 @param path the path that is changed
101 @param operation the operation to perform on the path
102 @param value the new value in the path (the Json_diff object
103 takes over the ownership of the value)
104 */
106 std::unique_ptr<Json_dom> value)
109 m_value(std::move(value)) {
110 for (const Json_path_leg *leg : path) m_path.append(*leg);
111 }
112
113 /// Get the path that is changed by this diff.
114 const Json_path &path() const { return m_path; }
115
116 /// Get the operation that is performed on the path.
118
119 /**
120 Get a Json_wrapper representing the new value to add to the path. The
121 wrapper is an alias, so the ownership of the contained Json_dom is retained
122 by the Json_diff object.
123 @see Json_wrapper::set_alias()
124 */
125 Json_wrapper value() const;
126
127 size_t binary_length() const;
128 /**
129 Serialize this Json_diff object and append to the given string
130
131 @param to The String to append to
132 @retval false Success
133 @retval true Failure, meaning out of memory
134 */
135 bool write_binary(String *to) const;
136};
137
138/**
139 Vector of logical diffs describing changes to a JSON column.
140*/
142 public:
143 /// Type of the allocator for the underlying invector.
145 /// Type of the underlying vector
146 typedef std::vector<Json_diff, allocator_type> vector;
147 /// Type of iterator over the underlying vector
148 typedef vector::iterator iterator;
149 /// Type of iterator over the underlying vector
150 typedef vector::const_iterator const_iterator;
151 /**
152 Constructor
153 @param arg Mem_root_allocator to use for the vector
154 */
155 explicit Json_diff_vector(allocator_type arg);
156 /**
157 Append a new diff at the end of this vector.
158 @param path Path to update
159 @param operation Operation
160 @param dom New value to insert
161 */
162 void add_diff(const Json_seekable_path &path,
163 enum_json_diff_operation operation,
164 std::unique_ptr<Json_dom> dom);
165 /**
166 Append a new diff at the end of this vector when operation == REMOVE.
167 @param path Path to update
168 @param operation Operation
169 */
170 void add_diff(const Json_seekable_path &path,
171 enum_json_diff_operation operation);
172 /// Clear the vector.
173 void clear();
174 /// Return the number of elements in the vector.
175 inline size_t size() const { return m_vector.size(); }
176
177 /**
178 Return the element at the given position
179 @param pos Position
180 @return the pos'th element
181 */
182 inline Json_diff &at(size_t pos) { return m_vector.at(pos); }
183
184 // Return forward iterator to the beginning
185 inline const_iterator begin() const { return m_vector.begin(); }
186
187 // Return forward iterator to the end
188 const_iterator end() const { return m_vector.end(); }
189
190 /**
191 Return the length of the binary representation of this
192 Json_diff_vector.
193
194 The binary format has this form:
195
196 +--------+--------+--------+ +--------+
197 | length | diff_1 | diff_2 | ... | diff_N |
198 +--------+--------+--------+ +--------+
199
200 This function returns the length of only the diffs, if
201 include_metadata==false. It returns the length of the 'length'
202 field plus the length of the diffs, if include_metadata=true. The
203 value of the 'length' field is exactly the return value from this
204 function when include_metadata=false.
205
206 @param include_metadata if true, include the length of the length
207 field in the computation, otherwise don't.
208
209 @return The computed length
210 */
211 size_t binary_length(bool include_metadata = true) const;
212
213 /**
214 Serialize this Json_diff_vector into the given String.
215
216 @param to String to which the vector will be appended
217
218 @retval false Success
219 @retval true Failure (out of memory)
220 */
221 bool write_binary(String *to) const;
222
223 /**
224 De-serialize Json_diff objects from the given String into this
225 Json_diff_vector.
226
227 @param[in,out] from Pointer to buffer to read from. The function
228 will move this to point to the next byte to read after those that
229 were read.
230
231 @param[in] table Table structure (used for error messages).
232
233 @param[in] field_name Field name (used for error messages).
234
235 @retval false Success
236 @retval true Failure (bad format or out of memory)
237 */
238 bool read_binary(const char **from, const struct TABLE *table,
239 const char *field_name);
240
241 /// An empty diff vector (having no diffs).
243
244 private:
245 // The underlying vector
247
248 /// Length in bytes of the binary representation, not counting the 4 bytes
249 /// length
251
252 /// The length of the field where the total length is encoded.
253 static const size_t ENCODED_LENGTH_BYTES = 4;
254};
255
256/**
257 The result of applying JSON diffs on a JSON value using apply_json_diffs().
258*/
260 /**
261 The JSON diffs were applied and the JSON value in the column was updated
262 successfully.
263 */
264 SUCCESS,
265
266 /**
267 An error was raised while applying one of the diffs. The value in the
268 column was not updated.
269 */
270 ERROR,
271
272 /**
273 One of the diffs was rejected. This could happen if the path specified in
274 the diff does not exist in the JSON value, or if the diff is supposed to
275 add a new value at a given path, but there already is a value at the path.
276
277 This return code would usually indicate that the replication slave where
278 the diff is applied, is out of sync with the replication master where the
279 diff was created.
280
281 The value in the column was not updated, but no error was raised.
282 */
283 REJECTED,
284};
285
286/**
287 Apply a sequence of JSON diffs to the value stored in a JSON column.
288
289 @param field the column to update
290 @param diffs the diffs to apply
291 @return an enum_json_diff_status value that tells if the diffs were
292 applied successfully
293*/
295 const Json_diff_vector *diffs);
296
297#endif /* JSON_DIFF_INCLUDED */
A field that stores a JSON value.
Definition: field.h:3987
Vector of logical diffs describing changes to a JSON column.
Definition: json_diff.h:141
std::vector< Json_diff, allocator_type > vector
Type of the underlying vector.
Definition: json_diff.h:146
size_t m_binary_length
Length in bytes of the binary representation, not counting the 4 bytes length.
Definition: json_diff.h:250
vector::iterator iterator
Type of iterator over the underlying vector.
Definition: json_diff.h:148
const_iterator end() const
Definition: json_diff.h:188
static const size_t ENCODED_LENGTH_BYTES
The length of the field where the total length is encoded.
Definition: json_diff.h:253
size_t binary_length(bool include_metadata=true) const
Return the length of the binary representation of this Json_diff_vector.
Definition: json_diff.cc:256
Mem_root_allocator< Json_diff > allocator_type
Type of the allocator for the underlying invector.
Definition: json_diff.h:144
size_t size() const
Return the number of elements in the vector.
Definition: json_diff.h:175
vector::const_iterator const_iterator
Type of iterator over the underlying vector.
Definition: json_diff.h:150
Json_diff_vector(allocator_type arg)
Constructor.
Definition: json_diff.cc:230
const_iterator begin() const
Definition: json_diff.h:185
Json_diff & at(size_t pos)
Return the element at the given position.
Definition: json_diff.h:182
vector m_vector
Definition: json_diff.h:246
bool write_binary(String *to) const
Serialize this Json_diff_vector into the given String.
Definition: json_diff.cc:260
static const Json_diff_vector EMPTY_JSON_DIFF_VECTOR
An empty diff vector (having no diffs).
Definition: json_diff.h:242
void clear()
Clear the vector.
Definition: json_diff.cc:251
bool read_binary(const char **from, const struct TABLE *table, const char *field_name)
De-serialize Json_diff objects from the given String into this Json_diff_vector.
Definition: json_diff.cc:282
void add_diff(const Json_seekable_path &path, enum_json_diff_operation operation, std::unique_ptr< Json_dom > dom)
Append a new diff at the end of this vector.
Definition: json_diff.cc:238
A class that represents a logical change to a JSON document.
Definition: json_diff.h:85
size_t binary_length() const
Definition: json_diff.cc:109
Json_wrapper value() const
Get a Json_wrapper representing the new value to add to the path.
Definition: json_diff.cc:52
static const size_t ENCODED_OPERATION_BYTES
The length of the operation when encoded in binary format.
Definition: json_diff.h:94
const Json_path & path() const
Get the path that is changed by this diff.
Definition: json_diff.h:114
std::unique_ptr< Json_dom > m_value
The new value to add to the changed path.
Definition: json_diff.h:91
Json_path m_path
The path that is changed.
Definition: json_diff.h:87
bool write_binary(String *to) const
Serialize this Json_diff object and append to the given string.
Definition: json_diff.cc:148
enum_json_diff_operation m_operation
The operation to perform on the changed path.
Definition: json_diff.h:89
enum_json_diff_operation operation() const
Get the operation that is performed on the path.
Definition: json_diff.h:117
Json_diff(const Json_seekable_path &path, enum_json_diff_operation operation, std::unique_ptr< Json_dom > value)
Construct a Json_diff object.
Definition: json_diff.h:105
JSON DOM abstract base class.
Definition: json_dom.h:173
One path leg in a JSON path expression.
Definition: json_path.h:150
A JSON path expression.
Definition: json_path.h:357
bool append(const Json_path_leg &leg)
Add a path leg to the end of this path.
Definition: json_path.h:415
A path expression which can be used to seek to a position inside a JSON value.
Definition: json_path.h:302
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1161
Mem_root_allocator is a C++ STL memory allocator based on MEM_ROOT.
Definition: mem_root_allocator.h:68
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
enum_json_diff_status apply_json_diffs(Field_json *field, const Json_diff_vector *diffs)
Apply a sequence of JSON diffs to the value stored in a JSON column.
Definition: json_diff.cc:403
enum_json_diff_status
The result of applying JSON diffs on a JSON value using apply_json_diffs().
Definition: json_diff.h:259
@ REJECTED
One of the diffs was rejected.
@ ERROR
An error was raised while applying one of the diffs.
@ SUCCESS
The JSON diffs were applied and the JSON value in the column was updated successfully.
enum_json_diff_operation
Enum that describes what kind of operation a Json_diff object represents.
Definition: json_diff.h:53
@ REPLACE
The JSON value in the given path is replaced with a new value.
@ INSERT
Add a new element at the given path.
@ REMOVE
The JSON value at the given path is removed from an array or object.
static const int JSON_DIFF_OPERATION_COUNT
The number of elements of the enumeration above.
Definition: json_diff.h:78
This file contains interface support for the JSON path abstraction.
static char * path
Definition: mysqldump.cc:137
Definition: gcs_xcom_synode.h:64
PSI_memory_key key_memory_JSON
Definition: psi_memory_key.cc:53
Definition: table.h:1399