diff options
Diffstat (limited to 'src/tests/cache.cpp')
-rw-r--r-- | src/tests/cache.cpp | 260 |
1 files changed, 0 insertions, 260 deletions
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp deleted file mode 100644 index e9dbd86..0000000 --- a/src/tests/cache.cpp +++ /dev/null | |||
@@ -1,260 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include <stdio.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <sys/stat.h> | ||
11 | #include <sys/types.h> | ||
12 | #include <errno.h> | ||
13 | #include <unistd.h> | ||
14 | |||
15 | #include "bu/cache.h" | ||
16 | #include "bu/file.h" | ||
17 | #include "bu/string.h" | ||
18 | #include "bu/cachecalc.h" | ||
19 | |||
20 | class Bob | ||
21 | { | ||
22 | public: | ||
23 | Bob() | ||
24 | { | ||
25 | TRACE(); | ||
26 | } | ||
27 | |||
28 | Bob( int i ) : | ||
29 | iInt( i ) | ||
30 | { | ||
31 | TRACE( i ); | ||
32 | } | ||
33 | |||
34 | virtual ~Bob() | ||
35 | { | ||
36 | TRACE(); | ||
37 | } | ||
38 | |||
39 | void setInt( int i ) | ||
40 | { | ||
41 | TRACE( i ); | ||
42 | iInt = i; | ||
43 | } | ||
44 | |||
45 | long getKey() const | ||
46 | { | ||
47 | TRACE( i ); | ||
48 | return iInt; | ||
49 | } | ||
50 | |||
51 | int getInt() | ||
52 | { | ||
53 | return iInt; | ||
54 | } | ||
55 | |||
56 | int iInt; | ||
57 | }; | ||
58 | |||
59 | namespace Bu { | ||
60 | template<> | ||
61 | void __tracer_format<Bob*>( Bob* const &c ) | ||
62 | { | ||
63 | printf("%08X=%d", (uint32_t)c, c->getInt() ); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | class BobStore : public Bu::CacheStore<long, Bob> | ||
68 | { | ||
69 | public: | ||
70 | BobStore() : | ||
71 | cLastId( 0 ) | ||
72 | { | ||
73 | TRACE(); | ||
74 | if( access( "bobcache/last", R_OK|W_OK ) ) | ||
75 | { | ||
76 | mkdir("bobcache", 0755 ); | ||
77 | Bu::File f("bobcache/last", Bu::File::Write|Bu::File::Create ); | ||
78 | f.write("0", 1); | ||
79 | printf("Initializing cache: %s\n", strerror( errno ) ); | ||
80 | } | ||
81 | else | ||
82 | { | ||
83 | cLastId = readNum("bobcache/last"); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | ~BobStore() | ||
88 | { | ||
89 | TRACE(); | ||
90 | writeNum("bobcache/last", cLastId ); | ||
91 | } | ||
92 | |||
93 | long readNum( const Bu::String &sFile ) | ||
94 | { | ||
95 | TRACE( sFile ); | ||
96 | Bu::File f( sFile, Bu::File::Read ); | ||
97 | char buf[80]; | ||
98 | buf[f.read( buf, 80 )] = '\0'; | ||
99 | return strtol( buf, NULL, 0 ); | ||
100 | } | ||
101 | |||
102 | void writeNum( const Bu::String &sFile, long num ) | ||
103 | { | ||
104 | TRACE( sFile, num ); | ||
105 | Bu::File f( sFile, | ||
106 | Bu::File::Write|Bu::File::Create|Bu::File::Truncate | ||
107 | ); | ||
108 | f.write( Bu::String("%1").arg( num ) ); | ||
109 | } | ||
110 | |||
111 | virtual void sync( Bob *, const long & ) | ||
112 | { | ||
113 | } | ||
114 | |||
115 | virtual void sync() | ||
116 | { | ||
117 | } | ||
118 | |||
119 | virtual bool has( const long & ) | ||
120 | { | ||
121 | return true; | ||
122 | } | ||
123 | |||
124 | virtual Bob *load( const long &key ) | ||
125 | { | ||
126 | TRACE( key ); | ||
127 | Bu::String sDest = Bu::String("bobcache/%1").arg( key ); | ||
128 | return new Bob( readNum( sDest ) ); | ||
129 | } | ||
130 | |||
131 | virtual void unload( Bob *pObj, const long &key ) | ||
132 | { | ||
133 | TRACE( pObj, key ); | ||
134 | Bu::String sDest = Bu::String("bobcache/%1").arg( key ); | ||
135 | writeNum( sDest, pObj->getInt() ); | ||
136 | delete pObj; | ||
137 | } | ||
138 | |||
139 | virtual long create( Bob *rSrc ) | ||
140 | { | ||
141 | TRACE( rSrc ); | ||
142 | long id = ++cLastId; | ||
143 | Bu::String sDest = Bu::String("bobcache/%1").arg( id ); | ||
144 | writeNum( sDest, rSrc->getInt() ); | ||
145 | return id; | ||
146 | } | ||
147 | |||
148 | virtual void destroy( Bob *pObj, const long &key ) | ||
149 | { | ||
150 | TRACE( pObj, key ); | ||
151 | Bu::String sDest = Bu::String("bobcache/%1").arg( key ); | ||
152 | if( !access( sDest.getStr(), F_OK ) ) | ||
153 | unlink( sDest.getStr() ); | ||
154 | delete pObj; | ||
155 | } | ||
156 | |||
157 | virtual void destroy( const long &key ) | ||
158 | { | ||
159 | TRACE( pObj, key ); | ||
160 | Bu::String sDest = Bu::String("bobcache/%1").arg( key ); | ||
161 | if( !access( sDest.getStr(), F_OK ) ) | ||
162 | unlink( sDest.getStr() ); | ||
163 | } | ||
164 | |||
165 | private: | ||
166 | long cLastId; | ||
167 | }; | ||
168 | |||
169 | class BobCalc : public Bu::CacheCalc<long, Bob> | ||
170 | { | ||
171 | public: | ||
172 | BobCalc() | ||
173 | { | ||
174 | } | ||
175 | |||
176 | virtual ~BobCalc() | ||
177 | { | ||
178 | } | ||
179 | |||
180 | virtual void onLoad( Bob *, const long & ) | ||
181 | { | ||
182 | } | ||
183 | |||
184 | virtual void onUnload( Bob *, const long & ) | ||
185 | { | ||
186 | } | ||
187 | |||
188 | virtual void onDestroy( Bob *, const long & ) | ||
189 | { | ||
190 | } | ||
191 | |||
192 | virtual void onDestroy( const long & ) | ||
193 | { | ||
194 | } | ||
195 | |||
196 | virtual bool shouldSync( Bob *, const long &, time_t ) | ||
197 | { | ||
198 | return false; | ||
199 | } | ||
200 | |||
201 | private: | ||
202 | |||
203 | }; | ||
204 | |||
205 | int main( int argc, char *argv[] ) | ||
206 | { | ||
207 | TRACE( argc, argv ); | ||
208 | typedef Bu::Cache<long, Bob> BobCache; | ||
209 | typedef BobCache::Ptr BobPtr; | ||
210 | |||
211 | if( argc < 3 ) | ||
212 | { | ||
213 | printf("Try: %s [crudl] [<id/value>]\n\n", argv[0] ); | ||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | BobCache cBob( new BobCalc(), new BobStore() ); | ||
218 | switch( argv[1][0] ) | ||
219 | { | ||
220 | case 'c': | ||
221 | { | ||
222 | BobCache::Ptr pBob = cBob.insert( | ||
223 | new Bob( strtol( argv[2], NULL, 0 ) ) | ||
224 | ); | ||
225 | printf("Key = %ld\n", pBob.getKey() ); | ||
226 | } | ||
227 | return 0; | ||
228 | |||
229 | case 'r': | ||
230 | { | ||
231 | BobCache::Ptr pBob = cBob.get( strtol( argv[2], NULL, 0 ) ); | ||
232 | printf("Value = %d\n", pBob->getInt() ); | ||
233 | } | ||
234 | return 0; | ||
235 | |||
236 | case 'u': | ||
237 | { | ||
238 | BobCache::Ptr pBob = cBob.get( strtol( argv[2], NULL, 0 ) ); | ||
239 | pBob->setInt( strtol( argv[3], NULL, 0 ) ); | ||
240 | } | ||
241 | return 0; | ||
242 | |||
243 | case 'd': | ||
244 | { | ||
245 | cBob.erase( strtol( argv[2], NULL, 0 ) ); | ||
246 | } | ||
247 | return 0; | ||
248 | |||
249 | case 'l': | ||
250 | { | ||
251 | BobCache::Ptr pBob = cBob.getLazy( strtol( argv[2], NULL, 0 ) ); | ||
252 | printf("isBound: %s\n", pBob.isBound()?"yes":"no"); | ||
253 | printf("Value = %d\n", pBob->getInt() ); | ||
254 | printf("isBound: %s\n", pBob.isBound()?"yes":"no"); | ||
255 | } | ||
256 | return 0; | ||
257 | } | ||
258 | |||
259 | } | ||
260 | |||