summaryrefslogtreecommitdiffstats
path: root/sql/my_json_writer.h
blob: 87d1a7facf15fa89844a3ea46a71e912ec2bf3d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/* Copyright (C) 2014 SkySQL Ab, MariaDB Corporation Ab

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

#ifndef JSON_WRITER_INCLUDED
#define JSON_WRITER_INCLUDED

#include "my_base.h"
#include "sql_string.h"

#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST) || defined ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
#include <set>
#include <stack>
#include <string>
#include <vector>
#endif

#ifdef JSON_WRITER_UNIT_TEST
// Also, mock objects are defined in my_json_writer-t.cc
#define VALIDITY_ASSERT(x) if (!(x)) this->invalid_json= true;
#else
#include "sql_class.h"  // For class THD
#include "log.h" // for sql_print_error
#define VALIDITY_ASSERT(x) DBUG_ASSERT(x)
#endif

#include <type_traits>

class Opt_trace_stmt;
class Opt_trace_context;
class Json_writer;

struct TABLE;
struct st_join_table;
using JOIN_TAB= struct st_join_table;

/*
  Single_line_formatting_helper is used by Json_writer to do better formatting
  of JSON documents. 

  The idea is to catch arrays that can be printed on one line:

    arrayName : [ "boo", 123, 456 ] 

  and actually print them on one line. Arrrays that occupy too much space on
  the line, or have nested members cannot be printed on one line.
  
  We hook into JSON printing functions and try to detect the pattern. While
  detecting the pattern, we will accumulate "boo", 123, 456 as strings.

  Then, 
   - either the pattern is broken, and we print the elements out, 
   - or the pattern lasts till the end of the array, and we print the 
     array on one line.
*/

class Single_line_formatting_helper
{
  enum enum_state
  {
    INACTIVE,
    ADD_MEMBER,
    IN_ARRAY,
    DISABLED
  };

  /*
    This works like a finite automaton. 

    state=DISABLED means the helper is disabled - all on_XXX functions will
    return false (which means "not handled") and do nothing.

                                      +->-+
                                      |   v
       INACTIVE ---> ADD_MEMBER ---> IN_ARRAY--->-+
          ^                                       |
          +------------------<--------------------+
                              
    For other states: 
    INACTIVE    - initial state, we have nothing.
    ADD_MEMBER  - add_member() was called, the buffer has "member_name\0".
    IN_ARRAY    - start_array() was called.


  */
  enum enum_state state;
  enum { MAX_LINE_LEN= 80 };
  char buffer[80];

  /* The data in the buffer is located between buffer[0] and buf_ptr */
  char *buf_ptr;
  uint line_len;

  Json_writer *owner;
public:
  Single_line_formatting_helper() : state(INACTIVE), buf_ptr(buffer) {}

  void init(Json_writer *owner_arg) { owner= owner_arg; }

  bool on_add_member(const char *name, size_t len);

  bool on_start_array();
  bool on_end_array();
  void on_start_object();
  // on_end_object() is not needed.

  bool on_add_str(const char *str, size_t num_bytes);

  /*
    Returns true if the helper is flushing its buffer and is probably
    making calls back to its Json_writer. (The Json_writer uses this
    function to avoid re-doing the processing that it has already done
    before making a call to fmt_helper)
  */
  bool is_making_writer_calls() const { return state == DISABLED; }

private:
  void flush_on_one_line();
  void disable_and_flush();
};


/*
  Something that looks like class String, but has an internal limit of
  how many bytes one can append to it.

  Bytes that were truncated due to the size limitation are counted.
*/

class String_with_limit
{
public:

  String_with_limit() : size_limit(SIZE_T_MAX), truncated_len(0)
  {
    str.length(0);
  }

  size_t get_truncated_bytes() const { return truncated_len; }
  size_t get_size_limit() { return size_limit; }

  void set_size_limit(size_t limit_arg)
  {
    // Setting size limit to be shorter than length will not have the desired
    // effect
    DBUG_ASSERT(str.length() < size_limit);
    size_limit= limit_arg;
  }

