acl/app/wizard_demo/pkv/proto/redis_object.h

107 lines
2.7 KiB
C
Raw Normal View History

2023-07-19 19:07:53 +08:00
//
// Created by shuxin <20><><EFBFBD><EFBFBD>zheng on 2023/7/19.
//
#pragma once
#include <string>
#include <vector>
2023-07-20 19:24:18 +08:00
#include "redis_type.h"
2023-07-19 19:07:53 +08:00
namespace pkv {
class redis_object;
using shared_redis = std::shared_ptr<redis_object>;
2023-07-19 19:07:53 +08:00
typedef enum {
REDIS_OBJ_UNKOWN,
REDIS_OBJ_NIL,
REDIS_OBJ_ERROR,
REDIS_OBJ_STATUS,
REDIS_OBJ_INTEGER,
REDIS_OBJ_STRING,
REDIS_OBJ_ARRAY,
} redis_obj_t;
2023-07-19 19:07:53 +08:00
class redis_object {
public:
explicit redis_object(std::vector<redis_object*>& cache, size_t cache_max);
2023-07-24 18:16:09 +08:00
~redis_object();
2023-07-21 14:20:47 +08:00
redis_object(const redis_object&) = delete;
void operator=(const redis_object&) = delete;
void set_parent(redis_object* parent);
void reset();
2023-07-21 14:20:47 +08:00
public:
2023-07-20 19:24:18 +08:00
const char* update(const char* data, size_t& len);
2023-07-19 19:07:53 +08:00
2023-07-20 22:56:34 +08:00
[[nodiscard]] bool finish() const {
2023-07-20 19:24:18 +08:00
return status_ == redis_s_finish;
}
2023-07-20 22:56:34 +08:00
[[nodiscard]] bool failed() const {
2023-07-20 19:24:18 +08:00
return status_ == redis_s_null;
2023-07-19 19:07:53 +08:00
}
2023-07-20 22:56:34 +08:00
[[nodiscard]] int get_status() const {
2023-07-19 19:07:53 +08:00
return status_;
}
[[nodiscard]] redis_obj_t get_type() const {
return type_;
2023-07-20 19:24:18 +08:00
}
2023-07-21 19:12:12 +08:00
[[nodiscard]] const char* get_cmd() const;
2023-07-23 23:18:37 +08:00
[[nodiscard]] const char* get_str() const;
[[nodiscard]] const std::vector<redis_object*>& get_objects() const {
2023-07-23 23:18:37 +08:00
return objs_;
}
2023-07-20 19:24:18 +08:00
public:
2023-07-21 16:20:04 +08:00
redis_object& set_status(const std::string& data, bool return_parent = false);
redis_object& set_error(const std::string& data, bool return_parent = false);
redis_object& set_number(int n, bool return_parent = false);
redis_object& set_string(const std::string& data, bool return_parent = false);
redis_object& create_child();
2023-07-20 19:24:18 +08:00
bool to_string(acl::string& out) const;
2023-07-19 19:07:53 +08:00
private:
2023-07-21 14:20:47 +08:00
int status_ = redis_s_begin;
redis_obj_t type_ = REDIS_OBJ_UNKOWN;
redis_object* parent_ = nullptr;
redis_object* obj_ = nullptr;
2023-07-19 19:07:53 +08:00
std::string buf_;
2023-07-21 14:20:47 +08:00
int cnt_ = 0;
size_t cache_max_;
std::vector<redis_object*>& cache_;
std::vector<redis_object*> objs_;
2023-07-19 19:07:53 +08:00
private:
const char* get_line(const char*, size_t&, std::string&, bool&);
2023-07-21 14:20:47 +08:00
const char* get_length(const char*, size_t&, int&, bool&);
2023-07-20 19:24:18 +08:00
const char* get_data(const char*, size_t&, size_t);
const char* parse_object(const char*, size_t&);
2023-07-19 19:07:53 +08:00
public:
2023-07-20 19:24:18 +08:00
const char* parse_begin(const char* data, size_t& len);
const char* parse_status(const char* data, size_t& len);
const char* parse_error(const char* data, size_t& len);
const char* parse_number(const char* data, size_t& len);
const char* parse_strlen(const char* data, size_t& len);
const char* parse_string(const char* data, size_t& len);
const char* parse_strend(const char* data, size_t& len);
const char* parse_arlen(const char* data, size_t& len);
const char* parse_array(const char* data, size_t& len);
2023-07-19 19:07:53 +08:00
};
} // namespace pkv