00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- 00002 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: 00003 * 00004 * Copyright (C) 2008-2009 Sun Microsystems, Inc. 00005 * Copyright (C) 2010 Jay Pipes <jaypipes@gmail.com> 00006 * 00007 * Authors: 00008 * 00009 * Jay Pipes <jaypipes@gmail.com.com> 00010 * 00011 * This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00024 */ 00025 00038 #pragma once 00039 00040 #include <drizzled/atomics.h> 00041 #include <drizzled/replication_services.h> 00042 00043 #include "transaction_log_entry.h" 00044 00045 #include <vector> 00046 #include <string> 00047 00048 class TransactionLog 00049 { 00050 public: 00051 typedef std::vector<TransactionLogEntry> Entries; 00052 typedef std::vector<TransactionLogTransactionEntry> TransactionEntries; 00056 enum Status 00057 { 00058 CRASHED= 0, 00059 OFFLINE, /* Default state, uninited. */ 00060 ONLINE, 00061 WRITING 00062 }; 00063 static const uint32_t FLUSH_FREQUENCY_OS= 0; 00064 static const uint32_t FLUSH_FREQUENCY_EVERY_WRITE= 1; //< Sync on every write to the log file 00065 static const uint32_t FLUSH_FREQUENCY_EVERY_SECOND= 2; 00066 public: 00067 TransactionLog(const std::string in_log_file_path, 00068 uint32_t in_flush_frequency, 00069 bool in_do_checksum); 00070 00072 ~TransactionLog(); 00073 00077 inline off_t getLogOffset() 00078 { 00079 return log_offset; 00080 } 00081 00085 const std::string &getLogFilename(); 00086 00090 const std::string &getLogFilepath(); 00091 00095 inline enum Status getState() 00096 { 00097 return state; 00098 } 00099 00107 static size_t getLogEntrySize(const drizzled::message::Transaction &trx); 00108 00121 uint8_t *packTransactionIntoLogEntry(const drizzled::message::Transaction &trx, 00122 uint8_t *buffer, 00123 uint32_t *checksum_out); 00124 00136 off_t writeEntry(const uint8_t *data, size_t data_length); 00137 00147 void truncate(); 00148 00164 bool findLogFilenameContainingTransactionId(const drizzled::ReplicationServices::GlobalTransactionId &to_find, 00165 std::string &out_filename) const; 00166 00170 bool hasError() const; 00171 00175 const std::string &getErrorMessage() const; 00176 private: 00177 static const uint32_t HEADER_TRAILER_BYTES= sizeof(uint32_t) + /* 4-byte msg type header */ 00178 sizeof(uint32_t) + /* 4-byte length header */ 00179 sizeof(uint32_t); /* 4 byte checksum trailer */ 00180 00181 /* Don't allows these */ 00182 TransactionLog(); 00183 TransactionLog(const TransactionLog &other); 00184 TransactionLog &operator=(const TransactionLog &other); 00188 void clearError(); 00198 int syncLogFile(); 00199 00200 int log_file; 00201 Status state; 00202 const std::string log_file_path; 00203 std::string log_file_name; 00204 drizzled::atomic<off_t> log_offset; 00205 bool has_error; 00206 std::string error_message; 00207 uint32_t flush_frequency; 00208 time_t last_sync_time; 00209 bool do_checksum; 00210 }; 00211