aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--default.bld7
-rw-r--r--src/tests/cache.cpp204
2 files changed, 205 insertions, 6 deletions
diff --git a/default.bld b/default.bld
index 9babbff..4dd27d0 100644
--- a/default.bld
+++ b/default.bld
@@ -151,11 +151,6 @@ target "viewcsv"
151 LDFLAGS += "-lncurses"; 151 LDFLAGS += "-lncurses";
152} 152}
153 153
154target ["myriad", "myriadfs", "tests/myriad", "tests/myriadfs", "unit/myriad", "tests/bigmyriad", "tests/synchroqueue", "tests/readwritemutex"]
155{
156 LDFLAGS += "-lpthread";
157}
158
159target files("src/extra/*.cpp").replace("src/extra/","").replace(".cpp","") 154target files("src/extra/*.cpp").replace("src/extra/","").replace(".cpp","")
160{ 155{
161 input "src/extra/${OUTPUT}.cpp"; 156 input "src/extra/${OUTPUT}.cpp";
@@ -197,7 +192,7 @@ target files("src/tests/*.cpp").replace("src/","").replace(".cpp","")
197 requires "libbu++.a"; 192 requires "libbu++.a";
198 tag ["tests", "general tests"]; 193 tag ["tests", "general tests"];
199 CXXFLAGS += "-I."; 194 CXXFLAGS += "-I.";
200 LDFLAGS += "-L. -lbu++"; 195 LDFLAGS += "-L. -lbu++ -lpthread";
201} 196}
202 197
203// Some tests need extra libs and whatnot, that goes here. 198// Some tests need extra libs and whatnot, that goes here.
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