00001 /* 00002 * BufferedBlockManager.h : part of the Mace toolkit for building distributed systems 00003 * 00004 * Copyright (c) 2007, Charles Killian, Dejan Kostic, Ryan Braud, James W. Anderson, John Fisher-Ogden, Calvin Hubble, Duy Nguyen, Justin Burke, David Oppenheimer, Amin Vahdat, Adolfo Rodriguez, Sooraj Bhat 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the names of Duke University nor The University of 00017 * California, San Diego, nor the names of the authors or contributors 00018 * may be used to endorse or promote products derived from 00019 * this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00030 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 * 00032 * ----END-OF-LEGAL-STUFF---- */ 00033 #include <pthread.h> 00034 #include <deque> 00035 #include "LRUCache.h" 00036 #include "BlockManager.h" 00037 #include "FileBlockManager.h" 00038 00039 #ifndef BUFFERED_BLOCK_MANAGER_H 00040 #define BUFFERED_BLOCK_MANAGER_H 00041 00042 typedef std::deque<int> BlockQueue; 00043 00054 00055 class BufferedBlockManager : public BlockManager { 00056 public: 00057 static const uint DEFAULT_BUFFER_CAPACITY = 256; 00058 BufferedBlockManager(FileBlockManager& m, uint capacity = DEFAULT_BUFFER_CAPACITY); 00059 virtual ~BufferedBlockManager(); 00060 virtual void prefetchBlock(uint64_t index); 00061 virtual int flush(); 00062 virtual std::string getBlock(uint64_t index); 00063 virtual size_t setBlock(uint64_t index, const std::string& buffer); 00064 virtual int open(const std::string& p, const char* mode); 00065 virtual int close(); 00066 virtual bool isOpen() const; 00067 virtual const std::string& getPath() const; 00068 virtual std::string getFileName() const; 00069 virtual size_t getBlockSize() const; 00070 virtual void setBlockSize(size_t s); 00071 virtual uint64_t getBlockCount() const { 00072 assert(isOpen()); 00073 return fbm->getBlockCount(); 00074 } 00075 virtual uint64_t getSize() const { 00076 assert(isOpen()); 00077 return fbm->getSize(); 00078 } 00079 void processQueues(); 00080 00081 protected: 00082 static void* startQueueThread(void* t); 00083 00084 00085 private: 00086 void writeLastDirty(); 00087 void readIntoCache(uint index); 00088 void acquireQueueLock(); 00089 void releaseQueueLock(); 00090 void signalQueueData(); 00091 void waitForQueueData(); 00092 00093 private: 00094 FileBlockManager* fbm; 00095 BlockQueue readQueue; 00096 mace::LRUCache<uint, std::string> bufferCache; 00097 bool isOpenFlag; 00098 bool processQueuesFlag; 00099 pthread_t queueThread; 00100 pthread_mutex_t queueLock; 00101 pthread_cond_t queueCond; 00102 00103 00104 }; // BlockManager 00105 00108 #endif // BUFFERED_BLOCK_MANAGER_H