~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/transaction_reader.cc

  • Committer: Brian Aker
  • Date: 2009-10-15 00:22:33 UTC
  • mto: (1183.1.11 merge)
  • mto: This revision was merged to the branch mainline in revision 1198.
  • Revision ID: brian@gaz-20091015002233-fa4ao2mbc67wls91
First pass of information engine. OMG, ponies... is it so much easier to
deal with creating and engine.

The list table iterator though... its ass, needs to go. We should also
abstract out share. Very few engines need a custom one. Just say'in

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; version 2 of the License.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program; if not, write to the Free Software
 
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 */
 
23
 
 
24
#include <drizzled/global.h>
 
25
#include <drizzled/gettext.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <fcntl.h>
 
29
#include <iostream>
 
30
#include <string>
 
31
#include <vector>
 
32
#include <unistd.h>
 
33
#include <drizzled/message/transaction.pb.h>
 
34
#include <drizzled/message/statement_transform.h>
 
35
 
 
36
#include <google/protobuf/io/coded_stream.h>
 
37
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
38
 
 
39
using namespace std;
 
40
using namespace drizzled;
 
41
using namespace google;
 
42
 
 
43
static void printStatement(const message::Statement &statement)
 
44
{
 
45
  cout << "/* Start Timestamp: " << statement.start_timestamp() << " ";
 
46
  cout << " End Timestamp: " << statement.end_timestamp() << " */" << endl;
 
47
 
 
48
  vector<string> sql_strings;
 
49
 
 
50
  message::transformStatementToSql(statement, sql_strings, message::DRIZZLE);
 
51
 
 
52
  vector<string>::iterator sql_string_iter= sql_strings.begin();
 
53
  const std::string newline= "\n";
 
54
  while (sql_string_iter != sql_strings.end())
 
55
  {
 
56
    string &sql= *sql_string_iter;
 
57
    /* 
 
58
     * Replace \n with spaces so that SQL statements 
 
59
     * are always on a single line 
 
60
     */
 
61
    while (sql.find(newline) != std::string::npos)
 
62
      sql.replace(sql.find(newline), 1, " ");
 
63
 
 
64
    cout << sql << ';' << endl;
 
65
    ++sql_string_iter;
 
66
  }
 
67
}
 
68
 
 
69
static void printTransaction(const message::Transaction &transaction)
 
70
{
 
71
  const message::TransactionContext trx= transaction.transaction_context();
 
72
 
 
73
  cout << "/* SERVER ID: " << trx.server_id() << " TRX ID: " << trx.transaction_id() << " */ " << endl;
 
74
 
 
75
  size_t num_statements= transaction.statement_size();
 
76
  size_t x;
 
77
 
 
78
  for (x= 0; x < num_statements; ++x)
 
79
  {
 
80
    const message::Statement &statement= transaction.statement(x);
 
81
    printStatement(statement);
 
82
  }
 
83
}
 
84
 
 
85
int main(int argc, char* argv[])
 
86
{
 
87
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
88
  int file;
 
89
 
 
90
  if (argc != 2)
 
91
  {
 
92
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG\n"), argv[0]);
 
93
    return -1;
 
94
  }
 
95
 
 
96
  message::Transaction transaction;
 
97
 
 
98
  file= open(argv[1], O_RDONLY);
 
99
  if (file == -1)
 
100
  {
 
101
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
 
102
    return -1;
 
103
  }
 
104
 
 
105
  protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
 
106
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
 
107
 
 
108
  char *buffer= NULL;
 
109
  char *temp_buffer= NULL;
 
110
  uint64_t length= 0;
 
111
  uint64_t previous_length= 0;
 
112
  bool result= true;
 
113
 
 
114
  /* Read in the length of the command */
 
115
  while (result == true && coded_input->ReadLittleEndian64(&length) == true)
 
116
  {
 
117
    if (length > SIZE_MAX)
 
118
    {
 
119
      fprintf(stderr, _("Attempted to read record bigger than SIZE_MAX\n"));
 
120
      exit(1);
 
121
    }
 
122
 
 
123
    if (buffer == NULL)
 
124
    {
 
125
      /* 
 
126
       * First time around...just malloc the length.  This block gets rid
 
127
       * of a GCC warning about uninitialized temp_buffer.
 
128
       */
 
129
      temp_buffer= (char *) malloc((size_t) length);
 
130
    }
 
131
    /* No need to allocate if we have a buffer big enough... */
 
132
    else if (length > previous_length)
 
133
    {
 
134
      temp_buffer= (char *) realloc(buffer, (size_t) length);
 
135
    }
 
136
 
 
137
    if (temp_buffer == NULL)
 
138
    {
 
139
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
 
140
              static_cast<uint64_t>(length));
 
141
      break;
 
142
    }
 
143
    else
 
144
      buffer= temp_buffer;
 
145
 
 
146
    /* Read the Command */
 
147
    result= coded_input->ReadRaw(buffer, (int) length);
 
148
    if (result == false)
 
149
    {
 
150
      fprintf(stderr, _("Could not read transaction message.\n"));
 
151
      fprintf(stderr, _("GPB ERROR: %s.\n"), strerror(errno));
 
152
      fprintf(stderr, _("Raw buffer read: %s.\n"), buffer);
 
153
      break;
 
154
    }
 
155
 
 
156
    result= transaction.ParseFromArray(buffer, static_cast<size_t>(length));
 
157
    if (result == false)
 
158
    {
 
159
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
 
160
      if (buffer != NULL)
 
161
        fprintf(stderr, _("BUFFER: %s\n"), buffer);
 
162
      break;
 
163
    }
 
164
 
 
165
    /* Print the transaction */
 
166
    printTransaction(transaction);
 
167
 
 
168
    previous_length= length;
 
169
  }
 
170
  if (buffer)
 
171
    free(buffer);
 
172
  
 
173
  delete coded_input;
 
174
  delete raw_input;
 
175
 
 
176
  return (result == true ? 0 : 1);
 
177
}