aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/cachestoremyriad.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/experimental/cachestoremyriad.h
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/experimental/cachestoremyriad.h')
-rw-r--r--src/experimental/cachestoremyriad.h158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/experimental/cachestoremyriad.h b/src/experimental/cachestoremyriad.h
new file mode 100644
index 0000000..e632a82
--- /dev/null
+++ b/src/experimental/cachestoremyriad.h
@@ -0,0 +1,158 @@
1/*
2 * Copyright (C) 2007-2011 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/string.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
19namespace Bu
20{
21 template<class keytype, class obtype>
22 keytype __cacheGetKey( const obtype *pObj );
23
24 template<class keytype, class obtype>
25 obtype *__cacheStoreMyriadAlloc( const keytype &key )
26 {
27 return new obtype();
28 }
29
30 template<class keytype, class obtype>
31 void __cacheStoreMyriadStore( Bu::Stream &s, obtype &rObj,
32 const keytype & )
33 {
34 Bu::Archive ar( s, Bu::Archive::save );
35 ar << rObj;
36 }
37
38 template<class keytype, class obtype>
39 obtype *__cacheStoreMyriadLoad( Bu::Stream &s, const keytype &key )
40 {
41 obtype *pObj = __cacheStoreMyriadAlloc<keytype, obtype>( key );
42 Bu::Archive ar( s, Bu::Archive::load );
43 ar >> (*pObj);
44 return pObj;
45 }
46
47 template<class keytype, class obtype>
48 class CacheStoreMyriad : public CacheStore<keytype, obtype>
49 {
50 public:
51 CacheStoreMyriad( Bu::Stream &sArch,
52 int iBlockSize=512, int iPreAllocate=8 ) :
53 mStore( sArch, iBlockSize, iPreAllocate )
54 {
55 try
56 {
57 MyriadStream ns = mStore.openStream( 1 );
58 Bu::Archive ar( ns, Bu::Archive::load );
59 ar >> hId;
60 }
61 catch( Bu::MyriadException &e )
62 {
63 int iStream = mStore.createStream();
64 if( iStream != 1 )
65 throw Bu::ExceptionBase("That's...horrible...id = %d.\n\n",
66 iStream );
67 MyriadStream ns = mStore.openStream( 1 );
68 Bu::Archive ar( ns, Bu::Archive::save );
69 ar << hId;
70 }
71 }
72
73 virtual ~CacheStoreMyriad()
74 {
75 MyriadStream ns = mStore.openStream( 1 );
76 Bu::Archive ar( ns, Bu::Archive::save );
77 ar << hId;
78 }
79
80 virtual obtype *load( const keytype &key )
81 {
82 int iStream = hId.get( key );
83 MyriadStream ns = mStore.openStream( iStream );
84 obtype *pOb = __cacheStoreMyriadLoad<keytype, obtype>( ns, key );
85 return pOb;
86 }
87
88 virtual void unload( obtype *pObj, const keytype & )
89 {
90 delete pObj;
91 }
92
93 virtual keytype create( obtype *pSrc )
94 {
95 keytype key = __cacheGetKey<keytype, obtype>( pSrc );
96 int iStream = mStore.createStream();
97 hId.insert( key, iStream );
98 MyriadStream ns = mStore.openStream( iStream );
99 __cacheStoreMyriadStore<keytype, obtype>( ns, *pSrc, key );
100 ns.setSize( ns.tell() );
101 return key;
102 }
103
104 virtual void sync()
105 {
106 MyriadStream ns = mStore.openStream( 1 );
107 Bu::Archive ar( ns, Bu::Archive::save );
108 ar << hId;
109 ns.setSize( ns.tell() );
110 mStore.sync();
111 }
112
113 virtual void sync( obtype *pSrc, const keytype &key )
114 {
115 int iStream = hId.get( key );
116 MyriadStream ns = mStore.openStream( iStream );
117 __cacheStoreMyriadStore<keytype, obtype>( ns, *pSrc, key );
118 ns.setSize( ns.tell() );
119 }
120
121 virtual void destroy( obtype *pObj, const keytype &key )
122 {
123 int iStream = hId.get( key );
124 mStore.deleteStream( iStream );
125 hId.erase( key );
126 delete pObj;
127 }
128
129 virtual void destroy( const keytype &key )
130 {
131 int iStream = hId.get( key );
132 mStore.deleteStream( iStream );
133 hId.erase( key );
134 }
135
136 virtual bool has( const keytype &key )
137 {
138 return hId.has( key );
139 }
140
141 virtual Bu::List<keytype> getKeys()
142 {
143 return hId.getKeys();
144 }
145
146 virtual int getSize()
147 {
148 return hId.getSize();
149 }
150
151 private:
152 Myriad mStore;
153 typedef Bu::Hash<keytype, long> StreamHash;
154 StreamHash hId;
155 };
156};
157
158#endif