uint64_t CRedisClient::hgetall(const string &key, CRedisClient::MapString &values) { CResult result; hgetall( key, result ); ReplyType type = result.getType(); if ( REDIS_REPLY_ERROR == type ) { throw ReplyErr( result.getErrorString() ); }else if ( REDIS_REPLY_ARRAY != type ) { throw ProtocolErr( "HGETALL: data recv is not arry" ); } CResult::ListCResult::const_iterator it = result.getArry().begin(); CResult::ListCResult::const_iterator it2 = it; CResult::ListCResult::const_iterator end = result.getArry().end(); for ( ; it != end; ++it ) { it2 = it++; // the next element is value. values.insert( MapString::value_type( *it2, *it ) ); } return values.size(); }
string CRedisClient::subscribe() { CResult result; _getReply(result); auto t = result.getArry().rbegin(); return *t; }
bool CRedisClient::hscan(const string &key, int64_t cursor, MapString &values, const string &match, uint64_t count ) { static uint64_t lastCur = 0; uint64_t realCur = 0; CResult result; if ( cursor >= 0 ) { realCur = cursor; }else { realCur = lastCur; } Command cmd( "HSCAN" ); cmd << key << realCur; if ( "" != match ) { cmd << "MATCH" << match; } if ( 0 != count ) { cmd << "COUNT" << count; } _getArry( cmd, result ); CResult::ListCResult::const_iterator it = result.getArry().begin(); lastCur = _valueFromString<uint64_t>( it->getString() ); ++it; _getStringMapFromArry( it->getArry(), values ); return ( lastCur == 0 ? false : true ); }
void CRedisClient::time(string& currentseconds,string& microseconds) { CResult result; Command cmd( "TIME" ); _getArry(cmd,result); CResult::ListCResult::const_iterator it = result.getArry().begin(); currentseconds=it->getString(); ++it; microseconds=it->getString(); }
int RedisClient::scan( VecString& values , const int& index , const string& pattern , const int& count ) { BuildCommand cmd("SCAN"); string val; SInt64 nextNo; CResult arry; cmd << index; if ( !pattern.empty() ) { LOG_DEBUG("PATTERN:%s", pattern.c_str()); cmd << "MATCH" << pattern; } if ( count > 0 && count != 10 ) { LOG_DEBUG("PATTERN:%s", pattern.c_str()); cmd << "COUNT" << count; } if ( !_getArry(cmd, arry) ) return -1; CResult::ListCResult arrList = arry.getArry(); if ( arrList.size() != 2 ) return -2; CResult::ListCResult::const_iterator it = arrList.begin(); val = it->getString(); //throw TypeErr std::istringstream istr(val); istr >> nextNo; if ( istr.fail() ) { LOG_ERROR("%s: data received is unexpected",val.c_str()); } LOG_DEBUG("nextNo:%ld", nextNo); ++it; CResult::ListCResult::const_iterator itKeybgein = it->getArry().begin(); CResult::ListCResult::const_iterator itKeyend = it->getArry().end(); values.clear(); while ( itKeybgein != itKeyend ) { val = itKeybgein->getString(); values.push_back(val); itKeybgein++; } return nextNo; }
bool RedisClient::scriptExists( const string& script ) { BuildCommand cmd("SCRIPT"); cmd << "EXISTS" << script; CResult rst; _getArry(cmd, rst); CResult::ListCResult lst = rst.getArry(); CResult::ListCResult::const_iterator it = lst.begin(); return it->getInt(); }
uint64_t CRedisClient::hvals(const string &key, CRedisClient::VecString &values) { CResult result; hvals( key, result ); ReplyType type = result.getType(); if ( REDIS_REPLY_ERROR == type ) { throw ReplyErr( result.getErrorString() ); }else if ( REDIS_REPLY_ARRAY != type ) { throw ProtocolErr( "HVALS: data recved is not arry"); } CResult::ListCResult::const_iterator it = result.getArry().begin(); CResult::ListCResult::const_iterator end = result.getArry().end(); for ( ; it != end; ++it ) { values.push_back( static_cast<string>(*it) ); } return values.size(); }
std::string CResult::display( const CResult &ele, int indent ) { ReplyType e = ele.getType( ); string type =CResult::getTypeString( e ); std::stringstream out; if ( REDIS_REPLY_ARRAY == e ) { CResult::ListCResult::const_iterator it = ele.getArry().begin() ; indent += 3; out <<"{\n"; for ( int i = 0; i < indent; i++ ) out << " "; indent += 3; out << "type:" << type <<", value:[" << std::endl; for ( ; it!=ele.getArry().end(); it++ ) { for ( int i = 0; i < indent; i++ ) out << " "; out << display( *it, indent ) << std::endl; } for ( int i = 0; i < indent-3; i++ ) out << " "; out << "]\n"; for ( int i = 0; i < indent-6; i++ ) out << " "; out << "};"; }else { out << "{type: " << type << " ,value: " << std::string( ele ) << "};"; } return out.str() ; }
uint64_t CRedisClient::hkeys(const string &key, CRedisClient::VecString &values) { CResult result; hkeys( key, result ); ReplyType type = result.getType(); if ( REDIS_REPLY_ERROR == type ) { throw ReplyErr( result.getErrorString() ); }else if ( REDIS_REPLY_ARRAY != type ) { throw ProtocolErr("HKEYS: data recved is not arry"); } _getValueFromArry( result.getArry(), values ); return values.size(); }
bool CRedisClient::hscan(const string &key, int64_t cursor, VecString& values,const string &match, uint64_t count ) { static uint64_t lastCur = 0; uint64_t realCur = 0; CResult result; if ( cursor >= 0 ) { realCur = cursor; }else { realCur = lastCur; } hscan( key, realCur, match, count, result ); ReplyType type = result.getType() ; if ( REDIS_REPLY_ERROR == type ) { throw ReplyErr( result.getErrorString() ); }else if ( REDIS_REPLY_ARRAY != type ) { throw ProtocolErr( "HSCAN: data recved is not arry" ); } CResult::ListCResult::const_iterator it = result.getArry().begin(); if ( REDIS_REPLY_STRING != it->getType() ) { throw ProtocolErr( "HSCAN: first ele is not string" ); } lastCur = _valueFromString<uint64_t>( it->getString() ); ++it; _getValueFromArry( it->getArry(), values ); return ( lastCur == 0 ? false : true ); }