// redis get -- deserializes from R format SEXP get(std::string key) { // uses binary protocol, see hiredis doc at github redisReply *reply = static_cast<redisReply*>(redisCommand(prc_, "GET %s", key.c_str())); int nc = reply->len; Rcpp::RawVector res(nc); memcpy(res.begin(), reply->str, nc); freeReplyObject(reply); SEXP obj = unserializeFromRaw(res); return(obj); }
// unserialize from the std::string inline SEXP unserializeFromStr(std::string s) { // parse the std::string into a raw vector std::vector<std::string> strs; boost::regex e("^\\d.+"); if (boost::regex_match(s, e)) { boost::split(strs,s,boost::is_any_of("\t")); } Rcpp::RawVector object(strs.size() - 1); for (size_t i = 0; i < strs.size() - 1; i++) { object[i] = static_cast<unsigned char>(std::stoi(strs[i])); } return unserializeFromRaw(object); }
// redis lrange: get list from start to end -- with R serialization Rcpp::List lrange(std::string key, int start, int end) { // uses binary protocol, see hiredis doc at github redisReply *reply = static_cast<redisReply*>(redisCommand(prc_, "LRANGE %s %d %d", key.c_str(), start, end)); unsigned int len = reply->elements; //Rcpp::Rcout << "Seeing " << len << " elements\n"; Rcpp::List x(len); for (unsigned int i = 0; i < len; i++) { //Rcpp::Rcout << " Seeing size " << reply->element[i]->len << "\n"; int nc = reply->element[i]->len; Rcpp::RawVector res(nc); memcpy(res.begin(), reply->element[i]->str, nc); SEXP obj = unserializeFromRaw(res); x[i] = obj; } freeReplyObject(reply); return(x); }