Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 1 | #include "object-db-file.h" |
| 2 | #include <assert.h> |
| 3 | |
| 4 | void |
| 5 | writeBytes(ostream &out, const Bytes &bytes) |
| 6 | { |
| 7 | int size = bytes.size(); |
| 8 | writeInt(out, size); |
| 9 | out.write(head(bytes), size); |
| 10 | } |
| 11 | |
| 12 | void |
| 13 | readBytes(istream &in, Bytes &bytes) |
| 14 | { |
| 15 | int size; |
| 16 | readInt(in, size); |
| 17 | bytes.reserve(size); |
| 18 | in.read(head(bytes), size); |
| 19 | } |
| 20 | |
| 21 | ObjectDBFile::ObjectDBFile(const string &filename) |
| 22 | : m_size(0) |
| 23 | , m_cap(0) |
| 24 | , m_index(0) |
| 25 | , m_initialized(false) |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 26 | , m_filename(filename) |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 27 | // This ensures file with filename exists (assuming having write permission) |
| 28 | // This is needed as file_lock only works with existing file |
| 29 | , m_ostream(m_filename, ios_base::binary | ios_base::app) |
| 30 | , m_istream(m_filename, ios_base::binary | ios_base::in) |
| 31 | , m_filelock(m_filename) |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 32 | { |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 33 | int magic; |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 34 | ReadLock(m_filelock); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 35 | readInt(m_istream, magic); |
| 36 | if (magic == MAGIC_NUM) |
| 37 | { |
| 38 | m_initialized = true; |
| 39 | readInt(m_istream, m_cap); |
| 40 | readInt(m_istream, m_size); |
| 41 | m_istream.seekg( (3 + m_cap) * sizeof(int), ios::beg); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | ObjectDBFile::~ObjectDBFile() |
| 46 | { |
| 47 | m_istream.close(); |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 48 | m_ostream.close(); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void |
| 52 | ObjectDBFile::init(int capacity) |
| 53 | { |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 54 | WriteLock(m_filelock); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 55 | if (m_initialized) |
| 56 | { |
| 57 | throwException("Trying to init already initialized ObjectDBFile object" + m_filename); |
| 58 | } |
| 59 | |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 60 | m_cap = capacity; |
| 61 | m_size = 0; |
| 62 | |
| 63 | int magic = MAGIC_NUM; |
| 64 | writeInt(m_ostream, magic); |
| 65 | writeInt(m_ostream, m_cap); |
| 66 | writeInt(m_ostream, m_size); |
| 67 | m_initialized = true; |
| 68 | |
| 69 | int count = size; |
| 70 | int offset = 0; |
| 71 | while (count-- > 0) |
| 72 | { |
| 73 | writeInt(m_ostream, offset); |
| 74 | } |
| 75 | |
| 76 | // prepare read pos |
| 77 | m_istream.seekg(m_ostream.tellp(), ios::beg); |
| 78 | |
| 79 | // DEBUG |
| 80 | assert(m_ostream.tellp() == ((3 + m_cap) * sizeof(int))); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | // Append is not super efficient as it needs to seek and update the pos for the |
| 85 | // Content object. However, in our app, it is the case the these objects are wrote |
| 86 | // once and read multiple times, so it's not a big problem. |
| 87 | void |
| 88 | ObjectDBFile::append(const Bytes &co) |
| 89 | { |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 90 | WriteLock(m_filelock); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 91 | checkInit("Trying to append to un-initialized ObjectDBFile: " + m_filename); |
| 92 | |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 93 | if (m_size >= m_cap) |
| 94 | { |
| 95 | throwException("Exceed Maximum capacity: " + boost::lexical_cast<string>(m_cap)); |
| 96 | } |
| 97 | |
| 98 | // pos for this CO |
| 99 | int coPos = m_ostream.tellp(); |
| 100 | // index field for this CO |
| 101 | int indexPos = (3 + m_size) * sizeof(int); |
| 102 | |
| 103 | m_size++; |
| 104 | |
| 105 | // Update size (is it necessary?) We'll do it for now anyway |
| 106 | m_ostream.seekp( 2 * sizeof(int), ios::beg); |
| 107 | writeInt(m_ostream, m_size); |
| 108 | |
| 109 | // Write the pos for the CO |
| 110 | m_ostream.seekp(indexPos, ios::beg); |
| 111 | writeInt(m_ostream, coPos); |
| 112 | |
| 113 | // write the content object |
| 114 | m_ostream.seekp(coPos, ios::beg); |
| 115 | writeBytes(m_ostream, co); |
| 116 | |
| 117 | // By the end, the write pos is at the end of the file |
| 118 | } |
| 119 | |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 120 | // forget about caching for now; but ideally, we should cache the next few COs in memory |
| 121 | // and the request for COs tends to be sequential |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 122 | Bytes |
| 123 | ObjectDBFile::next() |
| 124 | { |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 125 | ReadLock(m_filelock); |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame^] | 126 | // We are been lazy here; just use file lock as mutex |
| 127 | // for the access to the cache too |
| 128 | if (m_dummyCache.find(m_index) != map::end) |
| 129 | { |
| 130 | int index = m_index; |
| 131 | m_index++; |
| 132 | return m_dummyCache[index]; |
| 133 | } |
| 134 | |
| 135 | // m_index not found in cache |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 136 | Bytes co; |
| 137 | if (m_index >= m_size) |
| 138 | { |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame^] | 139 | // at the end of file, return empty |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 140 | return co; |
| 141 | } |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame^] | 142 | |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 143 | readBytes(m_istream, co); |
| 144 | m_index++; |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame^] | 145 | |
| 146 | // fill dummy cache with the next CACHE_SIZE COs |
| 147 | fillDummyCache(); |
| 148 | |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 149 | return co; |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame^] | 153 | ObjectDBFile::fillDummyCache() |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 154 | { |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame^] | 155 | m_dummyCache.clear(); |
| 156 | int stop = (m_index + CACHE_SIZE < m_size) ? m_index + CACHE_SIZE : m_size; |
| 157 | // the m_index should not change |
| 158 | int index = m_index; |
| 159 | while (index < stop) |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 160 | { |
| 161 | Bytes co; |
| 162 | readBytes(m_istream, co); |
Zhenkai Zhu | d8569c9 | 2012-12-31 00:53:25 -0800 | [diff] [blame^] | 163 | m_dummyCache.insert(make_pair(index, co)); |
| 164 | index++; |
Zhenkai Zhu | 8a75ea9 | 2012-12-31 00:14:04 -0800 | [diff] [blame] | 165 | } |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | int |
| 169 | ObjectDBFile::size() const |
| 170 | { |
| 171 | return m_size; |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | ObjectDBFile::updateSzie() |
| 176 | { |
| 177 | int pos = m_istream.tellg(); |
| 178 | m_istream.seekg(2 * sizeof(int), ios::beg); |
| 179 | readInt(m_istream, m_size); |
| 180 | // recover the original pos |
| 181 | m_istream.seekg(pos, ios::beg); |
| 182 | } |
| 183 | |
| 184 | int |
| 185 | ObjectDBFile::fSize() |
| 186 | { |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 187 | ReadLock(m_filelock); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 188 | updateSize(); |
| 189 | return m_size; |
| 190 | } |
| 191 | |
| 192 | int |
| 193 | ObjectDBFile::index() |
| 194 | { |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 195 | ReadLock(m_filelock); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 196 | return m_index; |
| 197 | } |
| 198 | |
| 199 | bool |
| 200 | ObjectDBFile::seek(int index) |
| 201 | { |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 202 | ReadLock(m_filelock); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 203 | updateSize(); |
| 204 | if (m_size <= index) |
| 205 | { |
| 206 | return false; |
| 207 | } |
| 208 | m_index = index; |
| 209 | m_istream.seekg( (3 + m_index) * sizeof(int), ios::beg); |
| 210 | int pos; |
| 211 | readInt(m_istream, pos); |
| 212 | m_istream.seekg(pos, ios::beg); |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | rewind() |
| 218 | { |
Zhenkai Zhu | 427bed4 | 2012-12-30 23:57:48 -0800 | [diff] [blame] | 219 | ReadLock(m_filelock); |
Zhenkai Zhu | 772c707 | 2012-12-30 12:40:23 -0800 | [diff] [blame] | 220 | m_index = 0; |
| 221 | // point to the start of the CO fields |
| 222 | m_istream.seekg( (3 + m_cap) * sizeof(int), ios::beg); |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | ObjectDBFile::checkInit(const string &msg) |
| 227 | { |
| 228 | if (!m_initialized) |
| 229 | { |
| 230 | throwException(msg); |
| 231 | } |
| 232 | } |