~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/binlog_writer.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-13 01:03:01 UTC
  • mto: (1126.9.2 captain-20090915-01)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: osullivan.padraig@gmail.com-20090913010301-tcvvezipx1124acy
Added calls to the dtrace delete begin/end probes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <drizzled/global.h>
 
2
 
 
3
#include <drizzled/message/binlog_encoding.h>
 
4
#include <drizzled/message/binary_log.h>
 
5
 
 
6
#include "ioutil.h"
 
7
 
 
8
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
9
#include <google/protobuf/io/coded_stream.h>
 
10
 
 
11
#include <iostream>
 
12
#include <fstream>
 
13
#include <sstream>
 
14
#include <string>
 
15
#include <map>
 
16
 
 
17
#include <getopt.h>
 
18
#include <sys/stat.h>
 
19
#include <fcntl.h>
 
20
 
 
21
using namespace std;
 
22
using namespace google;
 
23
 
 
24
typedef std::map<std::string,std::string> Assign;
 
25
 
 
26
static void print_usage_and_exit(char *prog) {
 
27
  using std::cerr;
 
28
  const char *name= strrchr(prog, '/');
 
29
 
 
30
  if (name)
 
31
    ++name;
 
32
  else
 
33
    name= "binlog_writer";
 
34
  cerr << "Usage: " << name << " <options> <query>\n"
 
35
       << "    --output name    Append query to file <name> (default: 'log.bin')\n"
 
36
       << "    --set var=val    Set value of user variable for query\n"
 
37
       << "    --trans-id <id>  Set transaction id to <id>\n"
 
38
       << flush;
 
39
  exit(1);
 
40
}
 
41
 
 
42
 
 
43
static void
 
44
write_query(protobuf::io::CodedOutputStream* out,
 
45
            unsigned long trans_id,
 
46
            const string& query,
 
47
            const Assign& assign)
 
48
{
 
49
  BinaryLog::Query *message = new BinaryLog::Query;
 
50
 
 
51
  {
 
52
    BinaryLog::Header *header= message->mutable_header();
 
53
    header->set_seqno(time(NULL));
 
54
    header->set_server_id(1);
 
55
    header->set_trans_id(trans_id);
 
56
  }
 
57
 
 
58
  message->set_query(query);
 
59
  for (Assign::const_iterator ii= assign.begin() ;
 
60
       ii != assign.end() ;
 
61
       ++ii )
 
62
  {
 
63
    BinaryLog::Query::Variable *var= message->add_variable();
 
64
    var->set_name(ii->first);
 
65
    var->set_val(ii->second);
 
66
  }
 
67
 
 
68
  BinaryLog::Event event(BinaryLog::Event::QUERY, message);
 
69
  event.write(out);
 
70
}
 
71
 
 
72
 
 
73
int main(int argc, char *argv[])
 
74
{
 
75
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
76
 
 
77
  static struct option options[] = {
 
78
    { "set",       1 /* has_arg */, NULL, 0 },
 
79
    { "trans-id",  1 /* has_arg */, NULL, 0 },
 
80
    { "output",    1 /* has_arg */, NULL, 0 },
 
81
    { 0, 0, 0, 0 }
 
82
  };
 
83
 
 
84
  Assign assign;
 
85
  unsigned long trans_id= 0;
 
86
  const char* file_name= "log.bin";
 
87
 
 
88
  int ch, option_index;
 
89
  while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
 
90
    if (ch == '?')
 
91
      print_usage_and_exit(argv[0]);
 
92
 
 
93
    switch (option_index) {
 
94
    case 0:                                     // --set
 
95
    {
 
96
      // Split the supplied string at the first '='
 
97
      char *end= optarg + strlen(optarg);
 
98
      char *pos= strchr(optarg, '=');
 
99
      if (!pos)
 
100
        pos= end;
 
101
      const string key(optarg, pos);
 
102
      const string value(pos == end ? end : pos+1, end);
 
103
      assign[key]= value;
 
104
    }
 
105
 
 
106
    case 1:                                     // --trans-id
 
107
      trans_id= strtoul(optarg, NULL, 0);
 
108
      break;
 
109
 
 
110
    case 2:                                     // --output
 
111
      file_name= optarg;
 
112
      break;
 
113
    }
 
114
  }
 
115
 
 
116
  if (optind >= argc)
 
117
    print_usage_and_exit(argv[0]);
 
118
 
 
119
  filebuf fb;
 
120
 
 
121
  fb.open(file_name, ios::app | ios::out);
 
122
 
 
123
  ostream os(&fb);
 
124
 
 
125
  protobuf::io::ZeroCopyOutputStream* raw_output=
 
126
    new protobuf::io::OstreamOutputStream(&os);
 
127
  protobuf::io::CodedOutputStream* coded_output=
 
128
    new protobuf::io::CodedOutputStream(raw_output);
 
129
 
 
130
  stringstream sout;
 
131
  sout << ioutil::join(" ", &argv[optind], &argv[argc]);
 
132
 
 
133
  write_query(coded_output, trans_id, sout.str(), assign);
 
134
 
 
135
  delete coded_output;
 
136
  delete raw_output;
 
137
  fb.close();
 
138
  return 0;
 
139
}