diff --git a/app/redis_tools/redis_builder/redis_commands.cpp b/app/redis_tools/redis_builder/redis_commands.cpp index cac37c7b4..b6730f605 100644 --- a/app/redis_tools/redis_builder/redis_commands.cpp +++ b/app/redis_tools/redis_builder/redis_commands.cpp @@ -35,13 +35,14 @@ static const REDIS_CMD __redis_cmds[] = redis_commands::redis_commands(const char* addr, const char* passwd, int conn_timeout, int rw_timeout, bool prefer_master, const char* cmds_file) - : conn_timeout_(conn_timeout) - , rw_timeout_(rw_timeout) - , prefer_master_(prefer_master) - , all_cmds_perm_("yes") +: conn_timeout_(conn_timeout) +, rw_timeout_(rw_timeout) +, prefer_master_(prefer_master) +, all_cmds_perm_("yes") { - if (passwd && *passwd) + if (passwd && *passwd) { passwd_ = passwd; + } set_addr(addr, addr_); conns_ = NULL; @@ -59,11 +60,9 @@ void redis_commands::init(const char* cmds_file) { set_commands(); - if (cmds_file && *cmds_file) - { + if (cmds_file && *cmds_file) { acl::ifstream in; - if (in.open_read(cmds_file) == false) - { + if (!in.open_read(cmds_file)) { logger_error("load file %s error: %s", cmds_file, acl::last_serror()); return; @@ -77,15 +76,15 @@ void redis_commands::init(const char* cmds_file) void redis_commands::set_commands(void) { - for (size_t i = 0; !__redis_cmds[i].cmd.empty(); i++) - { + for (size_t i = 0; !__redis_cmds[i].cmd.empty(); i++) { acl::string cmd(__redis_cmds[i].cmd); cmd.upper(); std::map::const_iterator cit = redis_cmds_.find(cmd); - if (cit != redis_cmds_.end()) + if (cit != redis_cmds_.end()) { continue; + } redis_cmds_[cmd] = __redis_cmds[i]; } @@ -96,15 +95,16 @@ void redis_commands::load_commands(acl::istream& in) acl::string line; size_t i = 0; - while (!in.eof()) - { - if (in.gets(line) == false) + while (!in.eof()) { + if (!in.gets(line)) { break; + } i++; line.trim_left_space().trim_right_space(); - if (line.empty() || line[0] == '#') + if (line.empty() || line[0] == '#') { continue; + } add_cmdline(line, i); } @@ -113,8 +113,7 @@ void redis_commands::load_commands(acl::istream& in) void redis_commands::add_cmdline(acl::string& line, size_t i) { std::vector& tokens = line.split2(" \t|:,;"); - if (tokens.size() < 3) - { + if (tokens.size() < 3) { logger_warn("skip line(%d): %s", (int) i, line.c_str()); return; } @@ -122,12 +121,12 @@ void redis_commands::add_cmdline(acl::string& line, size_t i) acl::string cmd(tokens[0]); cmd.upper(); - if (cmd == "ALL") - { + if (cmd == "ALL") { all_cmds_perm_ = tokens[2]; all_cmds_perm_.lower(); - if (all_cmds_perm_ == "warn" || all_cmds_perm_ == "no") + if (all_cmds_perm_ == "warn" || all_cmds_perm_ == "no") { return; + } return; } @@ -137,8 +136,9 @@ void redis_commands::add_cmdline(acl::string& line, size_t i) redis_cmd.perm = tokens[2]; redis_cmd.perm.lower(); - if (redis_cmd.perm != "yes" && redis_cmd.perm != "warn") + if (redis_cmd.perm != "yes" && redis_cmd.perm != "warn") { redis_cmd.perm = "no"; + } redis_cmds_[cmd] = redis_cmd; } @@ -155,8 +155,7 @@ void redis_commands::show_commands(void) "Command", "Broadcast", "Permission"); for (std::map::const_iterator cit = - redis_cmds_.begin(); cit != redis_cmds_.end(); ++cit) - { + redis_cmds_.begin(); cit != redis_cmds_.end(); ++cit) { #ifdef ACL_UNIX printf("\033[1;32;40m%-20s\033[0m" "\033[1;36;40m%-20s\033[0m" @@ -172,46 +171,52 @@ void redis_commands::show_commands(void) void redis_commands::set_addr(const char* in, acl::string& out) { - if (in == NULL || *in == 0) + if (in == NULL || *in == 0) { return; + } acl::string buf(in); std::vector& tokens = buf.split2(": \t"); - if (tokens.size() >= 2) + if (tokens.size() >= 2) { out.format("%s:%s", tokens[0].c_str(), tokens[1].c_str()); + } } void redis_commands::getline(acl::string& buf, const char* prompt /* = NULL */) { - if (prompt == NULL || *prompt == 0) + if (prompt == NULL || *prompt == 0) { prompt = "redis_builder> "; + } #ifdef HAS_READLINE char* ptr = readline(prompt); - if (ptr == NULL) + if (ptr == NULL) { exit (0); + } buf = ptr; buf.trim_right_line(); - if (!buf.empty() && !buf.equal("y", false) && !buf.equal("n", false)) + if (!buf.empty() && !buf.equal("y", false) && !buf.equal("n", false)) { add_history(buf.c_str()); + } #else printf("%s", prompt); fflush(stdout); acl::stdin_stream in; - if (in.gets(buf) == false) + if (!in.gets(buf)) { exit (0); + } #endif } void redis_commands::create_cluster(void) { - while (addr_.empty()) - { + while (addr_.empty()) { const char* prompt = "please enter one redis addr: "; acl::string buf; getline(buf, prompt); - if (buf.empty()) + if (buf.empty()) { continue; + } set_addr(buf, addr_); } @@ -219,8 +224,9 @@ void redis_commands::create_cluster(void) conns_->set(addr_, conn_timeout_, rw_timeout_); conns_->set_all_slot(addr_, 0); - if (!passwd_.empty()) + if (!passwd_.empty()) { conns_->set_password("default", passwd_); + } } void redis_commands::help(void) @@ -266,8 +272,7 @@ void redis_commands::help(void) bool redis_commands::check(const char* command) { - if (all_cmds_perm_ == "no") - { + if (all_cmds_perm_ == "no") { printf("All commands disable!\r\n"); return false; } @@ -278,27 +283,23 @@ bool redis_commands::check(const char* command) acl::string info; acl::string perm; - if (cit == redis_cmds_.end()) - { + if (cit == redis_cmds_.end()) { info = "BROADCAST"; perm = "yes"; - } - else - { + } else { info = cit->second.broadcast; - if (!info.equal("master", false) && !info.equal("slave", false)) + if (!info.equal("master", false) && !info.equal("slave", false)) { info = "SEND"; + } perm = cit->second.perm; } - if (perm == "no") - { + if (perm == "no") { printf("command %s disabled!\r\n", command); return false; } - if (all_cmds_perm_ == "warn" || perm == "warn") - { + if (all_cmds_perm_ == "warn" || perm == "warn") { acl::string buf; acl::string prompt; prompt.format("Do you want to %s DANGEROUS \"%s\"" @@ -306,8 +307,7 @@ bool redis_commands::check(const char* command) info.c_str(), command); getline(buf, prompt); buf.lower(); - if (buf != "y" && buf != "yes") - { + if (buf != "y" && buf != "yes") { printf("You discard \"%s\" command!\r\n", command); return false; } @@ -316,86 +316,126 @@ bool redis_commands::check(const char* command) return true; } +bool redis_commands::parse(acl::string& line, std::vector& out) +{ + acl::string token, right; + std::vector& tokens = line.split2(" \t", true); + for (std::vector::const_iterator cit = tokens.begin(); + cit != tokens.end(); ++cit) { + + const acl::string& curr = *cit; + + if (!right.empty()) { + if (curr != right) { + token << " " << curr; + } else if (!token.empty()) { + out.push_back(token); + token.clear(); + right.clear(); + } else { + token.clear(); + right.clear(); + } + } else if (curr[0] == '\\') { + if (curr.size() >= 2) { + token << " " << curr.c_str() + 1; + } else { + token << '\\'; + } + } else if (curr == "\'" || curr == "\"") { + right = curr; + } else { + out.push_back(curr); + } + } + + return !out.empty(); +} + void redis_commands::run(void) { acl::string buf; - while (true) - { + while (true) { getline(buf); if (buf.equal("quit", false) || buf.equal("exit", false) - || buf.equal("q", false)) - { + || buf.equal("q", false)) { + printf("Bye!\r\n"); break; } if (buf.empty() || buf.equal("help", false) - || buf.equal("h", false)) - { + || buf.equal("h", false)) { + help(); continue; } - std::vector& tokens = buf.split2(" \t", true); + std::vector tokens; + if (!parse(buf, tokens)) { + continue; + } + acl::string& cmd = tokens[0]; cmd.upper(); - if (check(cmd) == false) + if (!check(cmd)) { continue; + } - if (cmd == "DATE") + if (cmd == "DATE") { show_date(); - else if (cmd == "SERVER") + } else if (cmd == "SERVER") { set_server(tokens); - else if (cmd == "NODES") + } else if (cmd == "NODES") { show_nodes(); - else if (cmd == "KEYS") + } else if (cmd == "KEYS") { get_keys(tokens); - else if (cmd == "SCAN") + } else if (cmd == "SCAN") { scan_keys(tokens); - else if (cmd == "GET") + } else if (cmd == "GET") { get(tokens); - else if (cmd == "GETN") + } else if (cmd == "GETN") { getn(tokens); - else if (cmd == "REMOVE" || cmd == "RM") + } else if (cmd == "REMOVE" || cmd == "RM") { pattern_remove(tokens); - else if (cmd == "TYPE") + } else if (cmd == "TYPE") { check_type(tokens); - else if (cmd == "TTL") + } else if (cmd == "TTL") { check_ttl(tokens); - else if (cmd == "DBSIZE") + } else if (cmd == "DBSIZE") { get_dbsize(tokens); - else if (cmd == "CONFIG") + } else if (cmd == "CONFIG") { config(tokens); + } #ifdef HAS_READLINE - else if (cmd == "CLEAR" || cmd == "CL") - { + else if (cmd == "CLEAR" || cmd == "CL") { rl_clear_screen(0, 0); printf("\r\n"); } #endif - else + else { request(tokens); + } } } void redis_commands::set_server(const std::vector& tokens) { - if (tokens.size() < 2) - { + if (tokens.size() < 2) { printf("> usage: server ip:port\r\n"); return; } acl::string buf(tokens[1]); - if (tokens.size() >= 3) + if (tokens.size() >= 3) { buf << " " << tokens[2]; + } acl::string addr; set_addr(buf, addr); - if (addr_ == addr) - { + if (addr_ == addr) { printf("no change, redis server addr: %s\r\n", addr_.c_str()); return; } @@ -428,46 +468,43 @@ void redis_commands::show_date(void) void redis_commands::get_keys(const std::vector& tokens) { - if (tokens.size() < 2) - { + if (tokens.size() < 2) { printf("> usage: keys parameter\r\n"); return; } const char* pattern = tokens[1].c_str(); int max; - if (tokens.size() >= 3) - { + if (tokens.size() >= 3) { max = atoi(tokens[2].c_str()); - if (max < 0) + if (max < 0) { max = 10; - } - else + } + } else { max = 10; + } acl::redis redis(conns_); std::vector nodes; redis_util::get_nodes(redis, prefer_master_, nodes); int n = 0; - if (!nodes.empty()) - { + if (!nodes.empty()) { for (std::vector::const_iterator - cit = nodes.begin(); cit != nodes.end(); ++cit) - { + cit = nodes.begin(); cit != nodes.end(); ++cit) { + n += get_keys((*cit)->get_addr(), pattern, max); } - } - else + } else { n += get_keys(addr_, pattern, max); + } printf("-----keys %s: total count: %d----\r\n", tokens[1].c_str(), n); } int redis_commands::get_keys(const char* addr, const char* pattern, int max) { - if (addr == NULL || *addr == 0) - { + if (addr == NULL || *addr == 0) { printf("addr NULL\r\nEnter any key to continue ...\r\n"); getchar(); return 0; @@ -478,17 +515,19 @@ int redis_commands::get_keys(const char* addr, const char* pattern, int max) std::vector res; acl::redis_key redis(&conn); - if (redis.keys_pattern(pattern, &res) <= 0) + if (redis.keys_pattern(pattern, &res) <= 0) { return 0; + } int n = 0; for (std::vector::const_iterator cit = res.begin(); - cit != res.end(); ++cit) - { + cit != res.end(); ++cit) { + printf("%s\r\n", (*cit).c_str()); n++; - if (n >= max) + if (n >= max) { break; + } } printf("--- Addr: %s, Total: %d, Limit: %d, Show: %d ---\r\n", @@ -504,18 +543,20 @@ void redis_commands::scan_keys(const std::vector& tokens) acl::string addr; size_t size = tokens.size(); - if (size >= 2) + if (size >= 2) { pattern = tokens[1].c_str(); - else + } else { pattern = "*"; + } - if (size >= 3) + if (size >= 3) { display_count = (size_t) atoi(tokens[2].c_str()); - if (size >= 4) + } + if (size >= 4) { addr = tokens[3]; + } - if (!addr.empty()) - { + if (!addr.empty()) { scan(addr, pattern, display_count); return; } @@ -526,16 +567,16 @@ void redis_commands::scan_keys(const std::vector& tokens) int total = 0; for (std::vector::const_iterator cit - = nodes.begin(); cit != nodes.end();) - { + = nodes.begin(); cit != nodes.end();) { + int n = scan((*cit)->get_addr(), pattern, display_count); - if (n > 0) + if (n > 0) { total += n; + } printf("There are %d keys in %s.", n, (*cit)->get_addr()); - if (++cit == nodes.end()) - { + if (++cit == nodes.end()) { printf("\r\n"); break; } @@ -546,8 +587,9 @@ void redis_commands::scan_keys(const std::vector& tokens) acl::string buf; getline(buf, prompt); - if (!buf.empty() && !buf.equal("y", false)) + if (!buf.empty() && !buf.equal("y", false)) { break; + } } printf("-----scan keys %s: total count: %d----\r\n", @@ -568,11 +610,9 @@ int redis_commands::scan(const char* addr, const char* pattern, int cursor = 0, n = 0, i = 0; size_t n1 = 0; - while (true) - { + while (true) { cursor = redis.scan(cursor, res, pattern, &count); - if (cursor < 0) - { + if (cursor < 0) { printf("scan error: %s\r\n", redis.result_error()); break; } @@ -580,26 +620,29 @@ int redis_commands::scan(const char* addr, const char* pattern, i++; n += res.size(); - if (display_count > 0 && n1++ < display_count) - { + if (display_count > 0 && n1++ < display_count) { size_t n2 = 0; for (std::vector::const_iterator cit - = res.begin(); cit != res.end(); ++cit) - { - if (display_count > 0 && n2 >= display_count) + = res.begin(); cit != res.end(); ++cit) { + + if (display_count > 0 && n2 >= display_count) { break; + } get((*cit).c_str(), display_count); n2++; } } - if (cursor == 0 && res.empty()) + if (cursor == 0) { break; + } + redis.clear(); res.clear(); - if (i > 0 && i % 10 == 0) + if (i > 0 && i % 10 == 0) { acl_doze(100); + } } return n; @@ -607,8 +650,7 @@ int redis_commands::scan(const char* addr, const char* pattern, void redis_commands::getn(const std::vector& tokens) { - if (tokens.size() < 2) - { + if (tokens.size() < 2) { printf("> usage: getn key limit\r\n"); return; } @@ -616,41 +658,41 @@ void redis_commands::getn(const std::vector& tokens) const char* key = tokens[1].c_str(); int count; - if (tokens.size() >= 3) - { + if (tokens.size() >= 3) { count = atoi(tokens[2].c_str()); - if (count < 0) + if (count < 0) { count = 10; - } - else + } + } else { count = 10; + } get(key, count); } void redis_commands::get(const std::vector& tokens) { - if (tokens.size() < 2) // xxx + if (tokens.size() < 2) { // xxx return; + } std::vector::const_iterator cit = tokens.begin(); ++cit; int max; const char* ptr = (*cit).c_str(); - if (*ptr == ':' && *(ptr + 1) != 0) - { + if (*ptr == ':' && *(ptr + 1) != 0) { ptr++; max = atoi(ptr); - if (max < 0) + if (max < 0) { max = 10; + } ++cit; - } - else + } else { max = 10; + } - for (; cit != tokens.end(); ++cit) - { + for (; cit != tokens.end(); ++cit) { const char* key = (*cit).c_str(); get(key, max); } @@ -661,8 +703,7 @@ void redis_commands::get(const char* key, int max) acl::redis cmd(conns_); acl::redis_key_t type = cmd.type(key); - switch (type) - { + switch (type) { case acl::REDIS_KEY_NONE: break; case acl::REDIS_KEY_STRING: @@ -688,12 +729,12 @@ void redis_commands::get(const char* key, int max) void redis_commands::hash_get(const std::vector& tokens) { - if (tokens.empty()) // xxx + if (tokens.empty()) { // xxx return; + } std::vector::const_iterator cit = tokens.begin(); - for (++cit; cit != tokens.end(); ++cit) - { + for (++cit; cit != tokens.end(); ++cit) { hash_get((*cit).c_str(), 0); printf("-----------------------------------------------\r\n"); } @@ -704,38 +745,39 @@ void redis_commands::hash_get(const char* key, size_t max) std::map res; acl::redis cmd(conns_); - if (cmd.hgetall(key, res) == false) - { + if (!cmd.hgetall(key, res)) { printf("hgetall error: %s, key: [%s]\r\n", cmd.result_error(), key); return; } size_t n = 0, count = res.size(); - printf("HASH KEY: %s, COUNT: %d, MAX: %d\r\n", + printf("HASH KEY: %s, COUNT: %d, MAX: %d\r\n\r\n", key, (int) count, (int) max); for (std::map::const_iterator cit2 - = res.begin(); cit2 != res.end(); ++cit2) - { + = res.begin(); cit2 != res.end(); ++cit2) { + printf("[%s]: [%s]\r\n", cit2->first.c_str(), cit2->second.c_str()); n++; - if (max > 0 && n >= max) + if (max > 0 && n >= max) { break; + } } - printf("HASH KEY: %s, COUNT: %d, MAX: %d, SHOW: %d\r\n", + + printf("\r\nHASH KEY: %s, COUNT: %d, MAX: %d, SHOW: %d\r\n", key, (int) count, (int) max, (int) n); } void redis_commands::string_get(const std::vector& tokens) { - if (tokens.empty()) // xxx + if (tokens.empty()) { // xxx return; + } std::vector::const_iterator cit = tokens.begin(); - for (++cit; cit != tokens.end(); ++cit) - { + for (++cit; cit != tokens.end(); ++cit) { string_get((*cit).c_str()); printf("-----------------------------------------------\r\n"); } @@ -746,8 +788,7 @@ void redis_commands::string_get(const char* key) acl::string buf; acl::redis cmd(conns_); - if (cmd.get(key, buf) == false) - { + if (!cmd.get(key, buf)) { printf("get error: %s, key: [%s]\r\n", cmd.result_error(), key); return; } @@ -757,12 +798,12 @@ void redis_commands::string_get(const char* key) void redis_commands::list_get(const std::vector& tokens) { - if (tokens.empty()) // xxx + if (tokens.empty()) { // xxx return; + } std::vector::const_iterator cit = tokens.begin(); - for (++cit; cit != tokens.end(); ++cit) - { + for (++cit; cit != tokens.end(); ++cit) { list_get((*cit).c_str(), 0); printf("-----------------------------------------------\r\n"); } @@ -774,35 +815,33 @@ void redis_commands::list_get(const char* key, size_t max) acl::redis cmd(conns_); int len = cmd.llen(key), count = len; - if (len < 0) - { + if (len < 0) { printf("llen error: %s, key: [%s]\r\n", cmd.result_error(), key); return; } - if (len > LIMIT) - { + if (len > LIMIT) { acl::string prompt; prompt.format("Do you show all %d elements for key %s ? [y/n] ", len, key); getline(buf, prompt); - if (!buf.equal("y", false)) + if (!buf.equal("y", false)) { return; + } } - if (max > 0 && (size_t) len > max) + if (max > 0 && (size_t) len > max) { len = (int) max; + } printf("LIST KEY: [%s], COUNT: %d, MAX: %d, SHOW: %d\r\n", key, count, (int) max, len); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { buf.clear(); cmd.clear(false); - if (cmd.lindex(key, i, buf) == false) - { + if (!cmd.lindex(key, i, buf)) { printf("lindex error: %s, key: %s, idx: %d\r\n", cmd.result_error(), key, i); return; @@ -816,12 +855,12 @@ void redis_commands::list_get(const char* key, size_t max) void redis_commands::set_get(const std::vector& tokens) { - if (tokens.empty()) // xxx + if (tokens.empty()) { // xxx return; + } std::vector::const_iterator cit = tokens.begin(); - for (++cit; cit != tokens.end(); ++cit) - { + for (++cit; cit != tokens.end(); ++cit) { set_get((*cit).c_str(), 0); printf("-----------------------------------------------\r\n"); } @@ -832,33 +871,31 @@ void redis_commands::set_get(const char* key, size_t max) acl::string buf; acl::redis cmd(conns_); int len = cmd.scard(key), count = len; - if (len < 0) - { + if (len < 0) { printf("scard error: %s, key: %s\r\n", cmd.result_error(), key); return; } - if (len > LIMIT) - { + if (len > LIMIT) { acl::string prompt; prompt.format("Do you show all %d elements for key %s ? [y/n] ", len, key); getline(buf, prompt); - if (!buf.equal("y", false)) + if (!buf.equal("y", false)) { return; + } } - if (max > 0 && max > (size_t) len) + if (max > 0 && max > (size_t) len) { len = (int) max; + } printf("SET KEY: [%s], COUNT: %d\r\n", key, len); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { buf.clear(); cmd.clear(false); - if (cmd.spop(key, buf) == false) - { + if (!cmd.spop(key, buf)) { printf("spop error: %s, key: [%s], idx: %d\r\n", cmd.result_error(), key, i); return; @@ -872,12 +909,12 @@ void redis_commands::set_get(const char* key, size_t max) void redis_commands::zset_get(const std::vector& tokens) { - if (tokens.empty()) // xxx + if (tokens.empty()) { // xxx return; + } std::vector::const_iterator cit = tokens.begin(); - for (++cit; cit != tokens.end(); ++cit) - { + for (++cit; cit != tokens.end(); ++cit) { zset_get((*cit).c_str(), 0); printf("-----------------------------------------------\r\n"); } @@ -888,13 +925,11 @@ void redis_commands::zset_get(const char* key, size_t max) acl::string buf; acl::redis cmd(conns_); int len = cmd.zcard(key), count = len; - if (len < 0) - { + if (len < 0) { printf("zcard error: %s, key: [%s]\r\n", cmd.result_error(), key); return; } - if (len > LIMIT) - { + if (len > LIMIT) { acl::string prompt; prompt.format("Do you show all %d elements for key %s ? [y/n] ", len, key); @@ -904,32 +939,32 @@ void redis_commands::zset_get(const char* key, size_t max) return; } - if (max > 0 && max > (size_t) len) + if (max > 0 && max > (size_t) len) { len = (int) max; + } std::vector res; printf("ZSET KEY: [%s], COUNT: %d, MAX: %d, SHOW: %d\r\n", key, count, (int) max, len); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { buf.clear(); cmd.clear(false); res.clear(); int ret = cmd.zrange(key, i, i + 1, &res); - if (ret < 0) - { + if (ret < 0) { printf("zrange error: %s, key: [%s], idx: %d\r\n", cmd.result_error(), key, i); return; } - if (res.empty()) + if (res.empty()) { continue; + } for (std::vector::const_iterator cit - = res.begin(); cit != res.end(); ++cit) - { + = res.begin(); cit != res.end(); ++cit) { + printf("[%s]\r\n", (*cit).c_str()); } } @@ -940,8 +975,7 @@ void redis_commands::zset_get(const char* key, size_t max) void redis_commands::pattern_remove(const std::vector& tokens) { - if (tokens.size() < 2) - { + if (tokens.size() < 2) { printf("> usage: pattern_remove pattern\r\n"); return; } @@ -952,12 +986,14 @@ void redis_commands::pattern_remove(const std::vector& tokens) const char* pattern = tokens[1].c_str(); int cocurrent; - if (tokens.size() >= 3) + if (tokens.size() >= 3) { cocurrent = atoi(tokens[2]); - else + } else { cocurrent = 10; - if (cocurrent <= 0) + } + if (cocurrent <= 0) { cocurrent = 10; + } acl::redis redis(conns_); const std::map* masters = @@ -965,23 +1001,21 @@ void redis_commands::pattern_remove(const std::vector& tokens) long long deleted = 0; - if (masters != NULL) - { + if (masters != NULL) { for (std::map::const_iterator - cit = masters->begin(); cit != masters->end(); ++cit) - { + cit = masters->begin(); cit != masters->end(); ++cit) { + const char* addr = cit->second->get_addr(); - if (addr == NULL || *addr == 0) - { + if (addr == NULL || *addr == 0) { printf("addr NULL, skip it\r\n"); continue; } deleted += pattern_remove(addr, pattern, cocurrent); } - } - else + } else { deleted += pattern_remove(addr_, pattern, cocurrent); + } acl::log::close(); acl::log::stdout_open(true); @@ -996,33 +1030,31 @@ long long redis_commands::pattern_remove(const char* addr, const char* pattern, acl::string prompt; prompt.format("Do you want to delete them all in %s ? [y/n]: ", addr); getline(buf, prompt); - if (!buf.equal("y", false)) + if (!buf.equal("y", false)) { return 0; + } size_t count = 10000; std::vector res; res.reserve(count); acl::redis_client conn(addr, conn_timeout_, rw_timeout_); - if (!passwd_.empty()) + if (!passwd_.empty()) { conn.set_password(passwd_); + } acl::redis redis(&conn); acl::atomic_long deleted = 0, error = 0, notfound = 0; int cursor = 0; - while (true) - { + while (true) { cursor = redis.scan(cursor, res, pattern, &count); - if (cursor < 0) - { + if (cursor < 0) { printf("%s => scan error: %s\r\n", addr, redis.result_error()); break; - } - else if (cursor == 0 && res.empty()) - { + } else if (cursor == 0 && res.empty()) { printf("%s => scan over\r\n", addr); break; } @@ -1047,28 +1079,26 @@ void redis_commands::remove(const std::vector& keys, int display_count = 0; for (std::vector::const_iterator cit = keys.begin(); - cit != keys.end(); ++cit) - { + cit != keys.end(); ++cit) { + cmd.clear(false); int ret = cmd.del_one((*cit).c_str()); display_count++; - if (ret < 0) - { - if (display_count < 10) + if (ret < 0) { + if (display_count < 10) { printf("del_one error: %s, key: [%s]\r\n", cmd.result_error(), (*cit).c_str()); + } ++error; - } - else if (ret == 0) - { - if (display_count < 10) + } else if (ret == 0) { + if (display_count < 10) { printf("not exist, key: [%s]\r\n", (*cit).c_str()); + } ++notfound; - } - else - { - if (display_count < 10) + } else { + if (display_count < 10) { printf("Delete ok, key: [%s]\r\n", (*cit).c_str()); + } ++deleted; } } @@ -1140,13 +1170,11 @@ void redis_commands::check_type(const std::vector& tokens) acl::redis cmd(conns_); std::vector::const_iterator cit = tokens.begin(); ++cit; - for (; cit != tokens.end(); ++cit) - { + for (; cit != tokens.end(); ++cit) { cmd.clear(false); const char* key = (*cit).c_str(); acl::redis_key_t type = cmd.type(key); - switch (type) - { + switch (type) { case acl::REDIS_KEY_NONE: printf("[%s]: NONE\r\n", key); break; @@ -1177,8 +1205,7 @@ void redis_commands::check_ttl(const std::vector& tokens) acl::redis cmd(conns_); std::vector::const_iterator cit = tokens.begin(); ++cit; - for (; cit != tokens.end(); ++cit) - { + for (; cit != tokens.end(); ++cit) { cmd.clear(false); const char* key = (*cit).c_str(); int ttl = cmd.ttl(key); @@ -1194,14 +1221,12 @@ void redis_commands::get_dbsize(const std::vector&) int total = 0; - if (!nodes.empty()) - { + if (!nodes.empty()) { for (std::vector::const_iterator - cit = nodes.begin(); cit != nodes.end(); ++cit) - { + cit = nodes.begin(); cit != nodes.end(); ++cit) { + const char* addr = (*cit)->get_addr(); - if (addr == NULL || *addr == 0) - { + if (addr == NULL || *addr == 0) { printf("addr NULL\r\n"); continue; } @@ -1211,19 +1236,19 @@ void redis_commands::get_dbsize(const std::vector&) acl::redis cmd(&conn); int n = cmd.dbsize(); printf("----- ADDR: %s, DBSIZE: %d -----\r\n", addr, n); - if (n > 0) + if (n > 0) { total += n; + } } - } - else - { + } else { acl::redis_client conn(addr_, conn_timeout_, rw_timeout_); conn.set_password(passwd_); acl::redis cmd(&conn); int n = cmd.dbsize(); printf("----- ADDR: %s, DBSIZE: %d -----\r\n", addr_.c_str(), n); - if (n > 0) + if (n > 0) { total += n; + } } printf("---- Total DBSIZE: %d -----\r\n", total); @@ -1235,28 +1260,27 @@ void redis_commands::request(const std::vector& tokens) cmd.upper(); std::map::const_iterator cit = redis_cmds_.find(cmd); - if (cit == redis_cmds_.end()) - { + if (cit == redis_cmds_.end()) { request_one(tokens); return; } - if (cit->second.broadcast.equal("all", false)) + if (cit->second.broadcast.equal("all", false)) { request_all(tokens); - else if (cit->second.broadcast.equal("master", false)) + } else if (cit->second.broadcast.equal("master", false)) { request_masters(tokens); - else if (cit->second.broadcast.equal("slave", false)) + } else if (cit->second.broadcast.equal("slave", false)) { request_slaves(tokens); - else + } else { request_one(tokens); + } } void redis_commands::request_one(const std::vector& tokens) { acl::redis redis(conns_); const acl::redis_result* result = redis.request(tokens); - if (result == NULL) - { + if (result == NULL) { printf("request error: %s, addr: %s\r\n", redis.result_error(), redis.get_client_addr()); show_request(tokens); @@ -1275,18 +1299,16 @@ void redis_commands::request_all(const std::vector& tokens) std::vector nodes; redis_util::get_all_nodes(redis, nodes); - if (nodes.empty()) - { + if (nodes.empty()) { logger_error("no node of the cluster: %s", addr_.c_str()); return; } for (std::vector::const_iterator cit - = nodes.begin(); cit != nodes.end(); ++cit) - { + = nodes.begin(); cit != nodes.end(); ++cit) { + const char* addr = (*cit)->get_addr(); - if (addr == NULL || *addr == 0) - { + if (addr == NULL || *addr == 0) { logger_error("addr NULL"); continue; } @@ -1300,18 +1322,16 @@ void redis_commands::request_masters(const std::vector& tokens) acl::redis redis(conns_); const std::map* masters = redis_util::get_masters(redis); - if (masters == NULL) - { + if (masters == NULL) { logger_error("no masters node of the cluster"); return; } for (std::map::const_iterator cit - = masters->begin(); cit != masters->end(); ++cit) - { + = masters->begin(); cit != masters->end(); ++cit) { + const char* addr = cit->second->get_addr(); - if (addr == NULL || *addr == 0) - { + if (addr == NULL || *addr == 0) { logger_error("addr NULL"); continue; } @@ -1325,18 +1345,16 @@ void redis_commands::request_slaves(const std::vector& tokens) acl::redis redis(conns_); std::vector slaves; redis_util::get_slaves(redis, slaves); - if (slaves.empty()) - { + if (slaves.empty()) { logger_error("no slaves node of the cluster"); return; } for (std::vector::const_iterator cit - = slaves.begin(); cit != slaves.end(); ++cit) - { + = slaves.begin(); cit != slaves.end(); ++cit) { + const char* addr = (*cit)->get_addr(); - if (addr == NULL || *addr == 0) - { + if (addr == NULL || *addr == 0) { logger_error("addr NULL"); continue; } @@ -1353,40 +1371,44 @@ bool redis_commands::show_result(const acl::redis_result& result, const acl::redis_result** children; acl::redis_result_t type = result.get_type(); - switch (type) - { + switch (type) { case acl::REDIS_RESULT_NIL: - if (addr && *addr) + if (addr && *addr) { printf("%s-->", addr); + } printf("[nil]\r\n"); break; case acl::REDIS_RESULT_ERROR: - if (addr && *addr) + if (addr && *addr) { printf("%s-->", addr); + } printf("-%s\r\n", result.get_error()); return false; case acl::REDIS_RESULT_STATUS: - if (addr && *addr) + if (addr && *addr) { printf("%s-->", addr); + } printf("+%s\r\n", result.get_status()); break; case acl::REDIS_RESULT_INTEGER: - if (addr && *addr) + if (addr && *addr) { printf("%s-->", addr); + } printf(":%lld\r\n", result.get_integer64()); break; case acl::REDIS_RESULT_STRING: buf.clear(); result.argv_to_string(buf); - if (!buf.empty()) + if (!buf.empty()) { printf("$%d\r\n%s\r\n", (int) buf.size(), buf.c_str()); + } break; case acl::REDIS_RESULT_ARRAY: children = result.get_children(&size); - if (size > 0) + if (size > 0) { printf("*%d\r\n", (int) size); - for (size_t i = 0; i < size; i++) - { + } + for (size_t i = 0; i < size; i++) { const acl::redis_result* rr = children[i]; acl_assert(rr != NULL); show_result(*rr, addr); @@ -1394,8 +1416,9 @@ bool redis_commands::show_result(const acl::redis_result& result, break; case acl::REDIS_RESULT_UNKOWN: default: - if (addr && *addr) + if (addr && *addr) { printf("%s-->", addr); + } printf("unknown type: %d\r\n", (int) type); return false; } @@ -1406,12 +1429,13 @@ bool redis_commands::show_result(const acl::redis_result& result, void redis_commands::show_request(const std::vector& tokens) { for (std::vector::const_iterator cit = - tokens.begin(); cit != tokens.end(); ++cit) - { - if (cit == tokens.begin()) + tokens.begin(); cit != tokens.end(); ++cit) { + + if (cit == tokens.begin()) { printf("%s", (*cit).c_str()); - else + } else { printf(" \"%s\"", (*cit).c_str()); + } } } @@ -1425,8 +1449,7 @@ void redis_commands::config_usage(void) void redis_commands::config(const std::vector& tokens) { - if (tokens.size() < 2) - { + if (tokens.size() < 2) { config_usage(); return; } @@ -1437,18 +1460,16 @@ void redis_commands::config(const std::vector& tokens) std::vector nodes; redis_util::get_all_nodes(redis, nodes); - if (nodes.empty()) - { + if (nodes.empty()) { logger_error("no node of the cluster: %s", addr_.c_str()); return; } for (std::vector::const_iterator cit - = nodes.begin(); cit != nodes.end(); ++cit) - { + = nodes.begin(); cit != nodes.end(); ++cit) { + const char* addr = (*cit)->get_addr(); - if (addr == NULL || *addr == 0) - { + if (addr == NULL || *addr == 0) { logger_error("addr NULL"); continue; } @@ -1460,8 +1481,7 @@ void redis_commands::config(const std::vector& tokens) void redis_commands::config(const char* addr, const std::vector& tokens) { - if (tokens.size() < 2) - { + if (tokens.size() < 2) { config_usage(); return; } @@ -1475,19 +1495,13 @@ void redis_commands::request_one(const char* addr, client.set_password(passwd_); acl::redis redis(&client); const acl::redis_result* result = redis.request(tokens); - if (result == NULL) - { + if (result == NULL) { printf("request error: %s\r\n", redis.result_error()); show_request(tokens); printf("\r\n"); - } - else - { - if (show_result(*result, addr) == false) - { - printf("request error\r\n"); - show_request(tokens); - printf("\r\n"); - } + } else if (!show_result(*result, addr)) { + printf("request error\r\n"); + show_request(tokens); + printf("\r\n"); } } diff --git a/app/redis_tools/redis_builder/redis_commands.h b/app/redis_tools/redis_builder/redis_commands.h index 3b2c9c571..f2bfacdf6 100644 --- a/app/redis_tools/redis_builder/redis_commands.h +++ b/app/redis_tools/redis_builder/redis_commands.h @@ -33,6 +33,7 @@ private: void show_commands(void); bool check(const char* command); + bool parse(acl::string& line, std::vector& out); void set_addr(const char* in, acl::string& out); void getline(acl::string& buf, const char* prompt = NULL); void create_cluster(); diff --git a/lib_acl_cpp/src/acl_stdafx.hpp b/lib_acl_cpp/src/acl_stdafx.hpp index a9bc7362a..3942454d8 100644 --- a/lib_acl_cpp/src/acl_stdafx.hpp +++ b/lib_acl_cpp/src/acl_stdafx.hpp @@ -56,5 +56,7 @@ //#if defined(_WIN32) || defined(_WIN64) #include "acl_cpp/lib_acl.hpp" //#endif -//#define ACL_CPP_DEBUG_MIN 40 -//#define ACL_CPP_DEBUG_MAX 70 + +#define ACL_CPP_DEBUG_MIN 40 +#define ACL_CPP_DEBUG_CONN_MANAGER 41 +#define ACL_CPP_DEBUG_MAX 70 diff --git a/lib_acl_cpp/src/connpool/connect_manager.cpp b/lib_acl_cpp/src/connpool/connect_manager.cpp index b593e5e09..668e2266e 100644 --- a/lib_acl_cpp/src/connpool/connect_manager.cpp +++ b/lib_acl_cpp/src/connpool/connect_manager.cpp @@ -298,7 +298,8 @@ connect_pool* connect_manager::create_pool(const conn_config& cf, size_t idx) pool->set_check_inter(check_inter_); } - logger("Add one service, addr: %s, count: %d", + logger_debug(ACL_CPP_DEBUG_CONN_MANAGER, 1, + "Add one service, addr: %s, count: %d", cf.addr.c_str(), (int) cf.count); return pool; } @@ -337,7 +338,8 @@ connect_pool* connect_manager::get(const char* addr, if (exclusive) { lock_.unlock(); } - logger("no connect pool for addr %s", addr); + logger_debug(ACL_CPP_DEBUG_CONN_MANAGER, 1, + "no connect pool for addr %s", addr); return NULL; }