rule.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "rule.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include "../libs/hsql/include/SQLParser.h"
  5. #include "../libs/hsql/include/util/sqlhelper.h"
  6. #include "re_comm.h"
  7. #include "re_load.h"
  8. #include "re_match.h"
  9. #include "re_cache.h"
  10. #include "log.h"
  11. #include "yaml-cpp/yaml.h"
  12. #define SPECIFIC_L1_SCHEMA "L1"
  13. #define SPECIFIC_L2_SCHEMA "L2"
  14. #define SPECIFIC_L3_SCHEMA "L3"
  15. using namespace std;
  16. extern vector<vector<hsql::Expr*> > expr_rules;
  17. extern std::string conf_file;
  18. std::string get_key_info(std::string conf)
  19. {
  20. YAML::Node config;
  21. try {
  22. config = YAML::LoadFile(conf);
  23. } catch (const YAML::Exception &e) {
  24. log4cplus_error("config file error:%s\n", e.what());
  25. return "";
  26. }
  27. YAML::Node node = config["primary"]["cache"]["field"][0]["name"];
  28. if(node)
  29. {
  30. std::string keystr = node.as<string>();
  31. transform(keystr.begin(),keystr.end(),keystr.begin(),::toupper);
  32. return keystr;
  33. }
  34. return "";
  35. }
  36. extern "C" const char* rule_get_key(const char* conf)
  37. {
  38. std::string strkey = get_key_info(conf);
  39. printf("222222222222, conf file: %s\n", conf);
  40. printf("key len: %d, key: %s\n", strkey.length(), strkey.c_str());
  41. if(strkey.length() > 0)
  42. return strkey.c_str();
  43. else
  44. return NULL;
  45. }
  46. extern "C" int rule_get_key_type(const char* conf)
  47. {
  48. YAML::Node config;
  49. if(conf == NULL)
  50. return -1;
  51. try {
  52. config = YAML::LoadFile(conf);
  53. } catch (const YAML::Exception &e) {
  54. log4cplus_error("config file error:%s\n", e.what());
  55. return -1;
  56. }
  57. YAML::Node node = config["primary"]["cache"]["field"][0]["type"];
  58. if(node)
  59. {
  60. std::string str = node.as<string>();
  61. if(str == "signed")
  62. return 1;
  63. else if(str == "unsigned")
  64. return 2;
  65. else if(str == "float")
  66. return 3;
  67. else if(str == "string")
  68. return 4;
  69. else if(str == "binary")
  70. return 5;
  71. else
  72. return -1;
  73. }
  74. return -1;
  75. }
  76. extern "C" int rule_sql_match(const char* szsql, const char* dbname, const char* conf)
  77. {
  78. if(!szsql)
  79. return -1;
  80. std::string key = "";
  81. std::string sql = szsql;
  82. bool flag = false;
  83. init_log4cplus();
  84. if(conf)
  85. {
  86. conf_file = std::string(conf);
  87. flag = true;
  88. key = get_key_info(conf_file);
  89. if(key.length() == 0)
  90. return -1;
  91. }
  92. log4cplus_debug("key len: %d, key: %s, sql len: %d, sql: %s, dbname len: %d, dbname: %s", key.length(), key.c_str(), sql.length(), sql.c_str(), strlen(dbname), std::string(dbname).c_str());
  93. if(sql == "show databases" || sql == "SHOW DATABASES" || sql == "select database()" || sql == "SELECT DATABASE()")
  94. {
  95. return 3;
  96. }
  97. if(sql == "show tables" || sql == "SHOW TABLES")
  98. {
  99. if(dbname == NULL || strlen(dbname) == 0)
  100. return -6;
  101. else
  102. return 3;
  103. }
  104. log4cplus_debug("#############dbname:%s", dbname);
  105. if(dbname != NULL && strlen(dbname) > 0 && flag == false)
  106. {
  107. log4cplus_debug("#############111111111111");
  108. return 3;
  109. }
  110. log4cplus_debug("#############22222222222");
  111. int ret = re_load_rule();
  112. if(ret != 0)
  113. {
  114. log4cplus_error("load rule error:%d", ret);
  115. return -5;
  116. }
  117. if(sql.find("INSERT INTO") != sql.npos || sql.find("insert into") != sql.npos)
  118. {
  119. log4cplus_debug("INSERT request, force direct to L1.");
  120. //L1: DTC cache.
  121. return 1;
  122. }
  123. hsql::SQLParserResult sql_ast;
  124. if(re_parse_sql(sql, &sql_ast) != 0)
  125. return -1;
  126. ret = re_match_sql(&sql_ast, expr_rules);
  127. if(ret == 0)
  128. {
  129. if(re_is_cache_sql(&sql_ast, key))
  130. {
  131. //L1: DTC cache.
  132. return 1;
  133. }
  134. else
  135. {
  136. //L2: sharding hot database.
  137. return 2;
  138. }
  139. }
  140. else {
  141. //L3: full database.
  142. return 3;
  143. }
  144. return 3;
  145. }
  146. extern "C" int sql_parse_table(const char* szsql, char* out)
  147. {
  148. hsql::SQLParserResult sql_ast;
  149. if(re_parse_sql(szsql, &sql_ast) != 0)
  150. return -1;
  151. std::string tablename = get_table_name(&sql_ast);
  152. if(tablename.length() > 0)
  153. strcpy(out, tablename.c_str());
  154. return tablename.length();
  155. }