~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to myisam/mi_log.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
/* Logging of isamcommands and records on logfile */
 
18
 
 
19
#include "myisamdef.h"
 
20
#if defined(MSDOS) || defined(__WIN__)
 
21
#include <errno.h>
 
22
#include <fcntl.h>
 
23
#ifndef __WIN__
 
24
#include <process.h>
 
25
#endif
 
26
#endif
 
27
#ifdef VMS
 
28
#include <processes.h>
 
29
#endif
 
30
 
 
31
#undef GETPID                                   /* For HPUX */
 
32
#ifdef THREAD
 
33
#define GETPID() (log_type == 1 ? getpid() : (long) my_thread_id());
 
34
#else
 
35
#define GETPID() getpid()
 
36
#endif
 
37
 
 
38
        /* Activate logging if flag is 1 and reset logging if flag is 0 */
 
39
 
 
40
static int log_type=0;
 
41
 
 
42
int mi_log(int activate_log)
 
43
{
 
44
  int error=0;
 
45
  char buff[FN_REFLEN];
 
46
  DBUG_ENTER("mi_log");
 
47
 
 
48
  log_type=activate_log;
 
49
  if (activate_log)
 
50
  {
 
51
    if (myisam_log_file < 0)
 
52
    {
 
53
      if ((myisam_log_file = my_create(fn_format(buff,myisam_log_filename,
 
54
                                                "",".log",4),
 
55
                                      0,(O_RDWR | O_BINARY | O_APPEND),MYF(0)))
 
56
          < 0)
 
57
        DBUG_RETURN(my_errno);
 
58
    }
 
59
  }
 
60
  else if (myisam_log_file >= 0)
 
61
  {
 
62
    error=my_close(myisam_log_file,MYF(0)) ? my_errno : 0 ;
 
63
    myisam_log_file= -1;
 
64
  }
 
65
  DBUG_RETURN(error);
 
66
}
 
67
 
 
68
 
 
69
        /* Logging of records and commands on logfile */
 
70
        /* All logs starts with command(1) dfile(2) process(4) result(2) */
 
71
 
 
72
void _myisam_log(enum myisam_log_commands command, MI_INFO *info, const byte *buffert, uint length)
 
73
{
 
74
  char buff[11];
 
75
  int error,old_errno;
 
76
  ulong pid=(ulong) GETPID();
 
77
  old_errno=my_errno;
 
78
  bzero(buff,sizeof(buff));
 
79
  buff[0]=(char) command;
 
80
  mi_int2store(buff+1,info->dfile);
 
81
  mi_int4store(buff+3,pid);
 
82
  mi_int2store(buff+9,length);
 
83
 
 
84
  pthread_mutex_lock(&THR_LOCK_myisam);
 
85
  error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
 
86
  VOID(my_write(myisam_log_file,buff,sizeof(buff),MYF(0)));
 
87
  VOID(my_write(myisam_log_file,buffert,length,MYF(0)));
 
88
  if (!error)
 
89
    error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
 
90
  pthread_mutex_unlock(&THR_LOCK_myisam);
 
91
  my_errno=old_errno;
 
92
}
 
93
 
 
94
 
 
95
void _myisam_log_command(enum myisam_log_commands command, MI_INFO *info,
 
96
                         const byte *buffert, uint length, int result)
 
97
{
 
98
  char buff[9];
 
99
  int error,old_errno;
 
100
  ulong pid=(ulong) GETPID();
 
101
 
 
102
  old_errno=my_errno;
 
103
  buff[0]=(char) command;
 
104
  mi_int2store(buff+1,info->dfile);
 
105
  mi_int4store(buff+3,pid);
 
106
  mi_int2store(buff+7,result);
 
107
  pthread_mutex_lock(&THR_LOCK_myisam);
 
108
  error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
 
109
  VOID(my_write(myisam_log_file,buff,sizeof(buff),MYF(0)));
 
110
  if (buffert)
 
111
    VOID(my_write(myisam_log_file,buffert,length,MYF(0)));
 
112
  if (!error)
 
113
    error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
 
114
  pthread_mutex_unlock(&THR_LOCK_myisam);
 
115
  my_errno=old_errno;
 
116
}
 
117
 
 
118
 
 
119
void _myisam_log_record(enum myisam_log_commands command, MI_INFO *info,
 
120
                        const byte *record, my_off_t filepos, int result)
 
121
{
 
122
  char buff[21],*pos;
 
123
  int error,old_errno;
 
124
  uint length;
 
125
  ulong pid=(ulong) GETPID();
 
126
 
 
127
  old_errno=my_errno;
 
128
  if (!info->s->base.blobs)
 
129
    length=info->s->base.reclength;
 
130
  else
 
131
    length=info->s->base.reclength+ _my_calc_total_blob_length(info,record);
 
132
  buff[0]=(char) command;
 
133
  mi_int2store(buff+1,info->dfile);
 
134
  mi_int4store(buff+3,pid);
 
135
  mi_int2store(buff+7,result);
 
136
  mi_sizestore(buff+9,filepos);
 
137
  mi_int4store(buff+17,length);
 
138
  pthread_mutex_lock(&THR_LOCK_myisam);
 
139
  error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
 
140
  VOID(my_write(myisam_log_file,buff,sizeof(buff),MYF(0)));
 
141
  VOID(my_write(myisam_log_file,(byte*) record,info->s->base.reclength,MYF(0)));
 
142
  if (info->s->base.blobs)
 
143
  {
 
144
    MI_BLOB *blob,*end;
 
145
 
 
146
    for (end=info->blobs+info->s->base.blobs, blob= info->blobs;
 
147
         blob != end ;
 
148
         blob++)
 
149
    {
 
150
      memcpy_fixed(&pos,record+blob->offset+blob->pack_length,sizeof(char*));
 
151
      VOID(my_write(myisam_log_file,pos,blob->length,MYF(0)));
 
152
    }
 
153
  }
 
154
  if (!error)
 
155
    error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
 
156
  pthread_mutex_unlock(&THR_LOCK_myisam);
 
157
  my_errno=old_errno;
 
158
}