1 /* Copyright (c) 2000-2002, 2005-2007 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */
16 
17 /*
18   Read a record with random-access. The position to the record must
19   get by myrg_info(). The next record can be read with pos= -1 */
20 
21 
22 #include "myrg_def.h"
23 
24 static MYRG_TABLE *find_table(MYRG_TABLE *start,MYRG_TABLE *end,ulonglong pos);
25 
26 /*
27 	If filepos == HA_OFFSET_ERROR, read next
28 	Returns same as mi_rrnd:
29 	   0 = Ok.
30 	   HA_ERR_RECORD_DELETED = Record is deleted.
31 	   HA_ERR_END_OF_FILE = EOF.
32 */
33 
myrg_rrnd(MYRG_INFO * info,uchar * buf,ulonglong filepos)34 int myrg_rrnd(MYRG_INFO *info,uchar *buf,ulonglong filepos)
35 {
36   int error;
37   MI_INFO *isam_info;
38   DBUG_ENTER("myrg_rrnd");
39   DBUG_PRINT("info",("offset: %lu", (ulong) filepos));
40 
41   if (filepos == HA_OFFSET_ERROR)
42   {
43     if (!info->current_table)
44     {
45       if (info->open_tables == info->end_table)
46       {						/* No tables */
47 	DBUG_RETURN(my_errno=HA_ERR_END_OF_FILE);
48       }
49       isam_info=(info->current_table=info->open_tables)->table;
50       if (info->cache_in_use)
51 	mi_extra(isam_info,HA_EXTRA_CACHE,(uchar*) &info->cache_size);
52       filepos=isam_info->s->pack.header_length;
53       isam_info->lastinx= (uint) -1;	/* Can't forward or backward */
54     }
55     else
56     {
57       isam_info=info->current_table->table;
58       filepos= isam_info->nextpos;
59     }
60 
61     for (;;)
62     {
63       isam_info->update&= HA_STATE_CHANGED;
64       if ((error=(*isam_info->s->read_rnd)(isam_info,(uchar*) buf,
65 					   (my_off_t) filepos,1)) !=
66 	  HA_ERR_END_OF_FILE)
67 	DBUG_RETURN(error);
68       if (info->cache_in_use)
69 	mi_extra(info->current_table->table, HA_EXTRA_NO_CACHE,
70 		 (uchar*) &info->cache_size);
71       if (info->current_table+1 == info->end_table)
72 	DBUG_RETURN(HA_ERR_END_OF_FILE);
73       info->current_table++;
74       info->last_used_table=info->current_table;
75       if (info->cache_in_use)
76 	mi_extra(info->current_table->table, HA_EXTRA_CACHE,
77 		 (uchar*) &info->cache_size);
78       info->current_table->file_offset=
79 	info->current_table[-1].file_offset+
80 	info->current_table[-1].table->state->data_file_length;
81 
82       isam_info=info->current_table->table;
83       filepos=isam_info->s->pack.header_length;
84       isam_info->lastinx= (uint) -1;
85     }
86   }
87   info->current_table=find_table(info->open_tables,
88 				 info->end_table-1,filepos);
89   isam_info=info->current_table->table;
90   isam_info->update&= HA_STATE_CHANGED;
91   DBUG_RETURN((*isam_info->s->read_rnd)
92               (isam_info, (uchar*) buf,
93 	      (my_off_t) (filepos - info->current_table->file_offset),
94 	      0));
95 }
96 
97 
98 	/* Find which table to use according to file-pos */
99 
find_table(MYRG_TABLE * start,MYRG_TABLE * end,ulonglong pos)100 static MYRG_TABLE *find_table(MYRG_TABLE *start, MYRG_TABLE *end,
101 			      ulonglong pos)
102 {
103   MYRG_TABLE *mid;
104   DBUG_ENTER("find_table");
105 
106   while (start != end)
107   {
108     mid=start+((uint) (end-start)+1)/2;
109     if (mid->file_offset > pos)
110       end=mid-1;
111     else
112       start=mid;
113   }
114   DBUG_PRINT("info",("offset: %lu, table: %s",
115 		     (ulong) pos, start->table->filename));
116   DBUG_RETURN(start);
117 }
118