  void append(const char *s, size_t size)
  {
    if (str.length() + size <= size_limit)
    {
      // Whole string can be added, just do it
      str.append(s, size);
    }
    else
    {
      // We cannot add the whole string
      if (str.length() < size_limit)
      {
        // But we can still add something
        size_t bytes_to_add = size_limit - str.length();
        str.append(s, bytes_to_add);
        truncated_len += size - bytes_to_add;
      }
      else
        truncated_len += size;
    }
  }

  void append(const char *s)
  {
    append(s, strlen(s));
  }

  void append(char c)
  {
    if (str.length() + 1 > size_limit)
      truncated_len++;
    else
      str.append(c);
  }

  const String *get_string() { return &str; }
  size_t length() { return str.length(); }
private:
  String str;

  // str must not get longer than this many bytes.
  size_t size_limit;

  // How many bytes were truncated from the string
  size_t truncated_len;
};

/*
  A class to write well-formed JSON documents. The documents are also formatted
  for human readability.
*/

class Json_writer
{
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
  /*
    In debug mode, Json_writer will fail and assertion if one attempts to
    produce an invalid JSON document (e.g. JSON array having named elements).
  */
  std::vector<bool> named_items_expectation;
  std::stack<std::set<std::string> > named_items;

  bool named_item_expected() const;

  bool got_name;

#ifdef JSON_WRITER_UNIT_TEST
public:
  // When compiled for unit test, creating invalid JSON will set this to true
  // instead of an assertion.
  bool invalid_json= false;
#endif
#endif

public:
  /* Add a member. We must be in an object. */
  Json_writer& add_member(const char *name);
  Json_writer& add_member(const char *name, size_t len);
  
  /* Add atomic values */

  /* Note: the add_str methods do not do escapes. Should this change? */
  void add_str(const char* val);
  void add_str(const char* val, size_t num_bytes);
  void add_str(const String &str);
  void add_str(Item *item);
  void add_table_name(const JOIN_TAB *tab);
  void add_table_name(const TABLE* table);

  void add_ll(longlong val);
  void add_ull(ulonglong val);
  void add_size(longlong val);
  void add_double(double val);
  void add_bool(bool val);
  void add_null();

private:
  void add_unquoted_str(const char* val);
  void add_unquoted_str(const char* val, size_t len);

  bool on_add_str(const char *str, size_t num_bytes);
  void on_start_object();

public:
  /* Start a child object */
  void start_object();
  void start_array();

  void end_object();
  void end_array();
  
  /*
    One can set a limit of how large a JSON document should be.
    Writes beyond that size will be counted, but will not be collected.
  */
  void set_size_limit(size_t mem_size) { output.set_size_limit(mem_size); }

  size_t get_truncated_bytes() { return output.get_truncated_bytes(); }

  Json_writer() : 
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
    got_name(false),
#endif
    indent_level(0), document_start(true), element_started(false), 
    first_child(true)
  {
    fmt_helper.init(this);
  }
private:
  // TODO: a stack of (name, bool is_object_or_array) elements.
  int indent_level;
  enum { INDENT_SIZE = 2 };

  friend class Single_line_formatting_helper;
  friend class Json_writer_nesting_guard;
  bool document_start;
  bool element_started;
  bool first_child;

  Single_line_formatting_helper fmt_helper;

  void append_indent();
  void start_element();
  void start_sub_element();

public:
  String_with_limit output;
};

/* A class to add values to Json_writer_object and Json_writer_array */
class Json_value_helper
{
  Json_writer* writer;

public:
  void init(Json_writer *my_writer) { writer= my_writer; }
  void add_str(const char* val)
  {
      writer->add_str(val);
  }
  void add_str(const char* val, size_t length)
  {
      writer->add_str(val, length);
  }
  void add_str(const String &str)
  {
      writer->add_str(str.ptr(), str.length());
  }
  void add_str(const LEX_CSTRING &str)
  {
      writer->add_str(str.str, str.length);
  }
  void add_str(Item *item)
  {
      writer->add_str(item);
  }

