onfirのブログ -32ページ目

onfirのブログ

ブログの説明を入力します。

Since seven years, mainly in Java and Scala code to write, my C Michael Kors Handbags language skills are degraded. In fact, it may Michael Kors Designer have completely disappeared. Except occasionally to hack, after graduating from college, I basically did not use C up. Michael Kors Clutches Everyone says, reading other people's code is very Christmas Angles iPhone 5 5S Cases good learning methods, especially the code base is an expert author or if it has a high quality review. Therefore, I am ready to read such a code library: Redis. (Related reading: 'Reading the code is to improve a shortcut excellent developer of repair') Redis is written in ANSI C source server data structures available. 'Data structure server' just smart key-value storage services is another title. Not only can you store simple strings, you can also store include hash (or map, even dicts), list, set, sorted set. We have a large number of applications in the Top10 Redis, most for the user to search the date and hotel availability and price indexing. I found Redis code is very easy to read, even for a novice like me. Code written in a very clean, and the code is relatively small (about 45,000 lines), most of them are single-threaded, depending rarely. All dependencies related to source code together, which in practice allows the compiler it becomes very simple: clone its library, and then enter the Christmas Angles iPhone 4 4S Cases make can. I decided for it by adding a command into the code. This simple things you can let me know how to deal with a Redis command and dispatch response to Christmas Beauty iPhone 4 4S Cases it. Command rand, receives an integer value as the max, and returns a random integer from 0 to max (not included max) between. This is not the idea of ​​using a key-value store, but realized it would be very enlightening. And I certainly would not submit a pull request. Disclaimer: As I said before, I am not a C language expert, so here all the code and its interpretation are in line with this clause. And I link a Redis unstable branch, so it is unstable. If you go to get the source Redis, use your favorite editor to view, you will find a different more of this article, in particular, will find different if you compile and run time. Command table top position by src / redis.c Michael Kors Hot Sale file. It is an array, the array element type is redisCommand structure. redisCommand is defined in src / redis.h in. Above redisCommandTable have a more detailed notes, each field made its interpretation. Here is the definition of get command: {\u0026 quot; get \u0026 quot;, getCommand, 2, \u0026 quot; r \u0026 quot;, 0, NULL, 1,1,1,0,0}, the first field is the name of the Michael Kors Christmas Cases command 'get'. The second field is a function pointer to the concrete realization of the command (you can see the implementation details t_string.c). The third field is the number of parameters limiting command (command received number of arguments). Cheap Michael Kors Specify this means before calling the function pointer, to find and execute the command code can make a pre-validated. This approach reduces the error-handling code must be in each command function. Count the number of parameters of the command name itself, so it only accepts two parameters: its own name, key name (we want to get its value). The fourth field, is set to 'r', is used to indicate that the command is read-only, you can not modify the key of the value or condition. There are a lot of letter symbol, you can use at this location. And in the vicinity of comment blocks, each letter has a detailed explanation mark. Followed by the field of the field is always set to 0, the latter will be used to calculate. It's just a string of fourth field contains a bit mask information. The sixth field is NULL, because it only if you use a complex logic to tell Redis which is the real key parameters when it is needed. A key point to the value stored in the Redis in a reference to the corresponding simple parameters, such as our max parameters. This mechanism allows to achieve before calling Redis commands to extract the value of key (and check key if it exists). If this field is set to a value, it will be a function pointer to the function will return a parameter index integer array (db.c in zunionInterGetKeys is an example). In the get command (most other commands) scenario, this array of information with the message behind the same three field. get command is only one parameter, and it is the key. Thus, the first parameter (key) at position 1, the last parameter (also key) in position 1, from the first parameter to the last parameter increment is 1 (Translator's Note: Source comment is: intkeystep ; / * The step between first and last key * /). The last two field redisCommand term is a measure of command, from the Redis to set up, and always initialized to zero. At the bottom of the command table, plus our command: {\u0026 quot; rand Michael Kors Bedford & Astor \u0026 quot;, randCommand, 2, \u0026 quot; rRl \u0026 quot;, 0, NULL, 0,0,0,0,0} command name is 'rand', randCommand point achieved pointer (not yet implemented), which receives two parameters (command name and max). As for the flag, which is read-only (r), returns a random and uncertain output (R), and it can Michael Kors New Arrivals also load data in Redis when using (l). Michael Kors Satchels It is no critical parameters. The next step is to increase the randCommand function prototypes in src / redis.h in. Function accepts a parameter Redis command structure a redisClient as parameter command is also used to send a response to the actual client. void randCommand (redisClient * c); The prototype should be placed in src / redis.h together with the other prototypes for all commands. Search for the following line: / * Commands prototypes * / This will help you find the right position. We add an empty implemented in src / redis.c in: void randCommand (redisClient * c) {} I will add it in the next infoCommand definition. Now, we make command. make and then we just started to compile a total of Redis service (if you already have a Redis service running locally, you should stop it): \u0026 gt; src / redis-server Then we run Redis client in another terminal, and try to run our command: \u0026 gt; redis-cli First, we try our exception handling: redis 127.0.0.1:6379\u0026gt; rand (error) ERR wrong number of arguments for 'rand' command is very good, the number of parameters limit check is normal. This time we specify a parameter: redis 127.0.0.1:6379\u0026gt; rand 1Redis stuck. This is what I expected, because I do not have any response in randCommand function. The service stopped, we went back to look at the code. We want to return an integer, so I rummage in the code examples, and finally found zcardCommand in src / t_zset.c in. This command returns Christmas Beauty iPhone 5 5S Cases an integer addReplyLongLong to end a 64-bit (long long) to the client. We also try: void randCommand (redisClient * c) {addReplyLongLong (c, Michael Kors Hamilton 3);} Then, once we make and test the command: redis 127.0.0.1:6379\u0026gt; rand 1 (integer) 3 redis 127.0.0.1: 6379 \u0026 gt; rand Michael Kors Accessories 2 (integer) 3 redis 127.0.0.1:6379\u0026gt; rand 3 (integer) 3 Well, the result is not too random, but this is only the beginning. We get from the command in the parameter max, and returns a random number from the max limit: void randCommand (redisClient * c) {long max; if (getLongFromObjectOrReply (c, c- \u0026 gt; argv [1], \u0026 amp; max, NULL) ! = REDIS_OK) return; addReplyLongLong (c, random ()% max);} Although Redis throughout the code base are used primitive type and C-type string, but it also has its own in a more general way the existence of internal object system used to represent the string, long integer and more complex types. Examples of this type of use is this: each command parameter. Each command parameters are as an object exists redisClient Redis instance c of field, argv array inside. (Translation: the source src / redis.c inside redisClient is a structure, argv is a pointer redisObject). In src / t_string.c get inside a long integer examples from Redis objects: getrangeCommand, it calls the src / object.c the getLongFromObjectOrReply functions. getLongFromObjectOrReply function receives a redisClient instance as a parameter, and check its second argument is a long integer, if the pointer is assigned to the second argument of the third parameter (this parameter is a pointer type), and Returns REDIS_OK. If the second argument is not a long integer (or overflow), the function returns REDIS_ERR. The beauty of this approach is that: if we get our randCommand function return value is REDIS_ERR, all errors must have been sent in response to a client. We try at our command: redis 127.0.0.1:6379\u0026gt; rand 10 (integer) 9 redis 127.0.0.1:6379\u0026gt; rand notanumber (error) ERR value is not an integer or out of range redis 127.0.0.1:6379\u0026gt; rand 10 (integer) 3 redis 127.0.0.1:6379\u0026gt; rand 10 (integer) 1 redis 127.0.0.1:6379\u0026gt; rand 100 (integer) 43 redis 127.0.0.1:6379\u0026gt; rand 100 (integer) 55 redis 127.0.0.1: 6379 \u0026 gt; rand 100 (integer) 86 looks good! rand seems to be a sense of how many orders are not, but from the realization that it's a lot to learn about the process of Redis, I hope you do follow down also learned a lot. Please tell me in the comments if obvious errors in this article. I am also very pleased to know that this article useful to you or you liked it. I considered writing something like that, or on other open source Redis code base. NOTE: forenroll starting in his personal blog: http: //forenroll.iteye.com/blog/1967696Code to excellent learning: Redis source Overview