~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/command_reader.cc

Style cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <drizzled/server_includes.h>
 
2
#include <drizzled/gettext.h>
 
3
#include <sys/types.h>
 
4
#include <sys/stat.h>
 
5
#include <fcntl.h>
 
6
#include <iostream>
 
7
#include <fstream>
 
8
#include <string>
 
9
#include <unistd.h>
 
10
#include <cstdio>
 
11
#include <drizzled/message/replication.pb.h>
 
12
 
 
13
#include "drizzled/message/command_transform.h"
 
14
 
 
15
#include "drizzled/korr.h"
 
16
 
 
17
using namespace std;
 
18
using namespace drizzled;
 
19
 
 
20
static void printCommand(const message::Command &command)
 
21
{
 
22
  cout << "/* Timestamp: " << command.timestamp() << " */"<< endl;
 
23
 
 
24
  message::TransactionContext trx= command.transaction_context();
 
25
 
 
26
  cout << "/* SERVER ID: " << trx.server_id() << " TRX ID: " << trx.transaction_id();
 
27
  
 
28
  if (command.has_session_id())
 
29
    cout << " SESSION ID: " << command.session_id();
 
30
 
 
31
  cout << " */ ";
 
32
 
 
33
  string sql("");
 
34
 
 
35
  message::transformCommand2Sql(command, &sql, message::DRIZZLE);
 
36
 
 
37
  /* 
 
38
   * Replace \n with spaces so that SQL statements 
 
39
   * are always on a single line 
 
40
   */
 
41
  const std::string newline= "\n";
 
42
  while (sql.find(newline) != std::string::npos)
 
43
    sql.replace(sql.find(newline), 1, " ");
 
44
 
 
45
  cout << sql << ';' << endl;
 
46
}
 
47
 
 
48
int main(int argc, char* argv[])
 
49
{
 
50
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
51
  int file;
 
52
 
 
53
  if (argc != 2)
 
54
  {
 
55
    fprintf(stderr, _("Usage: %s COMMAND_LOG\n"), argv[0]);
 
56
    return -1;
 
57
  }
 
58
 
 
59
  message::Command command;
 
60
 
 
61
  file= open(argv[1], O_RDONLY);
 
62
  if (file == -1)
 
63
  {
 
64
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
 
65
  }
 
66
 
 
67
  char *buffer= NULL;
 
68
  char *temp_buffer= NULL;
 
69
  uint64_t previous_length= 0;
 
70
  ssize_t read_bytes= 0;
 
71
  uint64_t length= 0;
 
72
  uint32_t checksum= 0;
 
73
 
 
74
  /* We use korr.h macros when writing and must do the same when reading... */
 
75
  unsigned char coded_length[8];
 
76
  unsigned char coded_checksum[4];
 
77
 
 
78
  /* Read in the length of the command */
 
79
  while ((read_bytes= read(file, coded_length, sizeof(uint64_t))) != 0)
 
80
  {
 
81
    if (read_bytes == -1)
 
82
    {
 
83
      fprintf(stderr, _("Failed to read initial length header\n"));
 
84
      exit(1);
 
85
    }
 
86
    length= uint8korr(coded_length);
 
87
 
 
88
    if (length > SIZE_MAX)
 
89
    {
 
90
      fprintf(stderr, _("Attempted to read record bigger than SIZE_MAX\n"));
 
91
      exit(1);
 
92
    }
 
93
 
 
94
    if (buffer == NULL)
 
95
    {
 
96
      /* 
 
97
       * First time around...just malloc the length.  This block gets rid
 
98
       * of a GCC warning about uninitialized temp_buffer.
 
99
       */
 
100
      temp_buffer= (char *) malloc((size_t) length);
 
101
    }
 
102
    /* No need to allocate if we have a buffer big enough... */
 
103
    else if (length > previous_length)
 
104
    {
 
105
      temp_buffer= (char *) realloc(buffer, (size_t) length);
 
106
    }
 
107
 
 
108
    if (temp_buffer == NULL)
 
109
    {
 
110
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"), length);
 
111
      exit(1);
 
112
    }
 
113
    else
 
114
      buffer= temp_buffer;
 
115
 
 
116
    /* Read the Command */
 
117
    read_bytes= read(file, buffer, (size_t) length);
 
118
    if ((read_bytes != (ssize_t) length))
 
119
    {
 
120
      fprintf(stderr, _("Could not read entire transaction. Read %" PRIu64 " bytes instead of %" PRIu64 " bytes.\n"), (uint64_t) read_bytes, (uint64_t) length);
 
121
      exit(1);
 
122
    }
 
123
 
 
124
    if (! command.ParseFromArray(buffer, (int) length))
 
125
    {
 
126
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), command.InitializationErrorString().c_str());
 
127
      if (buffer != NULL)
 
128
        fprintf(stderr, _("BUFFER: %s\n"), buffer);
 
129
      exit(1);
 
130
    }
 
131
 
 
132
    /* Read the checksum */
 
133
    read_bytes= read(file, coded_checksum, sizeof(uint32_t));
 
134
    if ((read_bytes != (ssize_t) sizeof(uint32_t)))
 
135
    {
 
136
      fprintf(stderr, _("Could not read entire checksum. Read %" PRIu64 " bytes instead of 4 bytes.\n"), (uint64_t) read_bytes);
 
137
      exit(1);
 
138
    }
 
139
    checksum= uint4korr(coded_checksum);
 
140
 
 
141
    if (checksum != 0)
 
142
    {
 
143
      /* @TODO checksumming.. */
 
144
    }
 
145
 
 
146
    /* Print the command */
 
147
    printCommand(command);
 
148
 
 
149
    /* Reset our length check */
 
150
    previous_length= length;
 
151
    memset(coded_length, 0, sizeof(coded_length));
 
152
  }
 
153
  if (buffer)
 
154
    free(buffer);
 
155
  return 0;
 
156
}