swish-e代码分析,索引部分(3)

上一节中对于索引之前的初始化工作进行了分析,从这节开始,对于索引过程进行阐述。

2.2.3 indexPath索引文件过程

/* This should be printed by the module that's reading the source */

    tmpswline = sw->dirlist;

    while (tmpswline != NULL)

    {

        if (sw->verbose)

        {

            printf("Indexing /"%s/"/n", tmpswline->line);

            fflush(stdout);

        }

        indexpath(sw, tmpswline->line);

        tmpswline = tmpswline->next;

    }

/*         

 * Invoke the methods of the current Indexing Data Source

 */    

void    indexpath(SWISH * sw, char *path)

{      

    /* invoke routine to index a "path" */

    (*IndexingDataSource->indexpath_fn) (sw, path);

}

                                    索引代码片段

 

对配置文件遍历以后,根据IndexDir 配置项将需要索引的目录通过链表方式放入到sw->dirlist;

遍历dirlist,通过indexpath函数进行索引。

                                  索引代码分析

 indexpath通过调用函数指针,执行 fs_indexpath(在fs.c文件中有定义)。

2.2.4  fs_indexpath函数分析

   fs_indexpath函数先对于传入的path进行规则化,去除末尾的'/'等,然后根据path为文件或者

目录分开进行处理。

void    fs_indexpath(SWISH * sw, char *path)

{

 

    normalize_path( path );  /* flip backslashes and remove trailing slash */

 

 

    if (isdirectory(path))

    {

        if (sw->verbose >= 2)

            printf("/nChecking dir /"%s/".../n", path);

 

        indexadir(sw, path);

    }

   

    else if (isfile(path))

    {

        if (sw->verbose >= 2)

            printf("/nChecking file /"%s/".../n", path);

        indexafile(sw, path);

    }       

    else

        progwarnno("Invalid path '%s': ", path);

}

                               fs_indexpath 代码片段

  •   如果是文件,通过 indexafile 进行处理,其中调用了 printfile 索引;
  •    如果是目录,通过 indexadir 进行处理,先列出该目录下面的文件和目录,将文件和目录分别放入到数组 sortfilelist sortdirlist ,对于文件,遍历索引其中的文件,如果是目录,则遍历调用 indexadir 方法,递归处理;
  •    最后都转化为调用 printfile 函数。

                                  fs_indexpath 代码分析

2.2.5 printfile索引文件过程

    printfile函数对于需要索引的文件先建立一个 FileProp文件属性对象,然后通过do_index_file进行索引。

/* Indexes the words in the file

*/

 

static void    printfile(SWISH * sw, char *filename)

{

    /* Only index files once */

    if ( fs_already_indexed( sw, filename ) )

        return;

 

    fprop = file_properties(filename, filename, sw);

 

    do_index_file(sw, fprop);

 

    free_file_properties(fprop);

printfile 代码片段


  •    判断该文件是否已经被索引过,生成文件的 device inode ,作为 key 值,建立 hash 表;
  •    对于文件建立 FileProp 属性,文件名称、路径、大小等;
  •    分析、索引文件;

                                     printfile 代码分析

总结,通过以上的处理,核心索引之前的一些准备过程就完成了,下面就是读取文件,建立词条hash表、进行索引的过程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值