CUBRID Engine  latest
load_common.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * load_common.hpp - common code used by loader
21  */
22 
23 #ifndef _LOAD_COMMON_HPP_
24 #define _LOAD_COMMON_HPP_
25 
26 #include "packable_object.hpp"
27 
28 #include <atomic>
29 #include <cassert>
30 #include <functional>
31 #include <vector>
32 
33 #define NUM_LDR_TYPES (LDR_TYPE_MAX + 1)
34 #define NUM_DB_TYPES (DB_TYPE_LAST + 1)
35 
36 namespace cubload
37 {
38 
39  using batch_id = int64_t;
40  using class_id = int;
41 
46 
48  {
49  public:
50  batch ();
51  batch (batch_id id, class_id clsid, std::string &content, int64_t line_offset, int64_t rows);
52 
53  batch (batch &&other) noexcept; // MoveConstructible
54  batch &operator= (batch &&other) noexcept; // MoveAssignable
55 
56  batch (const batch &copy) = delete; // Not CopyConstructible
57  batch &operator= (const batch &copy) = delete; // Not CopyAssignable
58 
59  batch_id get_id () const;
60  class_id get_class_id () const;
61  int64_t get_line_offset () const;
62  const std::string &get_content () const;
63  int64_t get_rows_number () const;
64 
65  void pack (cubpacking::packer &serializator) const override;
66  void unpack (cubpacking::unpacker &deserializator) override;
67  size_t get_packed_size (cubpacking::packer &serializator, std::size_t start_offset) const override;
68 
69  private:
72  std::string m_content;
73  int64_t m_line_offset;
74  int64_t m_rows;
75  };
76 
77  using batch_handler = std::function<int64_t (const batch &)>;
78  using class_handler = std::function<int64_t (const batch &, bool &)>;
79 
80  /*
81  * loaddb executables command line arguments
82  */
84  {
85  load_args ();
86 
87  void pack (cubpacking::packer &serializator) const override;
88  void unpack (cubpacking::unpacker &deserializator) override;
89  size_t get_packed_size (cubpacking::packer &serializator, std::size_t start_offset) const override;
90 
91  int parse_ignore_class_file ();
92 
93  std::string volume;
94  std::string input_file;
95  std::string user_name;
96  std::string password;
98  bool load_only;
100  bool verbose;
105  std::string schema_file;
106  std::string index_file;
107  std::string trigger_file;
108  std::string object_file;
109  std::string error_file;
112  std::string table_name;
113  std::string ignore_class_file;
114  std::vector<std::string> ignore_classes;
115  std::vector<int> m_ignored_errors;
116  static const int PERIODIC_COMMIT_DEFAULT_VALUE = 10240;
117  };
118 
119  /*
120  * These are the "types" of strings that the lexer recognizes. The
121  * loader can specialize on each type.
122  * These values are used to set up a vector of type setting functions, based
123  * on information about each attribute parsed in the %CLASS line.
124  * The setter functions are invoked using the enumerated type as an index into
125  * the function vector. This gives us a significant saving when processing
126  * values in the instance line, over the previous loader.
127  */
128 
130  {
135  LDR_NUMERIC, /* Default real */
136  LDR_DOUBLE, /* Reals specified with scientific notation, 'e', or 'E' */
137  LDR_FLOAT, /* Reals specified with 'f' or 'F' notation */
138  LDR_OID, /* Object references */
139  LDR_CLASS_OID, /* Class object reference */
146  LDR_ELO_INT, /* Internal ELO's */
147  LDR_ELO_EXT, /* External ELO's */
149  LDR_SYS_CLASS, /* This type is not allowed currently. */
151  LDR_BSTR, /* Binary bit strings */
152  LDR_XSTR, /* Hexidecimal bit strings */
157 
159  };
160 
161  /*
162  * attribute_type
163  *
164  * attribute type identifiers for ldr_act_restrict_attributes().
165  * These attributes are handled specially since there modify the class object
166  * directly.
167  */
168 
170  {
175  };
176 
178  {
182  };
183 
184  struct string_type
185  {
186  string_type ();
187  string_type (char *val, std::size_t size, bool need_free_val);
188  ~string_type ();
189 
190  void destroy ();
191 
194  char *val;
195  size_t size;
197  };
198 
200  {
201  constructor_spec_type () = delete;
202  constructor_spec_type (string_type *id_name, string_type *arg_list);
203  ~constructor_spec_type () = default;
204 
207  };
208 
210  {
211  class_command_spec_type () = delete;
212  class_command_spec_type (int attr_type, string_type *attr_list, constructor_spec_type *ctor_spec);
213  ~class_command_spec_type () = default;
214 
218  };
219 
221  {
222  constant_type ();
223  constant_type (data_type type, void *val);
224  ~constant_type () = default;
225 
228  void *val;
230  };
231 
233  {
234  object_ref_type () = delete;
236  ~object_ref_type () = default;
237 
241  };
242 
244  {
245  monetary_type () = delete;
246  monetary_type (string_type *amount, int currency_type);
247  ~monetary_type () = default;
248 
251  };
252 
254  {
255  int64_t rows_committed; // equivalent of 'last_commit' from SA_MODE
256  std::atomic<int64_t> current_line;
258  int rows_failed; // // equivalent of 'errors' from SA_MODE
259  std::string error_message;
260  std::string log_message;
261 
262  // Default constructor
263  stats ();
264  // Copy constructor
265  stats (const stats &copy);
266  // Assignment operator
267  stats &operator= (const stats &other);
268 
269  void clear ();
270 
271  void pack (cubpacking::packer &serializator) const override;
272  void unpack (cubpacking::unpacker &deserializator) override;
273  size_t get_packed_size (cubpacking::packer &serializator, std::size_t start_offset) const override;
274  };
275 
277  {
278  public:
279  load_status ();
280  load_status (bool is_load_completed, bool is_session_failed, std::vector<stats> &load_stats);
281 
282  load_status (load_status &&other) noexcept;
283  load_status &operator= (load_status &&other) noexcept;
284 
285  load_status (const load_status &copy) = delete; // Not CopyConstructible
286  load_status &operator= (const load_status &copy) = delete; // Not CopyAssignable
287 
288  bool is_load_completed ();
289  bool is_load_failed ();
290  std::vector<stats> &get_load_stats ();
291 
292  void pack (cubpacking::packer &serializator) const override;
293  void unpack (cubpacking::unpacker &deserializator) override;
294  size_t get_packed_size (cubpacking::packer &serializator, std::size_t start_offset) const override;
295 
296  private:
299  std::vector<stats> m_load_stats;
300  };
301 
302  /*
303  * cubload::class_installer
304  *
305  * description
306  *
307  * how to use
308  */
310  {
311  public:
312  virtual ~class_installer () = default; // Destructor
313 
314  /*
315  * Function set class_id for class installer instance
316  *
317  * return: void
318  * clsid(in): generated id of the class
319  */
320  virtual void set_class_id (class_id clsid) = 0;
321 
322  /*
323  * Function to check a class, it is called when a line of the following form "%id foo 42" is reached
324  * in loaddb object file (where class_name will be "foo" and class_id will be 42)
325  *
326  * return: void
327  * class_name(in): name of the class
328  * class_id(in) : id of the class from the object file
329  */
330  virtual void check_class (const char *class_name, int class_id) = 0;
331 
332  /*
333  * Function to set up a class and class attributes list. Should be used when loaddb object file doesn't contain
334  * a %class line but instead "-t TABLE or --table=TABLE" parameter was passed to loaddb executable
335  * In this case class attributes list and their order will be fetched from class schema representation
336  *
337  * return: NO_ERROR in case of success or error code otherwise
338  * class_name(in): name of the class pass to loaddb executable
339  */
340  virtual int install_class (const char *class_name) = 0;
341 
342  /*
343  * Function to set up a class, class attributes and class constructor. It is called when a line of the following
344  * form "%class foo (id, name)" is reached in loaddb object file
345  *
346  * return: void
347  * class_name(in): loader string type which contains name of the class
348  * cmd_spec(in) : class command specification which contains
349  * attribute list and class constructor specification
350  */
351  virtual void install_class (string_type *class_name, class_command_spec_type *cmd_spec) = 0;
352  };
353 
354  /*
355  * cubload::object_loader
356  *
357  * description
358  * A pure virtual class that serves as an interface for inserting rows by the loaddb. Currently there are two
359  * implementations of this class: server loader and client loader.
360  * * server_object_loader: A object loader that is running on the cub_server on multi-threaded environment
361  * * sa_object_loader : Contains old loaddb code base and is running
362  * on SA mode (single threaded environment)
363  *
364  * how to use
365  * Loader is used by the cubload::driver, which later is passed to the cubload::parser. The parser class will then
366  * call specific functions on different grammar rules.
367  */
369  {
370  public:
371  virtual ~object_loader () = default; // Destructor
372 
373  /*
374  * Function to initialize object loader instance
375  *
376  * return: void
377  * clsid(in): generated id of the class
378  */
379  virtual void init (class_id clsid) = 0;
380 
381  /*
382  * Destroy function called when loader grammar reached the end of the loaddb object file
383  */
384  virtual void destroy () = 0;
385 
386  /*
387  * Function called by the loader grammar before every line with row data from loaddb object file.
388  *
389  * return: void
390  * object_id(in): id of the referenced object instance
391  */
392  virtual void start_line (int object_id) = 0;
393 
394  /*
395  * Process and inserts a row. constant_type contains the value and the type for each column from the row.
396  *
397  * return: void
398  * cons(in): array of constants
399  */
400  virtual void process_line (constant_type *cons) = 0;
401 
402  /*
403  * Called after process_line, should implement login for cleaning up data after insert if required.
404  */
405  virtual void finish_line () = 0;
406 
407  virtual void flush_records () = 0;
408 
409  virtual std::size_t get_rows_number () = 0;
410  };
411 
413 
414  /*
415  * Splits a loaddb object file into batches of a given size.
416  *
417  * return: NO_ERROR in case of success or ER_FAILED if file does not exists
418  * batch_size(in) : batch size
419  * object_file_name(in): loaddb object file name (absolute path is required)
420  * c_handler(in) : a function for handling/process a %class or %id line from object file
421  * b_handler(in) : a function for handling/process a batch of objects
422  */
423  int split (int batch_size, const std::string &object_file_name, class_handler &c_handler, batch_handler &b_handler);
424 
425 } // namespace cubload
426 
427 // alias declaration for legacy C files
430 
431 #define IS_OLD_GLO_CLASS(class_name) \
432  (strncasecmp ((class_name), "glo", MAX(strlen(class_name), 3)) == 0 || \
433  strncasecmp ((class_name), "glo_name", MAX(strlen(class_name), 8)) == 0 || \
434  strncasecmp ((class_name), "glo_holder", MAX(strlen(class_name), 10)) == 0)
435 
436 #endif /* _LOAD_COMMON_HPP_ */
std::string error_file
int split(int batch_size, const std::string &object_file_name, class_handler &c_handler, batch_handler &b_handler)
int64_t get_rows_number() const
std::string error_message
std::string object_file
string_type * last
std::string ignore_class_file
std::string input_file
Definition: load_common.hpp:94
std::string m_content
Definition: load_common.hpp:72
std::string index_file
constant_type * last
batch & operator=(batch &&other) noexcept
Definition: load_common.cpp:92
class_id m_clsid
Definition: load_common.hpp:71
std::string log_message
std::atomic< int64_t > current_line
const batch_id FIRST_BATCH_ID
Definition: load_common.hpp:45
std::string table_name
const batch_id NULL_BATCH_ID
Definition: load_common.hpp:43
std::function< int64_t(const batch &)> batch_handler
Definition: load_common.hpp:77
void unpack(cubpacking::unpacker &deserializator) override
const class_id FIRST_CLASS_ID
Definition: load_common.hpp:44
size_t get_packed_size(cubpacking::packer &serializator, std::size_t start_offset) const override
void pack(cubpacking::packer &serializator) const override
std::vector< stats > m_load_stats
int64_t last_committed_line
constructor_spec_type * ctor_spec
int64_t batch_id
Definition: load_common.hpp:39
const std::string & get_content() const
string_type * next
std::string schema_file
int64_t get_line_offset() const
std::string user_name
Definition: load_common.hpp:95
const class_id NULL_CLASS_ID
Definition: load_common.hpp:42
std::function< int64_t(const batch &, bool &)> class_handler
Definition: load_common.hpp:78
int64_t m_line_offset
Definition: load_common.hpp:73
class_id get_class_id() const
int64_t rows_committed
std::vector< std::string > ignore_classes
string_type * instance_number
constant_type * next
batch_id get_id() const
std::string volume
Definition: load_common.hpp:93
std::string trigger_file
int class_id
Definition: load_common.hpp:40
std::string password
Definition: load_common.hpp:96
std::vector< int > m_ignored_errors
cubload::load_status load_status
void clear(cub_regex_object *&regex, char *&pattern)