aboutsummaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/tests/cache.cpp125
1 files changed, 100 insertions, 25 deletions
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp
index 1cc008a..18e6a95 100644
--- a/src/tests/cache.cpp
+++ b/src/tests/cache.cpp
@@ -1,9 +1,12 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h>
2#include <sys/stat.h> 3#include <sys/stat.h>
3#include <sys/types.h> 4#include <sys/types.h>
4#include <errno.h> 5#include <errno.h>
5 6
6#include "bu/cache.h" 7#include "bu/cache.h"
8#include "bu/file.h"
9#include "bu/fstring.h"
7 10
8class Bob 11class Bob
9{ 12{
@@ -13,6 +16,12 @@ public:
13 TRACE(); 16 TRACE();
14 } 17 }
15 18
19 Bob( int i ) :
20 iInt( i )
21 {
22 TRACE( i );
23 }
24
16 virtual ~Bob() 25 virtual ~Bob()
17 { 26 {
18 TRACE(); 27 TRACE();
@@ -20,7 +29,7 @@ public:
20 29
21 void setInt( int i ) 30 void setInt( int i )
22 { 31 {
23 TRACE(); 32 TRACE( i );
24 iInt = i; 33 iInt = i;
25 } 34 }
26 35
@@ -29,15 +38,17 @@ public:
29 return iInt; 38 return iInt;
30 } 39 }
31 40
32 long getCacheId() const
33 {
34 TRACE();
35 return 0;
36 }
37
38 int iInt; 41 int iInt;
39}; 42};
40 43
44namespace Bu {
45 template<>
46 void __tracer_format<Bob*>( Bob* const &c )
47 {
48 printf("%08X=%d", (uint32_t)c, c->getInt() );
49 }
50}
51
41class BobStore : public Bu::CacheStore<Bob, long> 52class BobStore : public Bu::CacheStore<Bob, long>
42{ 53{
43public: 54public:
@@ -45,34 +56,79 @@ public:
45 cLastId( 0 ) 56 cLastId( 0 )
46 { 57 {
47 TRACE(); 58 TRACE();
59 if( access( "bobcache/last", R_OK|W_OK ) )
60 {
61 mkdir("bobcache", 0755 );
62 Bu::File f("bobcache/last", Bu::File::Write|Bu::File::Create );
63 f.write("0", 1);
64 printf("Initializing cache: %s\n", strerror( errno ) );
65 }
66 else
67 {
68 cLastId = readNum("bobcache/last");
69 }
48 } 70 }
49 71
50 ~BobStore() 72 ~BobStore()
51 { 73 {
52 TRACE(); 74 TRACE();
75 writeNum("bobcache/last", cLastId );
76 }
77
78 long readNum( const Bu::FString &sFile )
79 {
80 TRACE( sFile );
81 Bu::File f( sFile, Bu::File::Read );
82 char buf[80];
83 buf[f.read( buf, 80 )] = '\0';
84 return strtol( buf, NULL, 0 );
85 }
86
87 void writeNum( const Bu::FString &sFile, long num )
88 {
89 TRACE( sFile, num );
90 Bu::File f( sFile,
91 Bu::File::Write|Bu::File::Create|Bu::File::Truncate
92 );
93 Bu::FString s;
94 s.format("%d", num );
95 f.write( s );
53 } 96 }
54 97
55 virtual Bob *load( const long &key ) 98 virtual Bob *load( const long &key )
56 { 99 {
57 TRACE(); 100 TRACE( key );
58 return NULL; 101 Bu::FString sDest;
102 sDest.format("bobcache/%d", key );
103 return new Bob( readNum( sDest ) );
59 } 104 }
60 105
61 virtual void unload( Bob *pObj ) 106 virtual void unload( Bob *pObj, const long &key )
62 { 107 {
63 TRACE(); 108 TRACE( pObj, key );
109 Bu::FString sDest;
110 sDest.format("bobcache/%d", key );
111 writeNum( sDest, pObj->getInt() );
64 delete pObj; 112 delete pObj;
65 } 113 }
66 114
67 virtual long create( Bob *rSrc ) 115 virtual long create( Bob *rSrc )
68 { 116 {
69 TRACE(); 117 TRACE( rSrc );
70 return ++cLastId; 118 long id = ++cLastId;
119 Bu::FString sDest;
120 sDest.format("bobcache/%d", id );
121 writeNum( sDest, rSrc->getInt() );
122 return id;
71 } 123 }
72 124
73 virtual void destroy( Bob *pObj, const long &key ) 125 virtual void destroy( Bob *pObj, const long &key )
74 { 126 {
75 TRACE(); 127 TRACE( pObj, key );
128 Bu::FString sDest;
129 sDest.format("bobcache/%d", key );
130 if( !access( sDest.getStr(), F_OK ) )
131 unlink( sDest.getStr() );
76 delete pObj; 132 delete pObj;
77 } 133 }
78 134
@@ -82,28 +138,47 @@ private:
82 138
83int main( int argc, char *argv[] ) 139int main( int argc, char *argv[] )
84{ 140{
85 TRACE(); 141 TRACE( argc, argv );
142 typedef Bu::Cache<Bob, long> BobCache;
143 typedef BobCache::Ptr BobPtr;
144
86 if( argc < 3 ) 145 if( argc < 3 )
87 { 146 {
88 printf("Try: %s [icufd] [<id/value>]\n\n", argv[0] ); 147 printf("Try: %s [crud] [<id/value>]\n\n", argv[0] );
89 return 0; 148 return 0;
90 } 149 }
91 150
151 BobCache cBob;
152 cBob.appendStore( new BobStore() );
92 switch( argv[1][0] ) 153 switch( argv[1][0] )
93 { 154 {
94 case 'i':
95 mkdir("bobcache", 0755 );
96 printf("Initialized cache: %s\n", strerror( errno ) );
97 return 0;
98
99 case 'c': 155 case 'c':
100 typedef Bu::Cache<Bob, long> BobCache; 156 {
101 typedef BobCache::Ptr BobPtr; 157 BobCache::Ptr pBob = cBob.insert(
158 new Bob( strtol( argv[2], NULL, 0 ) )
159 );
160 printf("Key = %ld\n", pBob.getKey() );
161 }
162 return 0;
102 163
103 BobCache cBob; 164 case 'r':
165 {
166 BobCache::Ptr pBob = cBob.get( strtol( argv[2], NULL, 0 ) );
167 printf("Value = %d\n", pBob->getInt() );
168 }
169 return 0;
104 170
105 cBob.appendStore( new BobStore() ); 171 case 'u':
172 {
173 BobCache::Ptr pBob = cBob.get( strtol( argv[2], NULL, 0 ) );
174 pBob->setInt( strtol( argv[3], NULL, 0 ) );
175 }
176 return 0;
106 177
178 case 'd':
179 {
180 cBob.erase( strtol( argv[2], NULL, 0 ) );
181 }
107 return 0; 182 return 0;
108 } 183 }
109 184