  void add_ll(longlong val)
  {
      writer->add_ll(val);
  }
  void add_size(longlong val)
  {
      writer->add_size(val);
  }
  void add_double(double val)
  {
      writer->add_double(val);
  }
  void add_bool(bool val)
  {
      writer->add_bool(val);
  }
  void add_null()
  {
      writer->add_null();
  }
  void add_table_name(const JOIN_TAB *tab)
  {
      writer->add_table_name(tab);
  }
  void add_table_name(const TABLE* table)
  {
      writer->add_table_name(table);
  }
};

/* A common base for Json_writer_object and Json_writer_array */
class Json_writer_struct
{
  Json_writer_struct(const Json_writer_struct&)= delete;
  Json_writer_struct& operator=(const Json_writer_struct&)= delete;

#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;
  /*
    Tells when a json_writer_struct has been closed or not
  */
  bool closed;

  explicit Json_writer_struct(Json_writer *writer)
  : my_writer(writer)
  {
    context.init(my_writer);
    closed= false;
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
    named_items_expectation.push_back(expect_named_children);
#endif
  }
  explicit Json_writer_struct(THD *thd)
  : Json_writer_struct(thd->opt_trace.get_current_json())
  {
  }

public:

#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
  virtual ~Json_writer_struct()
  {
    named_items_expectation.pop_back();
  }
#else
  virtual ~Json_writer_struct() = default;
#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
};


/*
  RAII-based class to start/end writing a JSON object into the JSON document

  There is "ignore mode": one can initialize Json_writer_object with a NULL
  Json_writer argument, and then all its calls will do nothing. This is used
  by optimizer trace which can be enabled or disabled.
*/

class Json_writer_object : public Json_writer_struct
{
private:
  void add_member(const char *name)
  {
    my_writer->add_member(name);
  }
public:
  explicit Json_writer_object(Json_writer* writer, const char *str= nullptr)
  : Json_writer_struct(writer)
  {
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
    DBUG_ASSERT(named_item_expected());
#endif
    if (unlikely(my_writer))
    {
      if (str)
        my_writer->add_member(str);
      my_writer->start_object();
    }
  }

  explicit Json_writer_object(THD* thd, const char *str= nullptr)
  : Json_writer_object(thd->opt_trace.get_current_json(), str)
  {
  }

  ~Json_writer_object()
  {
    if (my_writer && !closed)
      my_writer->end_object();
    closed= TRUE;
  }

  Json_writer_object& add(const char *name, bool value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      context.add_bool(value);
    }
    return *this;
  }

  Json_writer_object& add(const char *name, ulonglong value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      my_writer->add_ull(value);
    }
    return *this;
  }

  template<class IntT,
    typename= typename ::std::enable_if<std::is_integral<IntT>::value>::type
  >
  Json_writer_object& add(const char *name, IntT value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      context.add_ll(value);
    }
    return *this;
  }

  Json_writer_object& add(const char *name, double value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      context.add_double(value);
    }
    return *this;
  }

  Json_writer_object& add(const char *name, const char *value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      context.add_str(value);
    }
    return *this;
  }
  Json_writer_object& add(const char *name, const char *value, size_t num_bytes)
  {
    add_member(name);
    context.add_str(value, num_bytes);
    return *this;
  }
  Json_writer_object& add(const char *name, const LEX_CSTRING &value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      context.add_str(value.str, value.length);
    }
    return *this;
  }
  Json_writer_object& add(const char *name, Item *value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      context.add_str(value);
    }
    return *this;
  }
  Json_writer_object& add_null(const char*name)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member(name);
      context.add_null();
    }
    return *this;
  }
  Json_writer_object& add_table_name(const JOIN_TAB *tab)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member("table");
      context.add_table_name(tab);
    }
    return *this;
  }
  Json_writer_object& add_table_name(const TABLE *table)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member("table");
      context.add_table_name(table);
    }
    return *this;
  }
  Json_writer_object& add_select_number(uint select_number)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
    {
      add_member("select_id");
      if (unlikely(select_number == FAKE_SELECT_LEX_ID))
        context.add_str("fake");
      else
        context.add_ll(static_cast<longlong>(select_number));
    }
    return *this;
  }
  void end()
  {
    DBUG_ASSERT(!closed);
    if (unlikely(my_writer))
      my_writer->end_object();
    closed= TRUE;
  }
};


