0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Table.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 "Table.h"
25 #include "TableScanner.h"
26 #include "TableMutator.h"
27 #include "TableMutatorShared.h"
28 #include "TableMutatorAsync.h"
29 #include "ScanSpec.h"
30 
32 
33 #include <Common/String.h>
34 #include <Common/DynamicBuffer.h>
35 #include <Common/Error.h>
36 #include <Common/Logger.h>
37 
39 #include <Hyperspace/Session.h>
40 
41 #include <boost/algorithm/string.hpp>
42 
43 #include <cstring>
44 
45 using namespace Hypertable;
46 using namespace Hyperspace;
47 
49  ConnectionManagerPtr &conn_manager,
51  NameIdMapperPtr &namemap, const string &name,
52  int32_t flags, uint32_t timeout_ms)
53  : m_props(props), m_comm(conn_manager->get_comm()),
54  m_conn_manager(conn_manager), m_hyperspace(hyperspace),
55  m_range_locator(range_locator), m_app_queue(app_queue), m_namemap(namemap),
56  m_name(name), m_flags(flags), m_timeout_ms(timeout_ms), m_stale(true),
57  m_namespace(0) {
58 
59  if (!m_timeout_ms)
60  m_timeout_ms = m_props->get_i32("Hypertable.Request.Timeout");
61 
62  initialize();
63 }
64 
65 
67  string tablefile;
68  DynamicBuffer value_buf(0);
69  string errmsg;
70  string table_id;
71  bool is_index = false;
72 
73  {
74  size_t pos = m_name.find_last_of('/');
75  if (pos == std::string::npos)
76  pos = 0;
77  else
78  pos++;
79  is_index = m_name[pos] == '^';
80  }
81 
82  m_toplevel_dir = m_props->get_str("Hypertable.Directory");
83  boost::trim_if(m_toplevel_dir, boost::is_any_of("/"));
85 
86  m_scanner_queue_size = m_props->get_i32("Hypertable.Scanner.QueueSize");
88 
89 
90  // Convert table name to ID string
91 
92  bool is_namespace;
93 
94  if (!m_namemap->name_to_id(m_name, table_id, &is_namespace) ||
95  is_namespace)
97  m_table.set_id(table_id);
98 
99  tablefile = m_toplevel_dir + "/tables/" + m_table.id;
100 
104  value_buf.clear();
105  // TODO: issue 11
106  try {
107  m_hyperspace->attr_get(tablefile, "schema", value_buf);
108  }
109  catch (Exception &e) {
112  HT_THROW2F(e.code(), e, "Unable to open Hyperspace table file '%s'",
113  tablefile.c_str());
114  }
115 
116  m_schema.reset( Schema::new_instance((const char *)value_buf.base) );
117 
118  if (is_index && m_schema->get_version() < 1)
119  HT_THROW(Error::BAD_FORMAT, "Unsupported index format. Indexes must be "
120  "rebuilt (see REBUILD INDEX)");
121 
122  m_table.generation = m_schema->get_generation();
123  m_stale = false;
124 }
125 
127  HT_ASSERT(m_name != "");
128  if (m_stale)
129  initialize();
130 }
131 
133  std::lock_guard<std::mutex> lock(m_mutex);
134  HT_ASSERT(m_name != "");
135  m_stale = true;
136  initialize();
137 }
138 
139 
140 void Table::get(TableIdentifierManaged &ident_copy, SchemaPtr &schema_copy) {
141  std::lock_guard<std::mutex> lock(m_mutex);
143  ident_copy = m_table;
144  schema_copy = m_schema;
145 }
146 
147 
148 void
149 Table::refresh(TableIdentifierManaged &ident_copy, SchemaPtr &schema_copy) {
150  std::lock_guard<std::mutex> lock(m_mutex);
151  HT_ASSERT(m_name != "");
152  m_stale = true;
153  initialize();
154  ident_copy = m_table;
155  schema_copy = m_schema;
156 }
157 
158 
160 }
161 
162 
164 Table::create_mutator(uint32_t timeout_ms, uint32_t flags,
165  uint32_t flush_interval_ms) {
166  uint32_t timeout = timeout_ms ? timeout_ms : m_timeout_ms;
167 
170 
171  {
172  std::lock_guard<std::mutex> lock(m_mutex);
174  }
175 
176  if (flush_interval_ms) {
178  m_app_queue, timeout, flush_interval_ms, flags);
179  }
180  return new TableMutator(m_props, m_comm, this, m_range_locator, timeout, flags);
181 }
182 
184 Table::create_mutator_async(ResultCallback *cb, uint32_t timeout_ms, uint32_t flags) {
185  uint32_t timeout = timeout_ms ? timeout_ms : m_timeout_ms;
186 
189 
190  {
191  std::lock_guard<std::mutex> lock(m_mutex);
193  }
194 
195  return new TableMutatorAsync(m_props, m_comm, m_app_queue, this, m_range_locator, timeout,
196  cb, flags);
197 }
198 
199 TableScanner *
200 Table::create_scanner(const ScanSpec &scan_spec, uint32_t timeout_ms,
201  int32_t flags) {
202 
203  {
204  std::lock_guard<std::mutex> lock(m_mutex);
206  }
207 
208  return new TableScanner(m_comm, this, m_range_locator, scan_spec,
209  timeout_ms ? timeout_ms : m_timeout_ms);
210 }
211 
213 Table::create_scanner_async(ResultCallback *cb, const ScanSpec &scan_spec, uint32_t timeout_ms,
214  int32_t flags) {
217 
218  {
219  std::lock_guard<std::mutex> lock(m_mutex);
221  }
222 
223  return new TableScannerAsync(m_comm, m_app_queue, this, m_range_locator, scan_spec,
224  timeout_ms ? timeout_ms : m_timeout_ms, cb,
225  flags);
226 }
#define HT_THROW2F(_code_, _ex_, _fmt_,...)
Definition: Error.h:494
TableScannerAsync * create_scanner_async(ResultCallback *cb, const ScanSpec &scan_spec, uint32_t timeout_ms=0, int32_t flags=0)
Creates an asynchronous scanner on this table.
Definition: Table.cc:213
TableScanner * create_scanner(const ScanSpec &scan_spec, uint32_t timeout_ms=0, int32_t flags=0)
Creates a synchronous scanner on this table.
Definition: Table.cc:200
void initialize()
Definition: Table.cc:66
bool has_qualifier_index_table()
returns true if this table has a qualifier index
Definition: Table.h:215
Hyperspace::SessionPtr m_hyperspace
Definition: Table.h:262
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
ApplicationQueueInterfacePtr m_app_queue
Definition: Table.h:265
bool needs_index_table()
returns true if this table requires a index table
Definition: Table.h:185
std::shared_ptr< RangeLocator > RangeLocatorPtr
Smart pointer to RangeLocator.
Definition: RangeLocator.h:198
Asynchronous table scanner.
NameIdMapperPtr m_namemap
Definition: Table.h:266
virtual ~Table()
Definition: Table.cc:159
TableIdentifierManaged m_table
Definition: Table.h:268
void get(TableIdentifierManaged &table_identifier, SchemaPtr &schema)
Get a copy of table identifier and schema atomically.
Definition: Table.cc:140
bool needs_qualifier_index_table()
returns true if this table requires a qualifier index table
Definition: Table.h:197
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
PropertiesPtr m_props
Definition: Table.h:259
Hyperspace definitions
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
Wrapper for TableIdentifier providing member storage.
Scan predicate and control specification.
Definition: ScanSpec.h:56
Provides the ability to mutate a table in the form of adding and deleting rows and cells...
Definition: TableMutator.h:55
A dynamic, resizable memory buffer.
std::shared_ptr< Session > SessionPtr
Definition: Session.h:734
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
Logging routines and macros.
Compatibility Macros for C/C++.
std::shared_ptr< ApplicationQueueInterface > ApplicationQueueInterfacePtr
Smart pointer to ApplicationQueueInterface.
Synchronous table scanner.
Definition: TableScanner.h:39
void refresh_if_required()
Definition: Table.cc:126
bool has_index_table()
returns true if this table has an index
Definition: Table.h:209
size_t m_scanner_queue_size
Definition: Table.h:273
Hypertable definitions
static Schema * new_instance(const std::string &buf)
Creates schema object from XML schema string.
Definition: Schema.cc:202
void set_id(const std::string &new_name)
void clear()
Clears the buffer.
std::string m_toplevel_dir
Definition: Table.h:272
SchemaPtr m_schema
Definition: Table.h:263
RangeLocatorPtr m_range_locator
Definition: Table.h:264
uint8_t * base
Pointer to the allocated memory buffer.
This is a generic exception class for Hypertable.
Definition: Error.h:314
A String class based on std::string.
std::string m_name
Definition: Table.h:267
std::mutex m_mutex
Mutex for serializing member access.
Definition: Table.h:258
int m_timeout_ms
Definition: Table.h:270
std::shared_ptr< Schema > SchemaPtr
Smart pointer to Schema.
Definition: Schema.h:465
Comm * m_comm
Definition: Table.h:260
Declarations for ApplicationQueue.
TableMutatorAsync * create_mutator_async(ResultCallback *cb, uint32_t timeout_ms=0, uint32_t flags=0)
Creates an asynchronous mutator on this table.
Definition: Table.cc:184
std::shared_ptr< ConnectionManager > ConnectionManagerPtr
Smart pointer to ConnectionManager.
TableMutator * create_mutator(uint32_t timeout_ms=0, uint32_t flags=0, uint32_t flush_interval_ms=0)
Creates a mutator on this table.
Definition: Table.cc:164
A TableMutator that can be shared from multiple threads and incidentally has an option to do periodic...
Table(PropertiesPtr &, RangeLocatorPtr &, ConnectionManagerPtr &, Hyperspace::SessionPtr &, ApplicationQueueInterfacePtr &, NameIdMapperPtr &, const std::string &name, int32_t flags=0, uint32_t default_timeout_ms=0)
Definition: Table.cc:48
Represents an open table.
std::shared_ptr< NameIdMapper > NameIdMapperPtr
Smart pointer to NameIdMapper.
Definition: NameIdMapper.h:121
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
void refresh()
Refresh schema etc.
Definition: Table.cc:132
int code() const
Returns the error code.
Definition: Error.h:391
#define HT_THROW2(_code_, _ex_, _msg_)
Definition: Error.h:484