LogCabin
|
00001 /* Copyright (c) 2012-2014 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 <cinttypes> 00017 #include <deque> 00018 #include <memory> 00019 #include <string> 00020 00021 #include "Storage/Log.h" 00022 00023 #ifndef LOGCABIN_STORAGE_MEMORYLOG_H 00024 #define LOGCABIN_STORAGE_MEMORYLOG_H 00025 00026 namespace LogCabin { 00027 namespace Storage { 00028 00029 // forward declaration 00030 class Globals; 00031 00032 class MemoryLog : public Log { 00033 public: 00034 MemoryLog(); 00035 ~MemoryLog(); 00036 00037 std::pair<uint64_t, uint64_t> 00038 append(const std::vector<const Entry*>& entries); 00039 const Entry& getEntry(uint64_t logIndex) const; 00040 uint64_t getLogStartIndex() const; 00041 uint64_t getLastLogIndex() const; 00042 std::string getName() const; 00043 uint64_t getSizeBytes() const; 00044 std::unique_ptr<Sync> takeSync(); 00045 void truncatePrefix(uint64_t firstIndex); 00046 void truncateSuffix(uint64_t lastIndex); 00047 void updateMetadata(); 00048 00049 protected: 00050 00051 /** 00052 * The index for the first entry in the log. Begins as 1 for new logs but 00053 * will be larger for logs that have been snapshotted. 00054 */ 00055 uint64_t startIndex; 00056 00057 /** 00058 * Stores the entries that make up the log. 00059 * The offset into 'entries' is the index of the entry minus 'startIndex'. 00060 * This is a deque rather than a vector to support fast prefix truncation 00061 * (used after snapshotting a prefix of the log). 00062 */ 00063 std::deque<Entry> entries; 00064 00065 /** 00066 * This is returned by the next call to getSync. 00067 * It's totally unnecessary to have this member for MemoryLog, as its syncs 00068 * don't do anything. However, it's useful for injecting different times of 00069 * Syncs into unit tests. 00070 */ 00071 std::unique_ptr<Sync> currentSync; 00072 }; 00073 00074 } // namespace LogCabin::Storage 00075 } // namespace LogCabin 00076 00077 #endif /* LOGCABIN_STORAGE_MEMORYLOG_H */