Drizzled Public API Documentation

transaction_log_index.cc

Go to the documentation of this file.
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  Authors:
00007  *
00008  *  Jay Pipes <joinfu@sun.com>
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00023  */
00024 
00031 #include <config.h>
00032 #include <drizzled/message/transaction.pb.h>
00033 
00034 #include "transaction_log_index.h"
00035 
00036 #include <pthread.h>
00037 
00038 using namespace std;
00039 using namespace drizzled;
00040 
00041 TransactionLogIndex *transaction_log_index= NULL; /* The singleton transaction log index */
00042 
00043 TransactionLogIndex::TransactionLogIndex(TransactionLog &in_log) :
00044   log(in_log),
00045   index_file(-1),
00046   index_file_path(),
00047   has_error(false),
00048   error_message(),
00049   min_end_timestamp(0),
00050   max_end_timestamp(0),
00051   min_transaction_id(0),
00052   max_transaction_id(0),
00053   entries(),
00054   transaction_entries()
00055 {
00056   (void) pthread_mutex_init(&index_lock, NULL);
00057   entries.reserve(1024);
00058   transaction_entries.reserve(1024);
00059   open();
00060 }
00061 
00062 TransactionLogIndex::~TransactionLogIndex()
00063 {
00064   entries.clear();
00065   transaction_entries.clear();
00066   pthread_mutex_destroy(&index_lock);
00067 }
00068 
00069 void TransactionLogIndex::clear()
00070 {
00071   pthread_mutex_lock(&index_lock);
00072   min_end_timestamp= 0;
00073   max_end_timestamp= 0;
00074   min_transaction_id= 0;
00075   max_transaction_id= 0;
00076   entries.clear();
00077   transaction_entries.clear();
00078   clearError();
00079   pthread_mutex_unlock(&index_lock);
00080 }
00081 
00082 void TransactionLogIndex::open()
00083 {
00084 
00085 }
00086 
00087 bool TransactionLogIndex::hasError() const
00088 {
00089   return has_error;
00090 }
00091 
00092 void TransactionLogIndex::clearError()
00093 {
00094   has_error= false;
00095   error_message.clear();
00096 }
00097 
00098 const std::string &TransactionLogIndex::getErrorMessage() const
00099 {
00100   return error_message;
00101 }
00102 
00103 uint64_t TransactionLogIndex::getMinEndTimestamp() const
00104 {
00105   return min_end_timestamp;
00106 }
00107 
00108 uint64_t TransactionLogIndex::getMaxEndTimestamp() const
00109 {
00110   return max_end_timestamp;
00111 }
00112 
00113 uint64_t TransactionLogIndex::getMinTransactionId() const
00114 {
00115   return min_transaction_id;
00116 }
00117 
00118 uint64_t TransactionLogIndex::getMaxTransactionId() const
00119 {
00120   return max_transaction_id;
00121 }
00122 
00123 uint64_t TransactionLogIndex::getNumLogEntries() const
00124 {
00125   return entries.size();
00126 }
00127 
00128 uint64_t TransactionLogIndex::getNumTransactionEntries() const
00129 {
00130   return transaction_entries.size();
00131 }
00132 
00133 TransactionLog::Entries &TransactionLogIndex::getEntries()
00134 {
00135   return entries;
00136 }
00137 
00138 TransactionLog::TransactionEntries &TransactionLogIndex::getTransactionEntries()
00139 {
00140   return transaction_entries;
00141 }
00142 
00143 size_t TransactionLogIndex::getTransactionEntriesSizeInBytes()
00144 {
00145   return transaction_entries.capacity() * sizeof(TransactionLog::TransactionEntries::value_type);
00146 }
00147 
00148 size_t TransactionLogIndex::getEntriesSizeInBytes()
00149 {
00150   return entries.capacity() * sizeof(TransactionLog::Entries::value_type);
00151 }
00152 
00153 
00154 size_t TransactionLogIndex::getSizeInBytes()
00155 {
00156   return sizeof(this) + getEntriesSizeInBytes() + getTransactionEntriesSizeInBytes();
00157 }
00158 
00159 void TransactionLogIndex::addEntry(const TransactionLogEntry &entry,
00160                                    const message::Transaction &transaction,
00161                                    uint32_t checksum)
00162 {
00163   pthread_mutex_lock(&index_lock);
00164   if (entries.empty())
00165   {
00166     /* First entry...set the minimums */
00167     min_transaction_id= transaction.transaction_context().transaction_id();
00168     min_end_timestamp= transaction.transaction_context().end_timestamp();
00169   }
00170   max_transaction_id= transaction.transaction_context().transaction_id();
00171   max_end_timestamp= transaction.transaction_context().end_timestamp();
00172   entries.push_back(entry);
00173   transaction_entries.push_back(TransactionLogTransactionEntry(entry.getOffset(),
00174                                                                transaction,
00175                                                                checksum));
00176   pthread_mutex_unlock(&index_lock);
00177 }