0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Schema.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 
26 
27 #ifndef Hypertable_Lib_Schema_h
28 #define Hypertable_Lib_Schema_h
29 
30 #include <Common/Properties.h>
32 #include <Common/StringExt.h>
33 
37 
38 #include <expat.h>
39 
40 #include <bitset>
41 #include <list>
42 #include <memory>
43 #include <unordered_map>
44 #include <vector>
45 
46 namespace Hypertable {
47 
50 
52  class Schema {
53  public:
54 
56  Schema() : m_arena(1024), m_counter_mask(256) {
59  }
60 
65  Schema(const Schema &other);
66 
69  ~Schema();
70 
74  void clear_generation();
75 
85  bool clear_generation_if_changed(Schema &original);
86 
96  void update_generation(int64_t generation);
97 
185  const std::string render_xml(bool with_ids=false);
186 
202  const std::string render_hql(const std::string &table_name);
203 
207  int64_t get_generation() const { return m_generation; }
208 
212  void set_generation(int64_t generation) { m_generation = generation; }
213 
217  int32_t get_version() const { return m_version; }
218 
222  void set_version(int32_t version) { m_version = version; }
223 
226  int32_t get_max_column_family_id();
227 
231 
235 
242  AccessGroupSpec *get_access_group(const std::string &name) {
243  return get_access_group(name.c_str());
244  }
245  AccessGroupSpec *get_access_group(const char* name) {
246  auto iter = m_access_group_map.find(name);
247  if (iter != m_access_group_map.end())
248  return iter->second;
249  return nullptr;
250  }
251 
255 
262  ColumnFamilySpec *get_column_family(const std::string &name) {
263  return get_column_family(name.c_str());
264  }
265  ColumnFamilySpec *get_column_family(const char* name) {
266  auto iter = m_column_family_map.find(name);
267  if (iter != m_column_family_map.end())
268  return iter->second;
269  return nullptr;
270  }
271 
280  ColumnFamilySpec *remove_column_family(const std::string &name);
281 
292  ColumnFamilySpec *get_column_family(int32_t id, bool get_deleted=false) {
293  auto iter = m_column_family_id_map.find(id);
294  if (iter != m_column_family_id_map.end()) {
295  if (get_deleted || iter->second->get_deleted() == false)
296  return (iter->second);
297  }
298  return nullptr;
299  }
300 
311 
319 
325  bool access_group_exists(const std::string &name) const;
326 
335  bool column_family_exists(int32_t id, bool get_deleted = false) const;
336 
343  void drop_column_family(const String& name);
344 
350  void rename_column_family(const String& old_name, const String& new_name);
351 
357  bool column_is_counter(int id) {
358  return m_counter_mask[id];
359  }
360 
364  void set_group_commit_interval(int32_t interval) {
365  m_group_commit_interval = interval;
366  }
367 
371 
376  m_ag_defaults = defaults;
377  }
378 
382 
387  m_cf_defaults = defaults;
388  }
389 
393 
403  void validate();
404 
410  static Schema *new_instance(const std::string &buf);
411 
412  private:
413 
417  void merge_table_defaults(ColumnFamilySpec *cf_spec);
418 
422  void merge_table_defaults(AccessGroupSpec *ag_spec);
423 
424  // Page arena allocator
427 
429  int64_t m_generation {};
430 
432  int32_t m_version {};
433 
436 
439 
442 
445 
447  typedef std::map<const char*, AccessGroupSpec*, LtCstr, CstrAlloc> CstrAccessGroupMap;
448  CstrAccessGroupMap m_access_group_map;
449 
452 
454  typedef std::map<const char*, ColumnFamilySpec*, LtCstr, CstrAlloc> CstrColumnFamilyMap;
455  CstrColumnFamilyMap m_column_family_map;
456 
458  std::map<int32_t, ColumnFamilySpec *> m_column_family_id_map;
459 
461  std::vector<bool> m_counter_mask;
462  };
463 
465  typedef std::shared_ptr<Schema> SchemaPtr;
466 
468 
469 }
470 
471 #endif // Hypertable_Lib_Schema_h
ColumnFamilyOptions m_cf_defaults
Default column family options.
Definition: Schema.h:441
void set_version(int32_t version)
Sets version number.
Definition: Schema.h:222
int32_t get_group_commit_interval()
Gets group commit interval.
Definition: Schema.h:370
Schema specification.
Definition: Schema.h:52
~Schema()
Destructor.
Definition: Schema.cc:130
CstrColumnFamilyMap m_column_family_map
Definition: Schema.h:455
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
void validate()
Validates schema and reconstructs data structures.
Definition: Schema.cc:505
std::vector< AccessGroupSpec * > AccessGroupSpecs
Vector of AccessGroupSpec pointers.
int32_t get_version() const
Gets version number.
Definition: Schema.h:217
AccessGroupSpec * get_access_group(const char *name)
Definition: Schema.h:245
void rename_column_family(const String &old_name, const String &new_name)
Renames a column family.
Definition: Schema.cc:447
Program options handling.
Column family specification.
PageArena memory allocator for STL classes.
Specification for column family options.
CharArena m_arena
Definition: Schema.h:426
The PageArenaAllocator is a STL allocator based on PageArena.
ColumnFamilySpecs m_column_families
&Column family specifications
Definition: Schema.h:451
std::map< int32_t, ColumnFamilySpec * > m_column_family_id_map
Map of column family specifications (key == ID)
Definition: Schema.h:458
void set_access_group_defaults(const AccessGroupOptions &defaults)
Sets default access group options.
Definition: Schema.h:375
std::map< const char *, AccessGroupSpec *, LtCstr, CstrAlloc > CstrAccessGroupMap
Map of access group specifications.
Definition: Schema.h:447
Specification for access group options.
Represents a set of table parts (sub-tables).
Definition: TableParts.h:47
ColumnFamilySpecs & get_column_families()
Returns reference to column family vector.
Definition: Schema.h:254
AccessGroupSpecs & get_access_groups()
Returns reference to access group vector.
Definition: Schema.h:234
CstrAccessGroupMap m_access_group_map
Definition: Schema.h:448
std::map< const char *, ColumnFamilySpec *, LtCstr, CstrAlloc > CstrColumnFamilyMap
Map of column family specifications (key == name)
Definition: Schema.h:454
void drop_column_family(const String &name)
Drops column family.
Definition: Schema.cc:470
TableParts get_table_parts()
Gets table parts.
Definition: Schema.cc:337
ColumnFamilySpec * get_column_family(const char *name)
Definition: Schema.h:265
bool access_group_exists(const std::string &name) const
Checks if access group exists.
Definition: Schema.cc:441
AccessGroupSpec * get_access_group(const std::string &name)
Gets an access group specification given its name.
Definition: Schema.h:242
AccessGroupOptions m_ag_defaults
Default access group options.
Definition: Schema.h:438
STL Strict Weak Ordering for comparing c-style strings.
Definition: StringExt.h:45
int32_t m_version
Version number.
Definition: Schema.h:432
The PageArena allocator is simple and fast, avoiding individual mallocs/frees.
Definition: PageArena.h:69
AccessGroupOptions & access_group_defaults()
Returns reference to default access group options.
Definition: Schema.h:381
void set_generation(int64_t generation)
Sets generation.
Definition: Schema.h:212
Access group specification.
bool clear_generation_if_changed(Schema &original)
Clears generation if different than original.
Definition: Schema.cc:223
int64_t get_generation() const
Gets generation.
Definition: Schema.h:207
Hypertable definitions
static Schema * new_instance(const std::string &buf)
Creates schema object from XML schema string.
Definition: Schema.cc:202
void set_column_family_defaults(const ColumnFamilyOptions &defaults)
Sets default column family options.
Definition: Schema.h:386
ColumnFamilySpec * get_column_family(int32_t id, bool get_deleted=false)
Gets a column family specification given its ID.
Definition: Schema.h:292
void add_access_group(AccessGroupSpec *ag)
Adds access group specification.
Definition: Schema.cc:354
ColumnFamilySpec * get_column_family(const std::string &name)
Gets a column family specification given its name.
Definition: Schema.h:262
Declarations for AccessGroupSpec.
AccessGroupSpecs m_access_groups
Access group specifications.
Definition: Schema.h:444
bool column_family_exists(int32_t id, bool get_deleted=false) const
Checks if column family exists.
Definition: Schema.cc:431
std::vector< bool > m_counter_mask
Bitmask describing which column families are counters.
Definition: Schema.h:461
ColumnFamilyOptions & column_family_defaults()
Returns reference to default column family options.
Definition: Schema.h:392
void merge_table_defaults(ColumnFamilySpec *cf_spec)
Merges default column family options into a column family spec.
Definition: Schema.cc:540
void clear_generation()
Clears generation values.
Definition: Schema.cc:216
const std::string render_hql(const std::string &table_name)
Renders schema as HQL CREATE TABLE statement.
Definition: Schema.cc:292
void set_group_commit_interval(int32_t interval)
Sets group commit interval.
Definition: Schema.h:364
AccessGroupSpec * replace_access_group(AccessGroupSpec *new_ag)
Replaces access group specification.
Definition: Schema.cc:414
PageArenaAllocator< const char * > CstrAlloc
Definition: Schema.h:425
int32_t get_max_column_family_id()
Gets the maximum column family ID.
Definition: Schema.cc:328
std::shared_ptr< Schema > SchemaPtr
Smart pointer to Schema.
Definition: Schema.h:465
int32_t m_group_commit_interval
Group commit interval.
Definition: Schema.h:435
std::vector< ColumnFamilySpec * > ColumnFamilySpecs
Vector of ColumnFamilySpec pointers.
Schema()
Default constructor.
Definition: Schema.h:56
int64_t m_generation
Generation.
Definition: Schema.h:429
String extensions and helpers: sets, maps, append operators etc.
ColumnFamilySpec * remove_column_family(const std::string &name)
Removes column family.
Definition: Schema.cc:396
bool column_is_counter(int id)
Checks if column is a counter column.
Definition: Schema.h:357
const std::string render_xml(bool with_ids=false)
Renders schema in XML format.
Definition: Schema.cc:265
Declarations for ColumnFamilySpec.
void update_generation(int64_t generation)
Updates generation and assigns column family IDs.
Definition: Schema.cc:244
Declarations for TableParts.