/*
  RAII-based class to start/end writing a JSON array into the JSON document

  There is "ignore mode": one can initialize Json_writer_array with a NULL
  Json_writer argument, and then all its calls will do nothing. This is used
  by optimizer trace which can be enabled or disabled.
*/

class Json_writer_array : public Json_writer_struct
{
public:
  explicit Json_writer_array(Json_writer *writer, const char *str= nullptr)
    : Json_writer_struct(writer)
  {
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
    DBUG_ASSERT(!named_item_expected());
#endif
    if (unlikely(my_writer))
    {
      if (str)
        my_writer->add_member(str);
      my_writer->start_array();
    }
  }

  explicit Json_writer_array(THD *thd, const char *str= nullptr)
    : Json_writer_array(thd->opt_trace.get_current_json(), str)
  {
  }

  ~Json_writer_array()
  {
    if (unlikely(my_writer && !closed))
    {
      my_writer->end_array();
      closed= TRUE;
    }
  }

  void end()
  {
    DBUG_ASSERT(!closed);
    if (unlikely(my_writer))
      my_writer->end_array();
    closed= TRUE;
  }

  Json_writer_array& add(bool value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_bool(value);
    return *this;
  }
  Json_writer_array& add(ulonglong value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_ll(static_cast<longlong>(value));
    return *this;
  }
  Json_writer_array& add(longlong value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_ll(value);
    return *this;
  }
  Json_writer_array& add(double value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_double(value);
    return *this;
  }
  #ifndef _WIN64
  Json_writer_array& add(size_t value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_ll(static_cast<longlong>(value));
    return *this;
  }
  #endif
  Json_writer_array& add(const char *value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_str(value);
    return *this;
  }
  Json_writer_array& add(const char *value, size_t num_bytes)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_str(value, num_bytes);
    return *this;
  }
  Json_writer_array& add(const LEX_CSTRING &value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_str(value.str, value.length);
    return *this;
  }
  Json_writer_array& add(Item *value)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_str(value);
    return *this;
  }
  Json_writer_array& add_null()
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_null();
    return *this;
  }
  Json_writer_array& add_table_name(const JOIN_TAB *tab)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_table_name(tab);
    return *this;
  }
  Json_writer_array& add_table_name(const TABLE *table)
  {
    DBUG_ASSERT(!closed);
    if (my_writer)
      context.add_table_name(table);
    return *this;
  }
};

/*
  RAII-based class to disable writing into the JSON document
  The tracing is disabled as soon as the object is created.
  The destuctor is called as soon as we exit the scope of the object
  and the tracing is enabled back.
*/

class Json_writer_temp_disable
{
public:
  Json_writer_temp_disable(THD *thd_arg);
  ~Json_writer_temp_disable();
  THD *thd;
};

/*
  RAII-based helper class to detect incorrect use of Json_writer.

  The idea is that a function typically must leave Json_writer at the same
  identation level as it was when it was invoked. Leaving it at a different 
  level typically means we forgot to close an object or an array

  So, here is a way to guard
  void foo(Json_writer *writer)
  {
    Json_writer_nesting_guard(writer);
    .. do something with writer

    // at the end of the function, ~Json_writer_nesting_guard() is called
    // and it makes sure that the nesting is the same as when the function was
    // entered.
  }
*/

class Json_writer_nesting_guard
{
#ifdef DBUG_OFF
public:
  Json_writer_nesting_guard(Json_writer *) {}
#else
  Json_writer* writer;
  int indent_level;
public:
  Json_writer_nesting_guard(Json_writer *writer_arg) : 
    writer(writer_arg),
    indent_level(writer->indent_level)
  {}

  ~Json_writer_nesting_guard()
  {
    DBUG_ASSERT(indent_level == writer->indent_level);
  }
#endif
};

#endif