Skip to content

Commit 052dda6

Browse files
author
Sergei Krivonos
committed
Made optional Json_writer_object / Json_writer_array consistency check
1 parent cf8e78a commit 052dda6

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ IF(DISABLE_SHARED)
185185
SET(WITHOUT_DYNAMIC_PLUGINS 1)
186186
ENDIF()
187187
OPTION(ENABLED_PROFILING "Enable profiling" ON)
188+
OPTION(ENABLED_JSON_WRITER_CONSISTENCY_CHECKS "Enable Json_writer_object / Json_writer_array checking to produce consistent JSON output" OFF)
189+
IF(ENABLED_JSON_WRITER_CONSISTENCY_CHECKS)
190+
ADD_DEFINITIONS(-DENABLED_JSON_WRITER_CONSISTENCY_CHECKS)
191+
ENDIF()
188192
OPTION(WITHOUT_SERVER "Build only the client library and clients" OFF)
189193
IF(UNIX)
190194
OPTION(WITH_VALGRIND "Valgrind instrumentation" OFF)

sql/my_json_writer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ void Json_writer::add_str(const String &str)
260260
add_str(str.ptr(), str.length());
261261
}
262262

263+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
263264
thread_local std::vector<bool> Json_writer_struct::named_items_expectation;
265+
#endif
264266

265267
Json_writer_temp_disable::Json_writer_temp_disable(THD *thd_arg)
266268
{

sql/my_json_writer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ class Json_value_helper
312312
/* A common base for Json_writer_object and Json_writer_array */
313313
class Json_writer_struct
314314
{
315+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
315316
static thread_local std::vector<bool> named_items_expectation;
317+
#endif
316318
protected:
317319
Json_writer* my_writer;
318320
Json_value_helper context;
@@ -327,24 +329,30 @@ class Json_writer_struct
327329
my_writer= thd->opt_trace.get_current_json();
328330
context.init(my_writer);
329331
closed= false;
332+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
330333
named_items_expectation.push_back(expect_named_children);
334+
#endif
331335
}
332336

333337
virtual ~Json_writer_struct()
334338
{
339+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
335340
named_items_expectation.pop_back();
341+
#endif
336342
}
337343

338344
bool trace_started() const
339345
{
340346
return my_writer != 0;
341347
}
342348

349+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
343350
bool named_item_expected() const
344351
{
345352
return named_items_expectation.size() > 1
346353
&& *(named_items_expectation.rbegin() + 1);
347354
}
355+
#endif
348356
};
349357

350358

@@ -367,15 +375,19 @@ class Json_writer_object : public Json_writer_struct
367375
explicit Json_writer_object(THD *thd)
368376
: Json_writer_struct(thd, true)
369377
{
378+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
370379
DBUG_ASSERT(!named_item_expected());
380+
#endif
371381
if (unlikely(my_writer))
372382
my_writer->start_object();
373383
}
374384

375385
explicit Json_writer_object(THD* thd, const char *str)
376386
: Json_writer_struct(thd, true)
377387
{
388+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
378389
DBUG_ASSERT(named_item_expected());
390+
#endif
379391
if (unlikely(my_writer))
380392
my_writer->add_member(str).start_object();
381393
}
@@ -542,15 +554,19 @@ class Json_writer_array : public Json_writer_struct
542554
Json_writer_array(THD *thd)
543555
: Json_writer_struct(thd, false)
544556
{
557+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
545558
DBUG_ASSERT(!named_item_expected());
559+
#endif
546560
if (unlikely(my_writer))
547561
my_writer->start_array();
548562
}
549563

550564
Json_writer_array(THD *thd, const char *str)
551565
: Json_writer_struct(thd, false)
552566
{
567+
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
553568
DBUG_ASSERT(named_item_expected());
569+
#endif
554570
if (unlikely(my_writer))
555571
my_writer->add_member(str).start_array();
556572
}

0 commit comments

Comments
 (0)