~stewart/percona-playback/cassert-header

« back to all changes in this revision

Viewing changes to src/query.cc

  • Committer: Oleg Tsarev
  • Date: 2011-03-21 02:10:49 UTC
  • Revision ID: oleg.tsarev@percona.com-20110321021049-0olp7o9gjk545yr7
splitfy progress
add "try-run" option
add "split-to" and "from-split" options
change boolean type "is_read_query" to "Unknow", "Read", "Write" types
fix result names of perf test files

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
}
64
64
 
65
65
const char* parse_query(const char*  begin, const std::size_t  length
66
 
                        , Enum_Query_Parse_State &state, bool &is_read)
 
66
                        , Enum_Query_Parse_State &state, Enum_Query_Type &type)
67
67
{
68
68
 
69
 
  /*  is_read= (strncmp(begin, "select", get_min(length, 6)) == 0)
 
69
  /*  is_read\= (strncmp(begin, "select", get_min(length, 6)) == 0)
70
70
    || (strncmp(begin, "SELECT", get_min(length, 6)) == 0)
71
71
    || (strncmp(begin, "show", get_min(length, 4)) == 0)
72
72
    || (strncmp(begin, "SHOW", get_min(length, 4)) == 0);
86
86
  b[length]= 0;
87
87
  std::cerr << "begin=" << b << "\nlength=" << length << "\nstart state=" << state << std::endl;*/
88
88
  const char *end= begin + length;
89
 
#define JUMP(s)                                                         \
90
 
  do                                                                    \
91
 
    {                                                                   \
92
 
      state= s;                                                         \
93
 
      if (state == FINAL || begin >= end)                               \
94
 
        {                                                               \
95
 
          /*std::cerr << "stop state=" << state << "\nis_read=" << (is_read ? "true" : "false") << std::endl;*/ \
96
 
          return begin;                                                 \
97
 
        }                                                               \
98
 
      else                                                              \
99
 
        goto s##_STATE;                                                 \
100
 
    }                                                                   \
101
 
  while(false)
 
89
#define JUMP(s) do { state= s; goto s##_STATE; } while(false)
102
90
#define GOTO(s)                                 \
103
91
  do                                            \
104
92
    {                                           \
105
 
      ++begin;                                  \
106
 
      JUMP(s);                                  \
 
93
      if (begin >= end)                         \
 
94
        return begin;                           \
 
95
      else                                      \
 
96
        {                                       \
 
97
          ++begin;                              \
 
98
          JUMP(s);                              \
 
99
        }                                       \
107
100
    }                                           \
108
101
  while(false)
109
102
#define START(s) case(s): JUMP(s); break;
213
206
 
214
207
  STATE_BEGIN(C);
215
208
  STEP('\\', ZERO_BACK);
216
 
  case ('t'): case ('T'): is_read= true;
 
209
  case ('t'): case ('T'): type= eRead;
217
210
    //std::cout << "is_read!\n";
218
211
    GOTO(ZERO);
219
212
  STATE_JUMP(C, ZERO);
225
218
 
226
219
  STATE_BEGIN(O);
227
220
  STEP('\\', ZERO_BACK);
228
 
 case ('w'): case ('W'): is_read= true;
 
221
 case ('w'): case ('W'): type= eRead;
229
222
   //std::cout << "is_read!\n";
230
223
   GOTO(ZERO);
231
224
  STATE_JUMP(O, ZERO);
243
236
 FINAL_STATE:
244
237
  {
245
238
    //std::cerr << "stop state=" << state << "\nis_read=" << (is_read ? "true" : "false") << std::endl;
 
239
    if (type == eUnknown)
 
240
      type= eWrite;
246
241
    return begin;
247
242
  }
248
243
#undef STEP
251
246
#undef BACK
252
247
}
253
248
const char* parse_query(const char*  begin, const std::size_t  length
254
 
                        , int &state, bool &is_read)
 
249
                        , int &state, Enum_Query_Type &type)
255
250
{
256
251
  Enum_Query_Parse_State enum_state= static_cast<Enum_Query_Parse_State>(state);
257
 
  const char *result= parse_query(begin, length, enum_state, is_read);
 
252
  const char *result= parse_query(begin, length, enum_state, type);
258
253
  state= static_cast<int>(enum_state);
259
254
  return result;
260
255
}
266
261
{
267
262
  return static_cast<int>(FINAL);
268
263
}
269
 
bool is_query(String const& query, bool &is_read)
 
264
bool is_query(String const& query, Enum_Query_Type &type)
270
265
{
271
266
  int state= parse_query_zero();
272
 
  is_read= false;
273
 
  const char* result= parse_query(query.data(), query.length(), state, is_read);
 
267
  type= eUnknown;
 
268
  const char* result= parse_query(query.data(), query.length(), state, type);
274
269
  return (state == parse_query_final() || state == parse_query_zero()) && result == query.data() + query.length();
275
270
}
276
 
bool is_read_query(String const &query)
 
271
Enum_Query_Type query_type(String const &query)
277
272
{
278
 
  bool is_read= false;
279
 
  bool result= is_query(query, is_read);
280
 
  return is_read && result;
 
273
  Enum_Query_Type type= eUnknown;
 
274
#ifdef DEBUG
 
275
  bool result=
 
276
#endif // DEBUG
 
277
    is_query(query, type);
 
278
  ASSERT(result);
 
279
  return type;
281
280
}
282
281
bool is_quit(Query const &query)
283
282
{
291
290
  s << ", database=" << c.database;
292
291
  return s;
293
292
}
 
293
const char* to_c_str(Enum_Query_Type type)
 
294
{
 
295
  switch (type)
 
296
    {
 
297
    case (eUnknown): return "unknown";
 
298
    case (eRead): return "read";
 
299
    case (eWrite): return "write";
 
300
    default: ASSERT(false && "make gcc happy"); return "const char* to_c_str(Enum_Query_Type type) FAILED";
 
301
    };
 
302
}
 
303
std::ostream& operator<<(std::ostream&s , Enum_Query_Type type)
 
304
{
 
305
  return (s << to_c_str(type));
 
306
}
294
307
std::ostream& operator<<(std::ostream &s, Query const &c)
295
308
{
296
309
  s << "context=(" << static_cast<Query_Context const &>(c) << ")";
297
310
  s << ", query_id=" << c.query_id;
298
311
  s << ", sql=" << c.sql;
299
 
  s << ", is_read_query=" << (is_read_query(c.sql) ? "true" : "false");
 
312
  s << ", type=" << c.type;
300
313
  return s;
301
314
}
302
315
std::ostream& operator<<(std::ostream &s, Query_Result const &c)