diff options
Diffstat (limited to '')
-rw-r--r-- | src/cachestoremyriad.h | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/cachestoremyriad.h b/src/cachestoremyriad.h new file mode 100644 index 0000000..a40ffb2 --- /dev/null +++ b/src/cachestoremyriad.h | |||
@@ -0,0 +1,156 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2010 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 | #ifndef BU_CACHE_STORE_MYRIAD_H | ||
9 | #define BU_CACHE_STORE_MYRIAD_H | ||
10 | |||
11 | #include "bu/fstring.h" | ||
12 | #include "bu/stream.h" | ||
13 | #include "bu/myriad.h" | ||
14 | #include "bu/cachestore.h" | ||
15 | #include "bu/myriadstream.h" | ||
16 | |||
17 | #include "bu/archive.h" | ||
18 | |||
19 | #include "bu/sio.h" | ||
20 | |||
21 | namespace Bu | ||
22 | { | ||
23 | template<class keytype, class obtype> | ||
24 | keytype __cacheGetKey( const obtype *pObj ); | ||
25 | |||
26 | template<class keytype, class obtype> | ||
27 | obtype *__cacheStoreMyriadAlloc( const keytype &key ) | ||
28 | { | ||
29 | return new obtype(); | ||
30 | } | ||
31 | |||
32 | template<class keytype, class obtype> | ||
33 | void __cacheStoreMyriadStore( Bu::Stream &s, obtype &rObj, | ||
34 | const keytype & ) | ||
35 | { | ||
36 | Bu::Archive ar( s, Bu::Archive::save ); | ||
37 | ar << rObj; | ||
38 | } | ||
39 | |||
40 | template<class keytype, class obtype> | ||
41 | obtype *__cacheStoreMyriadLoad( Bu::Stream &s, const keytype &key ) | ||
42 | { | ||
43 | obtype *pObj = __cacheStoreMyriadAlloc<keytype, obtype>( key ); | ||
44 | Bu::Archive ar( s, Bu::Archive::load ); | ||
45 | ar >> (*pObj); | ||
46 | return pObj; | ||
47 | } | ||
48 | |||
49 | template<class keytype, class obtype> | ||
50 | class CacheStoreMyriad : public CacheStore<keytype, obtype> | ||
51 | { | ||
52 | public: | ||
53 | CacheStoreMyriad( Bu::Stream &sArch, | ||
54 | int iBlockSize=1024, int iPreAllocate=1 ) : | ||
55 | mStore( sArch ) | ||
56 | { | ||
57 | try | ||
58 | { | ||
59 | mStore.initialize(); | ||
60 | MyriadStream ns = mStore.openStream( 1 ); | ||
61 | Bu::Archive ar( ns, Bu::Archive::load ); | ||
62 | ar >> hId; | ||
63 | Bu::sio << hId << Bu::sio.nl; | ||
64 | } | ||
65 | catch( Bu::MyriadException &e ) | ||
66 | { | ||
67 | mStore.initialize( iBlockSize, iPreAllocate ); | ||
68 | int iStream = mStore.createStream(); | ||
69 | if( iStream != 1 ) | ||
70 | throw Bu::ExceptionBase("That's...horrible...id = %d.\n\n", iStream ); | ||
71 | MyriadStream ns = mStore.openStream( 1 ); | ||
72 | Bu::Archive ar( ns, Bu::Archive::save ); | ||
73 | ar << hId; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | virtual ~CacheStoreMyriad() | ||
78 | { | ||
79 | MyriadStream ns = mStore.openStream( 1 ); | ||
80 | Bu::Archive ar( ns, Bu::Archive::save ); | ||
81 | ar << hId; | ||
82 | } | ||
83 | |||
84 | virtual obtype *load( const keytype &key ) | ||
85 | { | ||
86 | int iStream = hId.get( key ); | ||
87 | MyriadStream ns = mStore.openStream( iStream ); | ||
88 | obtype *pOb = __cacheStoreMyriadLoad<keytype, obtype>( ns, key ); | ||
89 | return pOb; | ||
90 | } | ||
91 | |||
92 | virtual void unload( obtype *pObj, const keytype &key ) | ||
93 | { | ||
94 | int iStream = hId.get( key ); | ||
95 | MyriadStream ns = mStore.openStream( iStream ); | ||
96 | __cacheStoreMyriadStore<keytype, obtype>( ns, *pObj, key ); | ||
97 | delete pObj; | ||
98 | } | ||
99 | |||
100 | virtual keytype create( obtype *pSrc ) | ||
101 | { | ||
102 | keytype key = __cacheGetKey<keytype, obtype>( pSrc ); | ||
103 | int iStream = mStore.createStream(); | ||
104 | hId.insert( key, iStream ); | ||
105 | MyriadStream ns = mStore.openStream( iStream ); | ||
106 | __cacheStoreMyriadStore<keytype, obtype>( ns, *pSrc, key ); | ||
107 | return key; | ||
108 | } | ||
109 | |||
110 | virtual void sync() | ||
111 | { | ||
112 | MyriadStream ns = mStore.openStream( 1 ); | ||
113 | Bu::Archive ar( ns, Bu::Archive::save ); | ||
114 | ar << hId; | ||
115 | |||
116 | mStore.sync(); | ||
117 | } | ||
118 | |||
119 | virtual void sync( obtype *pSrc, const keytype &key ) | ||
120 | { | ||
121 | int iStream = hId.get( key ); | ||
122 | MyriadStream ns = mStore.openStream( iStream ); | ||
123 | __cacheStoreMyriadStore<keytype, obtype>( ns, *pSrc, key ); | ||
124 | } | ||
125 | |||
126 | virtual void destroy( obtype *pObj, const keytype &key ) | ||
127 | { | ||
128 | int iStream = hId.get( key ); | ||
129 | mStore.deleteStream( iStream ); | ||
130 | hId.erase( key ); | ||
131 | delete pObj; | ||
132 | } | ||
133 | |||
134 | virtual bool has( const keytype &key ) | ||
135 | { | ||
136 | return hId.has( key ); | ||
137 | } | ||
138 | |||
139 | virtual Bu::List<keytype> getKeys() | ||
140 | { | ||
141 | return hId.getKeys(); | ||
142 | } | ||
143 | |||
144 | virtual int getSize() | ||
145 | { | ||
146 | return hId.getSize(); | ||
147 | } | ||
148 | |||
149 | private: | ||
150 | Myriad mStore; | ||
151 | typedef Bu::Hash<keytype, long> StreamHash; | ||
152 | StreamHash hId; | ||
153 | }; | ||
154 | }; | ||
155 | |||
156 | #endif | ||