diff options
author | Mike Buland <eichlan@xagasoft.com> | 2008-12-20 02:01:44 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2008-12-20 02:01:44 +0000 |
commit | b7a687b08e32adafbb609bec7aa79b54f161ee4c (patch) | |
tree | 9c0214eb7fa5c69619bb4226cb292d19445b92df | |
parent | 5db154c3fb36a677c551e39c3c6661207eb51778 (diff) | |
download | libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.tar.gz libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.tar.bz2 libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.tar.xz libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.zip |
All of the basic, core workings of the Cache are complete, and tested. Even
added some more tests and whatnot. A lot happened, but I can't remember
everything.
Also, Bu::File reports errors in more error cases.
Diffstat (limited to '')
-rw-r--r-- | src/cache.h | 55 | ||||
-rw-r--r-- | src/cachecalc.h | 26 | ||||
-rw-r--r-- | src/file.cpp | 10 | ||||
-rw-r--r-- | src/list.h | 16 | ||||
-rw-r--r-- | src/tests/cache.cpp | 27 | ||||
-rw-r--r-- | src/trace.cpp | 1 | ||||
-rw-r--r-- | src/unit/hash.cpp | 21 |
7 files changed, 128 insertions, 28 deletions
diff --git a/src/cache.h b/src/cache.h index cc13fc8..313d9fa 100644 --- a/src/cache.h +++ b/src/cache.h | |||
@@ -7,7 +7,6 @@ | |||
7 | #include "bu/cachestore.h" | 7 | #include "bu/cachestore.h" |
8 | #include "bu/cachecalc.h" | 8 | #include "bu/cachecalc.h" |
9 | 9 | ||
10 | #define BU_TRACE | ||
11 | #include "bu/trace.h" | 10 | #include "bu/trace.h" |
12 | 11 | ||
13 | namespace Bu | 12 | namespace Bu |
@@ -32,10 +31,11 @@ namespace Bu | |||
32 | typedef Bu::Hash<keytype, CacheEntry> CidHash; | 31 | typedef Bu::Hash<keytype, CacheEntry> CidHash; |
33 | 32 | ||
34 | public: | 33 | public: |
35 | Cache() : | 34 | Cache( Calc &rCalc ) : |
36 | pCalc( NULL ) | 35 | rCalc( rCalc ) |
37 | { | 36 | { |
38 | TRACE(); | 37 | TRACE(); |
38 | rCalc.setCache( this ); | ||
39 | } | 39 | } |
40 | 40 | ||
41 | virtual ~Cache() | 41 | virtual ~Cache() |
@@ -50,7 +50,7 @@ namespace Bu | |||
50 | __tracer_format( i.getKey() ); | 50 | __tracer_format( i.getKey() ); |
51 | printf("!\n"); | 51 | printf("!\n"); |
52 | } | 52 | } |
53 | if( pCalc ) pCalc->onUnload( | 53 | rCalc.onUnload( |
54 | i.getValue().pData, | 54 | i.getValue().pData, |
55 | i.getKey() | 55 | i.getKey() |
56 | ); | 56 | ); |
@@ -78,16 +78,6 @@ namespace Bu | |||
78 | lStore.prepend( pHand ); | 78 | lStore.prepend( pHand ); |
79 | } | 79 | } |
80 | 80 | ||
81 | void setCalc( Calc *pCalc ) | ||
82 | { | ||
83 | TRACE(); | ||
84 | if( this->pCalc ) | ||
85 | { | ||
86 | delete this->pCalc; | ||
87 | } | ||
88 | this->pCalc = pCalc; | ||
89 | } | ||
90 | |||
91 | Ptr insert( obtype *pData ) | 81 | Ptr insert( obtype *pData ) |
92 | { | 82 | { |
93 | TRACE( pData ); | 83 | TRACE( pData ); |
@@ -95,7 +85,7 @@ namespace Bu | |||
95 | keytype k = lStore.first()->create( pData ); | 85 | keytype k = lStore.first()->create( pData ); |
96 | hEnt.insert( k, e ); | 86 | hEnt.insert( k, e ); |
97 | 87 | ||
98 | if( pCalc ) pCalc->onLoad( pData, k ); | 88 | rCalc.onLoad( pData, k ); |
99 | 89 | ||
100 | return Ptr( *this, pData, k ); | 90 | return Ptr( *this, pData, k ); |
101 | } | 91 | } |
@@ -108,6 +98,7 @@ namespace Bu | |||
108 | } | 98 | } |
109 | catch( Bu::HashException &e ) { | 99 | catch( Bu::HashException &e ) { |
110 | CacheEntry e = {lStore.first()->load( cId ), 0}; | 100 | CacheEntry e = {lStore.first()->load( cId ), 0}; |
101 | rCalc.onLoad( e.pData, cId ); | ||
111 | hEnt.insert( cId, e ); | 102 | hEnt.insert( cId, e ); |
112 | return Ptr( *this, e.pData, cId ); | 103 | return Ptr( *this, e.pData, cId ); |
113 | } | 104 | } |
@@ -115,9 +106,33 @@ namespace Bu | |||
115 | 106 | ||
116 | int getRefCount( const keytype &cId ) | 107 | int getRefCount( const keytype &cId ) |
117 | { | 108 | { |
109 | TRACE( cId ); | ||
118 | return hEnt.get( cId ).iRefs; | 110 | return hEnt.get( cId ).iRefs; |
119 | } | 111 | } |
120 | 112 | ||
113 | void unload( const keytype &cId ) | ||
114 | { | ||
115 | TRACE( cId ); | ||
116 | try { | ||
117 | if( hEnt.get( cId ).iRefs > 0 ) | ||
118 | { | ||
119 | printf("Shouldn't delete, references still exist!\n"); | ||
120 | return; | ||
121 | } | ||
122 | } | ||
123 | catch( Bu::HashException &e ) { | ||
124 | // It's not here? Eh, return. | ||
125 | return; | ||
126 | } | ||
127 | obtype *pObj = hEnt.get( cId ).pData; | ||
128 | rCalc.onUnload( pObj, cId ); | ||
129 | hEnt.erase( cId ); | ||
130 | |||
131 | // The unload has to happen last just in case cId is a reference | ||
132 | // to data that is about to be deleted from memory by the unload. | ||
133 | lStore.first()->unload( pObj, cId ); | ||
134 | } | ||
135 | |||
121 | void erase( const keytype &cId ) | 136 | void erase( const keytype &cId ) |
122 | { | 137 | { |
123 | TRACE( cId ); | 138 | TRACE( cId ); |
@@ -132,18 +147,12 @@ namespace Bu | |||
132 | get( cId ); | 147 | get( cId ); |
133 | } | 148 | } |
134 | 149 | ||
135 | if( pCalc ) pCalc->onUnload( hEnt.get( cId ).pData, cId ); | 150 | rCalc.onUnload( hEnt.get( cId ).pData, cId ); |
136 | 151 | ||
137 | lStore.first()->destroy( hEnt.get( cId ).pData, cId ); | 152 | lStore.first()->destroy( hEnt.get( cId ).pData, cId ); |
138 | hEnt.erase( cId ); | 153 | hEnt.erase( cId ); |
139 | } | 154 | } |
140 | 155 | ||
141 | int getRefCnt( keytype cId ) | ||
142 | { | ||
143 | TRACE( cId ); | ||
144 | return hEnt.get( cId ).iRefs; | ||
145 | } | ||
146 | |||
147 | private: | 156 | private: |
148 | void incRef( keytype cId ) | 157 | void incRef( keytype cId ) |
149 | { | 158 | { |
@@ -161,7 +170,7 @@ namespace Bu | |||
161 | private: | 170 | private: |
162 | CidHash hEnt; | 171 | CidHash hEnt; |
163 | StoreList lStore; | 172 | StoreList lStore; |
164 | Calc *pCalc; | 173 | Calc &rCalc; |
165 | }; | 174 | }; |
166 | }; | 175 | }; |
167 | 176 | ||
diff --git a/src/cachecalc.h b/src/cachecalc.h index 289c4ac..dd9712f 100644 --- a/src/cachecalc.h +++ b/src/cachecalc.h | |||
@@ -1,25 +1,49 @@ | |||
1 | #ifndef BU_CACHE_CALC_H | 1 | #ifndef BU_CACHE_CALC_H |
2 | #define BU_CACHE_CALC_H | 2 | #define BU_CACHE_CALC_H |
3 | 3 | ||
4 | #include "bu/trace.h" | ||
5 | |||
4 | namespace Bu | 6 | namespace Bu |
5 | { | 7 | { |
8 | template<class obtype, class keytype> class Cache; | ||
9 | |||
6 | template<class obtype, class keytype> | 10 | template<class obtype, class keytype> |
7 | class CacheCalc | 11 | class CacheCalc |
8 | { | 12 | { |
13 | friend class Cache<obtype, keytype>; | ||
14 | private: | ||
15 | typedef Cache<obtype, keytype> MyCache; | ||
9 | public: | 16 | public: |
10 | CacheCalc() | 17 | CacheCalc() : |
18 | pCache( (MyCache *)0 ) | ||
11 | { | 19 | { |
20 | TRACE(); | ||
12 | } | 21 | } |
13 | 22 | ||
14 | virtual ~CacheCalc() | 23 | virtual ~CacheCalc() |
15 | { | 24 | { |
25 | TRACE(); | ||
16 | } | 26 | } |
17 | 27 | ||
18 | virtual void onLoad( obtype *pSrc, const keytype &key )=0; | 28 | virtual void onLoad( obtype *pSrc, const keytype &key )=0; |
19 | virtual void onUnload( obtype *pSrc, const keytype &key )=0; | 29 | virtual void onUnload( obtype *pSrc, const keytype &key )=0; |
20 | virtual void onTick() { }; | 30 | virtual void onTick() { }; |
21 | 31 | ||
32 | protected: | ||
33 | MyCache *getCache() | ||
34 | { | ||
35 | TRACE(); | ||
36 | return pCache; | ||
37 | } | ||
38 | |||
22 | private: | 39 | private: |
40 | void setCache( MyCache *pCache ) | ||
41 | { | ||
42 | TRACE(); | ||
43 | this->pCache = pCache; | ||
44 | } | ||
45 | |||
46 | MyCache *pCache; | ||
23 | }; | 47 | }; |
24 | }; | 48 | }; |
25 | 49 | ||
diff --git a/src/file.cpp b/src/file.cpp index 6e9d47e..7c18a06 100644 --- a/src/file.cpp +++ b/src/file.cpp | |||
@@ -54,7 +54,10 @@ size_t Bu::File::read( void *pBuf, size_t nBytes ) | |||
54 | if( fd < 0 ) | 54 | if( fd < 0 ) |
55 | throw FileException("File not open."); | 55 | throw FileException("File not open."); |
56 | 56 | ||
57 | return ::read( fd, pBuf, nBytes ); | 57 | ssize_t iRead = ::read( fd, pBuf, nBytes ); |
58 | if( iRead < 0 ) | ||
59 | throw FileException( errno, "%s", strerror( errno ) ); | ||
60 | return iRead; | ||
58 | } | 61 | } |
59 | 62 | ||
60 | size_t Bu::File::write( const void *pBuf, size_t nBytes ) | 63 | size_t Bu::File::write( const void *pBuf, size_t nBytes ) |
@@ -62,7 +65,10 @@ size_t Bu::File::write( const void *pBuf, size_t nBytes ) | |||
62 | if( fd < 0 ) | 65 | if( fd < 0 ) |
63 | throw FileException("File not open."); | 66 | throw FileException("File not open."); |
64 | 67 | ||
65 | return ::write( fd, pBuf, nBytes ); | 68 | ssize_t iWrote = ::write( fd, pBuf, nBytes ); |
69 | if( iWrote < 0 ) | ||
70 | throw FileException( errno, "%s", strerror( errno ) ); | ||
71 | return iWrote; | ||
66 | } | 72 | } |
67 | 73 | ||
68 | long Bu::File::tell() | 74 | long Bu::File::tell() |
@@ -532,6 +532,22 @@ namespace Bu | |||
532 | } | 532 | } |
533 | 533 | ||
534 | /** | 534 | /** |
535 | * Erase an item from the list if you already know the item. | ||
536 | *@param ob The item to find and erase. | ||
537 | */ | ||
538 | void erase( const value &v ) | ||
539 | { | ||
540 | for( iterator i = begin(); i != end(); i++ ) | ||
541 | { | ||
542 | if( (*i) == v ) | ||
543 | { | ||
544 | erase( i ); | ||
545 | return; | ||
546 | } | ||
547 | } | ||
548 | } | ||
549 | |||
550 | /** | ||
535 | * Get the current size of the list. | 551 | * Get the current size of the list. |
536 | *@returns (int) The current size of the list. | 552 | *@returns (int) The current size of the list. |
537 | */ | 553 | */ |
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 18e6a95..12b5bf4 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp | |||
@@ -7,6 +7,7 @@ | |||
7 | #include "bu/cache.h" | 7 | #include "bu/cache.h" |
8 | #include "bu/file.h" | 8 | #include "bu/file.h" |
9 | #include "bu/fstring.h" | 9 | #include "bu/fstring.h" |
10 | #include "bu/cachecalc.h" | ||
10 | 11 | ||
11 | class Bob | 12 | class Bob |
12 | { | 13 | { |
@@ -136,6 +137,29 @@ private: | |||
136 | long cLastId; | 137 | long cLastId; |
137 | }; | 138 | }; |
138 | 139 | ||
140 | class BobCalc : public Bu::CacheCalc<Bob, long> | ||
141 | { | ||
142 | public: | ||
143 | BobCalc() | ||
144 | { | ||
145 | } | ||
146 | |||
147 | virtual ~BobCalc() | ||
148 | { | ||
149 | } | ||
150 | |||
151 | virtual void onLoad( Bob *, const long & ) | ||
152 | { | ||
153 | } | ||
154 | |||
155 | virtual void onUnload( Bob *, const long & ) | ||
156 | { | ||
157 | } | ||
158 | |||
159 | private: | ||
160 | |||
161 | }; | ||
162 | |||
139 | int main( int argc, char *argv[] ) | 163 | int main( int argc, char *argv[] ) |
140 | { | 164 | { |
141 | TRACE( argc, argv ); | 165 | TRACE( argc, argv ); |
@@ -148,7 +172,8 @@ int main( int argc, char *argv[] ) | |||
148 | return 0; | 172 | return 0; |
149 | } | 173 | } |
150 | 174 | ||
151 | BobCache cBob; | 175 | BobCalc cc; |
176 | BobCache cBob( cc ); | ||
152 | cBob.appendStore( new BobStore() ); | 177 | cBob.appendStore( new BobStore() ); |
153 | switch( argv[1][0] ) | 178 | switch( argv[1][0] ) |
154 | { | 179 | { |
diff --git a/src/trace.cpp b/src/trace.cpp index 242d110..b5f11f1 100644 --- a/src/trace.cpp +++ b/src/trace.cpp | |||
@@ -1,4 +1,3 @@ | |||
1 | #define BU_TRACE | ||
2 | #include "bu/trace.h" | 1 | #include "bu/trace.h" |
3 | 2 | ||
4 | void Bu::__tracer( const char *pf ) | 3 | void Bu::__tracer( const char *pf ) |
diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp index de4edd1..77160e3 100644 --- a/src/unit/hash.cpp +++ b/src/unit/hash.cpp | |||
@@ -24,6 +24,7 @@ public: | |||
24 | addTest( Unit::insert1 ); | 24 | addTest( Unit::insert1 ); |
25 | addTest( Unit::insert2 ); | 25 | addTest( Unit::insert2 ); |
26 | addTest( Unit::probe1 ); | 26 | addTest( Unit::probe1 ); |
27 | addTest( Unit::erase1 ); | ||
27 | } | 28 | } |
28 | 29 | ||
29 | virtual ~Unit() | 30 | virtual ~Unit() |
@@ -66,6 +67,26 @@ public: | |||
66 | unitTest( h3["Hi"].getValue() = "Yo" ); | 67 | unitTest( h3["Hi"].getValue() = "Yo" ); |
67 | unitTest( h3["Bye"].getValue() = "Later" ); | 68 | unitTest( h3["Bye"].getValue() = "Later" ); |
68 | } | 69 | } |
70 | |||
71 | void erase1() | ||
72 | { | ||
73 | StrIntHash h; | ||
74 | h.insert("Number 1", 1 ); | ||
75 | h.insert("Number 2", 2 ); | ||
76 | h.insert("Number 3", 3 ); | ||
77 | h.erase("Number 2"); | ||
78 | h.get("Number 3"); | ||
79 | try { | ||
80 | h.get("Number 2"); | ||
81 | unitFailed("h.get(\"Number 2\") should have thrown an exception."); | ||
82 | } catch( Bu::HashException &e ) { } | ||
83 | |||
84 | printf("\n"); | ||
85 | for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) | ||
86 | { | ||
87 | printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); | ||
88 | } | ||
89 | } | ||
69 | }; | 90 | }; |
70 | 91 | ||
71 | int main( int argc, char *argv[] ) | 92 | int main( int argc, char *argv[] ) |