diff options
author | Sergei Krivonos <name@localhost.localdomain> | 2021-10-19 16:07:35 +0300 |
---|---|---|
committer | Sergei Krivonos <name@localhost.localdomain> | 2021-10-19 17:12:28 +0300 |
commit | 52f00198bc3d3fce4785b14d31b1c03bcd5f6545 (patch) | |
tree | 38e6017270437d433eae79a1e4f1bb3babe8729c | |
parent | e83a76a567fe6c5003048b6ac7385668890bdb5c (diff) | |
download | mariadb-git-10.5-MDEV-23766.tar.gz |
MDEV-23766: moving JSON writer consistency check into Json_writer10.5-MDEV-23766
-rw-r--r-- | sql/my_json_writer.cc | 30 | ||||
-rw-r--r-- | sql/my_json_writer.h | 48 |
2 files changed, 40 insertions, 38 deletions
diff --git a/sql/my_json_writer.cc b/sql/my_json_writer.cc index 8e6f0942857..15f8baa47af 100644 --- a/sql/my_json_writer.cc +++ b/sql/my_json_writer.cc @@ -18,6 +18,14 @@ #include "sql_string.h" #include "my_json_writer.h" +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS +bool Json_writer::named_item_expected() const +{ + return named_items_expectation.size() + && named_items_expectation.back(); +} +#endif + void Json_writer::append_indent() { if (!document_start) @@ -28,6 +36,9 @@ void Json_writer::append_indent() void Json_writer::start_object() { +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS + named_items_expectation.push_back(true); +#endif fmt_helper.on_start_object(); if (!element_started) @@ -42,6 +53,9 @@ void Json_writer::start_object() void Json_writer::start_array() { +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS + named_items_expectation.push_back(false); +#endif if (fmt_helper.on_start_array()) return; @@ -63,6 +77,9 @@ void Json_writer::end_object() append_indent(); first_child= false; output.append("}"); +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS + named_items_expectation.pop_back(); +#endif } @@ -74,11 +91,17 @@ void Json_writer::end_array() if (!first_child) append_indent(); output.append("]"); +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS + named_items_expectation.pop_back(); +#endif } Json_writer& Json_writer::add_member(const char *name) { +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS + DBUG_ASSERT(named_item_expected()); +#endif size_t len= strlen(name); if (fmt_helper.on_add_member(name, len)) return *this; // handled @@ -95,6 +118,9 @@ Json_writer& Json_writer::add_member(const char *name) Json_writer& Json_writer::add_member(const char *name, size_t len) { +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS + DBUG_ASSERT(named_item_expected()); +#endif if (fmt_helper.on_add_member(name, len)) return *this; // handled @@ -260,10 +286,6 @@ void Json_writer::add_str(const String &str) add_str(str.ptr(), str.length()); } -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS -thread_local std::vector<bool> Json_writer_struct::named_items_expectation; -#endif - Json_writer_temp_disable::Json_writer_temp_disable(THD *thd_arg) { thd= thd_arg; diff --git a/sql/my_json_writer.h b/sql/my_json_writer.h index 9686984ba9b..909ec349261 100644 --- a/sql/my_json_writer.h +++ b/sql/my_json_writer.h @@ -185,6 +185,14 @@ private: class Json_writer { +#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS + + std::vector<bool> named_items_expectation; + + bool named_item_expected() const; + +#endif + public: /* Add a member. We must be in an object. */ Json_writer& add_member(const char *name); @@ -312,9 +320,6 @@ public: /* A common base for Json_writer_object and Json_writer_array */ class Json_writer_struct { -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - static thread_local std::vector<bool> named_items_expectation; -#endif protected: Json_writer* my_writer; Json_value_helper context; @@ -324,35 +329,21 @@ protected: bool closed; public: - explicit Json_writer_struct(THD *thd, bool expect_named_children) + explicit Json_writer_struct(THD *thd) { my_writer= thd->opt_trace.get_current_json(); context.init(my_writer); closed= false; -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - named_items_expectation.push_back(expect_named_children); -#endif } virtual ~Json_writer_struct() { -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - named_items_expectation.pop_back(); -#endif } bool trace_started() const { return my_writer != 0; } - -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - bool named_item_expected() const - { - return named_items_expectation.size() > 1 - && *(named_items_expectation.rbegin() + 1); - } -#endif }; @@ -373,21 +364,15 @@ private: } public: explicit Json_writer_object(THD *thd) - : Json_writer_struct(thd, true) + : Json_writer_struct(thd) { -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - DBUG_ASSERT(!named_item_expected()); -#endif if (unlikely(my_writer)) my_writer->start_object(); } explicit Json_writer_object(THD* thd, const char *str) - : Json_writer_struct(thd, true) + : Json_writer_struct(thd) { -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - DBUG_ASSERT(named_item_expected()); -#endif if (unlikely(my_writer)) my_writer->add_member(str).start_object(); } @@ -552,21 +537,15 @@ class Json_writer_array : public Json_writer_struct { public: Json_writer_array(THD *thd) - : Json_writer_struct(thd, false) + : Json_writer_struct(thd) { -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - DBUG_ASSERT(!named_item_expected()); -#endif if (unlikely(my_writer)) my_writer->start_array(); } Json_writer_array(THD *thd, const char *str) - : Json_writer_struct(thd, false) + : Json_writer_struct(thd) { -#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS - DBUG_ASSERT(named_item_expected()); -#endif if (unlikely(my_writer)) my_writer->add_member(str).start_array(); } @@ -729,4 +708,5 @@ public: #endif }; + #endif |