0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DataGeneratorRowComponent.h
Go to the documentation of this file.
1 /* -*- c++ -*-
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 #ifndef Hypertable_Lib_DataGeneratorRowComponent_h
23 #define Hypertable_Lib_DataGeneratorRowComponent_h
24 
25 #include "DataGeneratorRandom.h"
26 
27 #include <Hypertable/Lib/Cell.h>
28 
29 #include <Common/Config.h>
31 #include <Common/Random.h>
32 #include <Common/String.h>
33 
34 extern "C" {
35 #include <limits.h>
36 #include <stdlib.h>
37 }
38 
39 #include <iostream>
40 #include <iterator>
41 #include <sstream>
42 #include <string>
43 #include <memory>
44 
45 using namespace Hypertable::Config;
46 using namespace std;
47 
48 namespace Hypertable {
49 
51 
52  enum Order { RANDOM, ASCENDING };
53 
55  public:
56  RowComponentSpec() : type(-1), order(-1), value_count(0), seed((unsigned)-1) { }
57  int type {};
58  int order {};
59  std::string format;
60  std::string min;
61  std::string max;
62  unsigned length_min {};
63  unsigned length_max {};
64  uint64_t value_count {};
65  unsigned seed {};
66  std::string distribution;
67  };
68 
69  class RowComponent : public RowComponentSpec {
70  public:
72  if (order == RANDOM) {
74  m_rng->set_seed(seed);
75  }
76  }
77  virtual ~RowComponent() { }
78  virtual bool next() = 0;
79  virtual void render(String &dst) = 0;
80  protected:
82  };
83 
85  public:
87 
88  if (length_min == 0 && length_max == 0) {
89  cout << "ERROR: length.min and/or length.max must be specified for row component type 'string'" << endl;
90  std::quick_exit(EXIT_FAILURE);
91  }
92  else if (length_max < length_min) {
93  cout << "ERROR: length.max must be less than length.min for row component" << endl;
94  std::quick_exit(EXIT_FAILURE);
95  }
96 
97  if (order != RANDOM) {
98  cout << "ERROR: 'random' is the only currently supported row component type" << endl;
99  std::quick_exit(EXIT_FAILURE);
100  }
101 
102  m_render_buf.reset( new char [length_max+1] );
103  m_render_buf.get()[length_max] = 0;
104  }
105  virtual ~RowComponentString() { }
106  virtual bool next() {
107  random_fill_with_chars(m_render_buf.get(), length_max);
108  return false;
109  }
110  virtual void render(String &dst) {
111  dst.append((const char *)m_render_buf.get());
112  }
113  private:
114  std::unique_ptr<char[]> m_render_buf;
115  };
116 
118  public:
120  m_min = (min != "") ? strtoll(min.c_str(), 0, 0) : 0;
121  if (m_min < 0)
122  m_min = 0;
123  m_max = (max != "") ? strtoll(max.c_str(), 0, 0) : std::numeric_limits<int64_t>::max();
124  HT_ASSERT(m_min < m_max);
125  m_next = m_max;
126  if (order == RANDOM) {
127  m_rng->set_pool_min(m_min);
128  m_rng->set_pool_max(m_max);
129  if (value_count)
130  m_rng->set_value_count(value_count);
131  }
132  if (format.length() == 0)
133  m_render_buf = new char [ 32 ];
134  else {
135  char tbuf[32];
136  int total_len = snprintf(tbuf, 32, format.c_str(), (Lld)1);
137  m_render_buf = new char [ total_len + 32 ];
138  }
139  }
140  virtual ~RowComponentInteger() {
141  delete [] m_render_buf;
142  }
143  virtual bool next() {
144  bool rval = true;
145  if (order == ASCENDING) {
146  if (m_next == m_max) {
147  m_next = m_min;
148  rval = false;
149  }
150  m_next++;
151  }
152  else if (order == RANDOM) {
153  m_next = m_rng->get_sample();
154  rval = false;
155  }
156  else
157  HT_FATAL("invalid order");
158  if (format != "")
159  sprintf(m_render_buf, format.c_str(), (Lld)m_next);
160  else
161  sprintf(m_render_buf, "%lld", (Lld)m_next);
162  return rval;
163  }
164  virtual void render(String &dst) {
165  dst += (const char *)m_render_buf;
166  }
167  private:
168  int64_t m_min, m_max, m_next;
170  };
171 
173  public:
175  struct tm tmval;
176  if (format != "") {
177  if (min != "") {
178  strptime(min.c_str(), format.c_str(), &tmval);
179  m_min = mktime(&tmval);
180  }
181  else
182  m_min = 0;
183  if (max != "") {
184  strptime(max.c_str(), format.c_str(), &tmval);
185  m_max = mktime(&tmval);
186  }
187  else
188  m_max = std::numeric_limits<time_t>::max();
189  }
190  else {
191  if (min != "") {
192  strptime(min.c_str(), "%F %T", &tmval);
193  m_min = mktime(&tmval);
194  }
195  else
196  m_min = 0;
197  if (max != "") {
198  strptime(max.c_str(), "%F %T", &tmval);
199  m_max = mktime(&tmval);
200  }
201  else
202  m_max = std::numeric_limits<time_t>::max();
203  }
204  HT_ASSERT(m_min < m_max);
205  m_next = m_max;
206  if (order == RANDOM) {
207  m_rng->set_pool_min(m_min);
208  m_rng->set_pool_max(m_min);
209  if (value_count)
210  m_rng->set_value_count(value_count);
211  }
212 
213  m_render_buf_len = 32;
214  if (format.length() == 0)
215  m_render_buf = new char [ m_render_buf_len ];
216  else {
217  char tbuf[32];
218  int total_len = snprintf(tbuf, 32, format.c_str(), (Lld)1);
219  m_render_buf_len = total_len + 32;
220  m_render_buf = new char [ m_render_buf_len ];
221  }
222 
223  }
225  delete [] m_render_buf;
226  }
227  virtual bool next() {
228  bool rval = true;
229  if (order == ASCENDING) {
230  if (m_next == m_max) {
231  m_next = m_min;
232  rval = false;
233  }
234  m_next++;
235  }
236  else if (order == RANDOM) {
237  m_next = (time_t)m_rng->get_sample();
238  rval = false;
239  }
240  else
241  HT_FATAL("invalid order");
242  struct tm tm_val;
243  const char *cformat = (format == "") ? "%F %T" : format.c_str();
244  localtime_r(&m_next, &tm_val);
245  strftime(m_render_buf, m_render_buf_len, cformat, &tm_val);
246  return rval;
247  }
248  virtual void render(String &dst) {
249  dst += (const char *)m_render_buf;
250  }
251  private:
252  time_t m_min, m_max, m_next;
255  };
256 
257 }
258 
259 #endif // Hypertable_Lib_DataGeneratorRowComponent_h
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
STL namespace.
#define HT_FATAL(msg)
Definition: Logger.h:339
DiscreteRandomGeneratorPtr m_rng
#define HT_ASSERT(_e_)
Definition: Logger.h:396
static DiscreteRandomGeneratorPtr create(const String &spec)
Creates a new DiscreteRandomGenerator instance.
std::shared_ptr< DiscreteRandomGenerator > DiscreteRandomGeneratorPtr
RowComponent(RowComponentSpec &spec)
RowComponentString(RowComponentSpec &spec)
Factory for Discrete Random Generators.
Hypertable definitions
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
void random_fill_with_chars(char *buf, size_t len, const char *charset=nullptr)
Fills a buffer with random values from a set of characters.
Random number generator for int32, int64, double and ascii arrays.
A String class based on std::string.
Configuration settings.