Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 1 | #ifndef OBJECT_DB_FILE_H |
| 2 | #define OBJECT_DB_FILE_H |
| 3 | |
| 4 | #include "object-db.h" |
| 5 | #include <stdio.h> |
| 6 | #include <fstream> |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 7 | #include <sstream> |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame] | 8 | #include <deque> |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 9 | #include <boost/thread/locks.hpp> |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 10 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | 9bcbd54 | 2012-12-31 01:01:25 -0800 | [diff] [blame] | 11 | #include <boost/thread/shared_mutex.hpp> |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 12 | #include <boost/interprocess/sync/file_lock.hpp> |
| 13 | #include <boost/interprocess/sync/sharable_lock.hpp> |
| 14 | #include <boost/interprocess/sync/scoped_lock.hpp> |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 15 | |
| 16 | #define _OVERRIDE |
| 17 | #ifdef __GNUC__ |
| 18 | #if __GNUC_MAJOR >= 4 && __GNUC_MINOR__ >= 7 |
| 19 | #undef _OVERRIDE |
| 20 | #define _OVERRIDE override |
| 21 | #endif // __GNUC__ version |
| 22 | #endif // __GNUC__ |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | // This is a file based ObjectDB implementation |
| 27 | // The assumption is, the Content Objects will be stored sequentially |
| 28 | |
| 29 | // To provide random access, we will have a table of "address" for each |
| 30 | // ContentObject at the beginning of the file. |
| 31 | // This also requires another assumption, that is the number of COs must |
| 32 | // be know a priori. This requirement is reasonable for our dropbox-like |
| 33 | // System, as the file we publish is static file and we can easily know |
| 34 | // the number of COs before we store them into ObjectDB. |
| 35 | |
| 36 | /* How file looks like: |
| 37 | * |MAGIC_NUM|capacity|size|pos for each CO ...|1st CO|2nd CO| ... | |
| 38 | */ |
| 39 | |
| 40 | class ObjectDBFile |
| 41 | { |
| 42 | public: |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 43 | typedef boost::interprocess::file_lock Filelock; |
| 44 | typedef boost::interprocess::scoped_lock<Filelock> WriteLock; |
| 45 | typedef boost::interprocess::sharable_lock<Filelock> ReadLock; |
Zhenkai Zhu | 9bcbd54 | 2012-12-31 01:01:25 -0800 | [diff] [blame] | 46 | typedef boost::shared_mutex Mutex; |
| 47 | typedef boost::shared_lock<Mutex> SLock; |
| 48 | typedef boost::unique_lock<Mutex> ULock; |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 49 | |
| 50 | ObjectDBFile(const string &filename); |
Zhenkai Zhu | 6204d47 | 2013-01-02 13:16:22 -0800 | [diff] [blame] | 51 | virtual ~ObjectDBFile(); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 52 | |
| 53 | // reserve the "address" table for n COs; must reserve before |
| 54 | // write anything (unless reserved quota has not be consumed yet) |
| 55 | void |
| 56 | init(int capacity); |
| 57 | |
| 58 | bool |
| 59 | initialized() const { return m_initialized; } |
| 60 | |
| 61 | // assume sequential |
| 62 | virtual void |
| 63 | append(const Bytes &co) _OVERRIDE; |
| 64 | |
| 65 | // get next CO |
| 66 | virtual Bytes |
| 67 | next() _OVERRIDE; |
| 68 | |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 69 | // size in terms of number of COs |
| 70 | // This is the lazy form of size, i.e. it returns the size cached in this object |
| 71 | // but that may not necessarily equal to the actual size kept in file |
| 72 | // This is enough if the caller knows for sure that no other thread is changing the |
| 73 | // file or the caller does not care about the new size. |
| 74 | virtual int |
| 75 | size() const _OVERRIDE; |
| 76 | |
| 77 | // this returns the actual size (also update the size cache in this object), but it is more costly, and requires file IO |
| 78 | int |
| 79 | fSize(); |
| 80 | |
| 81 | // the index of the CO to be read |
| 82 | int |
| 83 | index(); |
| 84 | |
| 85 | // set the pos to be the desired CO |
| 86 | // return true if success |
| 87 | bool |
| 88 | seek(int index); |
| 89 | |
| 90 | // reset pos to be zero |
| 91 | void |
| 92 | rewind(); |
| 93 | |
| 94 | protected: |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 95 | // read or write lock should have been grabbed already before the call |
| 96 | void |
| 97 | checkInit(const string &msg); |
| 98 | |
| 99 | // read lock should have been grabbed already before the call |
| 100 | void |
| 101 | updateSize(); |
| 102 | |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame] | 103 | // read lock should have been grabbed already before the call |
| 104 | void |
| 105 | fillDummyCache(); |
| 106 | |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 107 | #define MAGIC_NUM 0xAAAAAAAA |
| 108 | |
| 109 | protected: |
| 110 | string m_filename; |
| 111 | ifstream m_istream; |
| 112 | ofstream m_ostream; |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 113 | Filelock m_filelock; |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 114 | bool m_initialized; |
| 115 | // capacity in terms of number of COs |
| 116 | int m_cap; |
| 117 | int m_size; |
| 118 | // the index (or seq) of the CO to be read |
| 119 | int m_index; |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame] | 120 | |
| 121 | // A dummy Cache that holds the next 10 (or all remaining if less than 10) |
| 122 | // COs after a next() operation |
| 123 | // If needed and time allows, we can have more complex cache |
| 124 | #define CACHE_SIZE 10 |
| 125 | map<int, Bytes> m_dummyCache; |
Zhenkai Zhu | 9bcbd54 | 2012-12-31 01:01:25 -0800 | [diff] [blame] | 126 | Mutex m_cacheMutex; |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | void inline |
| 130 | writeInt(ostream &out, const int &x) |
| 131 | { |
| 132 | out.write((const char *)&x, sizeof(int)); |
| 133 | } |
| 134 | |
| 135 | void inline |
| 136 | readInt(istream &in, int &x) |
| 137 | { |
| 138 | in.read((char *)&x, sizeof(int)); |
| 139 | } |
| 140 | |
| 141 | // write size and then the actual bytes |
| 142 | // operator << overloading is not used to avoid confusion |
| 143 | void |
| 144 | writeBytes(ostream &out, const Bytes &bytes); |
| 145 | |
| 146 | // read size and then the actual bytes |
| 147 | void |
| 148 | readBytes(istream &in, Bytes &bytes); |
| 149 | |
Zhenkai Zhu | 6204d47 | 2013-01-02 13:16:22 -0800 | [diff] [blame] | 150 | char * |
| 151 | head(const Bytes &bytes); |
| 152 | |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 153 | #endif |