Drizzled Public API Documentation

transactional_storage_engine.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *  Copyright (C) 2009-2010 Jay Pipes <jaypipes@gmail.com>
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <drizzled/plugin/transactional_storage_engine.h>
00024 #include <drizzled/resource_context.h>
00025 #include <drizzled/session.h>
00026 
00027 #include <vector>
00028 #include <algorithm>
00029 #include <functional>
00030 
00031 namespace drizzled
00032 {
00033 
00034 namespace plugin
00035 {
00036 
00037 static std::vector<TransactionalStorageEngine *> vector_of_transactional_engines;
00038 
00039 TransactionalStorageEngine::TransactionalStorageEngine(const std::string name_arg,
00040                                                        const std::bitset<HTON_BIT_SIZE> &flags_arg)
00041     : StorageEngine(name_arg, flags_arg)
00042 {
00043 }
00044 
00045 TransactionalStorageEngine::~TransactionalStorageEngine()
00046 {
00047 }
00048 
00049 void TransactionalStorageEngine::setTransactionReadWrite(Session& session)
00050 {
00051   ResourceContext *resource_context= session.getResourceContext(this);
00052 
00053   /*
00054     When a storage engine method is called, the transaction must
00055     have been started, unless it's a DDL call, for which the
00056     storage engine starts the transaction internally, and commits
00057     it internally, without registering in the ha_list.
00058     Unfortunately here we can't know know for sure if the engine
00059     has registered the transaction or not, so we must check.
00060   */
00061   if (resource_context->isStarted())
00062   {
00063     resource_context->markModifiedData();
00064   }
00065 }
00066 
00085 int TransactionalStorageEngine::releaseTemporaryLatches(Session *session)
00086 {
00087   std::for_each(vector_of_transactional_engines.begin(), vector_of_transactional_engines.end(),
00088                 std::bind2nd(std::mem_fun(&TransactionalStorageEngine::doReleaseTemporaryLatches),session));
00089   return 0;
00090 }
00091 
00092 struct StartTransactionFunc :public std::unary_function<TransactionalStorageEngine *, int>
00093 {
00094   Session *session;
00095   start_transaction_option_t options;
00096   StartTransactionFunc(Session *in_session, start_transaction_option_t in_options) :
00097     session(in_session),
00098     options(in_options)
00099   {}
00100   result_type operator()(argument_type engine) const
00101   {
00102     return engine->startTransaction(session, options);
00103   }
00104 };
00105 
00106 int TransactionalStorageEngine::notifyStartTransaction(Session *session, start_transaction_option_t options)
00107 {
00108   if (vector_of_transactional_engines.empty())
00109   {
00110     return 0;
00111   }
00112   else
00113   {
00114     StartTransactionFunc functor(session, options);
00115     std::vector<int> results;
00116     results.reserve(vector_of_transactional_engines.size());
00117     transform(vector_of_transactional_engines.begin(),
00118               vector_of_transactional_engines.end(),
00119               results.begin(),
00120               functor);
00121     return *std::max_element(results.begin(), results.end());
00122   }
00123 }
00124 
00125 bool TransactionalStorageEngine::addPlugin(TransactionalStorageEngine *engine)
00126 {
00127   vector_of_transactional_engines.push_back(engine);
00128 
00129   return StorageEngine::addPlugin(engine);
00130 }
00131 
00132 void TransactionalStorageEngine::removePlugin(TransactionalStorageEngine *)
00133 {
00134   vector_of_transactional_engines.clear();
00135 }
00136 
00137 } /* namespace plugin */
00138 } /* namespace drizzled */