diff options
author | Mike Buland <eichlan@xagasoft.com> | 2014-01-22 16:28:46 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2014-01-22 16:28:46 +0000 |
commit | 86e37bec7b2101555635201f83352c0e054f1849 (patch) | |
tree | 4d2a3652689ac7b615db82bdab520c2ed42dd705 /src | |
parent | 44a228640c782e46baf528749b5776714749ef2d (diff) | |
download | libbu++-86e37bec7b2101555635201f83352c0e054f1849.tar.gz libbu++-86e37bec7b2101555635201f83352c0e054f1849.tar.bz2 libbu++-86e37bec7b2101555635201f83352c0e054f1849.tar.xz libbu++-86e37bec7b2101555635201f83352c0e054f1849.zip |
Updated the cache system. It now ensures that objects are initialized with
cache information before they are deserialized from storage. This changed the
signature of the cache loading template function, but the new function isn't
harder to use, and provides the key information as well.
Diffstat (limited to 'src')
-rw-r--r-- | src/stable/stdstream.cpp | 4 | ||||
-rw-r--r-- | src/tests/cache.cpp | 6 | ||||
-rw-r--r-- | src/unstable/cachebase.h | 28 | ||||
-rw-r--r-- | src/unstable/cacheobject.h | 34 | ||||
-rw-r--r-- | src/unstable/myriadcache.h | 8 |
5 files changed, 63 insertions, 17 deletions
diff --git a/src/stable/stdstream.cpp b/src/stable/stdstream.cpp index a622559..5bcc643 100644 --- a/src/stable/stdstream.cpp +++ b/src/stable/stdstream.cpp | |||
@@ -50,12 +50,12 @@ void Bu::StdStream::setPosEnd( Bu::size ) | |||
50 | 50 | ||
51 | bool Bu::StdStream::isEos() | 51 | bool Bu::StdStream::isEos() |
52 | { | 52 | { |
53 | return false; | 53 | return feof( stdin ) != 0; |
54 | } | 54 | } |
55 | 55 | ||
56 | bool Bu::StdStream::isOpen() | 56 | bool Bu::StdStream::isOpen() |
57 | { | 57 | { |
58 | return true; | 58 | return feof( stdin ) != 0; |
59 | } | 59 | } |
60 | 60 | ||
61 | void Bu::StdStream::flush() | 61 | void Bu::StdStream::flush() |
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 7982b2d..4751559 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp | |||
@@ -145,7 +145,7 @@ namespace Bu | |||
145 | } | 145 | } |
146 | 146 | ||
147 | template<> | 147 | template<> |
148 | Something *_cacheObjectLoad<Something>( Bu::Stream &s ) | 148 | Something *_cacheObjectLoad<Bu::Uuid, Something>( Bu::CacheObject<Bu::Uuid, Something>::Initializer &initObj, const Bu::Uuid &rKey, Bu::Stream &s ) |
149 | { | 149 | { |
150 | Bu::Archive ar( s, Bu::Archive::load ); | 150 | Bu::Archive ar( s, Bu::Archive::load ); |
151 | uint8_t uType; | 151 | uint8_t uType; |
@@ -154,7 +154,7 @@ namespace Bu | |||
154 | { | 154 | { |
155 | case 1: | 155 | case 1: |
156 | { | 156 | { |
157 | SubSomethingA *ret = new SubSomethingA(); | 157 | SubSomethingA *ret = initObj(new SubSomethingA()); |
158 | ar >> *ret; | 158 | ar >> *ret; |
159 | return ret; | 159 | return ret; |
160 | } | 160 | } |
@@ -162,7 +162,7 @@ namespace Bu | |||
162 | 162 | ||
163 | case 2: | 163 | case 2: |
164 | { | 164 | { |
165 | SubSomethingB *ret = new SubSomethingB(); | 165 | SubSomethingB *ret = initObj(new SubSomethingB()); |
166 | ar >> *ret; | 166 | ar >> *ret; |
167 | return ret; | 167 | return ret; |
168 | } | 168 | } |
diff --git a/src/unstable/cachebase.h b/src/unstable/cachebase.h index af2884c..bb543dd 100644 --- a/src/unstable/cachebase.h +++ b/src/unstable/cachebase.h | |||
@@ -328,6 +328,7 @@ namespace Bu | |||
328 | 328 | ||
329 | typedef CacheEntry<keytype, obtype> Entry; | 329 | typedef CacheEntry<keytype, obtype> Entry; |
330 | typedef Bu::List<keytype> KeyList; | 330 | typedef Bu::List<keytype> KeyList; |
331 | typedef CacheObject<keytype, obtype> ObjectType; | ||
331 | 332 | ||
332 | CachePtr<keytype, obtype> insert( obtype *pObject ) | 333 | CachePtr<keytype, obtype> insert( obtype *pObject ) |
333 | { | 334 | { |
@@ -466,7 +467,10 @@ namespace Bu | |||
466 | virtual void _create( const obtype *o )=0; | 467 | virtual void _create( const obtype *o )=0; |
467 | virtual void _erase( const keytype &k )=0; | 468 | virtual void _erase( const keytype &k )=0; |
468 | 469 | ||
469 | virtual obtype *_load( const keytype &k )=0; | 470 | virtual obtype *_load( |
471 | typename Bu::CacheObject<keytype, obtype>::Initializer &initObj, | ||
472 | const keytype &k | ||
473 | )=0; | ||
470 | virtual void _save( const obtype *o )=0; | 474 | virtual void _save( const obtype *o )=0; |
471 | 475 | ||
472 | virtual void _sync()=0; | 476 | virtual void _sync()=0; |
@@ -481,7 +485,10 @@ namespace Bu | |||
481 | hChanged.insert( pObject->getKey(), true ); | 485 | hChanged.insert( pObject->getKey(), true ); |
482 | mCacheEntry.unlockWrite(); | 486 | mCacheEntry.unlockWrite(); |
483 | _create( pObject ); | 487 | _create( pObject ); |
484 | pObject->setCache( this, pEnt ); | 488 | // I'm not sold on the ordering here...or the fact we're not |
489 | // using th initilizer | ||
490 | pObject->setCache( this ); | ||
491 | pObject->setCacheEntry( pEnt ); | ||
485 | 492 | ||
486 | return pEnt; | 493 | return pEnt; |
487 | } | 494 | } |
@@ -497,9 +504,12 @@ namespace Bu | |||
497 | catch(...) | 504 | catch(...) |
498 | { | 505 | { |
499 | // try to load the object from the backing store | 506 | // try to load the object from the backing store |
500 | obtype *pObject = _load( k ); | 507 | typename ObjectType::Initializer initObj( |
508 | this | ||
509 | ); | ||
510 | obtype *pObject = _load( initObj, k ); | ||
501 | pEnt = new Entry( pObject ); | 511 | pEnt = new Entry( pObject ); |
502 | pObject->setCache( this, pEnt ); | 512 | pObject->setCacheEntry( pEnt ); |
503 | Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); | 513 | Bu::ReadWriteMutex::WriteLocker wl( mCacheEntry ); |
504 | hCacheEntry.insert( k, pEnt ); | 514 | hCacheEntry.insert( k, pEnt ); |
505 | } | 515 | } |
@@ -536,11 +546,15 @@ namespace Bu | |||
536 | ar << *pObject; | 546 | ar << *pObject; |
537 | } | 547 | } |
538 | 548 | ||
539 | template<typename obtype> | 549 | template<typename keytype, typename obtype> |
540 | obtype *_cacheObjectLoad( Bu::Stream &s ) | 550 | obtype *_cacheObjectLoad( |
551 | typename Bu::CacheObject<keytype, obtype>::Initializer &initObject, | ||
552 | const keytype & /*rKey*/, | ||
553 | Bu::Stream &s | ||
554 | ) | ||
541 | { | 555 | { |
542 | Bu::Archive ar( s, Bu::Archive::load ); | 556 | Bu::Archive ar( s, Bu::Archive::load ); |
543 | obtype *ret = new obtype(); | 557 | obtype *ret = initObject(new obtype()); |
544 | ar >> *ret; | 558 | ar >> *ret; |
545 | return ret; | 559 | return ret; |
546 | } | 560 | } |
diff --git a/src/unstable/cacheobject.h b/src/unstable/cacheobject.h index e83f706..ca70bb4 100644 --- a/src/unstable/cacheobject.h +++ b/src/unstable/cacheobject.h | |||
@@ -21,6 +21,7 @@ namespace Bu | |||
21 | class CacheObject | 21 | class CacheObject |
22 | { | 22 | { |
23 | friend class CacheBase<keytype, obtype>; | 23 | friend class CacheBase<keytype, obtype>; |
24 | //friend class CacheObject<keytype, obtype>::Initializer; | ||
24 | public: | 25 | public: |
25 | CacheObject() : | 26 | CacheObject() : |
26 | pCache( NULL ), | 27 | pCache( NULL ), |
@@ -35,6 +36,7 @@ namespace Bu | |||
35 | 36 | ||
36 | typedef CacheObject<keytype, obtype> MyType; | 37 | typedef CacheObject<keytype, obtype> MyType; |
37 | typedef CacheBase<keytype, obtype> CacheType; | 38 | typedef CacheBase<keytype, obtype> CacheType; |
39 | typedef CacheEntry<keytype, obtype> EntryType; | ||
38 | 40 | ||
39 | virtual keytype getKey() const=0; | 41 | virtual keytype getKey() const=0; |
40 | virtual int getPersistenceScore() const { return 0; } | 42 | virtual int getPersistenceScore() const { return 0; } |
@@ -94,18 +96,44 @@ namespace Bu | |||
94 | { | 96 | { |
95 | return pCache; | 97 | return pCache; |
96 | } | 98 | } |
99 | |||
100 | public: | ||
101 | class Initializer | ||
102 | { | ||
103 | friend class CacheBase<keytype,obtype>; | ||
104 | private: | ||
105 | Initializer( CacheBase<keytype,obtype> *pCache ) : | ||
106 | pCache( pCache ) | ||
107 | { | ||
108 | } | ||
109 | |||
110 | public: | ||
111 | template<typename subtype> | ||
112 | subtype *operator()( subtype *pNewObj ) | ||
113 | { | ||
114 | pNewObj->setCache( pCache ); | ||
115 | return pNewObj; | ||
116 | } | ||
117 | |||
118 | private: | ||
119 | CacheBase<keytype,obtype> *pCache; | ||
120 | }; | ||
121 | |||
97 | 122 | ||
98 | private: | 123 | private: |
99 | typedef CacheEntry<keytype, obtype> Entry; | 124 | void setCache( CacheType *pCache ) |
100 | void setCache( CacheType *pCache, Entry *pEntry ) | ||
101 | { | 125 | { |
102 | this->pCache = pCache; | 126 | this->pCache = pCache; |
127 | } | ||
128 | |||
129 | void setCacheEntry( EntryType *pEntry ) | ||
130 | { | ||
103 | this->pEntry = pEntry; | 131 | this->pEntry = pEntry; |
104 | } | 132 | } |
105 | 133 | ||
106 | private: | 134 | private: |
107 | CacheType *pCache; | 135 | CacheType *pCache; |
108 | Entry *pEntry; | 136 | EntryType *pEntry; |
109 | bool bChanged; | 137 | bool bChanged; |
110 | }; | 138 | }; |
111 | } | 139 | } |
diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h index 9bc926f..d19395a 100644 --- a/src/unstable/myriadcache.h +++ b/src/unstable/myriadcache.h | |||
@@ -54,6 +54,7 @@ namespace Bu | |||
54 | } | 54 | } |
55 | 55 | ||
56 | using typename Bu::CacheBase<keytype,obtype>::KeyList; | 56 | using typename Bu::CacheBase<keytype,obtype>::KeyList; |
57 | using typename Bu::CacheBase<keytype,obtype>::ObjectType; | ||
57 | 58 | ||
58 | virtual typename Bu::CacheBase<keytype,obtype>::KeyList getKeys() const | 59 | virtual typename Bu::CacheBase<keytype,obtype>::KeyList getKeys() const |
59 | { | 60 | { |
@@ -92,10 +93,13 @@ namespace Bu | |||
92 | bStructureChanged = true; | 93 | bStructureChanged = true; |
93 | } | 94 | } |
94 | 95 | ||
95 | virtual obtype *_load( const keytype &k ) | 96 | virtual obtype *_load( |
97 | typename Bu::CacheObject<keytype, obtype>::Initializer &initObj, | ||
98 | const keytype &k | ||
99 | ) | ||
96 | { | 100 | { |
97 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); | 101 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); |
98 | return _cacheObjectLoad<obtype>( ms ); | 102 | return _cacheObjectLoad<keytype, obtype>( initObj, k, ms ); |
99 | } | 103 | } |
100 | 104 | ||
101 | virtual void _save( const obtype *o ) | 105 | virtual void _save( const obtype *o ) |