~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/command_reader.cc

Added code necessary for building plugins dynamically.
Merged in changes from lifeless to allow autoreconf to work.
Touching plugin.ini files now triggers a rebuid - so config/autorun.sh is no
longer required to be run after touching those.
Removed the duplicate plugin names - also removed the issue that getting them
different would silently fail weirdly later.

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
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include <drizzled/global.h>
21
 
#include <sys/types.h>
22
 
#include <sys/stat.h>
23
 
#include <fcntl.h>
24
 
#include <iostream>
25
 
#include <fstream>
26
 
#include <string>
27
 
#include <unistd.h>
28
 
#include <cstdio>
29
 
#include <drizzled/message/replication.pb.h>
30
 
#include "drizzled/gettext.h"
31
 
#include "drizzled/message/command_transform.h"
32
 
#include "drizzled/korr.h"
33
 
 
34
 
using namespace std;
35
 
using namespace drizzled;
36
 
 
37
 
static void printCommand(const message::Command &command)
38
 
{
39
 
  cout << "/* Timestamp: " << command.timestamp() << " */"<< endl;
40
 
 
41
 
  message::TransactionContext trx= command.transaction_context();
42
 
 
43
 
  cout << "/* SERVER ID: " << trx.server_id() << " TRX ID: " << trx.transaction_id();
44
 
  
45
 
  if (command.has_session_id())
46
 
    cout << " SESSION ID: " << command.session_id();
47
 
 
48
 
  cout << " */ ";
49
 
 
50
 
  string sql("");
51
 
 
52
 
  message::transformCommand2Sql(command, &sql, message::DRIZZLE);
53
 
 
54
 
  /* 
55
 
   * Replace \n with spaces so that SQL statements 
56
 
   * are always on a single line 
57
 
   */
58
 
  const std::string newline= "\n";
59
 
  while (sql.find(newline) != std::string::npos)
60
 
    sql.replace(sql.find(newline), 1, " ");
61
 
 
62
 
  cout << sql << ';' << endl;
63
 
}
64
 
 
65
 
int main(int argc, char* argv[])
66
 
{
67
 
  GOOGLE_PROTOBUF_VERIFY_VERSION;
68
 
  int file;
69
 
 
70
 
  if (argc != 2)
71
 
  {
72
 
    fprintf(stderr, _("Usage: %s COMMAND_LOG\n"), argv[0]);
73
 
    return -1;
74
 
  }
75
 
 
76
 
  message::Command command;
77
 
 
78
 
  file= open(argv[1], O_RDONLY);
79
 
  if (file == -1)
80
 
  {
81
 
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
82
 
  }
83
 
 
84
 
  char *buffer= NULL;
85
 
  char *temp_buffer= NULL;
86
 
  uint64_t previous_length= 0;
87
 
  ssize_t read_bytes= 0;
88
 
  uint64_t length= 0;
89
 
  uint32_t checksum= 0;
90
 
 
91
 
  /* We use korr.h macros when writing and must do the same when reading... */
92
 
  unsigned char coded_length[8];
93
 
  unsigned char coded_checksum[4];
94
 
 
95
 
  /* Read in the length of the command */
96
 
  while ((read_bytes= read(file, coded_length, sizeof(uint64_t))) != 0)
97
 
  {
98
 
    if (read_bytes == -1)
99
 
    {
100
 
      fprintf(stderr, _("Failed to read initial length header\n"));
101
 
      exit(1);
102
 
    }
103
 
    length= uint8korr(coded_length);
104
 
 
105
 
    if (length > SIZE_MAX)
106
 
    {
107
 
      fprintf(stderr, _("Attempted to read record bigger than SIZE_MAX\n"));
108
 
      exit(1);
109
 
    }
110
 
 
111
 
    if (buffer == NULL)
112
 
    {
113
 
      /* 
114
 
       * First time around...just malloc the length.  This block gets rid
115
 
       * of a GCC warning about uninitialized temp_buffer.
116
 
       */
117
 
      temp_buffer= (char *) malloc((size_t) length);
118
 
    }
119
 
    /* No need to allocate if we have a buffer big enough... */
120
 
    else if (length > previous_length)
121
 
    {
122
 
      temp_buffer= (char *) realloc(buffer, (size_t) length);
123
 
    }
124
 
 
125
 
    if (temp_buffer == NULL)
126
 
    {
127
 
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"), length);
128
 
      exit(1);
129
 
    }
130
 
    else
131
 
      buffer= temp_buffer;
132
 
 
133
 
    /* Read the Command */
134
 
    read_bytes= read(file, buffer, (size_t) length);
135
 
    if ((read_bytes != (ssize_t) length))
136
 
    {
137
 
      fprintf(stderr, _("Could not read entire transaction. Read %" PRIu64 " bytes instead of %" PRIu64 " bytes.\n"), (uint64_t) read_bytes, (uint64_t) length);
138
 
      exit(1);
139
 
    }
140
 
 
141
 
    if (! command.ParseFromArray(buffer, (int) length))
142
 
    {
143
 
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), command.InitializationErrorString().c_str());
144
 
      if (buffer != NULL)
145
 
        fprintf(stderr, _("BUFFER: %s\n"), buffer);
146
 
      exit(1);
147
 
    }
148
 
 
149
 
    /* Read the checksum */
150
 
    read_bytes= read(file, coded_checksum, sizeof(uint32_t));
151
 
    if ((read_bytes != (ssize_t) sizeof(uint32_t)))
152
 
    {
153
 
      fprintf(stderr, _("Could not read entire checksum. Read %" PRIu64 " bytes instead of 4 bytes.\n"), (uint64_t) read_bytes);
154
 
      exit(1);
155
 
    }
156
 
    checksum= uint4korr(coded_checksum);
157
 
 
158
 
    if (checksum != 0)
159
 
    {
160
 
      /* @TODO checksumming.. */
161
 
    }
162
 
 
163
 
    /* Print the command */
164
 
    printCommand(command);
165
 
 
166
 
    /* Reset our length check */
167
 
    previous_length= length;
168
 
    memset(coded_length, 0, sizeof(coded_length));
169
 
  }
170
 
  if (buffer)
171
 
    free(buffer);
172
 
  return 0;
173
 
}