~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to merge/rrnd.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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; either version 2 of the License, or
 
6
   (at your option) any later version.
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
16
 
 
17
/*
 
18
  Read a record with random-access. The position to the record must
 
19
  get by mrg_info(). The next record can be read with pos= -1 */
 
20
 
 
21
 
 
22
#include "mrgdef.h"
 
23
 
 
24
static MRG_TABLE *find_table(MRG_TABLE *start,MRG_TABLE *end,mrg_off_t pos);
 
25
 
 
26
/*
 
27
        If filepos == -1, read next
 
28
        Returns same as nisam_rrnd:
 
29
           0 = Ok.
 
30
           1 = Record deleted.
 
31
          -1 = EOF (or something, errno should be HA_ERR_END_OF_FILE)
 
32
*/
 
33
 
 
34
int mrg_rrnd(MRG_INFO *info,byte *buf,mrg_off_t filepos)
 
35
{
 
36
  int error;
 
37
  N_INFO *isam_info;
 
38
 
 
39
  if (filepos == ~(mrg_off_t) 0)                /* Can't use HA_POS_ERROR */
 
40
  {
 
41
    if (!info->current_table)
 
42
    {
 
43
      if (info->open_tables == info->end_table)
 
44
      {                                         /* No tables */
 
45
        my_errno=HA_ERR_END_OF_FILE;
 
46
        return -1;
 
47
      }
 
48
      isam_info=(info->current_table=info->open_tables)->table;
 
49
      if (info->cache_in_use)
 
50
        nisam_extra(isam_info,HA_EXTRA_CACHE);
 
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,(byte*) buf,
 
64
                                        filepos,1)) >= 0 ||
 
65
          my_errno != HA_ERR_END_OF_FILE)
 
66
        return (error);
 
67
      if (info->cache_in_use)
 
68
        nisam_extra(info->current_table->table,HA_EXTRA_NO_CACHE);
 
69
      if (info->current_table+1 == info->end_table)
 
70
        return(-1);
 
71
      info->current_table++;
 
72
      info->last_used_table=info->current_table;
 
73
      if (info->cache_in_use)
 
74
        nisam_extra(info->current_table->table,HA_EXTRA_CACHE);
 
75
      info->current_table->file_offset=
 
76
        info->current_table[-1].file_offset+
 
77
        info->current_table[-1].table->s->state.data_file_length;
 
78
 
 
79
      isam_info=info->current_table->table;
 
80
      filepos=isam_info->s->pack.header_length;
 
81
      isam_info->lastinx= (uint) -1;
 
82
    }
 
83
  }
 
84
  info->current_table=find_table(info->open_tables,
 
85
                                 info->last_used_table,filepos);
 
86
  isam_info=info->current_table->table;
 
87
  isam_info->update&= HA_STATE_CHANGED;
 
88
  return ((*isam_info->s->read_rnd)(isam_info,(byte*) buf,
 
89
                                    (ulong) (filepos -
 
90
                                             info->current_table->file_offset),
 
91
                                    0));
 
92
}
 
93
 
 
94
 
 
95
        /* Find which table to use according to file-pos */
 
96
 
 
97
static MRG_TABLE *find_table(MRG_TABLE *start,MRG_TABLE *end,mrg_off_t pos)
 
98
{
 
99
  MRG_TABLE *mid;
 
100
 
 
101
  while (start != end)
 
102
  {
 
103
    mid=start+((uint) (end-start)+1)/2;
 
104
    if (mid->file_offset > pos)
 
105
      end=mid-1;
 
106
    else
 
107
      start=mid;
 
108
  }
 
109
  return start;
 
110
}