Skip to content

Commit

Permalink
Replacing boost ptree by rapidjson
Browse files Browse the repository at this point in the history
Summary:
Rapidjson is more efficient in JSON parsing. This code was left out
while migrating most of trx_meta_data code.

Reviewed By: yashtc

Differential Revision: D15967068

fbshipit-source-id: 84fc923
  • Loading branch information
abhinav04sharma authored and facebook-github-bot committed Jul 2, 2019
1 parent 88d0a0e commit 9f014d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions sql/sql_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#ifdef HAVE_RAPIDJSON
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#endif
#include <sstream>

#ifdef TARGET_OS_LINUX
Expand Down Expand Up @@ -5788,10 +5792,22 @@ int THD::parse_query_info_attr()
static std::string get_shard_id(const std::string& db_metadata)
{
try {
#ifdef HAVE_RAPIDJSON
rapidjson::Document db_metadata_root;
if (db_metadata_root.Parse(db_metadata.c_str()).HasParseError()) {
return {};
}
const auto iter= db_metadata_root.FindMember("shard");
std::string shard_id;
if (iter == db_metadata_root.MemberEnd()) {
shard_id= iter->value.GetString();
}
#else
boost::property_tree::ptree db_metadata_root;
std::istringstream is(db_metadata);
boost::property_tree::read_json(is, db_metadata_root);
std::string shard_id = db_metadata_root.get<std::string>("shard");
#endif
return shard_id;
}
catch (std::exception)
Expand Down
14 changes: 14 additions & 0 deletions sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ Note: YYTHD is passed as an argument to yyparse(), and subsequently to yylex().
#include "opt_explain_json.h"
#include "lex_token.h"
#include <sstream>
#ifdef HAVE_RAPIDJSON
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#else
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#endif

/* this is to get the bison compilation windows warnings out */
#ifdef _MSC_VER
Expand Down Expand Up @@ -6579,6 +6584,14 @@ db_metadata_str:
/* Verify that a valid JSON is provided */
if ($3.length > 0)
{
#ifdef HAVE_RAPIDJSON
rapidjson::Document db_metadata_root;
if (db_metadata_root.Parse($3.str).HasParseError())
{
my_error(ER_DB_METADATA_INVALID_JSON, MYF(0), $3.str);
MYSQL_YYABORT;
}
#else
boost::property_tree::ptree db_metadata_root;
std::istringstream is($3.str);
try
Expand All @@ -6591,6 +6604,7 @@ db_metadata_str:
my_error(ER_DB_METADATA_INVALID_JSON, MYF(0), $3.str);
MYSQL_YYABORT;
}
#endif
}
Lex->create_info.db_metadata= String($3.str, $3.length,
&my_charset_bin);
Expand Down

0 comments on commit 9f014d3

Please sign in to comment.