~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 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, but abort if
20
	   the key changes.
21
	   One may have done a write, update or delete of the previous row.
22
	   NOTE! Even if one changes the previous row, the next read is done
23
	   based on the position of the last used key!
24
	*/
25
26
int mi_rnext_same(MI_INFO *info, uchar *buf)
27
{
28
  int error;
29
  uint inx,not_used[2];
30
  MI_KEYDEF *keyinfo;
31
32
  if ((int) (inx=info->lastinx) < 0 || info->lastpos == HA_OFFSET_ERROR)
51.1.109 by Jay Pipes
Removed/replaced DBUG
33
    return(my_errno=HA_ERR_WRONG_INDEX);
1 by brian
clean slate
34
  keyinfo=info->s->keyinfo+inx;
35
  if (fast_mi_readinfo(info))
51.1.109 by Jay Pipes
Removed/replaced DBUG
36
    return(my_errno);
1 by brian
clean slate
37
38
  if (info->s->concurrent_insert)
39
    rw_rdlock(&info->s->key_root_lock[inx]);
40
41
  switch (keyinfo->key_alg)
42
  {
43
    case HA_KEY_ALG_BTREE:
44
    default:
45
      if (!(info->update & HA_STATE_RNEXT_SAME))
46
      {
47
        /* First rnext_same; Store old key */
48
        memcpy(info->lastkey2,info->lastkey,info->last_rkey_length);
49
      }
50
      for (;;)
51
      {
52
        if ((error=_mi_search_next(info,keyinfo,info->lastkey,
53
			       info->lastkey_length,SEARCH_BIGGER,
54
			       info->s->state.key_root[inx])))
55
          break;
56
        if (ha_key_cmp(keyinfo->seg, info->lastkey, info->lastkey2,
57
                       info->last_rkey_length, SEARCH_FIND, not_used))
58
        {
59
          error=1;
60
          my_errno=HA_ERR_END_OF_FILE;
61
          info->lastpos= HA_OFFSET_ERROR;
62
          break;
63
        }
64
        /* Skip rows that are inserted by other threads since we got a lock */
65
        if (info->lastpos < info->state->data_file_length && 
66
            (!info->index_cond_func || mi_check_index_cond(info, inx, buf)))
67
          break;
68
      }
69
  }
70
  if (info->s->concurrent_insert)
71
    rw_unlock(&info->s->key_root_lock[inx]);
72
	/* Don't clear if database-changed */
73
  info->update&= (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED);
74
  info->update|= HA_STATE_NEXT_FOUND | HA_STATE_RNEXT_SAME;
75
76
  if (error)
77
  {
78
    if (my_errno == HA_ERR_KEY_NOT_FOUND)
79
      my_errno=HA_ERR_END_OF_FILE;
80
  }
81
  else if (!buf)
82
  {
51.1.109 by Jay Pipes
Removed/replaced DBUG
83
    return(info->lastpos==HA_OFFSET_ERROR ? my_errno : 0);
1 by brian
clean slate
84
  }
85
  else if (!(*info->read_record)(info,info->lastpos,buf))
86
  {
87
    info->update|= HA_STATE_AKTIV;		/* Record is read */
51.1.109 by Jay Pipes
Removed/replaced DBUG
88
    return(0);
1 by brian
clean slate
89
  }
51.1.109 by Jay Pipes
Removed/replaced DBUG
90
  return(my_errno);
1 by brian
clean slate
91
} /* mi_rnext_same */