~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/table_writer.cc

  • Committer: Brian Aker
  • Date: 2009-01-24 09:43:35 UTC
  • Revision ID: brian@gir-3.local-20090124094335-6qdtvc35gl5fvivz
Adding in an example singe thread scheduler

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 "config.h"
21
 
 
22
1
#include <iostream>
23
2
#include <fstream>
24
3
#include <string>
25
 
#include <drizzled/message/table.pb.h>
26
 
 
27
 
#include <boost/program_options.hpp>
28
 
 
 
4
#include <drizzled/serialize/table.pb.h>
29
5
using namespace std;
30
 
using namespace drizzled;
31
 
 
32
 
namespace po=boost::program_options;
33
6
 
34
7
/*
35
8
  Written from Google proto example
36
9
*/
37
10
 
38
 
static void fill_engine(message::Engine *engine)
 
11
void fill_engine(drizzle::Table::StorageEngine *engine)
39
12
{
 
13
  using namespace drizzle;
 
14
  using std::string;
 
15
  int16_t x;
 
16
 
40
17
  engine->set_name("InnoDB");
41
 
  message::Engine::Option *option;
 
18
  Table::StorageEngine::EngineOption *option;
42
19
 
43
20
  string option_names[2]= {
44
21
    "INDEX_DIRECTORY"
51
28
  };
52
29
 
53
30
  /* Add some engine options */
54
 
  for (int16_t x= 0; x < 2; x++)
 
31
  for (x= 0; x < 2; x++)
55
32
  {
56
 
    option= engine->add_options();
 
33
    option= engine->add_option();
57
34
    option->set_name(option_names[x]);
58
 
    option->set_state(option_values[x]);
 
35
    option->set_value(option_values[x]);
 
36
    option->set_type(Table::StorageEngine::EngineOption::STRING);
59
37
  }
60
38
}
61
39
 
62
 
static void new_index_to_table(message::Table *table,
63
 
                               const string name,
64
 
                               uint16_t num_index_parts,
65
 
                               uint32_t field_indexes[],
66
 
                               uint32_t compare_lengths[],
67
 
                               bool is_primary,
68
 
                               bool is_unique)
 
40
void new_index_to_table( drizzle::Table *table,
 
41
                         const std::string name,
 
42
                         uint16_t num_index_parts,
 
43
                         uint32_t field_indexes[],
 
44
                         uint32_t compare_lengths[],
 
45
                         bool is_primary,
 
46
                         bool is_unique)
69
47
{
70
 
  uint16_t x= 0;
71
 
 
72
 
  message::Table::Index *index;
73
 
  message::Table::Index::IndexPart *index_part;
74
 
 
75
 
  index= table->add_indexes();
 
48
  using namespace drizzle;
 
49
  uint16_t x;
 
50
 
 
51
  Table::Index *index;
 
52
  Table::Field *field;
 
53
  Table::Index::IndexPart *index_part;
 
54
 
 
55
  index= table->add_index();
76
56
 
77
57
  index->set_name(name);
78
 
  index->set_type(message::Table::Index::BTREE);
 
58
  index->set_type(Table::Index::BTREE);
79
59
  index->set_is_primary(is_primary);
80
60
  index->set_is_unique(is_unique);
81
61
 
82
 
  int key_length= 0;
83
 
 
84
 
  for(int i=0; i< num_index_parts; i++)
85
 
    key_length+= compare_lengths[i];
86
 
 
87
 
  index->set_key_length(key_length);
88
 
 
89
62
  while (x < num_index_parts)
90
63
  {
91
64
    index_part= index->add_index_part();
92
65
 
93
 
    index_part->set_fieldnr(field_indexes[x]);
 
66
    field= index_part->mutable_field();
 
67
    *field= table->field(field_indexes[x]);
94
68
 
95
69
    if (compare_lengths[x] > 0)
96
70
      index_part->set_compare_length(compare_lengths[x]);
99
73
  }
100
74
}
101
75
 
102
 
static void fill_table(message::Table *table, const char *name)
 
