aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/myriadcache.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2013-03-17 23:45:21 +0000
committerMike Buland <eichlan@xagasoft.com>2013-03-17 23:45:21 +0000
commitfb5176bbd5355b02b7d0e65da3ef3f0105824cd0 (patch)
tree32a9d8114ce4852b901a35f6aec982e6071a3bac /src/unstable/myriadcache.h
parenta6d249cad214dc0baff0e80e56ffdec91d8a1cf0 (diff)
downloadlibbu++-fb5176bbd5355b02b7d0e65da3ef3f0105824cd0.tar.gz
libbu++-fb5176bbd5355b02b7d0e65da3ef3f0105824cd0.tar.bz2
libbu++-fb5176bbd5355b02b7d0e65da3ef3f0105824cd0.tar.xz
libbu++-fb5176bbd5355b02b7d0e65da3ef3f0105824cd0.zip
The new cache system has been broken out into it's individual headers, and is
now ready for actual use.
Diffstat (limited to 'src/unstable/myriadcache.h')
-rw-r--r--src/unstable/myriadcache.h126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h
new file mode 100644
index 0000000..f8c0e00
--- /dev/null
+++ b/src/unstable/myriadcache.h
@@ -0,0 +1,126 @@
1/*
2 * Copyright (C) 2007-2013 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_MYRIAD_CACHE_H
9#define BU_MYRIAD_CACHE_H
10
11#include "bu/cachebase.h"
12#include "bu/myriad.h"
13#include "bu/myriadstream.h"
14#include "bu/file.h"
15#include "bu/streamstack.h"
16
17namespace Bu
18{
19 template<typename keytype, typename obtype>
20 class MyriadCache : public Bu::CacheBase<keytype, obtype>
21 {
22 public:
23 MyriadCache( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ) :
24 sStore( sStore ),
25 mStore( sStore, iBlockSize, iPreallocate ),
26 bStructureChanged( false )
27 {
28 try
29 {
30 Bu::ReadWriteMutex::ReadLocker l( rwStore );
31 Bu::MyriadStream ms = mStore.openStream( 1 );
32 Bu::Archive ar( ms, Bu::Archive::load );
33 uint8_t uVer;
34 ar >> uVer;
35 switch( uVer )
36 {
37 case 0:
38 ar >> hIndex;
39 break;
40 }
41 }
42 catch(...)
43 {
44 if( mStore.createStreamWithId( 1 ) != 1 )
45 throw Bu::ExceptionBase("Error creating index stream.");
46
47 _sync();
48 }
49 }
50
51 virtual ~MyriadCache()
52 {
53 _sync();
54 }
55
56 using typename Bu::CacheBase<keytype,obtype>::KeyList;
57
58 virtual KeyList getKeys() const
59 {
60 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
61 return hIndex.getKeys();
62 }
63
64 virtual int getSize() const
65 {
66 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
67 return hIndex.getSize();
68 }
69
70 protected:
71 virtual void _create( const obtype *o )
72 {
73 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
74 hIndex.insert( o->getKey(), mStore.createStream() );
75 _save( o );
76
77 bStructureChanged = true;
78 }
79
80 virtual void _erase( const keytype &k )
81 {
82 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
83 mStore.deleteStream( hIndex.get( k ) );
84 hIndex.erase( k );
85
86 bStructureChanged = true;
87 }
88
89 virtual obtype *_load( const keytype &k )
90 {
91 Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) );
92 return _cacheObjectLoad<obtype>( ms );
93 }
94
95 virtual void _save( const obtype *o )
96 {
97 Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) );
98 _cacheObjectSave( ms, o );
99 ms.setSize( ms.tell() );
100 }
101
102 virtual void _sync()
103 {
104 Bu::ReadWriteMutex::ReadLocker wl( rwStore );
105 if( !bStructureChanged )
106 return;
107
108 Bu::MyriadStream ms = mStore.openStream( 1 );
109 Bu::Archive ar( ms, Bu::Archive::save );
110 ar << (uint8_t)0 << hIndex;
111 ar.close();
112 ms.setSize( ms.tell() );
113
114 bStructureChanged = false;
115 }
116
117 private:
118 Bu::Stream &sStore;
119 Bu::Myriad mStore;
120 Bu::Hash<keytype, int> hIndex;
121 mutable Bu::ReadWriteMutex rwStore;
122 bool bStructureChanged;
123 };
124}
125
126#endif