在前一节中通过getentry函数的处理,在hash表中查找是否含有该词条,如果没有,则初始化一个词条entry变量。然后通过addentry进行处理。
2.3.4 addentry函数分析
加入词条到hash表的过程主要分为两部分:已有词条、新词条。
如果是 hash 表中未出现的词条。 在这个过程中写入了频率和位置信息(位置信息含有 strcuture 结构信息,便于在以后的压缩过程中处理);先将这些 TP 位置信息放在 currentChunkLocationList 链表中。
/* Check for first time */ if(!e->tfrequency) { /* create a location record */ tp = (LOCATION *) new_location(idx); tp->filenum = filenum; tp->frequency = 1; tp->metaID = metaID; tp->posdata[0] = SET_POSDATA(position,structure); tp->next = NULL; e->currentChunkLocationList = tp; e->tfrequency = 1; e->u1.last_filenum = filenum; return; }
addentry代码片段1
在原先的 hash 表中出现的词条,则需要判断是否属于同一个 field (mata)和file中的词条。如果是,直接在 Location 中增加位置信息,否则需要在增加 Location 结构保存。 在处理 Location 位置信息时,需要考虑随着位置信息的逐渐增加,需要扩容位置信息。
/* Word found -- look for same metaID and filename */ /* $$$ To do it right, should probably compare the structure, too */ /* Note: filename not needed due to compress we are only looking at the current file */ /* Oct 18, 2001 -- filename is needed since merge adds words in non-filenum order */ tp = e->currentChunkLocationList; found = 0; while (tp != e->currentlocation) { if(tp->metaID == metaID && tp->filenum == filenum ) { found =1; break; } tp = tp->next; } ------------------------- /* Otherwise, found matching LOCATION record (matches filenum and metaID) */ /* Just add the position number onto the end by expanding the size of the LOCATION record */ /* 2001/08 jmruiz - Much better memory usage occurs if we use MemZones */ /* MemZone will be reset when the doc is completely proccesed */ newtp = add_position_location(tp, idx, tp->frequency); if(newtp != tp) { if(e->currentChunkLocationList == tp) e->currentChunkLocationList = newtp; else for(prevtp = e->currentChunkLocationList;;prevtp = prevtp->next) { if(prevtp->next == tp) { prevtp->next = newtp; break; } } tp = newtp; } tp->posdata[tp->frequency++] = SET_POSDATA(position,structure);
addentry代码片段2
如果发现不在同一个meta或者文件中,则需要在建立一个LOCATION结构保存新的位置信息。
if(!found) { /* create the new LOCATION entry */ tp = (LOCATION *) new_location(idx); tp->filenum = filenum; tp->frequency = 1; /* count of times this word in this file:metaID */ tp->metaID = metaID; tp->posdata[0] = SET_POSDATA(position,structure); /* add the new LOCATION onto the array */ tp->next = e->currentChunkLocationList; e->currentChunkLocationList = tp; /* Count number of different files that this word is used in */ if ( e->u1.last_filenum != filenum ) { e->tfrequency++; e->u1.last_filenum = filenum; } return; /* all done */ }
addentry代码片段3