|
| 1 | + |
| 2 | +#include <string> |
| 3 | + |
| 4 | +#include <aws/core/Aws.h> |
| 5 | + |
| 6 | +#include "redis.hpp" |
| 7 | +#include "utils.hpp" |
| 8 | + |
| 9 | +Redis::Redis(std::string redis_hostname, int redis_port) |
| 10 | +{ |
| 11 | + _context = redisConnect(redis_hostname.c_str(), redis_port); |
| 12 | + if (_context == nullptr || _context->err) { |
| 13 | + if (_context) { |
| 14 | + std::cerr << "Redis Error: " << _context->errstr << '\n'; |
| 15 | + } else { |
| 16 | + std::cerr << "Can't allocate redis context\n"; |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +bool Redis::is_initialized() |
| 22 | +{ |
| 23 | + return _context != nullptr; |
| 24 | +} |
| 25 | + |
| 26 | +Redis::~Redis() |
| 27 | +{ |
| 28 | + redisFree(_context); |
| 29 | +} |
| 30 | + |
| 31 | +uint64_t Redis::download_file(Aws::String const &key, |
| 32 | + int &required_retries, bool with_backoff) |
| 33 | +{ |
| 34 | + std::string comm = "GET " + key; |
| 35 | + |
| 36 | + auto bef = timeSinceEpochMillisec(); |
| 37 | + int retries = 0; |
| 38 | + const int MAX_RETRIES = 1500; |
| 39 | + |
| 40 | + while (retries < MAX_RETRIES) { |
| 41 | + |
| 42 | + redisReply* reply = (redisReply*) redisCommand(_context, comm.c_str()); |
| 43 | + |
| 44 | + if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) { |
| 45 | + |
| 46 | + retries += 1; |
| 47 | + if(with_backoff) { |
| 48 | + int sleep_time = retries; |
| 49 | + if (retries > 100) { |
| 50 | + sleep_time = retries * 2; |
| 51 | + } |
| 52 | + std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time)); |
| 53 | + } |
| 54 | + |
| 55 | + } else { |
| 56 | + |
| 57 | + uint64_t finishedTime = timeSinceEpochMillisec(); |
| 58 | + required_retries = retries; |
| 59 | + |
| 60 | + freeReplyObject(reply); |
| 61 | + return finishedTime - bef; |
| 62 | + |
| 63 | + } |
| 64 | + freeReplyObject(reply); |
| 65 | + } |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +uint64_t Redis::upload_file(Aws::String const &key, |
| 70 | + int size, char* pBuf) |
| 71 | +{ |
| 72 | + std::string comm = "SET " + key + " %b"; |
| 73 | + |
| 74 | + |
| 75 | + uint64_t bef = timeSinceEpochMillisec(); |
| 76 | + redisReply* reply = (redisReply*) redisCommand(_context, comm.c_str(), pBuf, size); |
| 77 | + uint64_t finishedTime = timeSinceEpochMillisec(); |
| 78 | + |
| 79 | + if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) { |
| 80 | + std::cerr << "Failed to write in Redis!" << std::endl; |
| 81 | + abort(); |
| 82 | + } |
| 83 | + freeReplyObject(reply); |
| 84 | + |
| 85 | + return finishedTime - bef; |
| 86 | +} |
| 87 | + |
| 88 | +uint64_t Redis::delete_file(std::string const &key) |
| 89 | +{ |
| 90 | + std::string comm = "DEL " + key; |
| 91 | + |
| 92 | + uint64_t bef = timeSinceEpochMillisec(); |
| 93 | + redisReply* reply = (redisReply*) redisCommand(_context, comm.c_str()); |
| 94 | + uint64_t finishedTime = timeSinceEpochMillisec(); |
| 95 | + |
| 96 | + if (reply->type == REDIS_REPLY_NIL || reply->type == REDIS_REPLY_ERROR) { |
| 97 | + std::cerr << "Couldn't delete the key!" << '\n'; |
| 98 | + abort(); |
| 99 | + } |
| 100 | + freeReplyObject(reply); |
| 101 | + |
| 102 | + return finishedTime - bef; |
| 103 | +} |
| 104 | + |
0 commit comments