aboutsummaryrefslogtreecommitdiff
path: root/src/tests/cachedel.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2024-11-06 16:45:09 -0800
committerMike Buland <mike@xagasoft.com>2024-11-06 16:45:09 -0800
commit6c066b6bbd4a44ae7c5874abf6bd3a3e04f76b88 (patch)
treed6abea40ac9b1a194ab06a52e341b0452918ae40 /src/tests/cachedel.cpp
parent700d4bbcbf59c4447becbab21a6aa7204a8da2f4 (diff)
downloadlibbu++-6c066b6bbd4a44ae7c5874abf6bd3a3e04f76b88.tar.gz
libbu++-6c066b6bbd4a44ae7c5874abf6bd3a3e04f76b88.tar.bz2
libbu++-6c066b6bbd4a44ae7c5874abf6bd3a3e04f76b88.tar.xz
libbu++-6c066b6bbd4a44ae7c5874abf6bd3a3e04f76b88.zip
Tests are back, minor fixes.
There is a cache tracking bug exposed in cachedel test, it is unclear if this is a regression yet.
Diffstat (limited to 'src/tests/cachedel.cpp')
-rw-r--r--src/tests/cachedel.cpp291
1 files changed, 291 insertions, 0 deletions
diff --git a/src/tests/cachedel.cpp b/src/tests/cachedel.cpp
new file mode 100644
index 0000000..3fa3e86
--- /dev/null
+++ b/src/tests/cachedel.cpp
@@ -0,0 +1,291 @@
1#include "bu/myriadcache.h"
2#include "bu/uuid.h"
3#include "bu/string.h"
4#include "bu/sio.h"
5#include "bu/membuf.h"
6
7class Something : public Bu::CacheObject<Bu::Uuid, Something>
8{
9friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s );
10friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s );
11public:
12 Something()
13 {
14 }
15
16 Something( const Bu::String &sName ) :
17 uId( Bu::Uuid::generate() ),
18 sName( sName )
19 {
20 }
21
22 virtual ~Something()
23 {
24 }
25
26 virtual Bu::Uuid getKey() const
27 {
28 return uId;
29 }
30
31 Bu::String getName() const
32 {
33 return sName;
34 }
35
36 void setName( const Bu::String &sNewName )
37 {
38 sName = sNewName;
39 changed();
40 }
41
42 virtual Bu::String toString() const=0;
43
44private:
45 Bu::Uuid uId;
46 Bu::String sName;
47};
48
49class SubSomethingA : public Something
50{
51friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingA &s );
52friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingA &s );
53public:
54 SubSomethingA()
55 {
56 }
57
58 SubSomethingA( const Bu::String &sName, int iNumber ) :
59 Something( sName ),
60 iNumber( iNumber )
61 {
62 }
63
64 virtual Bu::String toString() const
65 {
66 return Bu::String("[typeA] %1 (%2)").arg( getName() ).arg( iNumber );
67 }
68
69private:
70 int iNumber;
71};
72
73class SubSomethingB : public Something
74{
75friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingB &s );
76friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingB &s );
77public:
78 SubSomethingB()
79 {
80 }
81
82 SubSomethingB( const Bu::String &sName, const Bu::String &sString ) :
83 Something( sName ),
84 sString( sString )
85 {
86 }
87
88 virtual Bu::String toString() const
89 {
90 return Bu::String("[typeB] %1 (%2)").arg( getName() ).arg( sString );
91 }
92
93private:
94 Bu::String sString;
95};
96
97Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, Something &s )
98{
99 return ar >> s.uId >> s.sName;
100}
101
102Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const Something &s )
103{
104 return ar << s.uId << s.sName;
105}
106
107Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingA &s )
108{
109 return ar >> (Something &)s >> s.iNumber;
110}
111
112Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingA &s )
113{
114 return ar << (Something &)s << s.iNumber;
115}
116
117Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, SubSomethingB &s )
118{
119 return ar >> (Something &)s >> s.sString;
120}
121
122Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const SubSomethingB &s )
123{
124 return ar << (Something &)s << s.sString;
125}
126
127namespace Bu
128{
129 template<>
130 void _cacheObjectSave<const Something>( Bu::Stream &s, const Something *pObject )
131 {
132 Bu::Archive ar( s, Bu::Archive::save );
133 if( typeid(*pObject) == typeid(SubSomethingA) )
134 {
135 ar << (uint8_t)1 << (SubSomethingA &)*pObject;
136 }
137 else if( typeid(*pObject) == typeid(SubSomethingB) )
138 {
139 ar << (uint8_t)2 << (SubSomethingB &)*pObject;
140 }
141 else
142 {
143 Bu::println("Not a recognized type!");
144 throw Bu::ExceptionBase("Not recognized type!");
145 }
146 }
147
148 template<>
149 Something *_cacheObjectLoad<Bu::Uuid, Something>( Bu::CacheObject<Bu::Uuid, Something>::Initializer &initObj, const Bu::Uuid &rKey, Bu::Stream &s )
150 {
151 Bu::Archive ar( s, Bu::Archive::load );
152 uint8_t uType;
153 ar >> uType;
154 switch( uType )
155 {
156 case 1:
157 {
158 SubSomethingA *ret = initObj(new SubSomethingA());
159 ar >> *ret;
160 return ret;
161 }
162 break;
163
164 case 2:
165 {
166 SubSomethingB *ret = initObj(new SubSomethingB());
167 ar >> *ret;
168 return ret;
169 }
170 break;
171
172 default:
173 throw Bu::ExceptionBase("Flagrant error! Invalid type!");
174 }
175
176 return NULL;
177 }
178}
179
180typedef Bu::CachePtr<Bu::Uuid, Something> SomethingPtr;
181typedef Bu::CachePtr<Bu::Uuid, SubSomethingA, Something> SomethingAPtr;
182typedef Bu::CachePtr<Bu::Uuid, SubSomethingB, Something> SomethingBPtr;
183typedef Bu::MyriadCache<Bu::Uuid, Something> SomethingCache;
184
185int main( int, char *[] )
186{
187 Bu::MemBuf mbStore;
188 SomethingCache c( mbStore );
189
190 SomethingPtr ptr;
191 if( time(NULL)%2 )
192 ptr = c.insert( new SubSomethingA("Hello", 55) ).cast<Something>();
193 else
194 ptr = c.insert( new SubSomethingB("Goodbye", "Things") ).cast<Something>();
195
196 Bu::Uuid id = ptr.getKey();
197 Bu::println("Something[%1]: %2").
198 arg( ptr.getKey() ).
199 arg( ptr->getName() );
200
201 Bu::println("has %1: %2").arg( id ).arg( c.has( id ) );
202 c.erase( ptr.getKey() );
203 Bu::println("has %1: %2").arg( id ).arg( c.has( id ) );
204 SomethingPtr b = ptr;
205
206 SomethingPtr p2 = c.insert( new SubSomethingA("new test", 123) ).cast<Something>();
207 id = p2.getKey();
208 Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) );
209 p2.unbind();
210 Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) );
211 c.erase( id );
212 Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) );
213
214 return 0;
215}
216
217/*
218int main( int argc, char *argv[] )
219{
220 Bu::File fStore("test.myr", Bu::File::Create|Bu::File::ReadWrite);
221 SomethingCache c( fStore );
222
223 for( int j = 1; j < argc; j++ )
224 {
225 SomethingPtr ptr = c.insert( new Something(argv[j]) );
226
227 Bu::println("Something[%1]: %2").
228 arg( ptr.getKey() ).
229 arg( ptr->getName() );
230 }
231
232 SomethingCache::KeyList lKeys = c.getKeys();
233 Bu::println("Count: %1").arg( lKeys.getSize() );
234 int j = 0;
235 for( SomethingCache::KeyList::iterator i = lKeys.begin(); i; i++ )
236 {
237 Bu::println(" - %1: '%2'").arg( *i ).arg( c.get( *i )->getName() );
238 if( ((j++)%2) )
239 c.erase( *i );
240 }
241 Bu::println("Count: %1").arg( c.getSize() );
242
243 c._debug();
244
245 SomethingPtr p2;
246 SomethingPtr p1( c.get( lKeys.first() ) );
247
248 c._debug();
249
250 {
251 SomethingPtr p2( p1 );
252 c._debug();
253 }
254
255 c._debug();
256
257 p2 = p1;
258
259 c._debug();
260
261 p1.unbind();
262
263 c._debug();
264
265 Bu::println("Name: %1").arg( p1->getName() );
266
267 p1.unbind();
268 p1.lock();
269 p1.unlock();
270
271 c._debug();
272
273 SomethingPtr p3 = c.getLazy( lKeys.first() );
274
275 c._debug();
276
277 {
278 SomethingPtr::Locker l( p3 );
279
280 Bu::println("Name again: %1").arg( p3->getName() );
281
282 p3->setName( p3->getName() + " - again" );
283 }
284
285 c.sync();
286
287 c._debug();
288
289 return 0;
290}
291*/