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