aboutsummaryrefslogtreecommitdiff
path: root/src/tests/cache.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2013-02-23 06:27:52 +0000
committerMike Buland <eichlan@xagasoft.com>2013-02-23 06:27:52 +0000
commit180bee907a63817196b9e3ec8472603c8b5b8756 (patch)
tree30424ca832ed8804c323897728b92c4794f5eb22 /src/tests/cache.cpp
parentfc7fc244600f504b6f832d8106a9218ef6a9b88b (diff)
downloadlibbu++-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/tests/cache.cpp')
-rw-r--r--src/tests/cache.cpp204
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
13template<typename keytype, typename obtype> class CacheBase;
14
15template<typename keytype, typename obtype>
16class CacheObject
17{
18friend class CacheBase<keytype, obtype>;
19public:
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
42private:
43 void setCache( CacheType *pCache )
44 {
45 this->pCache = pCache;
46 }
47
48private:
49 CacheType *pCache;
50 bool bChanged;
51};
52
53template<typename keytype, typename obtype, typename basetype=obtype>
54class CachePtr
55{
56friend class CacheBase<keytype, basetype>;
57public:
58 CachePtr()
59 {
60 }
61
62 virtual ~CachePtr()
63 {
64 }
65};
66
67template<typename keytype, typename obtype>
68class CacheBase
69{
70public:
71 CacheBase()
72 {
73 }
74
75 virtual ~CacheBase()
76 {
77 }
78
79protected:
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
87template<typename keytype, typename obtype>
88class MyriadCache : public CacheBase<keytype, obtype>
89{
90public:
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
116protected:
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
148private:
149 Bu::Stream &sStore;
150 Bu::Myriad mStore;
151 Bu::Hash<keytype, int> hIndex;
152};
153
154//
155// Test
156//
157
158class Something : public CacheObject<Bu::Uuid, Something>
159{
160friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s );
161friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s );
162public:
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
182private:
183 Bu::Uuid uId;
184 Bu::String sName;
185};
186
187Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s )
188{
189 return ar >> s.uId >> s.sName;
190}
191
192Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s )
193{
194 return ar << s.uId << s.sName;
195}
196
197int 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