Drizzled Public API Documentation

hexdump_transaction_message.cc
Go to the documentation of this file.
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  Authors:
00007  *
00008  *  Jay Pipes <joinfu@sun.com>
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00023  */
00024 
00031 #include <config.h>
00032 #include <drizzled/plugin/function.h>
00033 #include <drizzled/item/func.h>
00034 #include <drizzled/function/str/strfunc.h>
00035 #include <drizzled/error.h>
00036 #include <drizzled/internal/my_sys.h>
00037 #include <drizzled/charset.h>
00038 #include <drizzled/errmsg_print.h>
00039 #include <drizzled/gettext.h>
00040 
00041 #include <fcntl.h>
00042 #include <errno.h>
00043 
00044 #include "transaction_log.h"
00045 #include "hexdump_transaction_message.h"
00046 
00047 #include <drizzled/message/transaction.pb.h>
00048 #include <drizzled/util/convert.h>
00049 #include <google/protobuf/io/zero_copy_stream.h>
00050 #include <google/protobuf/io/zero_copy_stream_impl.h>
00051 #include <google/protobuf/io/coded_stream.h>
00052 #include <google/protobuf/text_format.h>
00053 
00054 using namespace std;
00055 using namespace drizzled;
00056 using namespace google;
00057 
00059 extern TransactionLog *transaction_log;
00060 
00061 plugin::Create_function<HexdumpTransactionMessageFunction> *hexdump_transaction_message_func_factory= NULL;
00062 
00063 void HexdumpTransactionMessageFunction::fix_length_and_dec()
00064 {
00065   max_length= 2 * 1024 * 1024; /* 2MB size limit seems ok... */
00066   args[0]->collation.set(
00067     get_charset_by_csname(args[0]->collation.collation->csname,
00068                           MY_CS_BINSORT), DERIVATION_COERCIBLE);
00069 }
00070 
00071 String *HexdumpTransactionMessageFunction::val_str(String *str)
00072 {
00073   assert(fixed == true);
00074 
00075   String *filename_arg= args[0]->val_str(str);
00076   off_t offset_arg= static_cast<int64_t>(args[1]->val_int());
00077 
00078   if (filename_arg == NULL || args[1]->null_value == true)
00079   {
00080     my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
00081     null_value= true;
00082     return NULL;
00083   }
00084 
00085   if (transaction_log == NULL)
00086   {
00087     my_error(ER_INVALID_NULL_ARGUMENT, MYF(0), func_name());
00088     null_value= true;
00089     return NULL;
00090   }
00091 
00092   null_value= false;
00093 
00094   message::Transaction transaction_message;
00095 
00102   const string &filename= transaction_log->getLogFilename();
00103   int log_file= open(filename.c_str(), O_RDONLY);
00104   if (log_file == -1)
00105   {
00106     drizzled::sql_perror(_("Failed to open transaction log file"), filename);
00107     null_value= true;
00108 
00109     return NULL;
00110   }
00111 
00112   (void) lseek(log_file, offset_arg, SEEK_SET);
00113 
00114   protobuf::io::FileInputStream *file_input= new protobuf::io::FileInputStream(log_file);
00115   file_input->SetCloseOnDelete(true);
00116 
00117   protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(file_input);
00118 
00119   /* Grab our message type and length */
00120   uint32_t message_type;
00121   if (! coded_input->ReadLittleEndian32(&message_type))
00122   {
00123     delete file_input;
00124 
00126     null_value= true;
00127     return NULL;
00128   }
00129 
00130   uint32_t message_size;
00131   if (! coded_input->ReadLittleEndian32(&message_size))
00132   {
00133     delete file_input;
00134 
00136     null_value= true;
00137     return NULL;
00138   }
00139 
00140   uint8_t *buffer= (uint8_t *) malloc(message_size);
00141 
00142   bool result= coded_input->ReadRaw(buffer, message_size);
00143   if (result == false)
00144   {
00145     char errmsg[STRERROR_MAX];
00146     strerror_r(errno, errmsg, sizeof(errmsg));
00147     fprintf(stderr, _("Could not read transaction message.\n"));
00148     fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
00149     fprintf(stderr, _("Raw buffer read: %s.\n"), buffer);
00150   }
00151 
00152   /*
00153    * Convert raw bytes to a hex representation and store back into
00154    * the return String.
00155    */
00156   string hexdump;
00157   hexdump.reserve(message_size * 4);
00158   bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), message_size);
00159 
00160   /* 
00161    * Check that the transaction message is actually not corrupt before
00162    * printing out the raw byte representation of the transaction message...
00163    */
00164   result= transaction_message.ParseFromArray(buffer, static_cast<int32_t>(message_size));
00165   if (result == false)
00166   {
00167     fprintf(stderr, _("Unable to parse transaction. Got error: %s.\n"), transaction_message.InitializationErrorString().c_str());
00168     if (buffer != NULL)
00169       fprintf(stderr, _("BUFFER: %s\n"), buffer);
00170   }
00171 
00172   str->alloc(message_size * 4); /* Hex representation is ~4 times number of bytes */
00173 
00174   strncpy(str->ptr(), hexdump.c_str(), hexdump.length());
00175   str->length(hexdump.length());
00176 
00177   free(buffer);
00178 
00179   delete coded_input;
00180   delete file_input;
00181 
00182   return str;
00183 }