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