~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/transaction_log/print_transaction_message.cc

  • Committer: Brian Aker
  • Date: 2009-11-27 18:34:26 UTC
  • mfrom: (1143.3.9 transaction-log)
  • Revision ID: brian@gaz-20091127183426-uj7730j5c5xwmo2x
Merge Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
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
 *
 
28
 * Implements the PRINT_TRANSACTION_MESSAGE(filename, offset) UDF.
 
29
 */
 
30
 
 
31
#include <drizzled/server_includes.h>
 
32
#include <drizzled/plugin/function.h>
 
33
#include <drizzled/item/func.h>
 
34
#include <drizzled/function/str/strfunc.h>
 
35
#include <drizzled/error.h>
 
36
 
 
37
#include "transaction_log.h"
 
38
#include "print_transaction_message.h"
 
39
 
 
40
#include <drizzled/message/transaction.pb.h>
 
41
#include <google/protobuf/io/zero_copy_stream.h>
 
42
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
43
#include <google/protobuf/io/coded_stream.h>
 
44
#include <google/protobuf/text_format.h>
 
45
 
 
46
using namespace std;
 
47
using namespace drizzled;
 
48
using namespace google;
 
49
 
 
50
/** Defined in transaction_log.cc */
 
51
extern TransactionLog *transaction_log;
 
52
 
 
53
plugin::Create_function<PrintTransactionMessageFunction> *print_transaction_message_func_factory= NULL;
 
54
 
 
55
void PrintTransactionMessageFunction::fix_length_and_dec()
 
56
{
 
57
  max_length= 2 * 1024 * 1024; /* 2MB size limit seems ok... */
 
58
  args[0]->collation.set(
 
59
    get_charset_by_csname(args[0]->collation.collation->csname,
 
60
                          MY_CS_BINSORT), DERIVATION_COERCIBLE);
 
61
}
 
62
 
 
63
String *PrintTransactionMessageFunction::val_str(String *str)
 
64
{
 
65
  assert(fixed == true);
 
66
 
 
67
  String *filename_arg= args[0]->val_str(str);
 
68
  off_t offset_arg= static_cast<int64_t>(args[1]->val_int());
 
69
 
 
70
  if (filename_arg == NULL || args[1]->null_value == true)
 
71
  {
 
72
    my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
 
73
    null_value= true;
 
74
    return NULL;
 
75
  }
 
76
 
 
77
  if (transaction_log == NULL)
 
78
  {
 
79
    my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
 
80
    null_value= true;
 
81
    return NULL;
 
82
  }
 
83
 
 
84
  null_value= false;
 
85
 
 
86
  message::Transaction transaction_message;
 
87
 
 
88
  /**
 
89
   * @todo Of course, this is not efficient to create a
 
90
   * new input stream every time we call the UDF.  Create
 
91
   * a pool of TransactionLogReader objects that can be 
 
92
   * re-used.
 
93
   */
 
94
  const string &filename= transaction_log->getLogFilename();
 
95
  int log_file= open(filename.c_str(), O_RDONLY);
 
96
  if (log_file == -1)
 
97
  {
 
98
    errmsg_printf(ERRMSG_LVL_ERROR, _("Failed to open transaction log file %s.  Got error: %s\n"), 
 
99
                  filename.c_str(), 
 
100
                  strerror(errno));
 
101
    null_value= true;
 
102
    return NULL;
 
103
  }
 
104
 
 
105
  (void) lseek(log_file, offset_arg, SEEK_SET);
 
106
 
 
107
  protobuf::io::FileInputStream *file_input= new protobuf::io::FileInputStream(log_file);
 
108
  file_input->SetCloseOnDelete(true);
 
109
 
 
110
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(file_input);
 
111
 
 
112
  /* Grab our message type and length */
 
113
  uint32_t message_type;
 
114
  if (! coded_input->ReadLittleEndian32(&message_type))
 
115
  {
 
116
    delete file_input;
 
117
 
 
118
    /** @todo Error message for this... */
 
119
    null_value= true;
 
120
    return NULL;
 
121
  }
 
122
 
 
123
  uint32_t message_size;
 
124
  if (! coded_input->ReadLittleEndian32(&message_size))
 
125
  {
 
126
    delete file_input;
 
127
 
 
128
    /** @todo Error message for this... */
 
129
    null_value= true;
 
130
    return NULL;
 
131
  }
 
132
 
 
133
  uint8_t *buffer= (uint8_t *) malloc(message_size);
 
134
 
 
135
  bool result= coded_input->ReadRaw(buffer, message_size);
 
136
  if (result == false)
 
137
  {
 
138
    fprintf(stderr, _("Could not read transaction message.\n"));
 
139
    fprintf(stderr, _("GPB ERROR: %s.\n"), strerror(errno));
 
140
    fprintf(stderr, _("Raw buffer read: %s.\n"), buffer);
 
141
  }
 
142
 
 
143
  result= transaction_message.ParseFromArray(buffer, static_cast<int32_t>(message_size));
 
144
  if (result == false)
 
145
  {
 
146
    fprintf(stderr, _("Unable to parse transaction. Got error: %s.\n"), transaction_message.InitializationErrorString().c_str());
 
147
    if (buffer != NULL)
 
148
      fprintf(stderr, _("BUFFER: %s\n"), buffer);
 
149
  }
 
150
 
 
151
  free(buffer);
 
152
 
 
153
  string transaction_text;
 
154
  protobuf::TextFormat::PrintToString(transaction_message, &transaction_text);
 
155
 
 
156
  if (str->alloc(transaction_text.length()))
 
157
  {
 
158
    null_value= true;
 
159
    return NULL;
 
160
  }
 
161
 
 
162
  str->length(transaction_text.length());
 
163
 
 
164
  strncpy(str->ptr(), transaction_text.c_str(), transaction_text.length());
 
165
 
 
166
  delete coded_input;
 
167
  delete file_input;
 
168
 
 
169
  return str;
 
170
}