搜狗workflow--04.redis_cli

鲁钱明
2023-12-01
class RedisValue
{
public:
	// nil
	RedisValue();
	virtual ~RedisValue();

	//copy constructor
	RedisValue(const RedisValue& copy);
	//copy operator
	RedisValue& operator= (const RedisValue& copy);
	//move constructor
	RedisValue(RedisValue&& move);
	//move operator
	RedisValue& operator= (RedisValue&& move);

	// release memory and change type to nil
	void set_nil();
	void set_int(int64_t intv);
	void set_string(const std::string& strv);
	void set_status(const std::string& strv);
	void set_error(const std::string& strv);
	void set_string(const char *str, size_t len);
	void set_status(const char *str, size_t len);
	void set_error(const char *str, size_t len);
	void set_string(const char *str);
	void set_status(const char *str);
	void set_error(const char *str);
	// array(resize)
	void set_array(size_t new_size);
	// set data by C style data struct
	void set(const redis_reply_t *reply);

	// Return true if not error
	bool is_ok() const;
	// Return true if error
	bool is_error() const;
	// Return true if nil
	bool is_nil() const;
	// Return true if integer
	bool is_int() const;
	// Return true if array
	bool is_array() const;
	// Return true if string/status
	bool is_string() const;
	// Return type of C style data struct
	int get_type() const;

	// Copy. If type isnot string/status/error, returns an empty std::string
	std::string string_value() const;
	// No copy. If type isnot string/status/error, returns NULL.
	const std::string *string_view() const;
	// If type isnot integer, returns 0
	int64_t int_value() const;
	// If type isnot array, returns 0
	size_t arr_size() const;
	// If type isnot array, do nothing
	void arr_clear();
	// If type isnot array, do nothing
	void arr_resize(size_t new_size);
	// Always return std::vector.at(pos); notice overflow exception
	RedisValue& arr_at(size_t pos) const;
	// Always return std::vector[pos]; notice overflow exception
	RedisValue& operator[] (size_t pos) const;

	// transform data into C style data struct
	bool transform(redis_reply_t *reply) const;
	// equal to set_nil();
	void clear();
	// format data to text
	std::string debug_string() const;

public:
	struct StatusTag {};
	struct ErrorTag {};
	// integer
	RedisValue(int64_t intv);
	// string
	RedisValue(const char *str);
	RedisValue(const char *str, size_t len);
	RedisValue(const std::string& strv);
	// status
	RedisValue(const char *str, StatusTag status_tag);
	RedisValue(const char *str, size_t len, StatusTag status_tag);
	RedisValue(const std::string& strv, StatusTag status_tag);
	// error
	RedisValue(const char *str, ErrorTag error_tag);
	RedisValue(const char *str, size_t len, ErrorTag error_tag);
	RedisValue(const std::string& strv, ErrorTag error_tag);

private:
	void free_data();
	void only_set_string_data(const std::string& strv);
	void only_set_string_data(const char *str, size_t len);
	void only_set_string_data(const char *str);

	int type_;
	void *data_;
};
//g++ -std=c++11 -o testredis testredis.cc -I/usr/local/include/workflow -lworkflow

#include <netdb.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include "workflow/RedisMessage.h"
#include "workflow/WFTaskFactory.h"
#include "workflow/WFFacilities.h"

#define RETRY_MAX       2

struct tutorial_task_data
{
	std::string url;
	std::string key;
};

static WFFacilities::WaitGroup wait_group(1);

void redis_callback(WFRedisTask *task)
{
	fprintf(stderr, "redis_callback\n");

	protocol::RedisRequest *req = task->get_req();
	protocol::RedisResponse *resp = task->get_resp();
	int state = task->get_state();
	int error = task->get_error();
	protocol::RedisValue val;

	switch (state)
	{
	case WFT_STATE_SYS_ERROR:
		fprintf(stderr, "system error: %s\n", strerror(error));
		break;
	case WFT_STATE_DNS_ERROR:
		fprintf(stderr, "DNS error: %s\n", gai_strerror(error));
		break;
	case WFT_STATE_SSL_ERROR:
		fprintf(stderr, "SSL error: %d\n", error);
		break;
	case WFT_STATE_TASK_ERROR:
		fprintf(stderr, "Task error: %d\n", error);
		break;
	case WFT_STATE_SUCCESS:
		resp->get_result(val);
		if (val.is_error())
		{
		   fprintf(stderr, "Error reply. Need a password?\n");
		   state = WFT_STATE_TASK_ERROR;
		}
		break;
	}

	if (state != WFT_STATE_SUCCESS)
	{
		fprintf(stderr, "Failed. Press Ctrl-C to exit.\n");
		return;
	}

	std::string cmd;
	req->get_command(cmd);
	if (cmd == "SET")
	{
		tutorial_task_data *data = (tutorial_task_data *)task->user_data;
		WFRedisTask *next = WFTaskFactory::create_redis_task(data->url,
															 RETRY_MAX,
															 redis_callback);

		//void set_request(const std::string& command, const std::vector<std::string>& params);
		next->get_req()->set_request("GET", { data->key });
		/* Push next task(GET task) to current series. */
		series_of(task)->push_back(next);
		fprintf(stderr, "Redis SET request success. Trying to GET...\n");
	}
	else /* if (cmd == "GET") */
	{
		if (val.is_string())
		{
			fprintf(stderr, "Redis GET success. value = %s\n",
					val.string_value().c_str());
		}
		else
		{
			fprintf(stderr, "Error: Not a string value. \n");
		}

		fprintf(stderr, "Finished. Press Ctrl-C to exit.\n");
	}
	//
	wait_group.done();
}


