ft_search.c 2.14 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* Written by Sergei A. Golubchik, who has a shared copyright to this code */

#include "ftdefs.h"

21
/* queries myisam and returns list of documents matched */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
22 23 24 25 26 27

static int FT_DOC_cmp(FT_DOC *a, FT_DOC *b)
{
    return sgn(b->weight - a->weight);
}

28 29
FT_DOCLIST *ft_init_search(void *info, uint keynr, byte *query,
			    uint query_len, my_bool presort)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
30 31
{
  FT_DOCLIST *dlist;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
32
  my_off_t saved_lastpos=((MI_INFO *)info)->lastpos;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
33 34 35 36 37 38 39 40

/* black magic ON */
  if ((int) (keynr = _mi_check_index((MI_INFO *)info,keynr)) < 0)
    return NULL;
  if (_mi_readinfo((MI_INFO *)info,F_RDLCK,1))
    return NULL;
/* black magic OFF */

41 42 43 44
  if (is_boolean(query, query_len))
    dlist=ft_boolean_search(info,keynr,query,query_len);
  else
    dlist=ft_nlq_search(info,keynr,query,query_len);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
45

46
  if(dlist && presort)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
47 48 49 50
  {
    qsort(dlist->doc, dlist->ndocs, sizeof(FT_DOC), (qsort_cmp)&FT_DOC_cmp);
  }

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
51
  ((MI_INFO *)info)->lastpos=saved_lastpos;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
52 53 54
  return dlist;
}

55
int ft_read_next(FT_DOCLIST *handler, char *record)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
56 57 58
{
  MI_INFO *info=handler->info;

59
  if (++handler->curdoc >= handler->ndocs)
60 61
  {
    --handler->curdoc;
62
    return HA_ERR_END_OF_FILE;
63
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
64 65 66

  info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);

67 68
  info->lastpos=handler->doc[handler->curdoc].dpos;
  if (!(*info->read_record)(info,info->lastpos,record))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
69 70
  {
    info->update|= HA_STATE_AKTIV;		/* Record is read */
71
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
72
  }
73
  return my_errno;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74
}