Skip to content

Commit 1b9ecbe

Browse files
author
Catalin Besleaga
committed
Bug#35963242: Add workers for JSON_STORAGE_SIZE/JSON_STORAGE_FREE to the JSON client library
Exposed get_free_space function to the json client library by getting rid of the dependency on THD and by further exposing Json_wrapper::get_free_space. Change-Id: Ia0f102ae975eb6c94351aae06e97933acf6c7ac3
1 parent 62b7dd1 commit 1b9ecbe

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

client/json_client_library_main.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,16 @@ int main() {
167167
std::cout << "ERRROR!!" << std::endl;
168168

169169
json_binary::Value v = json_binary::parse_binary(buf.ptr(), buf.length());
170+
171+
// Test call to get_free_space.
172+
size_t free_space;
173+
v.get_free_space(CoutSerializationErrorHandler(), &free_space);
174+
std::cout << "5.1. free space: " << free_space << std::endl;
175+
170176
std::string std_string;
171177
if (v.to_pretty_std_string(&std_string, CoutDefaultDepthHandler))
172178
std::cout << "ERRROR!!" << std::endl;
173-
std::cout << "5. val[" << std_string << "]" << std::endl;
179+
std::cout << "5.2. val[" << std_string << "]" << std::endl;
174180
}
175181

176182
return 0;

sql-common/json_binary.cc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,15 +1978,17 @@ bool Value::remove_in_shadow(const Field_json *field, size_t pos,
19781978
return field->table->add_binary_diff(field, m_data - original,
19791979
offset_size(m_large));
19801980
}
1981+
#endif // ifdef MYSQL_SERVER
19811982

