22 #include <drizzled/table/cache.h>
24 #include <sys/types.h>
28 #include <drizzled/identifier.h>
29 #include <drizzled/open_tables_state.h>
30 #include <drizzled/pthread_globals.h>
31 #include <drizzled/session.h>
32 #include <drizzled/sql_base.h>
33 #include <drizzled/sys_var.h>
34 #include <drizzled/table.h>
35 #include <drizzled/table/concurrent.h>
36 #include <drizzled/table/unused.h>
41 CacheMap Cache::cache;
42 boost::mutex Cache::_mutex;
46 return Cache::getCache();
60 static void free_cache_entry(table::Concurrent *table)
62 table->intern_close_table();
63 if (not table->in_use)
65 getUnused().unlink(table);
68 boost::checked_delete(table);
71 void remove_table(table::Concurrent *arg)
74 ppp= getCache().equal_range(arg->getShare()->getCacheKey());
76 for (CacheMap::const_iterator iter= ppp.first;
77 iter != ppp.second; ++iter)
79 table::Concurrent *found_table= iter->second;
81 if (found_table == arg)
83 free_cache_entry(arg);
84 getCache().erase(iter);
96 bool Cache::areTablesUsed(Table *table,
bool wait_for_name_lock)
100 const identifier::Table::Key &key(table->getShare()->getCacheKey());
102 table::CacheRange ppp= table::getCache().equal_range(key);
104 for (table::CacheMap::const_iterator iter= ppp.first; iter != ppp.second; ++iter)
106 Table *search= iter->second;
107 if (search->in_use == table->in_use)
116 if ( (search->locked_by_name && wait_for_name_lock) ||
117 (search->is_name_opened() && search->needs_reopen_or_name_lock()))
120 }
while ((table=table->getNext()));
137 void Cache::removeSchema(
const identifier::Schema &schema_identifier)
139 boost::mutex::scoped_lock scopedLock(_mutex);
141 for (table::CacheMap::const_iterator iter= table::getCache().begin();
142 iter != table::getCache().end();
145 table::Concurrent *table= iter->second;
147 if (not schema_identifier.getPath().compare(table->getShare()->getSchemaName()))
149 table->getMutableShare()->resetVersion();
150 if (not table->in_use)
151 table::getUnused().relink(table);
155 table::getUnused().cullByVersion();
173 bool Cache::removeTable(Session& session,
const identifier::Table &identifier, uint32_t flags)
175 const identifier::Table::Key &key(identifier.getKey());
177 bool signalled=
false;
181 result= signalled=
false;
183 table::CacheRange ppp;
184 ppp= table::getCache().equal_range(key);
186 for (table::CacheMap::const_iterator iter= ppp.first;
187 iter != ppp.second; ++iter)
189 table::Concurrent *table= iter->second;
192 table->getMutableShare()->resetVersion();
193 if (not (in_use= table->in_use))
195 table::getUnused().relink(table);
197 else if (in_use != &session)
204 if (table->is_name_opened())
217 for (Table *session_table= in_use->open_tables.open_tables_;
219 session_table= session_table->getNext())
222 if (session_table->db_stat)
223 signalled|= session.abortLockForThread(session_table);
228 result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
232 table::getUnused().cullByVersion();
235 table::instance::release(identifier);
237 if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
244 if (not (flags & RTFC_CHECK_KILLED_FLAG) || not session.getKilled())
247 if (likely(signalled))
249 boost::mutex::scoped_lock scoped(table::Cache::mutex(), boost::adopt_lock_t());
250 COND_refresh.wait(scoped);
266 xtime_get(&xt, boost::TIME_UTC_);
268 boost::mutex::scoped_lock scoped(table::Cache::mutex(), boost::adopt_lock_t());
269 COND_refresh.timed_wait(scoped, xt);
283 void Cache::insert(table::Concurrent* arg)
285 CacheMap::iterator returnable= cache.insert(std::make_pair(arg->getShare()->getCacheKey(), arg));
286 assert(returnable != cache.end());