Nginx 哈希
来源:互联网 发布:淘宝二手psv哪家好 编辑:程序博客网 时间:2024/05/18 12:42
1. Nginx哈希中的存储数据区是一个大的数组; 每个key指向的存储数据区大小在初始化时就已经分配好了, Nginx哈希大小是不可以改变的, 即不支持数据的增减,只是在初始化完成后用于查询.
2. Nginx数据中用于存储的字符串都是转化为小写, 因此用Nginx哈希查询时,需要将相应的字符串先转化为小写。
3. 由于哈希的存储区是大数组, 并且每个桶对应的区域大小固定, 每个桶的对应的数组要比存储的个数大一个, 最用一个的value置为NULL, 表示一个桶的结束。
4. 哈希查找过程: 由value采用hash算法得到key, 由key在bucket数据区段数组得到ngx_hash_elt_t*的指针, 指针指向这个桶的起始地址, 遍历这个桶的数组比较就能找到相应的置
图片来源: http://code.google.com/p/nginxsrp/wiki/NginxCodeReview#ngx_hash
哈希通配符的查找:
Nginx哈希支持三种类型的通配:
"*.example.com", ".example.com", and "www.example.*"
对这些字符串进行哈希前进行了预处理(ngx_hash_add_key):
"*.example.com", 经过预处理后变成了: "com.example.\0"
".example.com" 经过预处理后变成了: "com.example\0"
"www.example.*" 经过预处理后变成了: "www.example\0"
通配符hash表的实现原理 : 当构造此类型的hash表的时候,实际上是构造了一个hash表的一个“链表”,是通过hash表中的key“链接”起来的。比如:对于“*.example.com”将会构造出2个hash表,第一个hash表中有一个key为com的表项,该表项的value包含有指向第二个hash表的指针,而第二个hash表中有一个表项abc,该表项的value包含有指*.example.com对应的value的指针。那么查询的时候,比如查询www.example.com的时候,先查com,通过查com可以找到第二级的hash表,在第二级hash表中,再查找example,依次类推,直到在某一级的hash表中查到的表项对应的value对应一个真正的值而非一个指向下一级hash表的指针的时候,查询过程结束。而查找到哪里是由value地址的最低两bit表示: (这也是在申请内存时要求4字节对齐的原因, 最后两bit是0, 可以被修改来表示下述情况)
头部通配情况:
/*
* the 2 low bits of value have the special meaning:
* 00 - value is data pointer for both "example.com"
* and "*.example.com";
* 01 - value is data pointer for "*.example.com" only;
* 10 - value is pointer to wildcard hash allowing
* both "example.com" and "*.example.com";
* 11 - value is pointer to wildcard hash allowing
* "*.example.com" only.
*/
尾部通配情况:
/*
* the 2 low bits of value have the special meaning:
* 00 - value is data pointer;
* 11 - value is pointer to wildcard hash allowing "example.*".
*/
- Nginx 哈希
- nginx
- Nginx
- Nginx
- Nginx
- Nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- Nginx
- nginx
- nginx
- nginx
- C++静态成员变量和静态成员函数
- Apache 中 KeepAlive 配置的合理使用
- Rose画状态图
- LPC2103控制一个LED灯闪烁
- 写插入存储过程,并在注册页面中使用。
- Nginx 哈希
- Quartz的Job无法实例化原因及解决方案总结
- 系统内核(kernel)
- httpwatcher
- 【转】USB--设备、配置、接口、端点
- C++ 及 Windows 的异常处理(try catch; __try __except; __try __finally)
- 类的静态成员与全局对象比较
- 常见web前台技术之间的关系html,css,javascript...
- C++之类的构造与析构(一)