MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
myrg_rrnd.c
1 /* Copyright (C) 2000-2002 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 /*
17  Read a record with random-access. The position to the record must
18  get by myrg_info(). The next record can be read with pos= -1 */
19 
20 
21 #include "myrg_def.h"
22 
23 static MYRG_TABLE *find_table(MYRG_TABLE *start,MYRG_TABLE *end,ulonglong pos);
24 
25 /*
26  If filepos == HA_OFFSET_ERROR, read next
27  Returns same as mi_rrnd:
28  0 = Ok.
29  HA_ERR_RECORD_DELETED = Record is deleted.
30  HA_ERR_END_OF_FILE = EOF.
31 */
32 
33 int myrg_rrnd(MYRG_INFO *info,uchar *buf,ulonglong filepos)
34 {
35  int error;
36  MI_INFO *isam_info;
37  DBUG_ENTER("myrg_rrnd");
38  DBUG_PRINT("info",("offset: %lu", (ulong) filepos));
39 
40  if (filepos == HA_OFFSET_ERROR)
41  {
42  if (!info->current_table)
43  {
44  if (info->open_tables == info->end_table)
45  { /* No tables */
46  DBUG_RETURN(my_errno=HA_ERR_END_OF_FILE);
47  }
48  isam_info=(info->current_table=info->open_tables)->table;
49  if (info->cache_in_use)
50  mi_extra(isam_info,HA_EXTRA_CACHE,(uchar*) &info->cache_size);
51  filepos=isam_info->s->pack.header_length;
52  isam_info->lastinx= (uint) -1; /* Can't forward or backward */
53  }
54  else
55  {
56  isam_info=info->current_table->table;
57  filepos= isam_info->nextpos;
58  }
59 
60  for (;;)
61  {
62  isam_info->update&= HA_STATE_CHANGED;
63  if ((error=(*isam_info->s->read_rnd)(isam_info,(uchar*) buf,
64  (my_off_t) filepos,1)) !=
65  HA_ERR_END_OF_FILE)
66  DBUG_RETURN(error);
67  if (info->cache_in_use)
68  mi_extra(info->current_table->table, HA_EXTRA_NO_CACHE,
69  (uchar*) &info->cache_size);
70  if (info->current_table+1 == info->end_table)
71  DBUG_RETURN(HA_ERR_END_OF_FILE);
72  info->current_table++;
73  info->last_used_table=info->current_table;
74  if (info->cache_in_use)
75  mi_extra(info->current_table->table, HA_EXTRA_CACHE,
76  (uchar*) &info->cache_size);
77  info->current_table->file_offset=
78  info->current_table[-1].file_offset+
79  info->current_table[-1].table->state->data_file_length;
80 
81  isam_info=info->current_table->table;
82  filepos=isam_info->s->pack.header_length;
83  isam_info->lastinx= (uint) -1;
84  }
85  }
86  info->current_table=find_table(info->open_tables,
87  info->end_table-1,filepos);
88  isam_info=info->current_table->table;
89  isam_info->update&= HA_STATE_CHANGED;
90  DBUG_RETURN((*isam_info->s->read_rnd)
91  (isam_info, (uchar*) buf,
92  (my_off_t) (filepos - info->current_table->file_offset),
93  0));
94 }
95 
96 
97  /* Find which table to use according to file-pos */
98 
99 static MYRG_TABLE *find_table(MYRG_TABLE *start, MYRG_TABLE *end,
100  ulonglong pos)
101 {
102  MYRG_TABLE *mid;
103  DBUG_ENTER("find_table");
104 
105  while (start != end)
106  {
107  mid=start+((uint) (end-start)+1)/2;
108  if (mid->file_offset > pos)
109  end=mid-1;
110  else
111  start=mid;
112  }
113  DBUG_PRINT("info",("offset: %lu, table: %s",
114  (ulong) pos, start->table->filename));
115  DBUG_RETURN(start);
116 }