~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2004 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include "myisamdef.h"
17
18
	/*
19
	   Read next row with the same key as previous read
20
	   One may have done a write, update or delete of the previous row.
21
	   NOTE! Even if one changes the previous row, the next read is done
22
	   based on the position of the last used key!
23
	*/
24
25
int mi_rnext(MI_INFO *info, uchar *buf, int inx)
26
{
27
  int error,changed;
28
  uint flag;
29
  int res= 0;
30
  DBUG_ENTER("mi_rnext");
31
32
  if ((inx = _mi_check_index(info,inx)) < 0)
33
    DBUG_RETURN(my_errno);
34
  flag=SEARCH_BIGGER;				/* Read next */
35
  if (info->lastpos == HA_OFFSET_ERROR && info->update & HA_STATE_PREV_FOUND)
36
    flag=0;					/* Read first */
37
38
  if (fast_mi_readinfo(info))
39
    DBUG_RETURN(my_errno);
40
  if (info->s->concurrent_insert)
41
    rw_rdlock(&info->s->key_root_lock[inx]);
42
  changed=_mi_test_if_changed(info);
43
  if (!flag)
44
  {
45
    switch(info->s->keyinfo[inx].key_alg){
46
    case HA_KEY_ALG_BTREE:
47
    default:
48
      error=_mi_search_first(info,info->s->keyinfo+inx,
49
			   info->s->state.key_root[inx]);
50
      break;
51
    }
52
  }
53
  else
54
  {
55
    switch (info->s->keyinfo[inx].key_alg) {
56
    case HA_KEY_ALG_BTREE:
57
    default:
58
      if (!changed)
59
	error= _mi_search_next(info,info->s->keyinfo+inx,info->lastkey,
60
			       info->lastkey_length,flag,
61
			       info->s->state.key_root[inx]);
62
      else
63
	error= _mi_search(info,info->s->keyinfo+inx,info->lastkey,
64
			  USE_WHOLE_KEY,flag, info->s->state.key_root[inx]);
65
    }
66
  }
67
68
  if (!error)
69
  {
70
    while ((info->s->concurrent_insert &&
71
            info->lastpos >= info->state->data_file_length) ||
72
           (info->index_cond_func &&
73
           !(res= mi_check_index_cond(info, inx, buf))))
74
    {
75
      /* Skip rows inserted by other threads since we got a lock */
76
      if  ((error=_mi_search_next(info,info->s->keyinfo+inx,
77
                                  info->lastkey,
78
                                  info->lastkey_length,
79
                                  SEARCH_BIGGER,
80
                                  info->s->state.key_root[inx])))
81
        break;
82
    }
83
    if (!error && res == 2)
84
    {
85
      if (info->s->concurrent_insert)
86
        rw_unlock(&info->s->key_root_lock[inx]);
87
      info->lastpos= HA_OFFSET_ERROR;
88
      DBUG_RETURN(my_errno= HA_ERR_END_OF_FILE);
89
    }
90
  }
91
  
92
  if (info->s->concurrent_insert)
93
    rw_unlock(&info->s->key_root_lock[inx]);
94
95
	/* Don't clear if database-changed */
96
  info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
97
  info->update|= HA_STATE_NEXT_FOUND;
98
99
  if (error)
100
  {
101
    if (my_errno == HA_ERR_KEY_NOT_FOUND)
102
      my_errno=HA_ERR_END_OF_FILE;
103
  }
104
  else if (!buf)
105
  {
106
    DBUG_RETURN(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0);
107
  }
108
  else if (!(*info->read_record)(info,info->lastpos,buf))
109
  {
110
    info->update|= HA_STATE_AKTIV;		/* Record is read */
111
    DBUG_RETURN(0);
112
  }
113
  DBUG_PRINT("error",("Got error: %d,  errno: %d",error, my_errno));
114
  DBUG_RETURN(my_errno);
115
} /* mi_rnext */