bool RedisClient::getRange(const std::string &key, int start, int stop, std::string &ret) { Batch *batch; Executor *executor; if( !exists(key) ) { Log::warn("redis no key: %s", key.c_str()); return true; } bzero(cmd,CMD_SIZE); snprintf(cmd, CMD_SIZE, "ZREVRANGE %s %d %d\r\n", key.c_str(), start, stop); batch = Batch_new(); Batch_write(batch, cmd, strlen(cmd), 1); executor = Executor_new(); Executor_add(executor, connection, batch); int rr = Executor_execute(executor, timeOutMSec); Executor_free(executor); if(rr <= 0) { Log::err("redis cmd false: %s",cmd); Batch_free(batch); return false; } else { ReplyType reply_type; char *reply_data; size_t reply_len; int level; while((level = Batch_next_reply(batch, &reply_type, &reply_data, &reply_len))) { if(reply_type == RT_ERROR) { Log::err("redis getRange: false: %s", reply_data); } else if(RT_BULK == reply_type) ret += std::string(reply_data, reply_len) + ","; } } if (!ret.empty()) ret.erase(std::prev(ret.end())); Batch_free(batch); return true; }
bool RedisClient::getRange(const std::string &key, int start, int stop, std::list<std::string> &ret) { bool vret = false; Batch *batch; Executor *executor; if( !exists(key) ) { return true; } bzero(cmd,CMD_SIZE); int rrr = snprintf(cmd, CMD_SIZE, "ZREVRANGE %s %d %d\r\n", key.c_str(), start, stop); batch = Batch_new(); Batch_write(batch, cmd, rrr, 1); executor = Executor_new(); Executor_add(executor, connection, batch); int rr = Executor_execute(executor, timeOutMSec); if(rr <= 0) { Log::err("redis cmd false: %s",cmd); } else { ReplyType reply_type; char *reply_data; size_t reply_len; int level; while((level = Batch_next_reply(batch, &reply_type, &reply_data, &reply_len))) { if(reply_type == RT_ERROR) { Log::err("redis cmd: getRange false: %s", reply_data); } else if(RT_BULK == reply_type) { ret.push_back(std::string(reply_data, reply_len)); } } vret = true; } Executor_free(executor); Batch_free(batch); return vret; }
int RedisClient::zcount(const std::string &key, long Min, long Max) const { int ret; Batch *batch; Executor *executor; ret = -1; batch= Batch_new(); bzero(cmd,CMD_SIZE); snprintf(cmd, CMD_SIZE, "ZSCORE %s %ld %ld\r\n", key.c_str(), Min, Max); Batch_write(batch, cmd, strlen(cmd), 1); executor = Executor_new(); Executor_add(executor, connection, batch); int rr = Executor_execute(executor, timeOutMSec); Executor_free(executor); if(rr <= 0) { Log::err("redis cmd false: %s",cmd); Batch_free(batch); return ret; } else { ReplyType reply_type; char *reply_data; size_t reply_len; int level; while((level = Batch_next_reply(batch, &reply_type, &reply_data, &reply_len))) { if(reply_type == RT_ERROR) { Log::err("redis cmd: zcount false: %s",reply_data); } else if(reply_type == RT_INTEGER) { ret = strtol(reply_data,NULL,10); break; } } } Batch_free(batch); return ret; }
bool RedisClient::execCmd(const std::string &cmd) { Batch *batch; Executor *executor; batch= Batch_new(); Batch_write(batch, cmd.c_str(), cmd.size(), 1); executor = Executor_new(); Executor_add(executor, connection, batch); int rr = Executor_execute(executor, timeOutMSec); Executor_free(executor); if(rr <= 0) { Log::err("redis cmd false: %s",cmd.c_str()); Batch_free(batch); return false; } else { ReplyType reply_type; char *reply_data; size_t reply_len; int level; while((level = Batch_next_reply(batch, &reply_type, &reply_data, &reply_len))) { if(reply_type == RT_ERROR) { Log::err("redis cmd: %s false: %s",cmd.c_str(), reply_data); } else if(reply_type == RT_INTEGER) { // ret = strtol(reply_data,NULL,10); break; } } } Batch_free(batch); return true; }
std::string RedisClient::get(const std::string &key) { std::string ret; Batch *batch = Batch_new(); bzero(cmd,CMD_SIZE); snprintf(cmd, CMD_SIZE, "GET %s\r\n", key.c_str()); Batch_write(batch, cmd, strlen(cmd), 1); Executor *executor = Executor_new(); Executor_add(executor, connection, batch); int rr = Executor_execute(executor, timeOutMSec); Executor_free(executor); if(rr <= 0) { Log::err("redis cmd false: %s",cmd); Batch_free(batch); return std::string(); } //read out replies ReplyType reply_type; char *reply_data; size_t reply_len; int level; while((level = Batch_next_reply(batch, &reply_type, &reply_data, &reply_len))) { if(reply_type == RT_ERROR) { Log::err("redis cmd: isConnected false: %s", reply_data); } else if(reply_type == RT_BULK) { ret = base64_decode(std::string(reply_data)); } } Batch_free(batch); return ret; }
bool RedisClient::isConnected() const { bool ret = false; Batch *batch = Batch_new(); bzero(cmd,CMD_SIZE); snprintf(cmd, CMD_SIZE, "PING\r\n"); Batch_write(batch, cmd, strlen(cmd), 1); Executor *executor = Executor_new(); Executor_add(executor, connection, batch); int rr = Executor_execute(executor, timeOutMSec); Executor_free(executor); if(rr <= 0) { Log::err("redis cmd false: %s",cmd); Batch_free(batch); return false; } //read out replies ReplyType reply_type; char *reply_data; size_t reply_len; int level; while((level = Batch_next_reply(batch, &reply_type, &reply_data, &reply_len))) { if(reply_type == RT_ERROR) { Log::err("redis cmd: isConnected false: %s", reply_data); } else if(reply_type == RT_OK) { std::string ans = std::string(reply_data); if(ans.compare(0, 4, "PONG") == 0){ ret = true; break; } } } Batch_free(batch); return ret; }
bool RedisClient::_addVal(const std::string &key, double score, const std::string &member) { Batch *batch = Batch_new(); bzero(cmd,CMD_SIZE); snprintf(cmd, CMD_SIZE, "ZADD %s %f %s\r\n", key.c_str(), score, member.c_str()); Batch_write(batch, cmd, strlen(cmd), 1); // printf("cmd: %s\n", cmd); Executor *executor = Executor_new(); Executor_add(executor, connection, batch); int rr = Executor_execute(executor, timeOutMSec); Executor_free(executor); if(rr <= 0) { Log::err("redis cmd false: %s",cmd); Batch_free(batch); return false; } Batch_free(batch); return false; }
int main( int argc, char *argv[], char *env[] ) { char Prompt[MAXSIZE]; char Input[MAXSIZE]; int i; int count; char *look; Process *child; Parser *p; Executor *e; /* Point to our name */ PROGNAME = argv[0]; /* * Construct a parser to handle the input * * We delimit on the space and "|" for now, but need to keep the * "|" so we need to it as a Special Delimiter */ p = Parser_new(); Parser_setDelimiters( p, " |" ); Parser_setSpecialDelimiters( p, "|" ); /* * Construct an executor to run the commands * * We delimit on the "|" for now */ e = Executor_new(); Executor_setExecDelimiters( e, "|" ); /* * Setup the signals */ setupSignals(); /* * Starting from the first true argument, scan the argument table */ for( i = 1; i < argc; i++ ) { if( !strncmp( argv[i], "--version", 10 ) ) { Version(); } if( !strncmp( argv[i], "--help", 7 ) ) { Usage(); } } /* Construct the prompt */ constructPrompt( Prompt, MAXSIZE ); do { gotSignal = 0; /* Infinite loop to read in from the user */ for( printf("%s ", Prompt); fgets( Input, MAXSIZE, stdin) != NULL; printf("%s ", Prompt) ) { /* They just pressed Enter */ if( *Input == '\n' ) { continue; } /* Quit */ if( !strncmp( Input, "exit\n", 5 ) ) { gotSignal = 0; break; } /* * Background the process */ if( !strncmp( Input, "bg\n", 3 ) ) { child = ProcControl_get(ProcControl_numProcs()); if( child != NULL ) { printf("Backgrounded %d ", Process_getPID(child)); Process_printCommand( child, stdout ); printf("\n"); Process_resume(child); Parser_clear( p ); gotSignal = 0; } else { fprintf( stderr, "\n!!! There is no child to background.\n" ); } ProcControl_printTable( stdout ); continue; } /* * List current jobs */ if( !strncmp( Input, "jobs\n", 5 ) ) { ProcControl_printTable( stdout ); Parser_clear(p); gotSignal = 0; continue; } /* * Foreground the last process */ if( !strncmp( Input, "fg\n", 3 ) ) { child = ProcControl_get(ProcControl_numProcs()); RUNNING = child; if( child != NULL ) { Process_resume( RUNNING ); printf("Forground process %d ", Process_getPID(RUNNING) ); Process_printCommand( RUNNING, stdout ); printf("\n"); Process_wait( RUNNING, PROCESS_WAIT ); Parser_clear(p); gotSignal = 0; } else { fprintf(stderr, "\n!!! There are no backgrounded children to " "bring to the fore.\n\n"); } continue; } /* Parse the input */ Parser_parse( p, Input ); while( (RUNNING = Executor_execute( e, p ) ) != NULL) { ProcControl_add(RUNNING); Process_start( RUNNING ); if( RUNNING != NULL ) { Process_wait( RUNNING, PROCESS_WAIT); } } /* Clear the parser's token list */ Parser_clear( p ); gotSignal = 0; } }while( gotSignal ); printf("\nProgram exit.\n"); /* Clean up children that haven't finished */ ProcControl_free(); /* Free our parser and executor */ Parser_free( p ); Executor_free( e ); Signal_clear(); return 0; }