Nginx Source Learning Notes (10)--Basic container--ngx_hash

Source: Internet
Author: User
Ngx_hash. {C|h} implements a hash structure that is more important in nginx, which is often used in module configuration parsing. The hash structure is read-only, only in the initial creation can be given to save in the Key-val, and then only the "Delete and change" operation.

First 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 initialize
    3. Ngx_hash_key_pt key; //function pointer for calculating hash value
    4. ngx_uint_t max_size; //maximum number of buckets allowed
    5. ngx_uint_t bucket_size; //For the maximum space allowed per bucket
    6. Char *name; //initialized hash name (used in error log)
    7. ngx_pool_t *pool; //memory pool for allocating hash structure space
    8. ngx_pool_t *temp_pool; //memory pool for allocating temporary data space
    9. } ngx_hash_init_t;


The concrete meaning also depends on the figure understanding. The code is not parsed here, although it looks cumbersome, but it is quite convenient to use.

The general operation is to create a hash and hash in the search.

To create a hash:

1. Construct an array of ngx_hash_key_t members, containing key, value, and a hash value computed using key

2. Construct a variable of the ngx_hash_init_t structure, which contains the members of the ngx_hash_t, the structure of the hash, also includes some other initial settings, such as bucket size, memory pool, etc.

3. Call Ngx_hash_init into the ngx_hash_init_t structure, ngx_hash_key_t arrays, and arrays of length, to initialize, so that the ngx_hash_init_t hash member is the hash structure we want

The process of finding is simple

1. Calculate the hash value of the key

2. Using Ngx_hash_find to find, you need to pass in both the hash value and key, return is the pointer of value

It is important to note that Nginx hash is used in the search for the post-bucket linear lookup method, so when the number of buckets is determined, the search efficiency is inversely proportional to the total key-val.

The above describes the Nginx source learning notes (10)-The basic container--ngx_hash, including the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.