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?
- typedef struct {
- ngx_hash_t *hash; //point to the hash structure to initialize
- Ngx_hash_key_pt key; //function pointer for calculating hash value
- ngx_uint_t max_size; //maximum number of buckets allowed
- ngx_uint_t bucket_size; //For the maximum space allowed per bucket
- Char *name; //initialized hash name (used in error log)
- ngx_pool_t *pool; //memory pool for allocating hash structure space
- ngx_pool_t *temp_pool; //memory pool for allocating temporary data space
- } 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.