diff options
Diffstat (limited to 'src/unstable/myriadcache.h')
-rw-r--r-- | src/unstable/myriadcache.h | 126 |
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 | |||
17 | namespace 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 | ||