acl/app/wizard_demo/pkv/action/redis_key.cpp

95 lines
2.2 KiB
C++
Raw Normal View History

2023-07-31 18:34:16 +08:00
//
// Created by shuxin <20><><EFBFBD><EFBFBD>zheng on 2023/7/31.
//
#include "stdafx.h"
2023-08-01 14:16:44 +08:00
#include "proto/redis_coder.h"
#include "redis_handler.h"
2023-07-31 18:34:16 +08:00
#include "redis_key.h"
namespace pkv {
2023-08-01 14:16:44 +08:00
redis_key::redis_key(redis_handler& handler, const redis_object& obj)
2023-08-01 16:06:17 +08:00
: redis_command(handler, obj)
2023-07-31 18:34:16 +08:00
{
2023-08-01 14:16:44 +08:00
(void) handler_;
2023-07-31 18:34:16 +08:00
}
bool redis_key::del(redis_coder& result) {
auto& objs = obj_.get_objects();
if (objs.size() < 2) {
logger_error("invalid SET params' size=%zd", objs.size());
return false;
}
auto key = objs[1]->get_str();
if (key == nullptr || *key == 0) {
logger_error("key null");
return false;
}
2023-08-01 14:16:44 +08:00
if (!handler_.get_db()->del(key)) {
2023-07-31 18:34:16 +08:00
logger_error("db del error, key=%s", key);
return false;
}
result.create_object().set_number(1);
return true;
}
bool redis_key::type(redis_coder& result) {
2023-08-01 16:06:17 +08:00
auto& objs = obj_.get_objects();
if (objs.size() < 2) {
logger_error("invalid TYPE params' size=%zd", objs.size());
return false;
}
auto key = objs[1]->get_str();
if (key == nullptr || *key == 0) {
logger_error("key null");
return false;
}
std::string buff;
if (!handler_.get_db()->get(key, buff) || buff.empty()) {
logger_error("db get error, key=%s", key);
return false;
}
2023-08-01 14:16:44 +08:00
auto& coder = handler_.get_coder();
coder.clear();
2023-08-01 16:06:17 +08:00
size_t len = buff.size();
(void) coder.update(buff.c_str(), len);
if (len > 0) {
logger_error("invalid buff in db, key=%s", key);
return false;
}
auto& objs2 = coder.get_objects();
if (objs2.size() != 2) {
logger_error("invalid object in db, key=%s, size=%zd", key, objs2.size());
return false;
}
auto o = objs2[0];
if (o->get_type() != REDIS_OBJ_ARRAY) {
logger_error("invalid object type=%d, key=%s", (int) o->get_type(), key);
return false;
}
auto& objs3 = o->get_objects();
if (objs3.size() < 2) {
logger_error("invalid objects size=%zd", objs3.size());
return false;
}
auto type = objs3[0]->get_str();
result.create_object().set_status(type);
2023-07-31 18:34:16 +08:00
return true;
}
} // namespace pkv