19821983
/**
19831984
Get the amount of unused space in the binary representation of this value.
19841985
1985-
@param thd THD handle
1986+
@param[out] error_handler the handler that is invoked if an error occurs
19861987
@param[out] space the amount of free space
1987-
@return false on success, true on error
1988+
@return false on success, true if the JSON is invalid or the stack si overrun
19881989
*/
1989-
bool Value::get_free_space(const THD *thd, size_t *space) const {
1990+
bool Value::get_free_space(const JsonSerializationErrorHandler &error_handler,
1991+
size_t *space) const {
19901992
*space = 0;
19911993

19921994
switch (m_type) {
@@ -2006,7 +2008,7 @@ bool Value::get_free_space(const THD *thd, size_t *space) const {
20062008
for (size_t i = 0; i < m_element_count; ++i) {
20072009
Value key = this->key(i);
20082010
if (key.type() == ERROR) {
2009-
my_error(ER_INVALID_JSON_BINARY_DATA, MYF(0));
2011+
error_handler.InvalidJson();
20102012
return true;
20112013
}
20122014
*space += key.get_data() - next_key;
@@ -2016,7 +2018,7 @@ bool Value::get_free_space(const THD *thd, size_t *space) const {
20162018

20172019
size_t next_value_offset;
20182020
if (first_value_offset(&next_value_offset)) {
2019-
my_error(ER_INVALID_JSON_BINARY_DATA, MYF(0));
2021+
error_handler.InvalidJson();
20202022
return true;
20212023
}
20222024

@@ -2026,14 +2028,14 @@ bool Value::get_free_space(const THD *thd, size_t *space) const {
20262028
size_t elt_end;
20272029
bool inlined;
20282030
if (element_offsets(i, &elt_start, &elt_end, &inlined)) {
2029-
my_error(ER_INVALID_JSON_BINARY_DATA, MYF(0));
2031+
error_handler.InvalidJson();
20302032
return true;
20312033
}
20322034

20332035
if (inlined) continue;
20342036

20352037
if (elt_start < next_value_offset || elt_end > m_length) {
2036-
my_error(ER_INVALID_JSON_BINARY_DATA, MYF(0));
2038+
error_handler.InvalidJson();
20372039
return true;
20382040
}
20392041

@@ -2045,16 +2047,17 @@ bool Value::get_free_space(const THD *thd, size_t *space) const {
20452047
case ARRAY:
20462048
case OBJECT: {
20472049
// Recursively process nested arrays or objects.
2048-
if (check_stack_overrun(thd, STACK_MIN_SIZE, nullptr))
2050+
if (error_handler.CheckStack()) {
20492051
return true; /* purecov: inspected */
2052+
}
20502053
size_t elt_space;
2051-
if (elt.get_free_space(thd, &elt_space)) return true;
2054+
if (elt.get_free_space(error_handler, &elt_space)) return true;
20522055
*space += elt_space;
20532056
break;
20542057
}
20552058
case ERROR:
20562059
/* purecov: begin inspected */
2057-
my_error(ER_INVALID_JSON_BINARY_DATA, MYF(0));
2060+
error_handler.InvalidJson();
20582061
return true;
20592062
/* purecov: end */
20602063
default:
@@ -2066,6 +2069,7 @@ bool Value::get_free_space(const THD *thd, size_t *space) const {
20662069
return false;
20672070
}
20682071

2072+
#ifdef MYSQL_SERVER
20692073
/**
20702074
Check whether two binary JSON scalars are equal. This function is used by
20712075
multi-valued index updating code. Unlike JSON comparator implemented in
@@ -2077,7 +2081,6 @@ bool Value::get_free_space(const THD *thd, size_t *space) const {
20772081
Since MV index doesn't support indexing of arrays/objects in arrays, these
20782082
two aren't supported and cause assert.
20792083
*/
2080-
20812084
int Value::eq(const Value &val) const {
20822085
assert(is_valid() && val.is_valid());
20832086

@@ -2121,6 +2124,7 @@ int Value::eq(const Value &val) const {
21212124
}
21222125
return -1;
21232126
}
2127+
21242128
#endif // ifdef MYSQL_SERVER
21252129

21262130
bool Value::to_std_string(std::string *buffer,

sql-common/json_binary.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ class Value {
346346
EXPORT_JSON_FUNCTION
347347
bool raw_binary(const JsonSerializationErrorHandler &error_handler,
348348
String *buf) const;
349+
EXPORT_JSON_FUNCTION
350+
bool get_free_space(const JsonSerializationErrorHandler &error_handler,
351+
size_t *space) const;
349352
#ifdef MYSQL_SERVER
350-
bool get_free_space(const THD *thd, size_t *space) const;
351353
bool update_in_shadow(const Field_json *field, size_t pos,
352354
Json_wrapper *new_value, size_t data_offset,
353355
size_t data_length, const char *original,

sql-common/json_dom.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3448,17 +3448,17 @@ ulonglong Json_wrapper::make_hash_key(ulonglong hash_val) const {
34483448
return result;
34493449
}
34503450

3451-
#ifdef MYSQL_SERVER
3452-
3453-
bool Json_wrapper::get_free_space(size_t *space) const {
3451+
bool Json_wrapper::get_free_space(
3452+
const JsonSerializationErrorHandler &error_handler, size_t *space) const {
34543453
if (m_is_dom) {
34553454
*space = 0;
34563455
return false;
34573456
}
34583457

3459-
return m_value.get_free_space(current_thd, space);
3458+
return m_value.get_free_space(error_handler, space);
34603459
}
34613460

3461+
#ifdef MYSQL_SERVER
34623462
bool Json_wrapper::attempt_binary_update(const Field_json *field,
34633463
const Json_seekable_path &path,
34643464
Json_wrapper *new_value, bool replace,

sql-common/json_dom.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,11 +1702,13 @@ class Json_wrapper {
17021702
/**
17031703
Calculate the amount of unused space inside a JSON binary value.
17041704
1705+
@param[in] error_handler the handler that is invoked if an error occurs
17051706
@param[out] space the amount of unused space, or zero if this is a DOM
17061707
@return false on success
17071708
@return true if the JSON binary value was invalid
17081709
*/
1709-
bool get_free_space(size_t *space) const;
1710+
bool get_free_space(const JsonSerializationErrorHandler &error_handler,
1711+
size_t *space) const;
17101712

17111713
/**
17121714
Attempt a binary partial update by replacing the value at @a path with @a

sql/item_json_func.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3515,8 +3515,10 @@ longlong Item_func_json_storage_free::val_int() {
35153515
if (null_value) return 0;
35163516

35173517
size_t space;
3518-
if (wrapper.get_free_space(&space))
3518+
if (wrapper.get_free_space(JsonSerializationDefaultErrorHandler(current_thd),
3519+
&space)) {
35193520
return error_int(); /* purecov: inspected */
3521+
}
35203522

35213523
return space;
35223524
}

unittest/gunit/json_binary-t.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,8 @@ static void check_corrupted_binary(THD *thd, const char *data, size_t length) {
825825
*/
826826
Invalid_binary_handler handler(thd);
827827
size_t space;
828-
bool err = val.get_free_space(thd, &space);
828+
JsonSerializationDefaultErrorHandler error_handler{thd};
829+
bool err = val.get_free_space(error_handler, &space);
829830
// If it returns true, an error should have been raised.
830831
EXPECT_EQ(err, handler.is_called());
831832
}

0 commit comments

Comments
 (0)