summaryrefslogtreecommitdiff
path: root/myisam/ft_search.c
blob: c5a43734d9a6fa36a4584153412ab7665fb36dfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   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.

   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.

   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"

/* queries myisam and returns list of documents matched */

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

FT_DOCLIST *ft_init_search(void *info, uint keynr, byte *query,
			    uint query_len, my_bool presort)
{
  FT_DOCLIST *dlist;
  my_off_t saved_lastpos=((MI_INFO *)info)->lastpos;

/* 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 */

  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);

  if(dlist && presort)
  {
    qsort(dlist->doc, dlist->ndocs, sizeof(FT_DOC), (qsort_cmp)&FT_DOC_cmp);
  }

  ((MI_INFO *)info)->lastpos=saved_lastpos;
  return dlist;
}

int ft_read_next(FT_DOCLIST *handler, char *record)
{
  MI_INFO *info= (MI_INFO *) handler->info;

  if (++handler->curdoc >= handler->ndocs)
  {
    --handler->curdoc;
    return HA_ERR_END_OF_FILE;
  }

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

  info->lastpos=handler->doc[handler->curdoc].dpos;
  if (!(*info->read_record)(info,info->lastpos,record))
  {
    info->update|= HA_STATE_AKTIV;		/* Record is read */
    return 0;
  }
  return my_errno;
}