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

Swish-e通过配置文件,进行索引过程的配置。swish.c中main函数开始,首先通过swish_new()初始化SWISH变量;

1.基本流程:

1.1 SWISH初始化

  • 通过 SwishNew() 分配一个 Swish 指针变量,然后调用 initModule_DB 来初始化在索引过程中用到的文件读写函数,此时将索引文件的操作看做是 DB 操作;

  • 然后通过 initModule_Index 方法对于 MOD_Index 变量进行初始化,核心索引变量。

然后通过判断run_mode如果为-c confile这种方式,则switch到cmd_index( sw, params );进行索引处理。


2.2 cm_index函数处理过程

  •       cm_index函数完成了所有的索引过程。

  • 通过 getdefaults 读取 conf 配置文件,初始化 sw 变量;
  •    通过 addindexfile 加载 INDEXFILE 索引文件;
  •    此时我们传入的 run_mode INDEX_MODE 模式,通过 DB_Create 创建索引文件 ;
  •    通过 indexpath 函数对于目标文件进行索引;
  •    通过 write_index_file 写入索引文件。


2.2. getdefaults 具体分析

Void    getdefaults(SWISH * sw, char *conffile, int *hasdir, int *hasindex, int hasverbose)

{   if ((fp = fopen(conffile, F_READ_TEXT)) == NULL || !isfile(conffile))

        progerrno("Couldn't open the configuration file '%s': ", conffile);

   

    if ( sw->verbose >= 2 )

        printf("Parsing config file '%s'/n", conffile );

               

    /* Init default index file */

    addindexfile(sw, INDEXFILE);

indexf = sw->indexlist;

while ( !feof( fp ) )

    {

        /* Free previous line */

        if ( line )

            efree( line );

        /* Read a line */

        line = read_line_from_file( &linenumber, fp );

        if (strcasecmp(w0, "IndexDir") == 0)

        {

            if (sl->n > 1)

            {

                if (!*hasdir)

                {

                    gotdir = 1;

                    grabCmdOptions(sl, 1, &sw->dirlist);

                }

            }

            else

                progerr("%s: requires at least one value", w0);

 

            continue;

        }

                                                 getdefaults 代码片段

通过解析 conf 文件中的配置项,对于 swish 中的项进行初始化;

其中如果对于 Directive 有多个参数的时候,通过 grabCmdOption 放入到数组中。

                                                                                 getdefaults 函数分析


  2.2.2 DB_Create 创建索引文件

    此时我们传入的run_mode不是update或者remove,因此需要创建索引文件,参见swish.c: L1379行代码。DB_create函数通过函数指针指向db_native.c中的 DB_Create_Native函数实现。

在索引文档之前,先建立临时索引文件,主要写入了文件的版本标识、系统时间戳等。

DB_Create_Native 函数:

if ( is_directory( dbname ) )

        progerr( "Index file '%s' is a directory", dbname );

 

    swish_magic = SWISH_MAGIC;

   /* Allocate structure */

    DB = (struct Handle_DBNative *) newNativeDBHandle(sw, dbname);

    DB->mode = DB_CREATE;

DB->unique_ID = (long) time(NULL); /* Ok, so if more than one index is created the second... */

------------

    /* Create index File */

 

    CreateEmptyFile(filename);

    if (!(DB->fp = openIndexFILEForReadAndWrite(filename)))

        progerrno("Couldn't create the index file /"%s/": ", filename);

    DB->cur_index_file = estrdup(filename);

    printlong(DB->fp, swish_magic, sw_fwrite);

printlong(DB->fp, DB->unique_ID, sw_fwrite);

-------------

    for (i = 0; i < VERYBIGHASHSIZE; i++)

        DB->hashoffsets[i] = (sw_off_t)0;

    for (i = 0; i < VERYBIGHASHSIZE; i++)

        DB->lasthashval[i] = (sw_off_t)0;

DB_Create_native 代码片段

 

  •    判断是否给定的文件名称是否为目录;
  •    创建了一个 Handle_DBNative 结构体,主要是写索引文件;
  •    通过 CreateEmptyFile(filename) 创建索引文件;
  • 在索引文件头部写入 swish magic 数值和当时的系统时间戳,此时用到了 printlong 函数;
  •    hashoffset 赋值为 0 ,词条的hash表;
  •    此时也创建了 property 文件。

DB_Create_native 函数分析


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值