~drizzle-trunk/drizzle/development

1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
5
 *
6
 *  Authors:
7
 *
8
 *  Jay Pipes <joinfu@sun.com>
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program; if not, write to the Free Software
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
 */
24
25
/**
26
 * @file
27
 *
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
28
 * Implements the PRINT_TRANSACTION_MESSAGE(filename, offset) UDF.
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
29
 */
30
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
31
#include <config.h>
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
32
#include <drizzled/plugin/function.h>
33
#include <drizzled/item/func.h>
34
#include <drizzled/function/str/strfunc.h>
35
#include <drizzled/error.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
36
#include <drizzled/internal/my_sys.h>
37
#include <drizzled/charset.h>
2148.7.12 by Brian Aker
Merge in header fixes.
38
#include <drizzled/gettext.h>
39
#include <drizzled/errmsg_print.h>
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
40
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
41
#include <fcntl.h>
1702.3.3 by LinuxJedi
Fix errors in FreeBSD build
42
#include <errno.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
43
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
44
#include "transaction_log.h"
45
#include "print_transaction_message.h"
46
47
#include <drizzled/message/transaction.pb.h>
1764.1.1 by David Shrewsbury
Validate message type and size when reading from transaction log; fix potential memory leaks
48
#include <drizzled/replication_services.h>
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
49
#include <google/protobuf/io/zero_copy_stream.h>
50
#include <google/protobuf/io/zero_copy_stream_impl.h>
51
#include <google/protobuf/io/coded_stream.h>
52
#include <google/protobuf/text_format.h>
53
54
using namespace std;
55
using namespace drizzled;
56
using namespace google;
57
58
/** Defined in transaction_log.cc */
59
extern TransactionLog *transaction_log;
60
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
61
plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory= NULL;
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
62
63
void PrintTransactionMessageFunction::fix_length_and_dec()
64
{
65
  max_length= 2 * 1024 * 1024; /* 2MB size limit seems ok... */
66
  args[0]->collation.set(
67
    get_charset_by_csname(args[0]->collation.collation->csname,
68
                          MY_CS_BINSORT), DERIVATION_COERCIBLE);
69
}
70
71
String *PrintTransactionMessageFunction::val_str(String *str)
72
{
73
  assert(fixed == true);
74
75
  String *filename_arg= args[0]->val_str(str);
76
  off_t offset_arg= static_cast<int64_t>(args[1]->val_int());
77
78
  if (filename_arg == NULL || args[1]->null_value == true)
79
  {
80
    my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
81
    null_value= true;
82
    return NULL;
83
  }
84
85
  if (transaction_log == NULL)
86
  {
87
    my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
88
    null_value= true;
89
    return NULL;
90
  }
91
92
  null_value= false;
93
94
  message::Transaction transaction_message;
95
96
  /**
97
   * @todo Of course, this is not efficient to create a
98
   * new input stream every time we call the UDF.  Create
99
   * a pool of TransactionLogReader objects that can be 
100
   * re-used.
101
   */
102
  const string &filename= transaction_log->getLogFilename();
103
  int log_file= open(filename.c_str(), O_RDONLY);
104
  if (log_file == -1)
105
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
106
    sql_perror(_("Failed to open transaction log file"), filename);
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
107
    null_value= true;
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
108
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
109
    return NULL;
110
  }
111
112
  (void) lseek(log_file, offset_arg, SEEK_SET);
113
114
  protobuf::io::FileInputStream *file_input= new protobuf::io::FileInputStream(log_file);
115
  file_input->SetCloseOnDelete(true);
116
117
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(file_input);
118
119
  /* Grab our message type and length */
120
  uint32_t message_type;
121
  if (! coded_input->ReadLittleEndian32(&message_type))
122
  {
1764.1.1 by David Shrewsbury
Validate message type and size when reading from transaction log; fix potential memory leaks
123
    delete coded_input;
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
124
    delete file_input;
125
126
    /** @todo Error message for this... */
127
    null_value= true;
128
    return NULL;
129
  }
130
1764.1.1 by David Shrewsbury
Validate message type and size when reading from transaction log; fix potential memory leaks
131
  /* Validate message type */
132
  if (message_type != ReplicationServices::TRANSACTION)
133
  {
134
    fprintf(stderr, _("GPB message is not a valid type.\n"));
135
    delete coded_input;
136
    delete file_input;
137
    null_value= true;
138
    return NULL;
139
  }
140
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
141
  uint32_t message_size;
142
  if (! coded_input->ReadLittleEndian32(&message_size))
143
  {
1764.1.1 by David Shrewsbury
Validate message type and size when reading from transaction log; fix potential memory leaks
144
    delete coded_input;
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
145
    delete file_input;
146
147
    /** @todo Error message for this... */
148
    null_value= true;
149
    return NULL;
150
  }
151
1764.1.1 by David Shrewsbury
Validate message type and size when reading from transaction log; fix potential memory leaks
152
  if (message_size > INT_MAX)
153
  {
154
    fprintf(stderr, _("GPB message is not a valid size.\n"));
155
    delete coded_input;
156
    delete file_input;
157
    null_value= true;
158
    return NULL;
159
  }
160
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
161
  uint8_t *buffer= (uint8_t *) malloc(message_size);
162
163
  bool result= coded_input->ReadRaw(buffer, message_size);
164
  if (result == false)
165
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
166
    // 120 was arbitrary
167
    sql_perror(_("Could not read transaction message. Raw buffer read "), std::string((const char *)buffer, std::min(message_size, 120U)));
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
168
  }
169
170
  result= transaction_message.ParseFromArray(buffer, static_cast<int32_t>(message_size));
171
  if (result == false)
172
  {
173
    fprintf(stderr, _("Unable to parse transaction. Got error: %s.\n"), transaction_message.InitializationErrorString().c_str());
174
    if (buffer != NULL)
175
      fprintf(stderr, _("BUFFER: %s\n"), buffer);
176
  }
177
178
  free(buffer);
179
180
  string transaction_text;
181
  protobuf::TextFormat::PrintToString(transaction_message, &transaction_text);
182
2275.2.12 by Olaf van der Spek
Return void
183
  str->alloc(transaction_text.length());
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
184
  str->length(transaction_text.length());
185
186
  strncpy(str->ptr(), transaction_text.c_str(), transaction_text.length());
187
1143.3.9 by Jay Pipes
Fixes information schema tests, adds a hexdump UDF for the transaction message.
188
  delete coded_input;
1143.3.4 by Jay Pipes
Adds INFORMATION_SCHEMA views for the transaction log:
189
  delete file_input;
190
191
  return str;
192
}