Skip to content

Commit

Permalink
Update StorageInterface and Redis Implementation (#96)
Browse files Browse the repository at this point in the history
* Update StorageInterface and redis implementation

* Use unique_ptr
  • Loading branch information
jasondu7297 authored Dec 28, 2023
1 parent 0f1cfc7 commit 89af52f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class ClientHandler {
// # of operations corresponds to max_key (if seed data is not used)
config.num_operations = config.max_key;

redisCli rd;
auto pipeline = rd.pipe();
std::unique_ptr<StorageInterface> storage_server = std::make_unique<redisCli>(HOST_IP);

std::vector<std::pair<std::string, std::string>> seed_operations;
while (moreOperationsExist(config)) {
Operation op = getInitKV(config);
pipeline.set(op.key, op.value);
seed_operations.push_back({op.key, op.value});
}
pipeline.exec();
storage_server->put_batch(seed_operations);
}

void runThreaded() {
Expand Down
8 changes: 4 additions & 4 deletions src/host/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ bool check_simulate(int argc, char *argv[]) {
class RPCHandler : virtual public RPCIf {
private:
inline static oe_enclave_t *enclave;
redisCli rd;
std::unique_ptr<StorageInterface> storage_server;

public:
RPCHandler() {}
RPCHandler(): storage_server{std::make_unique<redisCli>(HOST_IP)} {}

static void setEnclaveArgs(int argc, char *argv[]) {
assert(argc >= 2);
Expand All @@ -63,7 +63,7 @@ class RPCHandler : virtual public RPCIf {
}

void access(std::string &_return, const Operation &operation) {
std::string rd_value = rd.get(operation.key);
std::string rd_value = storage_server->get(operation.key);

std::unique_ptr<unsigned char> out(new unsigned char[4096]);
size_t out_len;
Expand All @@ -81,7 +81,7 @@ class RPCHandler : virtual public RPCIf {
updated_val, out_len);
#endif

rd.put(operation.key, updated_val);
storage_server->put(operation.key, updated_val);
_return = updated_val;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libstorage/include/StorageInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class StorageInterface {
public:
virtual std::string get(const std::string &key) = 0;
virtual void put(const std::string &key, const std::string &value) = 0;
virtual void put_batch(const std::vector<std::pair<std::string, std::string>> &operations) = 0;
};

#endif
21 changes: 11 additions & 10 deletions src/libstorage/include/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

#include "StorageInterface.h"

class redisCli : public StorageInterface
{
public:
redisCli();
std::string get(const std::string &key) override;
void put(const std::string &key, const std::string &value) override;
sw::redis::Pipeline pipe();
void reconnect();
using namespace sw::redis;

private:
sw::redis::Redis redisConn = sw::redis::Redis("tcp://127.0.0.1:6379");
class redisCli : public StorageInterface {
private:
ConnectionOptions connection_options;
std::unique_ptr<Redis> redisConn;

public:
redisCli(const std::string &redis_ip, int redis_port = 6379);
std::string get(const std::string &key) override;
void put(const std::string &key, const std::string &value) override;
void put_batch(const std::vector<std::pair<std::string, std::string>> &operations) override;
};

#endif //REDIS_H
26 changes: 14 additions & 12 deletions src/libstorage/src/redis.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#include "redis.h"

using namespace sw::redis;
redisCli::redisCli(const std::string &redis_ip, int redis_port) {
connection_options.host = redis_ip;
connection_options.port = redis_port;

redisCli::redisCli() {
}

void redisCli::reconnect() {
this->redisConn = sw::redis::Redis("tcp://127.0.0.1:6379");
redisConn = std::make_unique<Redis>(connection_options);
}

std::string redisCli::get(const std::string &key) {
return this->redisConn.get(key).value_or("");
}

sw::redis::Pipeline redisCli::pipe() {
return this->redisConn.pipeline();
return redisConn->get(key).value_or("");
}

void redisCli::put(const std::string &key, const std::string &value) {
this->redisConn.set(key, value);
redisConn->set(key, value);
}

void redisCli::put_batch(const std::vector<std::pair<std::string, std::string>> &operations) {
auto pipe = redisConn->pipeline();
for (auto &op : operations) {
pipe.set(op.first, op.second);
}
pipe.exec();
}

0 comments on commit 89af52f

Please sign in to comment.