re_load.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "re_load.h"
  2. #include "yaml-cpp/yaml.h"
  3. #include "log.h"
  4. #include <string>
  5. #include <iostream>
  6. #include "re_comm.h"
  7. std::string conf_file = "../conf/dtc.yaml";
  8. using namespace hsql;
  9. hsql::SQLParserResult rule_ast;
  10. vector<vector<hsql::Expr*> > expr_rules;
  11. // load rule from dtc.yaml
  12. std::string do_get_rule()
  13. {
  14. YAML::Node config;
  15. try {
  16. config = YAML::LoadFile(conf_file);
  17. } catch (const YAML::Exception &e) {
  18. log4cplus_error("config file error:%s\n", e.what());
  19. return "";
  20. }
  21. YAML::Node node = config["primary"]["layered.rule"];
  22. if(node)
  23. {
  24. std::string rules = node.as<string>();
  25. return rules;
  26. }
  27. return "";
  28. }
  29. int get_rule_condition_num(hsql::Expr* rule)
  30. {
  31. int num = 0;
  32. if(rule->isType(kExprOperator) && rule->opType == kOpAnd)
  33. {
  34. num += get_rule_condition_num(rule->expr);
  35. num += get_rule_condition_num(rule->expr2);
  36. return num;
  37. }
  38. else if(rule->isType(kExprOperator) &&
  39. (rule->opType >= kOpEquals && rule->opType <= kOpGreaterEq))
  40. {
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. // parse rule txt to AST.
  46. int do_parse_rule(std::string rules)
  47. {
  48. std::string sql = "select * from rules where ";
  49. sql += rules;
  50. sql += ";";
  51. log4cplus_debug("rule sql: %s", sql.c_str());
  52. bool r = hsql::SQLParser::parse(sql, &rule_ast);
  53. if (r && rule_ast.isValid() && rule_ast.size() > 0)
  54. {
  55. return 0;
  56. }
  57. return -1;
  58. }
  59. int traverse_sub_ast(hsql::Expr* where, vector<hsql::Expr*>* v_expr)
  60. {
  61. if(where->isType(kExprOperator) && where->opType == kOpAnd)
  62. {
  63. traverse_sub_ast(where->expr, v_expr);
  64. traverse_sub_ast(where->expr2, v_expr);
  65. }
  66. else
  67. {
  68. v_expr->push_back(where);
  69. }
  70. }
  71. int traverse_ast(hsql::Expr* where)
  72. {
  73. if(where->isType(kExprOperator) && where->opType == kOpOr)
  74. {
  75. traverse_ast(where->expr);
  76. traverse_ast(where->expr2);
  77. }
  78. else
  79. {
  80. vector<hsql::Expr*> v_expr;
  81. traverse_sub_ast(where, &v_expr);
  82. expr_rules.push_back(v_expr);
  83. }
  84. return 0;
  85. }
  86. //legitimacy check.
  87. int do_check_rule()
  88. {
  89. hsql::Expr *where = NULL;
  90. if(rule_ast.size() != 1)
  91. return -1;
  92. if(rule_ast.getStatement(0)->type() != hsql::kStmtSelect)
  93. return -2;
  94. hsql::SelectStatement* stmt = (SelectStatement*)rule_ast.getStatement(0);
  95. where = stmt->whereClause;
  96. if(!where)
  97. return -3;
  98. traverse_ast(where);
  99. return 0;
  100. }
  101. // make `OR` top.
  102. int do_optimize_rule()
  103. {
  104. return 0;
  105. }
  106. // split whole rule to sub rules.
  107. int do_split_rules()
  108. {
  109. return 0;
  110. }
  111. int re_load_rule()
  112. {
  113. log4cplus_debug("load rule start...");
  114. if(rule_ast.isValid() && rule_ast.size() > 0)
  115. return 0;
  116. std::string rules = do_get_rule();
  117. if(rules.length() <= 0)
  118. return -1;
  119. int ret = do_parse_rule(rules);
  120. if(ret != 0)
  121. {
  122. log4cplus_error("match rules parsed failed, %d", ret);
  123. return -2;
  124. }
  125. ret = do_check_rule();
  126. if(ret != 0)
  127. {
  128. log4cplus_error("match rules check failed, %d", ret);
  129. return -3;
  130. }
  131. log4cplus_debug("load rule end.");
  132. return 0;
  133. }
  134. extern "C" int re_load_table_key(char* key)
  135. {
  136. YAML::Node config;
  137. try {
  138. config = YAML::LoadFile(conf_file);
  139. } catch (const YAML::Exception &e) {
  140. log4cplus_error("config file error:%s\n", e.what());
  141. return -1;
  142. }
  143. YAML::Node node = config["primary"]["cache"]["field"][0]["name"];
  144. if(node)
  145. {
  146. if(node.as<string>().length() >= 50)
  147. {
  148. return -1;
  149. }
  150. strcpy(key, node.as<string>().c_str());
  151. return 0;
  152. }
  153. return -1;
  154. }
  155. std::string re_load_table_name()
  156. {
  157. YAML::Node config;
  158. try {
  159. config = YAML::LoadFile(conf_file);
  160. } catch (const YAML::Exception &e) {
  161. log4cplus_error("config file error:%s\n", e.what());
  162. return "";
  163. }
  164. YAML::Node node = config["primary"]["table"];
  165. if(node)
  166. {
  167. if(node.as<string>().length() >= 50)
  168. {
  169. return "";
  170. }
  171. std::string res = node.as<string>();
  172. transform(res.begin(),res.end(),res.begin(),::toupper);
  173. return res;
  174. }
  175. return "";
  176. }