RocketMQ的Index File
RocketMQ的Index File#
Index File由三部分组成:Index Head、Hash Slot、Index Item
Index Item的结构#
索引的生成#
- 插入一条消息
- 计算出消息key的hashcode
- 根据hashcode%500w,计算出应该放到哪个槽中
- 然后在插入Index Item,并在槽中记录Index Item的位置
假设hash slot只有5个、index item 只有20个,演示一下插入索引的过程
插入key1, 计算出hashcode=5,5%5=0在hashslot的0位置
在index item list中插入一个Index Item
hashslot 0位置指向插入的Index item
插入key2, 计算出hashcode=7,7%5=2在hashslot的0位置
在index item list中插入一个Index Item
hashslot 2位置指向插入的Index item
插入key3, 计算出hashcode=10,10%5=0在hashslot的0位置
此时发生了hash碰撞,它在slot的位置和key1相同,怎么处理?
在index item list中插入一个Index Item
hashslot 0位置指向插入的Index item key3
那key1岂不是变成一个孤岛了?
Index item key3会将它的preIndexNo指向Index item key1,这样index item key3和index item key1形成了一个单向链表
索引查找#
假设要查询的key是key1
-
根据key计算出所在hash槽,计算出位置是0
-
根据hash槽的值定位对应的Index item, 定位到key3的index item,它是一个单向链表,key3 -> key1
-
遍历这个链表,定位到key1
-
根据item的pyhoffset,到commitLog中定位到消息
Index Head#
-
beginTimestamp:文件中消息的最小存储时间
-
endTimestamp:文件中消息的最大存储时间
-
beginPhyoffset:消息的最小偏移量
-
endphyoffset:消息的最大物理偏移量
-
hashSlotCount:已用 hash 槽个数
-
indexCount:已用 index 个数
作者:roylee666
出处:https://www.cnblogs.com/roylee666/p/15766236.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)