diff options
author | Mike Buland <eichlan@xagasoft.com> | 2009-01-16 00:30:59 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2009-01-16 00:30:59 +0000 |
commit | 90a51615bec786fac6750616e82570cf8727db06 (patch) | |
tree | 82bc15c721efd41cadd62b8d9ee44e1c116aa016 | |
parent | 46f2f8c2962532e010fcb4e3492ce97e2216fb0e (diff) | |
download | libbu++-90a51615bec786fac6750616e82570cf8727db06.tar.gz libbu++-90a51615bec786fac6750616e82570cf8727db06.tar.bz2 libbu++-90a51615bec786fac6750616e82570cf8727db06.tar.xz libbu++-90a51615bec786fac6750616e82570cf8727db06.zip |
Whoa, Bu::CacheStoreNids totally works now, it's even tested and everything.
Isn't that great?
-rw-r--r-- | src/cachestorenids.h | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/cachestorenids.h b/src/cachestorenids.h index e69de29..f488117 100644 --- a/src/cachestorenids.h +++ b/src/cachestorenids.h | |||
@@ -0,0 +1,100 @@ | |||
1 | #ifndef BU_CACHE_STORE_NIDS_H | ||
2 | #define BU_CACHE_STORE_NIDS_H | ||
3 | |||
4 | #include "bu/fstring.h" | ||
5 | #include "bu/stream.h" | ||
6 | #include "bu/nids.h" | ||
7 | #include "bu/nidsstream.h" | ||
8 | #include "bu/cachestore.h" | ||
9 | |||
10 | namespace Bu | ||
11 | { | ||
12 | template<class obtype, class keytype> | ||
13 | class CacheStoreNids : public CacheStore<obtype, keytype> | ||
14 | { | ||
15 | public: | ||
16 | CacheStoreNids( Bu::Stream &sArch, | ||
17 | int iBlockSize=1024, int iPreAllocate=1 ) : | ||
18 | nStore( sArch ) | ||
19 | { | ||
20 | try | ||
21 | { | ||
22 | nStore.initialize(); | ||
23 | NidsStream ns = nStore.openStream( 0 ); | ||
24 | Bu::Archive ar( ns, Bu::Archive::load ); | ||
25 | ar >> hId; | ||
26 | } | ||
27 | catch( Bu::NidsException &e ) | ||
28 | { | ||
29 | nStore.initialize( iBlockSize, iPreAllocate ); | ||
30 | int iStream = nStore.createStream(); | ||
31 | if( iStream != 0 ) | ||
32 | printf("That's...horrible...id = %d.\n\n", iStream ); | ||
33 | NidsStream ns = nStore.openStream( 0 ); | ||
34 | Bu::Archive ar( ns, Bu::Archive::save ); | ||
35 | ar << hId; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | virtual ~CacheStoreNids() | ||
40 | { | ||
41 | NidsStream ns = nStore.openStream( 0 ); | ||
42 | Bu::Archive ar( ns, Bu::Archive::save ); | ||
43 | ar << hId; | ||
44 | } | ||
45 | |||
46 | virtual obtype *load( const keytype &key ) | ||
47 | { | ||
48 | int iStream = hId.get( key ); | ||
49 | NidsStream ns = nStore.openStream( iStream ); | ||
50 | Bu::Archive ar( ns, Bu::Archive::load ); | ||
51 | obtype *pOb = new obtype(); | ||
52 | ar >> (*pOb); | ||
53 | return pOb; | ||
54 | } | ||
55 | |||
56 | virtual void unload( obtype *pObj, const keytype &key ) | ||
57 | { | ||
58 | int iStream = hId.get( key ); | ||
59 | NidsStream ns = nStore.openStream( iStream ); | ||
60 | Bu::Archive ar( ns, Bu::Archive::save ); | ||
61 | ar << (*pObj); | ||
62 | delete pObj; | ||
63 | } | ||
64 | |||
65 | virtual keytype getKey( obtype *pSrc )=0; | ||
66 | |||
67 | virtual keytype create( obtype *pSrc ) | ||
68 | { | ||
69 | keytype key = getKey( pSrc ); | ||
70 | int iStream = nStore.createStream(); | ||
71 | hId.insert( key, iStream ); | ||
72 | printf("Creating stream: %d\n", iStream ); | ||
73 | NidsStream ns = nStore.openStream( iStream ); | ||
74 | Bu::Archive ar( ns, Bu::Archive::save ); | ||
75 | obtype *pOb = new obtype(); | ||
76 | ar << (*pOb); | ||
77 | return key; | ||
78 | } | ||
79 | |||
80 | virtual void destroy( obtype *pObj, const keytype &key ) | ||
81 | { | ||
82 | int iStream = hId.get( key ); | ||
83 | nStore.deleteStream( iStream ); | ||
84 | hId.erase( key ); | ||
85 | delete pObj; | ||
86 | } | ||
87 | |||
88 | Bu::List<keytype> getKeys() | ||
89 | { | ||
90 | return hId.getKeys(); | ||
91 | } | ||
92 | |||
93 | private: | ||
94 | Nids nStore; | ||
95 | typedef Bu::Hash<keytype, long> NidHash; | ||
96 | NidHash hId; | ||
97 | }; | ||
98 | }; | ||
99 | |||
100 | #endif | ||