re_load.cc 4.5 KB

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