Home >Backend Development >PHP Tutorial >nginx source code study notes (10) - basic container - ngx_hash

nginx source code study notes (10) - basic container - ngx_hash

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:10:54876browse

ngx_hash.{c|h} implements a more important hash structure in nginx, which is often used in module configuration analysis. The hash structure is read-only. Only the key-val pairs stored in it can be given during initial creation, and then only "add, delete, modify, and check" operations can be performed.

First let’s take a look at the memory layout of the hash structure:

nginx 源码学习笔记(十)——基本容器——ngx_hash

[cpp] view plaincopyprint?

  1. typedef struct {
  2. ngx_hash_t *hash; / Points to the hash structure to be initialized
  3. ngx_hash_key_pt key; Function pointer for column values ​​
  4. ngx_uint_t max_size;
  5. //Maximum number of buckets allowed
  6. ngx_uint_t bucket_size;
  7. //The maximum space allowed for each bucket
  8. char ) ngx_pool_t *pool; //Use In the memory pool that distributes the laidance structure
  9. ngx_pool_t*temp_pool; // Memory Ponds used to allocate temporary data space}} ngx_hash_init_t;
  10. Specific meaning meaning Also look at the pictures to understand. The code is not parsed here. Although it looks cumbersome, it is still quite convenient to use.
  11. General operations include creating hash and searching in hash. Create hash:1. Construct an array with ngx_hash_key_t as a member, including key, value and a hash value calculated using key
  12. 2. Construct a variable of the ngx_hash_init_t structure, which contains the members of ngx_hash_t, as The hash structure also includes some other initial settings, such as bucket size, memory pool, etc.
  13. 3. Call ngx_hash_init and pass in the ngx_hash_init_t structure, the array of ngx_hash_key_t, and the length of the array for initialization, so that the hash member of ngx_hash_init_t is ours The required hash structure
  14. The search process is very simple
1. Calculate the hash value of the key

2. Use ngx_hash_find to search. You need to pass in the hash value and key at the same time. What is returned is the value pointer

It should be noted that , nginx’s hash uses the linear search method after bucketing, so when the number of buckets is determined, the search efficiency is inversely proportional to the total number of key-val pairs

The above introduces the nginx source code study notes (10) - basic container - ngx_hash, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn