LogCabin
|
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 <mutex> 00017 #include <string> 00018 #include <vector> 00019 00020 #include "build/Storage/SimpleFileLog.pb.h" 00021 #include "Storage/FilesystemUtil.h" 00022 #include "Storage/MemoryLog.h" 00023 #include "Storage/Log.h" 00024 00025 #ifndef LOGCABIN_STORAGE_SIMPLEFILELOG_H 00026 #define LOGCABIN_STORAGE_SIMPLEFILELOG_H 00027 00028 namespace LogCabin { 00029 namespace Storage { 00030 00031 // forward declaration 00032 class Globals; 00033 00034 /** 00035 * TODO(ongaro): docs 00036 */ 00037 class SimpleFileLog : public Log { 00038 class Sync : public Log::Sync { 00039 public: 00040 explicit Sync(uint64_t lastIndex); 00041 void wait(); 00042 /** 00043 * Set of file descriptors that are fsynced and closed on wait(). 00044 * If the bool is true, close it too. 00045 */ 00046 std::vector<std::pair<int, bool>> fds; 00047 }; 00048 00049 public: 00050 typedef Protocol::Raft::Entry Entry; 00051 00052 explicit SimpleFileLog(const Storage::FilesystemUtil::File& parentDir); 00053 ~SimpleFileLog(); 00054 std::pair<uint64_t, uint64_t> 00055 append(const std::vector<const Entry*>& entries); 00056 std::string getName() const; 00057 std::unique_ptr<Log::Sync> takeSync(); 00058 void truncatePrefix(uint64_t firstEntryId); 00059 void truncateSuffix(uint64_t lastEntryId); 00060 00061 const Entry& getEntry(uint64_t) const; 00062 uint64_t getLogStartIndex() const; 00063 uint64_t getLastLogIndex() const; 00064 uint64_t getSizeBytes() const; 00065 00066 00067 void updateMetadata(); 00068 00069 protected: 00070 Storage::FilesystemUtil::File updateMetadataCallerSync(); 00071 MemoryLog memoryLog; 00072 SimpleFileLogMetadata::Metadata metadata; 00073 Storage::FilesystemUtil::File dir; 00074 Storage::FilesystemUtil::File lostAndFound; 00075 std::unique_ptr<Sync> currentSync; 00076 00077 std::string readMetadata(const std::string& filename, 00078 SimpleFileLogMetadata::Metadata& metadata) const; 00079 std::vector<uint64_t> getEntryIds() const; 00080 Entry read(const std::string& entryPath) const; 00081 }; 00082 00083 } // namespace LogCabin::Storage 00084 } // namespace LogCabin 00085 00086 #endif /* LOGCABIN_STORAGE_SIMPLEFILELOG_H */