|
61 | 61 | the collected statistics in the persistent statistical tables only
|
62 | 62 | when the value of the variable 'use_stat_tables' is not
|
63 | 63 | equal to "never".
|
64 |
| -*/ |
65 |
| - |
| 64 | +*/ |
| 65 | + |
| 66 | +/* |
| 67 | + * json_get_array_items expects a JSON array as argument, |
| 68 | + * and pushes the elements of the array into the `container` vector. |
| 69 | + * It only works if all the elements in the original JSON array |
| 70 | + * are scalar values (i.e., strings, numbers, true or false), and returns JSV_BAD_JSON if: |
| 71 | + * the original JSON is not an array OR the JSON array contains non-scalar elements. |
| 72 | + */ |
| 73 | +bool json_get_array_items(const char *json, const char *json_end, int *value_type, std::vector<std::string> &container); |
| 74 | + |
| 75 | +std::vector<std::string> parse_histogram_from_json(const char *json); |
| 76 | + |
| 77 | +void test_parse_histogram_from_json(); |
| 78 | + |
66 | 79 | /* Currently there are only 3 persistent statistical tables */
|
67 | 80 | static const uint STATISTICS_TABLES= 3;
|
68 | 81 |
|
@@ -1651,83 +1664,81 @@ std::vector<std::string> bucket_bounds = {};
|
1651 | 1664 |
|
1652 | 1665 | test_parse_histogram_from_json();
|
1653 | 1666 | }
|
| 1667 | +}; |
1654 | 1668 |
|
1655 |
| - static std::vector<std::string> parse_histogram_from_json(const char *json) |
1656 |
| - { |
1657 |
| - std::vector<std::string> hist_buckets= {}; |
1658 |
| - enum json_types vt = json_get_array_items(json, json + strlen(json), hist_buckets); |
1659 |
| - printf("%d", vt); |
1660 |
| - printf("%zu", hist_buckets.size()); |
| 1669 | +void test_parse_histogram_from_json() |
| 1670 | +{ |
| 1671 | + std::vector<std::string> bucket = {}; |
| 1672 | + std::string json; |
| 1673 | + std::string tests[7] = { |
| 1674 | + R"(["aabbb", "ccccdd", "eeefff"])", |
| 1675 | + R"(["aabbb", "ccc{}dd", "eeefff"])", |
| 1676 | + R"(["aabbb", {"a": "b"}, "eeefff"])", |
| 1677 | + R"({})", |
| 1678 | + R"([1,2,3, null])", |
| 1679 | + R"([null])", |
| 1680 | + R"([])" |
| 1681 | + }; |
1661 | 1682 |
|
1662 |
| - return hist_buckets; |
| 1683 | + for(const auto& test : tests) { |
| 1684 | + json = test; |
| 1685 | + bucket = parse_histogram_from_json(json.c_str()); |
1663 | 1686 | }
|
| 1687 | +} |
1664 | 1688 |
|
1665 |
| - static void test_parse_histogram_from_json() |
1666 |
| - { |
1667 |
| - std::vector<std::string> bucket = {}; |
1668 |
| - std::string json; |
1669 |
| - std::string tests[7] = { |
1670 |
| - R"(["aabbb", "ccccdd", "eeefff"])", |
1671 |
| - R"(["aabbb", "ccc{}dd", "eeefff"])", |
1672 |
| - R"(["aabbb", {"a": "b"}, "eeefff"])", |
1673 |
| - R"({})", |
1674 |
| - R"([1,2,3, null])", |
1675 |
| - R"([null])", |
1676 |
| - R"([])" |
1677 |
| - }; |
1678 |
| - |
1679 |
| - for(const auto& test : tests) { |
1680 |
| - json = test; |
1681 |
| - bucket = parse_histogram_from_json(json.c_str()); |
1682 |
| - printf("%zu", bucket.size()); |
1683 |
| - } |
1684 |
| - } |
| 1689 | +std::vector<std::string> parse_histogram_from_json(const char *json) |
| 1690 | +{ |
| 1691 | + std::vector<std::string> hist_buckets= {}; |
| 1692 | + int vt; |
| 1693 | + bool result = json_get_array_items(json, json + strlen(json), &vt, hist_buckets); |
| 1694 | + fprintf(stderr,"==============\n"); |
| 1695 | + fprintf(stderr,"histogram: %s\n", json); |
| 1696 | + fprintf(stderr, "json_get_array_items() returned %s\n", result ? "true" : "false"); |
| 1697 | + fprintf(stderr, "value type after json_get_array_items() is %d\n", vt); |
| 1698 | + fprintf(stderr, " JSV_BAD_JSON=%d, JSON_VALUE_ARRAY=%d\n", (int)JSV_BAD_JSON, (int)JSON_VALUE_ARRAY); |
| 1699 | + fprintf(stderr, "hist_buckets.size()=%zu\n", hist_buckets.size()); |
| 1700 | + |
| 1701 | + return hist_buckets; |
| 1702 | +} |
1685 | 1703 |
|
1686 |
| - /* |
1687 |
| - * json_get_array_items expects a JSON array as argument, |
1688 |
| - * and pushes the elements of the array into the `container` vector. |
1689 |
| - * It only works if all the elements in the original JSON array |
1690 |
| - * are scalar values (i.e., strings, numbers, true or false), and returns JSV_BAD_JSON if: |
1691 |
| - * the original JSON is not an array OR the JSON array contains non-scalar elements. |
1692 |
| - */ |
1693 |
| - static json_types json_get_array_items(const char *json, const char *json_end, std::vector<std::string> &container) { |
1694 |
| - json_engine_t je; |
1695 |
| - enum json_types value_type; |
1696 |
| - int vl; |
1697 |
| - const char *v; |
| 1704 | +bool json_get_array_items(const char *json, const char *json_end, int *value_type, std::vector<std::string> &container) { |
| 1705 | + json_engine_t je; |
| 1706 | + int vl; |
| 1707 | + const char *v; |
1698 | 1708 |
|
1699 |
| - json_scan_start(&je, &my_charset_utf8mb4_bin, (const uchar *)json, (const uchar *)json_end); |
| 1709 | + json_scan_start(&je, &my_charset_utf8mb4_bin, (const uchar *)json, (const uchar *)json_end); |
1700 | 1710 |
|
1701 |
| - if (json_read_value(&je) || je.value_type != JSON_VALUE_ARRAY) |
1702 |
| - { |
1703 |
| - return JSV_BAD_JSON; |
1704 |
| - } |
1705 |
| - value_type = static_cast<json_types>(je.value_type); |
| 1711 | + if (json_read_value(&je) || je.value_type != JSON_VALUE_ARRAY) |
| 1712 | + { |
| 1713 | + *value_type = JSV_BAD_JSON; |
| 1714 | + return false; |
| 1715 | + } |
| 1716 | + *value_type = je.value_type; |
1706 | 1717 |
|
1707 |
| - std::string val; |
1708 |
| - while(!json_scan_next(&je)) |
| 1718 | + std::string val; |
| 1719 | + while(!json_scan_next(&je)) |
| 1720 | + { |
| 1721 | + switch(je.state) |
1709 | 1722 | {
|
1710 |
| - switch(je.state) |
| 1723 | + case JST_VALUE: |
| 1724 | + *value_type = json_smart_read_value(&je, &v, &vl); |
| 1725 | + if (je.value_type != JSON_VALUE_STRING && |
| 1726 | + je.value_type != JSON_VALUE_NUMBER && |
| 1727 | + je.value_type != JSON_VALUE_TRUE && |
| 1728 | + je.value_type != JSON_VALUE_FALSE) |
1711 | 1729 | {
|
1712 |
| - case JST_VALUE: |
1713 |
| - if (je.value_type != JSON_VALUE_STRING && |
1714 |
| - je.value_type != JSON_VALUE_NUMBER && |
1715 |
| - je.value_type != JSON_VALUE_TRUE && |
1716 |
| - je.value_type != JSON_VALUE_FALSE) |
1717 |
| - { |
1718 |
| - return JSV_BAD_JSON; |
1719 |
| - } |
1720 |
| - value_type = smart_read_value(&je, &v, &vl); |
1721 |
| - val = std::string(v, vl); |
1722 |
| - container.emplace_back(val); |
1723 |
| - case JST_ARRAY_END: |
1724 |
| - break; |
| 1730 | + *value_type = JSV_BAD_JSON; |
| 1731 | + return false; |
1725 | 1732 | }
|
| 1733 | + val = std::string(v, vl); |
| 1734 | + container.emplace_back(val); |
| 1735 | + case JST_ARRAY_END: |
| 1736 | + break; |
1726 | 1737 | }
|
1727 |
| - |
1728 |
| - return value_type; |
1729 | 1738 | }
|
1730 |
| -}; |
| 1739 | + |
| 1740 | + return true; |
| 1741 | +} |
1731 | 1742 |
|
1732 | 1743 | C_MODE_START
|
1733 | 1744 |
|
|
0 commit comments