diff options
author | Mike Buland <eichlan@xagasoft.com> | 2013-02-23 06:27:52 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2013-02-23 06:27:52 +0000 |
commit | 180bee907a63817196b9e3ec8472603c8b5b8756 (patch) | |
tree | 30424ca832ed8804c323897728b92c4794f5eb22 /src | |
parent | fc7fc244600f504b6f832d8106a9218ef6a9b88b (diff) | |
download | libbu++-180bee907a63817196b9e3ec8472603c8b5b8756.tar.gz libbu++-180bee907a63817196b9e3ec8472603c8b5b8756.tar.bz2 libbu++-180bee907a63817196b9e3ec8472603c8b5b8756.tar.xz libbu++-180bee907a63817196b9e3ec8472603c8b5b8756.zip |
Just made all tests include libpthread, it makes sense these days.
Creating an experimental new cache system that's controlled primarily with a
subclass. This should cut down significantly on the amount of code needed to
use the cache. The new one also features a required (for now) base class for
objects that use the cache. This simplifies so many different things.
Diffstat (limited to 'src')
-rw-r--r-- | src/tests/cache.cpp | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp new file mode 100644 index 0000000..911b829 --- /dev/null +++ b/src/tests/cache.cpp | |||
@@ -0,0 +1,204 @@ | |||
1 | #include <bu/archive.h> | ||
2 | #include <bu/hash.h> | ||
3 | #include <bu/uuid.h> | ||
4 | #include <bu/myriad.h> | ||
5 | #include <bu/myriadstream.h> | ||
6 | #include <bu/file.h> | ||
7 | #include <bu/streamstack.h> | ||
8 | |||
9 | // | ||
10 | // New Cache Implementation | ||
11 | // | ||
12 | |||
13 | template<typename keytype, typename obtype> class CacheBase; | ||
14 | |||
15 | template<typename keytype, typename obtype> | ||
16 | class CacheObject | ||
17 | { | ||
18 | friend class CacheBase<keytype, obtype>; | ||
19 | public: | ||
20 | CacheObject() : | ||
21 | pCache( NULL ), | ||
22 | bChanged( false ) | ||
23 | { | ||
24 | } | ||
25 | |||
26 | virtual ~CacheObject() | ||
27 | { | ||
28 | } | ||
29 | |||
30 | typedef CacheBase<keytype, obtype> CacheType; | ||
31 | |||
32 | virtual const keytype getKey() const=0; | ||
33 | |||
34 | void changed( bool bChanged=true ) | ||
35 | { | ||
36 | if( this->bChanged == false && bChanged == true ) | ||
37 | pCache->objectChanged( getKey() ); | ||
38 | |||
39 | this->bChanged = bChanged; | ||
40 | } | ||
41 | |||
42 | private: | ||
43 | void setCache( CacheType *pCache ) | ||
44 | { | ||
45 | this->pCache = pCache; | ||
46 | } | ||
47 | |||
48 | private: | ||
49 | CacheType *pCache; | ||
50 | bool bChanged; | ||
51 | }; | ||
52 | |||
53 | template<typename keytype, typename obtype, typename basetype=obtype> | ||
54 | class CachePtr | ||
55 | { | ||
56 | friend class CacheBase<keytype, basetype>; | ||
57 | public: | ||
58 | CachePtr() | ||
59 | { | ||
60 | } | ||
61 | |||
62 | virtual ~CachePtr() | ||
63 | { | ||
64 | } | ||
65 | }; | ||
66 | |||
67 | template<typename keytype, typename obtype> | ||
68 | class CacheBase | ||
69 | { | ||
70 | public: | ||
71 | CacheBase() | ||
72 | { | ||
73 | } | ||
74 | |||
75 | virtual ~CacheBase() | ||
76 | { | ||
77 | } | ||
78 | |||
79 | protected: | ||
80 | virtual void _create( obtype *o )=0; | ||
81 | virtual void _erase( const keytype &k )=0; | ||
82 | |||
83 | virtual obtype *_load( const keytype &k )=0; | ||
84 | virtual void _save( obtype *o )=0; | ||
85 | }; | ||
86 | |||
87 | template<typename keytype, typename obtype> | ||
88 | class MyriadCache : public CacheBase<keytype, obtype> | ||
89 | { | ||
90 | public: | ||
91 | MyriadCache( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ) : | ||
92 | sStore( sStore ), | ||
93 | mStore( sStore, iBlockSize, iPreallocate ) | ||
94 | { | ||
95 | try | ||
96 | { | ||
97 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
98 | Bu::Archive ar( ms, Bu::Archive::load ); | ||
99 | ar >> hIndex; | ||
100 | } | ||
101 | catch(...) | ||
102 | { | ||
103 | if( mStore.createStreamWithId( 1 ) != 1 ) | ||
104 | throw Bu::ExceptionBase("Error creating index stream."); | ||
105 | |||
106 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
107 | Bu::Archive ar( ms, Bu::Archive::save ); | ||
108 | ar << hIndex; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | virtual ~MyriadCache() | ||
113 | { | ||
114 | } | ||
115 | |||
116 | protected: | ||
117 | virtual void _create( obtype *o ) | ||
118 | { | ||
119 | hIndex.insert( o->getKey(), mStore.createStream() ); | ||
120 | _save( o ); | ||
121 | } | ||
122 | |||
123 | virtual void _erase( const keytype &k ) | ||
124 | { | ||
125 | mStore.deleteStream( hIndex.get( k ) ); | ||
126 | hIndex.erase( k ); | ||
127 | } | ||
128 | |||
129 | virtual obtype *_load( const keytype &k ) | ||
130 | { | ||
131 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); | ||
132 | Bu::Archive ar( ms, Bu::Archive::load ); | ||
133 | obtype *ret = new obtype(); | ||
134 | ar >> *ret; | ||
135 | return ret; | ||
136 | } | ||
137 | |||
138 | virtual void _save( obtype *o ) | ||
139 | { | ||
140 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) ); | ||
141 | { | ||
142 | Bu::Archive ar( ms, Bu::Archive::save ); | ||
143 | ar << *o; | ||
144 | } | ||
145 | ms.setSize( ms.tell() ); | ||
146 | } | ||
147 | |||
148 | private: | ||
149 | Bu::Stream &sStore; | ||
150 | Bu::Myriad mStore; | ||
151 | Bu::Hash<keytype, int> hIndex; | ||
152 | }; | ||
153 | |||
154 | // | ||
155 | // Test | ||
156 | // | ||
157 | |||
158 | class Something : public CacheObject<Bu::Uuid, Something> | ||
159 | { | ||
160 | friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ); | ||
161 | friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ); | ||
162 | public: | ||
163 | Something() | ||
164 | { | ||
165 | } | ||
166 | |||
167 | Something( const Bu::String &sName ) : | ||
168 | uId( Bu::Uuid::generate() ), | ||
169 | sName( sName ) | ||
170 | { | ||
171 | } | ||
172 | |||
173 | virtual ~Something() | ||
174 | { | ||
175 | } | ||
176 | |||
177 | virtual const Bu::Uuid getKey() const | ||
178 | { | ||
179 | return uId; | ||
180 | } | ||
181 | |||
182 | private: | ||
183 | Bu::Uuid uId; | ||
184 | Bu::String sName; | ||
185 | }; | ||
186 | |||
187 | Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s ) | ||
188 | { | ||
189 | return ar >> s.uId >> s.sName; | ||
190 | } | ||
191 | |||
192 | Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s ) | ||
193 | { | ||
194 | return ar << s.uId << s.sName; | ||
195 | } | ||
196 | |||
197 | int main() | ||
198 | { | ||
199 | Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite); | ||
200 | MyriadCache<Bu::Uuid, Something> c( fStore ); | ||
201 | |||
202 | return 0; | ||
203 | } | ||
204 | |||