blob: b90bd91809e5e6be3c0f338d44324bc71685f695 [file] [log] [blame]
Zhenkai Zhu772c7072012-12-30 12:40:23 -08001#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 Zhud8569c92012-12-31 00:53:25 -080010#include <deque>
Zhenkai Zhu772c7072012-12-30 12:40:23 -080011#include <boost/thread/locks.hpp>
Zhenkai Zhu772c7072012-12-30 12:40:23 -080012#include <boost/lexical_cast.hpp>
Zhenkai Zhu427bed42012-12-30 23:57:48 -080013#include <boost/interprocess/sync/file_lock.hpp>
14#include <boost/interprocess/sync/sharable_lock.hpp>
15#include <boost/interprocess/sync/scoped_lock.hpp>
Zhenkai Zhu772c7072012-12-30 12:40:23 -080016
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
25using 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
41class ObjectDBFile
42{
43public:
Zhenkai Zhu427bed42012-12-30 23:57:48 -080044 typedef boost::interprocess::file_lock Filelock;
45 typedef boost::interprocess::scoped_lock<Filelock> WriteLock;
46 typedef boost::interprocess::sharable_lock<Filelock> ReadLock;
Zhenkai Zhu772c7072012-12-30 12:40:23 -080047
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 Zhu772c7072012-12-30 12:40:23 -080067 // 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
92protected:
Zhenkai Zhu772c7072012-12-30 12:40:23 -080093 // 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 Zhud8569c92012-12-31 00:53:25 -0800101 // read lock should have been grabbed already before the call
102 void
103 fillDummyCache();
104
Zhenkai Zhu772c7072012-12-30 12:40:23 -0800105 #define MAGIC_NUM 0xAAAAAAAA
106
107protected:
108 string m_filename;
109 ifstream m_istream;
110 ofstream m_ostream;
Zhenkai Zhu427bed42012-12-30 23:57:48 -0800111 Filelock m_filelock;
Zhenkai Zhu772c7072012-12-30 12:40:23 -0800112 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 Zhud8569c92012-12-31 00:53:25 -0800118
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 Zhu772c7072012-12-30 12:40:23 -0800124};
125
126void inline
127writeInt(ostream &out, const int &x)
128{
129 out.write((const char *)&x, sizeof(int));
130}
131
132void inline
133readInt(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
140void
141writeBytes(ostream &out, const Bytes &bytes);
142
143// read size and then the actual bytes
144void
145readBytes(istream &in, Bytes &bytes);
146
147#endif