blob: 1eb1ef8abe8681a136d03c05760eb0e4482d04a1 [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>
Zhenkai Zhu772c7072012-12-30 12:40:23 -08007#include <sstream>
Zhenkai Zhud8569c92012-12-31 00:53:25 -08008#include <deque>
Zhenkai Zhu772c7072012-12-30 12:40:23 -08009#include <boost/thread/locks.hpp>
Zhenkai Zhu772c7072012-12-30 12:40:23 -080010#include <boost/lexical_cast.hpp>
Zhenkai Zhu9bcbd542012-12-31 01:01:25 -080011#include <boost/thread/shared_mutex.hpp>
Zhenkai Zhu427bed42012-12-30 23:57:48 -080012#include <boost/interprocess/sync/file_lock.hpp>
13#include <boost/interprocess/sync/sharable_lock.hpp>
14#include <boost/interprocess/sync/scoped_lock.hpp>
Zhenkai Zhu772c7072012-12-30 12:40:23 -080015
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
24using 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
40class ObjectDBFile
41{
42public:
Zhenkai Zhu427bed42012-12-30 23:57:48 -080043 typedef boost::interprocess::file_lock Filelock;
44 typedef boost::interprocess::scoped_lock<Filelock> WriteLock;
45 typedef boost::interprocess::sharable_lock<Filelock> ReadLock;
Zhenkai Zhu9bcbd542012-12-31 01:01:25 -080046 typedef boost::shared_mutex Mutex;
47 typedef boost::shared_lock<Mutex> SLock;
48 typedef boost::unique_lock<Mutex> ULock;
Zhenkai Zhu772c7072012-12-30 12:40:23 -080049
50 ObjectDBFile(const string &filename);
Zhenkai Zhu6204d472013-01-02 13:16:22 -080051 virtual ~ObjectDBFile();
Zhenkai Zhu772c7072012-12-30 12:40:23 -080052
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 Zhu772c7072012-12-30 12:40:23 -080069 // 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
94protected:
Zhenkai Zhu772c7072012-12-30 12:40:23 -080095 // 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 Zhud8569c92012-12-31 00:53:25 -0800103 // read lock should have been grabbed already before the call
104 void
105 fillDummyCache();
106
Zhenkai Zhu772c7072012-12-30 12:40:23 -0800107 #define MAGIC_NUM 0xAAAAAAAA
108
109protected:
110 string m_filename;
111 ifstream m_istream;
112 ofstream m_ostream;
Zhenkai Zhu427bed42012-12-30 23:57:48 -0800113 Filelock m_filelock;
Zhenkai Zhu772c7072012-12-30 12:40:23 -0800114 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 Zhud8569c92012-12-31 00:53:25 -0800120
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 Zhu9bcbd542012-12-31 01:01:25 -0800126 Mutex m_cacheMutex;
Zhenkai Zhu772c7072012-12-30 12:40:23 -0800127};
128
129void inline
130writeInt(ostream &out, const int &x)
131{
132 out.write((const char *)&x, sizeof(int));
133}
134
135void inline
136readInt(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
143void
144writeBytes(ostream &out, const Bytes &bytes);
145
146// read size and then the actual bytes
147void
148readBytes(istream &in, Bytes &bytes);
149
Zhenkai Zhu6204d472013-01-02 13:16:22 -0800150char *
151head(const Bytes &bytes);
152
Zhenkai Zhu772c7072012-12-30 12:40:23 -0800153#endif