76
void fill_table(drizzle::Table *table, const char *name)
103
77
{
104
78
  uint16_t x;
105
79
 
106
 
  message::Table::Field *field;
107
 
  message::Table::Field::FieldConstraints *field_constraints;
108
 
  message::Table::Field::StringFieldOptions *string_field_options;
109
 
  message::Table::Field::NumericFieldOptions *numeric_field_options;
110
 
  message::Table::Field::EnumerationValues *enumeration_options;
 
80
  using namespace drizzle;
 
81
 
 
82
  Table::Field *field;
 
83
  Table::Field::FieldConstraints *field_constraints;
 
84
  Table::Field::FieldOptions *field_options;
 
85
  Table::Field::StringFieldOptions *string_field_options;
 
86
  Table::Field::NumericFieldOptions *numeric_field_options;
 
87
  Table::Field::SetFieldOptions *set_field_options;
111
88
 
112
89
  table->set_name(name);
113
 
  table->set_type(message::Table::STANDARD);
 
90
  table->set_type(Table::STANDARD);
114
91
 
115
92
  /* Write out some random varchar */
116
93
  for (x= 0; x < 3; x++)
120
97
    field_constraints= field->mutable_constraints();
121
98
    string_field_options= field->mutable_string_options();
122
99
 
123
 
    snprintf(buffer, sizeof(buffer), "sample%u", x);
 
100
    sprintf(buffer, "sample%u", x);
124
101
 
125
102
    field->set_name(buffer);
126
 
    field->set_type(message::Table::Field::VARCHAR);
 
103
    field->set_type(Table::Field::VARCHAR);
127
104
 
128
105
    field_constraints->set_is_nullable((x % 2));
129
106
 
139
116
  {
140
117
    field= table->add_field();
141
118
    field->set_name("number");
142
 
    field->set_type(message::Table::Field::INTEGER);
 
119
    field->set_type(Table::Field::INTEGER);
143
120
  }
144
121
  /* Write out a ENUM */
145
122
  {
146
123
    field= table->add_field();
147
 
    field->set_type(message::Table::Field::ENUM);
 
124
    field->set_type(Table::Field::ENUM);
148
125
    field->set_name("colors");
149
126
 
150
 
    enumeration_options= field->mutable_enumeration_values();
151
 
    enumeration_options->add_field_value("red");
152
 
    enumeration_options->add_field_value("blue");
153
 
    enumeration_options->add_field_value("green");
 
127
    set_field_options= field->mutable_set_options();
 
128
    set_field_options->add_value("red");
 
129
    set_field_options->add_value("blue");
 
130
    set_field_options->add_value("green");
 
131
    set_field_options->set_count_elements(set_field_options->value_size());
154
132
  }
155
133
  /* Write out a BLOB */
156
134
  {
157
135
    field= table->add_field();
158
136
    field->set_name("some_btye_string");
159
 
    field->set_type(message::Table::Field::BLOB);
 
137
    field->set_type(Table::Field::BLOB);
160
138
  }
161
139
 
162
140
  /* Write out a DECIMAL */
163
141
  {
164
142
    field= table->add_field();
165
143
    field->set_name("important_number");
166
 
    field->set_type(message::Table::Field::DECIMAL);
 
144
    field->set_type(Table::Field::DECIMAL);
167
145
 
168
146
    field_constraints= field->mutable_constraints();
169
147
    field_constraints->set_is_nullable(true);
192
170
  }
193
171
 
194
172
  /* Do engine-specific stuff */
195
 
  message::Engine *engine= table->mutable_engine();
 
173
  Table::StorageEngine *engine= table->mutable_engine();
196
174
  fill_engine(engine);
197
175
 
198
176
}
199
177
 
200
 
static void fill_table1(message::Table *table)
201
 
{
202
 
  message::Table::Field *field;
203
 
  message::Table::TableOptions *tableopts;
204
 
 
205
 
  table->set_name("t1");
206
 
  table->set_type(message::Table::INTERNAL);
207
 
 
208
 
  tableopts= table->mutable_options();
209
 
  tableopts->set_comment("Table without a StorageEngine message");
210
 
 
211
 
  {
212
 
    field= table->add_field();
213
 
    field->set_name("number");
214
 
    field->set_type(message::Table::Field::INTEGER);
215
 
  }
216
 
 
217
 
}
218
 
 
219
 
 
220
178
int main(int argc, char* argv[])
221
179
{
222
 
  int table_number= 0;
223
 
 
224
180
  GOOGLE_PROTOBUF_VERIFY_VERSION;
225
181
 
226
 
  po::options_description desc("Allowed options");
227
 
  desc.add_options()
228
 
    ("help", "produce help message")
229
 
    ("table-number,t", po::value<int>(&table_number)->default_value(0), "Table Number");
230
 
 
231
 
  po::variables_map vm;
232
 
  po::positional_options_description p;
233
 
  p.add("table-name", 1);
234
 
 
235
 
  // Disable allow_guessing
236
 
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
237
 
 
238
 
  po::store(po::command_line_parser(argc, argv).options(desc).style(style).
239
 
            positional(p).run(), vm);
240
 
 
241
 
  if (not vm.count("table-name"))
242
 
  {
243
 
    fprintf(stderr, "Expected Table name argument\n\n");
244
 
    cerr << desc << endl;
245
 
    exit(EXIT_FAILURE);
246
 
  }
247
 
 
248
 
  message::Table table;
249
 
 
250
 
  switch (table_number)
251
 
  {
252
 
  case 0:
253
 
    fill_table(&table, "example_table");
254
 
    break;
255
 
  case 1:
256
 
    fill_table1(&table);
257
 
    break;
258
 
  default:
259
 
    fprintf(stderr, "Invalid table number.\n\n");
260
 
    cerr << desc << endl;
261
 
    exit(EXIT_FAILURE);
262
 
  }
263
 
 
264
 
  fstream output(vm["table-name"].as<string>().c_str(),
265
 
                 ios::out | ios::trunc | ios::binary);
 
182
  if (argc != 2)
 
183
  {
 
184
    cerr << "Usage:  " << argv[0] << " SCHEMA" << endl;
 
185
    return -1;
 
186
  }
 
187
 
 
188
  drizzle::Table table;
 
189
 
 
190
  fill_table(&table, "example_table");
 
191
 
 
192
  fstream output(argv[1], ios::out | ios::trunc | ios::binary);
266
193
  if (!table.SerializeToOstream(&output))
267
194
  {
268
195
    cerr << "Failed to write schema." << endl;