Nginx source code learning notes (10) -- Basic container -- ngx_hash

Source: Internet
Author: User
: This article mainly introduces the nginx source code study notes (10)-The basic container-ngx_hash. if you are interested in the PHP Tutorial, refer to it. Ngx_hash. {c | h} implements an important hash structure in nginx, which is often used in module configuration parsing. The hash structure is read-only. the key-val pair stored in the hash structure can be provided only during initial creation, and then the "add, delete, modify, and query" operation can only be performed.

Let's take a look at the memory layout of the hash structure:


[Cpp] view plaincopyprint?

  1. Typedef struct {
  2. Ngx_hash_t * hash; // point to the hash structure to be initialized
  3. Ngx_hash_key_pt key; // Function pointer used to calculate the hash value
  4. Ngx_uint_t max_size; // maximum number of buckets allowed
  5. Ngx_uint_t bucket_size; // maximum space per bucket
  6. Char * name; // The initialized hash name (available in error logs)
  7. Ngx_pool_t * pool; // memory pool used to allocate the hash structure space
  8. Ngx_pool_t * temp_pool; // memory pool used to allocate temporary data space
  9. } Ngx_hash_init_t;


For more information, see the figure. The code will not be parsed here. although it looks complicated, it is quite convenient to use.

Common operations include creating hash and hash for search.

Create hash:

1. construct an array of ngx_hash_key_t members, including the key, value, and a hash value calculated using the key.

2. construct a variable of the ngx_hash_init_t struct, which contains the ngx_hash_t member. it is a hash struct and includes some other initial settings, such as the bucket size and memory pool.

3. call ngx_hash_init to input the ngx_hash_init_t structure, the ngx_hash_key_t array, and the length of the array. in this way, the ngx_hash_init_t hash member is the hash structure we want.

The search process is simple.

1. calculate the hash value of the key

2. use ngx_hash_find for search. Both the hash value and key need to be input, and the returned value pointer is

Note that nginx hash uses the bucket-based linear lookup method for search, therefore, when the number of buckets is determined, the search efficiency is inversely proportional to the total key-val.

The above introduces the nginx source code learning notes (10)-The basic container-ngx_hash, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

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.