//set,hmset
void redis_callback1(WFRedisTask *task)
{
	fprintf(stderr, "redis_callback\n");

	protocol::RedisRequest *req = task->get_req();
	protocol::RedisResponse *resp = task->get_resp();
	int state = task->get_state();
	int error = task->get_error();
	protocol::RedisValue val;

	switch (state)
	{
	case WFT_STATE_SYS_ERROR:
		fprintf(stderr, "system error: %s\n", strerror(error));
		break;
	case WFT_STATE_DNS_ERROR:
		fprintf(stderr, "DNS error: %s\n", gai_strerror(error));
		break;
	case WFT_STATE_SSL_ERROR:
		fprintf(stderr, "SSL error: %d\n", error);
		break;
	case WFT_STATE_TASK_ERROR:
		fprintf(stderr, "Task error: %d\n", error);
		break;
	case WFT_STATE_SUCCESS:
		resp->get_result(val);
		if (val.is_error())
		{
		   fprintf(stderr, "Error reply. Need a password?\n");
		   state = WFT_STATE_TASK_ERROR;
		}
		break;
	}

	if (state != WFT_STATE_SUCCESS)
	{
		fprintf(stderr, "Failed. Press Ctrl-C to exit.\n");
		return;
	}
    else
    {
        fprintf(stderr, "Redis SET request success.\n");
    }
    wait_group.done();
}

//hgetall
void redis_callback2(WFRedisTask *task)
{
	fprintf(stderr, "redis_callback\n");

	protocol::RedisRequest *req = task->get_req();
	protocol::RedisResponse *resp = task->get_resp();
	int state = task->get_state();
	int error = task->get_error();
	protocol::RedisValue val;

	switch (state)
	{
	case WFT_STATE_SYS_ERROR:
		fprintf(stderr, "system error: %s\n", strerror(error));
		break;
	case WFT_STATE_DNS_ERROR:
		fprintf(stderr, "DNS error: %s\n", gai_strerror(error));
		break;
	case WFT_STATE_SSL_ERROR:
		fprintf(stderr, "SSL error: %d\n", error);
		break;
	case WFT_STATE_TASK_ERROR:
		fprintf(stderr, "Task error: %d\n", error);
		break;
	case WFT_STATE_SUCCESS:
		resp->get_result(val);
		if (val.is_error())
		{
		   fprintf(stderr, "Error reply. Need a password?\n");
		   state = WFT_STATE_TASK_ERROR;
		}
		break;
	}

	if (state != WFT_STATE_SUCCESS)
	{
		fprintf(stderr, "Failed. Press Ctrl-C to exit.\n");
		return;
	}
    else
    {
        if (val.is_array())
		{
			fprintf(stderr, "Redis hgetall success. is_array\n");
            size_t sz = val.arr_size();
            for (int i=0; i<sz; i++) 
            {
                fprintf(stderr, "%s\n", val[i].string_value().c_str());

            }
		}
		else
		{
			fprintf(stderr, "Error: Not a array value. \n");
		}

    }
    wait_group.done();
}

int main(int argc, char *argv[])
{
	WFRedisTask *task;


	//signal(SIGINT, sig_handler);

	/* This struct only used in this tutorial. */
	struct tutorial_task_data data;

	/* Redis URL format: redis://:password@host:port/dbnum
	   examples:
	   redis://127.0.0.1
	   redis://:12345@redis.sogou:6379/3
	*/
	data.url = "redis://:123456@localhost:6379/0"; //argv[1];
    /*
	if (strncasecmp(argv[1], "redis://", 8) != 0 &&
		strncasecmp(argv[1], "rediss://", 9) != 0)
	{
		data.url = "redis://" + data.url;
	}
    */

	data.key = "person";

	task = WFTaskFactory::create_redis_task(data.url, RETRY_MAX,
											redis_callback2);//1
	protocol::RedisRequest *req = task->get_req();
	//req->set_request("SET", { data.key, argv[3] });

    //./testredis redis://:123456@localhost:6379/0 person name qqq age 10
    

	/* task->user_data is a public (void *), can store anything. */
	//task->user_data = &data;

	
    //req->set_request("HMSET", { data.key, "name", "qqq", "age", "10"});
    req->set_request("HGETALL", { data.key});
    

    /* task->start() equel to:
	 * Workflow::start_series_work(task, nullptr) or
	 * Workflow::create_series_work(task, nullptr)->start() */
	task->start();

	wait_group.wait();
	return 0;
}

//hmset person name qqq age 10
//hgetall person
[gdut17@localhost test]$ g++ -std=c++11 -o testredis testredis.cc -I/usr/local/include/workflow -lworkflow
[gdut17@localhost test]$ ./testredis 
redis_callback
Redis hgetall success. is_array
name
qqq
age
10
127.0.0.1:6379> hgetall person
1) "name"
2) "qqq"
3) "age"
4) "10"

 类似资料: