LogCabin
Storage/Log.cc
Go to the documentation of this file.
00001 /* Copyright (c) 2012-2013 Stanford University
00002  *
00003  * Permission to use, copy, modify, and distribute this software for any
00004  * purpose with or without fee is hereby granted, provided that the above
00005  * copyright notice and this permission notice appear in all copies.
00006  *
00007  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00008  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00009  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00010  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00011  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00012  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00013  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00014  */
00015 
00016 #include <algorithm>
00017 #include <fcntl.h>
00018 #include <ostream>
00019 #include <sys/stat.h>
00020 #include <unistd.h>
00021 
00022 #include "build/Protocol/Client.pb.h"
00023 #include "build/Protocol/Raft.pb.h"
00024 #include "Core/Debug.h"
00025 #include "Core/ProtoBuf.h"
00026 #include "Storage/Log.h"
00027 
00028 namespace LogCabin {
00029 namespace Storage {
00030 
00031 ////////// Log::Sync //////////
00032 
00033 Log::Sync::Sync(uint64_t lastIndex)
00034     : lastIndex(lastIndex)
00035     , completed(false) {
00036 }
00037 
00038 Log::Sync::~Sync()
00039 {
00040     assert(completed);
00041 }
00042 
00043 ////////// Log //////////
00044 
00045 Log::Log()
00046     : metadata()
00047 {
00048 }
00049 
00050 Log::~Log()
00051 {
00052 }
00053 
00054 std::ostream&
00055 operator<<(std::ostream& os, const Log& log)
00056 {
00057     os << "Log:" << std::endl;
00058     os << "metadata start: " << std::endl;
00059     os << Core::ProtoBuf::dumpString(log.metadata);
00060     os << "end of metadata" << std::endl;
00061     os << "startIndex: " << log.getLogStartIndex() << std::endl;
00062     os << std::endl;
00063     for (uint64_t i = log.getLogStartIndex();
00064          i <= log.getLastLogIndex();
00065          ++i) {
00066         os << "Entry " << i << " start:" << std::endl;
00067         Log::Entry e = log.getEntry(i);
00068         if (e.type() == Protocol::Raft::EntryType::DATA) {
00069             std::string data = e.data();
00070             Core::Buffer buffer(const_cast<char*>(data.data()),
00071                                 data.length(), NULL);
00072             Protocol::Client::StateMachineCommand::Request command;
00073             if (!Core::ProtoBuf::parse(buffer, command)) {
00074                 WARNING("Could not parse protobuf in log entry %lu", i);
00075                 os << Core::ProtoBuf::dumpString(e);
00076             } else {
00077                 e.clear_data();
00078                 os << Core::ProtoBuf::dumpString(e);
00079                 os << "data: " << Core::ProtoBuf::dumpString(command);
00080             }
00081         } else {
00082             os << Core::ProtoBuf::dumpString(e);
00083         }
00084         os << "end of entry " << i << std::endl;
00085         os << std::endl;
00086     }
00087     os << std::endl;
00088     return os;
00089 }
00090 
00091 } // namespace LogCabin::Storage
00092 } // namespace LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines