Redis client protocol implementation, redisprotocol

Source: Internet
Author: User

Redis client protocol implementation, redisprotocol

On the official website, you can visit http://redis.io/clientsto find some redis clients that have already been proven. For more information, see.

In fact, the previous article http://blog.csdn.net/yitouhan/article/details/46612925 redis client protocol parsing, I Have To RESP to do the main parsing.


The following are all the functions for parsing RESP. The external function is RedisProtocol: Decode:

Https://github.com/XJM2013/GameEngine/blob/master/lib/src/redis/redisprotocol.cpp

Static unsigned intEndLine (const char * buf, unsigned int len); static intReadNumber (const char * buf, unsigned int len); static char_ReadString (const char * buf, unsigned int left_len, int & read_len, RedisData ** data); static intReadMessage (char type, const char * buf, unsigned int len, RedisBulkData ** bulk_data); static intReadInteger (const char * buf, unsigned int len, comment ** bulk_data); static intReadString (const char * buf, unsigned int len, RedisBulkData ** bulk_data); static intReadArray (const char * buf, unsigned int left_len, redisBulkData ** bulk_data);/*: 1 \ r \ n prefix + Result + end must contain at least 4 bytes */int RedisProtocol: Decode (const char * buf, unsigned int len, RedisBulkData ** bulk_data) {if (len <4) {return OPR_MORE_DATA;} switch (buf [0]) {case '+': return ReadMessage (REPLY_TYPE_ OK, buf + 1, len-1, bulk_data); case '-': return ReadMessage (REPLY_TYPE_ERROR, buf + 1, len-1, bulk_data); case ':': return ReadInteger (buf + 1, len-1, bulk_data); case '$': return ReadString (buf + 1, len-1, bulk_data); case '*': return ReadArray (buf + 1, len-1, bulk_data); default: return OPR_DATA_INVALID ;}
From the code above, we can see that the code structure is quite clear and concise, and the function of this Code is only to process the protocol, excluding the network module and other more complex encapsulation, you do not need to perform additional read identification. I personally think it is a good choice for study collaborators. In the code, I have some memory pool functions that I don't want to use. Instead, I can use new/delete or malloc/free.
The following is a test example based on the five examples mentioned in the previous article:

Https://github.com/XJM2013/GameEngine/blob/master/Test/testredis.h

# Ifndef TEST_REDIS_H # define TEST_REDIS_H # include <string> # include "lib/include/redis/redisprotocol. h "namespace TestRedis {void ShowBulkData (RedisBulkData * data) {std: list <RedisData * >:: iterator itr = data-> data_list.begin (); for (; itr! = Data-> data_list.end (); ++ itr) {printf ("type = % d \ n", (* itr)-> type); switch (* itr) -> type) {case RedisProtocol: REPLY_TYPE_INTEGER: case RedisProtocol: REPLY_TYPE_STRING_ERROR: case RedisProtocol: REPLY_TYPE_ARRAY_ERROR: printf ("data = % d \ n", * (int *) (* itr)-> data); break; default: printf ("data = %. * s \ n ", (* itr)-> len, (* itr)-> data); break ;}} void Decode (char * reply) {RedisBulkData * data = NULL; if (RedisProtocol: Decode (reply, strlen (reply), & data) <= RedisProtocol: OPR_MORE_DATA) {return;} ShowBulkData (data ); delete data;} // short string void Test1 () {char * reply = "+ OK \ r \ n"; Decode (reply) ;}// error void Test2 () {char * reply = "-ERR unknown command 'secret' \ r \ n"; Decode (reply) ;}// integer void Test3 () {char * reply = ": 11 \ r \ n "; Decode (reply);} // long string void Test4 () {char * reply1 =" $3 \ r \ ncat \ r \ n "; printf ("reply1: \ n"); Decode (reply1); char * reply2 = "$-1 \ r \ n"; printf ("reply2: \ n "); decode (reply2);} // array void Test5 () {char * reply1 = "* 2 \ r \ n $3 \ r \ ncat \ r \ n $2 \ r \ n11 \ r \ n"; printf ("reply1: \ n "); Decode (reply1); char * reply2 =" * 2 \ r \ n $4 \ r \ nfish \ r \ n $-1 \ r \ n "; printf ("reply2: \ n"); Decode (reply2); char * reply3 = "*-1 \ r \ n"; printf ("reply3: \ n "); decode (reply3) ;}# endif
The following is the result of Test5:


Protocol parsing summary:

1. From the perspective of C/C ++, serialize and save data, and parse data faster.

For example, serialize 1000 pieces of data into 1 piece of data:

From the perspective of space allocation, 1000 pieces of data need to be allocated 1000 times, and 1 piece of data needs to be allocated 1 time.

From the parsing point of view, the RESP structure is the LV structure, that is, the length-value structure, which does not need the Flag type (T, type ), because it is of the string type. Serialization can be a combination of the TV and TLV structures. Therefore, the types and lengths in C/C ++ correspond one to one, and the integer type is 4 bytes. For strings, the TLV structure is used. Here, we only need to compare the L of RESP with the serialized T. One must match "\ r \ n" and convert the string to a number. One can determine the Data Length by reading the first byte.

2. set key val \ r \ n. When val is long, for example, 10 K Bytes long, the server must match 10 K times to match \ r \ n, in addition, \ r \ n cannot appear in val; otherwise, an error will occur.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.