0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TableCache.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 "TableCache.h"
25 
26 using namespace Hypertable;
27 using namespace std;
28 
30  ConnectionManagerPtr &conn_manager, Hyperspace::SessionPtr &hyperspace,
31  ApplicationQueueInterfacePtr &app_queue, NameIdMapperPtr &namemap,
32  uint32_t default_timeout_ms)
33  : m_props(props), m_range_locator(range_locator),
34  m_comm(conn_manager->get_comm()), m_conn_manager(conn_manager),
35  m_hyperspace(hyperspace), m_app_queue(app_queue),
36  m_namemap(namemap), m_timeout_ms(default_timeout_ms) {
37  HT_ASSERT(m_props && m_range_locator && conn_manager && m_hyperspace &&
39  m_hyperspace->add_callback(this);
40 }
41 
43  m_hyperspace->remove_callback(this);
44 }
45 
46 TablePtr TableCache::get(const string &table_name, int32_t flags) {
47  lock_guard<mutex> lock(m_mutex);
48  return get_unlocked(table_name, flags);
49 }
50 
51 TablePtr TableCache::get_unlocked(const string &table_name, int32_t flags) {
52  string id;
53 
54  TableMap::iterator it = m_table_map.find(table_name);
55  if (it != m_table_map.end()) {
56  if ((flags & Table::OPEN_FLAG_REFRESH_TABLE_CACHE) || it->second->need_refresh())
57  it->second->refresh();
58  return it->second;
59  }
60 
61  TablePtr table =
62  make_shared<Table>(m_props, m_range_locator, m_conn_manager,
63  m_hyperspace, m_app_queue, m_namemap, table_name,
64  flags, m_timeout_ms);
65 
66  m_table_map.insert(make_pair(table_name, table));
67 
68  return table;
69 }
70 
71 bool TableCache::get_schema_str(const string &table_name, string &schema, bool with_ids)
72 {
73  lock_guard<mutex> lock(m_mutex);
74  TableMap::const_iterator it = m_table_map.find(table_name);
75 
76  if (it == m_table_map.end())
77  return false;
78  schema = it->second->schema()->render_xml(with_ids);
79  return true;
80 }
81 
82 bool TableCache::get_schema(const string &table_name, SchemaPtr &output_schema) {
83  lock_guard<mutex> lock(m_mutex);
84  TableMap::const_iterator it = m_table_map.find(table_name);
85 
86  if (it == m_table_map.end())
87  return false;
88  output_schema = make_shared<Schema>(*(it->second->schema()));
89  return true;
90 }
91 
92 bool TableCache::remove(const string &table_name) {
93  lock_guard<mutex> lock(m_mutex);
94  bool found = false;
95  TableMap::iterator it = m_table_map.find(table_name);
96 
97  if (it != m_table_map.end()) {
98  found = true;
99  m_table_map.erase(it);
100  }
101  return found;
102 }
103 
105  lock_guard<mutex> lock(m_mutex);
106  for( TableMap::iterator it = m_table_map.begin(); it != m_table_map.end(); ++it)
107  (*it).second->invalidate();
108 }
TablePtr get(const std::string &table_name, int32_t flags)
Definition: TableCache.cc:46
std::shared_ptr< RangeLocator > RangeLocatorPtr
Smart pointer to RangeLocator.
Definition: RangeLocator.h:198
bool get_schema_str(const std::string &table_name, std::string &output_schema, bool with_ids=false)
Definition: TableCache.cc:71
STL namespace.
PropertiesPtr m_props
Definition: TableCache.h:91
RangeLocatorPtr m_range_locator
Definition: TableCache.h:92
ApplicationQueueInterfacePtr m_app_queue
Definition: TableCache.h:96
#define HT_ASSERT(_e_)
Definition: Logger.h:396
bool remove(const std::string &table_name)
Definition: TableCache.cc:92
bool get_schema(const std::string &table_name, SchemaPtr &output_schema)
Definition: TableCache.cc:82
std::shared_ptr< Session > SessionPtr
Definition: Session.h:734
NameIdMapperPtr m_namemap
Definition: TableCache.h:97
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
Compatibility Macros for C/C++.
std::shared_ptr< ApplicationQueueInterface > ApplicationQueueInterfacePtr
Smart pointer to ApplicationQueueInterface.
TableCache(PropertiesPtr &, RangeLocatorPtr &, ConnectionManagerPtr &, Hyperspace::SessionPtr &, ApplicationQueueInterfacePtr &, NameIdMapperPtr &namemap, uint32_t default_timeout_ms)
Definition: TableCache.cc:29
ConnectionManagerPtr m_conn_manager
Definition: TableCache.h:94
Hypertable definitions
TablePtr get_unlocked(const std::string &table_name, int32_t flags)
Definition: TableCache.cc:51
std::shared_ptr< Schema > SchemaPtr
Smart pointer to Schema.
Definition: Schema.h:465
virtual void reconnected()
Definition: TableCache.cc:104
Hyperspace::SessionPtr m_hyperspace
Definition: TableCache.h:95
std::shared_ptr< ConnectionManager > ConnectionManagerPtr
Smart pointer to ConnectionManager.
std::shared_ptr< NameIdMapper > NameIdMapperPtr
Smart pointer to NameIdMapper.
Definition: NameIdMapper.h:121
std::shared_ptr< Table > TablePtr
Definition: Table.h:53