TermsHashPerField 类
一、类功能概述:
负责词项的索引过程,每个字段有相应的一个TermsHashPerField;当索引某字段词项时,使用对应TermsHashPerField的add()函数完成(一个)词项索引过程,并将索引内容(词项字符串/指针信息/位置信息等)存储于内存缓冲中
二、类成员说明:
2.1 final int streamCount;
如果需要记录词频和位置,此值为2(用两个int记录偏移指针),否则为1
// 代码如下
streamCount = consumer.getStreamCount(); // FreqProxTermsWriterPerField //
2.2 TermAttribute termAtt;
分词后的词项存储于此,因此索引时从termAtt中将词项取出
2.3 RawPostingList[]和RawPostingList
代码如下
private RawPostingList[] postingsHash = new RawPostingList[postingsHashSize];
private RawPostingList p;
每个p记录了该词项的偏移地址、文档编号、文档频率(df)、位置信息
并存储在postingsHash中,下标是哈希值
保存了偏移地址
abstract class RawPostingList {
int textStart;
int intStart;
int byteStart;
}
但实际上存储的是FreqProxTermsWriter.PostingList
意思很浅显,代码如下:
static final class PostingList extends RawPostingList {
int docFreq; // # times this term occurs in the current doc
int lastDocID; // Last docID where this term occurred
int lastDocCode; // Code for prior doc
int lastPosition; // Last position where this term occurred
}
因此postingsHash存储的其实是
[0]= FreqProxTermsWriter$PostingList (id=23)
byteStart= 0
docFreq= 1
intStart= 0
lastDocCode= 0
lastDocID= 0
lastPosition= 0
textStart= 0
postingsHash存储的是该字段所有词项的一些索引信息,根据词项字符的编码值作为初始值hash到postingsHash中
代码如下:
int hashPos = code & postingsHashMask;
如果出现地址冲突,以固定步长递增编码值,查找空位置,填入p,此步长为
final int inc = ((code>>8)+code)|1;
代码如下
do {
code += inc;
hashPos = code & postingsHashMask ;
p = postingsHash[hashPos];
} while (p != null && !postingEquals(tokenText, tokenTextLen));
另外,如果postingsHash的使用率超过一半(使用率可调),将扩展之
代码如下
if (numPostings == postingsHashHalfSize)
rehashPostings(2*postingsHashSize);
2.2 字符串缓冲
以下三个是存储索引内容的缓冲,最终还是从DocumentsWriter中获得,此缓冲中的索引结构并不是最终写入磁盘的索引内容结构
intPool = perThread.intPool;
charPool = perThread.charPool