0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
HqlCommandInterpreter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "Client.h"
25 #include "HqlCommandInterpreter.h"
26 #include "HqlHelpText.h"
27 #include "HqlParser.h"
28 #include "Key.h"
29 #include "LoadDataSource.h"
30 #include "Schema.h"
31 
32 #include <Common/Error.h>
33 #include <Common/FileUtils.h>
34 #include <Common/Stopwatch.h>
35 
36 #include <boost/progress.hpp>
37 
38 #include <cassert>
39 #include <cstdio>
40 #include <cstring>
41 #include <memory>
42 
43 using namespace std;
44 using namespace Hypertable;
45 using namespace Hql;
46 
47 namespace {
48 
49  struct CommandCallback : HqlInterpreter::Callback {
50  CommandInterpreter &commander;
51  int command {};
52  unique_ptr<boost::progress_display> progress;
53  Stopwatch stopwatch;
54  bool m_profile {};
55 
56  CommandCallback(CommandInterpreter &interp, bool profile=false)
57  : HqlInterpreter::Callback(interp.normal_mode()), commander(interp),
58  m_profile(profile) {
59  format_ts_in_nanos = interp.timestamp_output_format()
60  == CommandInterpreter::TIMESTAMP_FORMAT_NANOS;
61  output = stdout; // set to stdout
62  }
63 
64  ~CommandCallback() {
65  if (output == stdout || output == stderr)
66  fflush(output);
67  else if (output)
68  fclose(output);
69  }
70 
71  void on_parsed(ParserState &state) override { command = state.command; }
72 
73  void on_return(const string &str) override { cout << str << endl; }
74 
75  void on_update(size_t total) override {
76  if (!normal_mode)
77  return;
78 
79  HT_ASSERT(command);
80 
81  if (command == COMMAND_LOAD_DATA) {
82  HT_ASSERT(!progress);
83  cout <<"\nLoading "<< format_number(total)
84  <<" bytes of input data..." << endl;
85  progress = make_unique<boost::progress_display>(total);
86  }
87  }
88 
89  void on_progress(size_t amount) override {
90  HT_ASSERT(progress);
91  *progress += amount;
92  }
93 
94  void on_finish(TableMutatorPtr &mutator) override {
95  Callback::on_finish(mutator);
96  stopwatch.stop();
97 
98  if (normal_mode) {
99  if (progress && progress->count() < file_size)
100  *progress += file_size - progress->count();
101 
102  double elapsed = stopwatch.elapsed();
103 
104  if (command == COMMAND_LOAD_DATA)
105  fprintf(stderr, "Load complete.\n");
106 
107  fputc('\n', stderr);
108  fprintf(stderr, " Elapsed time: %.2f s\n", elapsed);
109 
110  if (total_values_size)
111  fprintf(stderr, "Avg value size: %.2f bytes\n",
112  (double)total_values_size / total_cells);
113 
114  if (total_keys_size)
115  fprintf(stderr, " Avg key size: %.2f bytes\n",
116  (double)total_keys_size / total_cells);
117 
118  if (total_keys_size && total_values_size) {
119  fprintf(stderr, " Throughput: %.2f bytes/s",
120  (total_keys_size + total_values_size) / elapsed);
121 
122  if (file_size)
123  fprintf(stderr, " (%.2f bytes/s)", file_size / elapsed);
124 
125  fputc('\n', stderr);
126  }
127  if (total_cells) {
128  fprintf(stderr, " Total cells: %llu\n", (Llu)total_cells);
129  fprintf(stderr, " Throughput: %.2f cells/s\n",
130  total_cells / elapsed);
131  }
132  if (mutator)
133  fprintf(stderr, " Resends: %llu\n",
134  (Llu)mutator->get_resend_count());
135 
136  fflush(stderr);
137  }
138  }
139 
140  void on_finish(TableScannerPtr &scanner) override {
141  if (scanner && m_profile) {
142  fputc('\n', stderr);
143  fprintf(stderr, " Elapsed time: %lld ms\n", (Lld)stopwatch.elapsed_millis());
144  ProfileDataScanner profile_data;
145  scanner->get_profile_data(profile_data);
146  fprintf(stderr, " Cells scanned: %lld\n", (Lld)profile_data.cells_scanned);
147  fprintf(stderr, "Cells returned: %lld\n", (Lld)profile_data.cells_returned);
148  fprintf(stderr, " Bytes scanned: %lld\n", (Lld)profile_data.bytes_scanned);
149  fprintf(stderr, "Bytes returned: %lld\n", (Lld)profile_data.bytes_returned);
150  fprintf(stderr, " Disk read: %lld\n", (Lld)profile_data.disk_read);
151  fprintf(stderr, " Scan blocks: %d\n", (int)profile_data.scanblocks);
152  fprintf(stderr, " Sub scanners: %d\n", (int)profile_data.subscanners);
153  string servers;
154  bool first = true;
155  for (auto & server : profile_data.servers) {
156  if (first)
157  first = false;
158  else
159  servers += ",";
160  servers += server;
161  }
162  fprintf(stderr, " Servers: %s\n", servers.c_str());
163  fputc('\n', stderr);
164  fflush(stderr);
165  }
166  }
167 
168  };
169 
170 } // local namespace
171 
172 
173 HqlCommandInterpreter::HqlCommandInterpreter(Client *client, bool profile)
174  : m_interp(client->create_hql_interpreter(false)), m_profile(profile) {
175 }
176 
177 
179  : m_interp(interp) {
180 }
181 
182 
183 int HqlCommandInterpreter::execute_line(const string &line) {
184  CommandCallback cb(*this, m_profile);
185  return m_interp->execute(line, cb);
186 }
187 
double elapsed()
Returns the elapsed time.
Definition: Stopwatch.h:72
String format_number(int64_t n, int sep)
Return decimal number string separated by a separator (default: comma) for every 3 digits...
Definition: String.cc:73
bool m_profile
Flag indicating if SELECT commands should be profiled.
The Stopwatch measures elapsed time.
long long unsigned int Llu
Shortcut for printf formats.
Definition: String.h:50
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
int execute_line(const std::string &line) override
Executes an HQL command.
std::shared_ptr< TableScanner > TableScannerPtr
Smart pointer to TableScanner.
Definition: TableScanner.h:124
Declarations for Schema.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
std::shared_ptr< TableMutator > TableMutatorPtr
Smart pointer to TableMutator.
Definition: TableMutator.h:257
File system utility functions.
The API of HQL interpreter.
Compatibility Macros for C/C++.
int64_t elapsed_millis()
Returns elapsed time in milliseconds.
Definition: Stopwatch.h:84
Hypertable definitions
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
Callback interface/base class for execute.
Declarations for HqlCommandInterpreter.
The Stopwatch class measures elapsed time between instantiation (or a call to start) and a call to st...
Definition: Stopwatch.h:40
HqlCommandInterpreter(Client *client, bool profile=false)
Constructor.
Declarations for HqlHelpText.
Error codes, Exception handling, error logging.
HqlInterpreterPtr m_interp
HQL interpreter.
void stop()
Stops the Stopwatch.
Definition: Stopwatch.h:58