diff options
author | Mike Buland <mike@xagasoft.com> | 2024-09-26 14:43:22 -0700 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2024-09-26 14:43:22 -0700 |
commit | 76821551f312dd447a03748a01670f3718cd8345 (patch) | |
tree | aca702a69c9eb3f7dd8080dcd20d7f238bbbaa81 /src/unstable/myriadcache.h | |
parent | d5e966a0563e3f01c02c3cfb1f3a83fec852168b (diff) | |
download | libbu++-76821551f312dd447a03748a01670f3718cd8345.tar.gz libbu++-76821551f312dd447a03748a01670f3718cd8345.tar.bz2 libbu++-76821551f312dd447a03748a01670f3718cd8345.tar.xz libbu++-76821551f312dd447a03748a01670f3718cd8345.zip |
Basic update to new API for existing components.
This may not all work yet, but it all compiles!
Diffstat (limited to 'src/unstable/myriadcache.h')
-rw-r--r-- | src/unstable/myriadcache.h | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h new file mode 100644 index 0000000..d6842a5 --- /dev/null +++ b/src/unstable/myriadcache.h | |||
@@ -0,0 +1,150 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2023 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.open( | ||
32 | 1, Bu::Myriad::WriteNew|Bu::Myriad::Read | ||
33 | ); | ||
34 | Bu::Archive ar( ms, Bu::Archive::load ); | ||
35 | uint8_t uVer; | ||
36 | ar >> uVer; | ||
37 | switch( uVer ) | ||
38 | { | ||
39 | case 0: | ||
40 | ar >> hIndex; | ||
41 | break; | ||
42 | } | ||
43 | } | ||
44 | catch(...) | ||
45 | { | ||
46 | try | ||
47 | { | ||
48 | mStore.open( 1, Bu::Myriad::Create|Bu::Myriad::ReadWrite ); | ||
49 | _sync(); | ||
50 | } | ||
51 | catch(...) | ||
52 | { | ||
53 | throw Bu::ExceptionBase("Error creating index stream."); | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | |||
58 | virtual ~MyriadCache() | ||
59 | { | ||
60 | Bu::CacheBase<keytype,obtype>::sync(); | ||
61 | } | ||
62 | |||
63 | using typename Bu::CacheBase<keytype,obtype>::KeyList; | ||
64 | using typename Bu::CacheBase<keytype,obtype>::ObjectType; | ||
65 | |||
66 | virtual typename Bu::CacheBase<keytype,obtype>::KeyList getKeys() const | ||
67 | { | ||
68 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
69 | return hIndex.getKeys(); | ||
70 | } | ||
71 | |||
72 | virtual int getSize() const | ||
73 | { | ||
74 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
75 | return hIndex.getSize(); | ||
76 | } | ||
77 | |||
78 | protected: | ||
79 | virtual bool _has( const keytype &key ) | ||
80 | { | ||
81 | Bu::ReadWriteMutex::ReadLocker rl( rwStore ); | ||
82 | return hIndex.has( key ); | ||
83 | } | ||
84 | |||
85 | virtual void _create( const obtype *o ) | ||
86 | { | ||
87 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
88 | { | ||
89 | Bu::MyriadStream ms = mStore.create( Bu::Myriad::Create ); | ||
90 | hIndex.insert( o->getKey(), ms.getId() ); | ||
91 | } | ||
92 | _save( o ); | ||
93 | |||
94 | bStructureChanged = true; | ||
95 | } | ||
96 | |||
97 | virtual void _erase( const keytype &k ) | ||
98 | { | ||
99 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
100 | mStore.erase( hIndex.get( k ) ); | ||
101 | hIndex.erase( k ); | ||
102 | |||
103 | bStructureChanged = true; | ||
104 | } | ||
105 | |||
106 | virtual obtype *_load( | ||
107 | typename Bu::CacheObject<keytype, obtype>::Initializer &initObj, | ||
108 | const keytype &k | ||
109 | ) | ||
110 | { | ||
111 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) ); | ||
112 | return _cacheObjectLoad<keytype, obtype>( initObj, k, ms ); | ||
113 | } | ||
114 | |||
115 | virtual void _save( const obtype *o ) | ||
116 | { | ||
117 | Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) ); | ||
118 | _cacheObjectSave( ms, o ); | ||
119 | ms.setSize( ms.tell() ); | ||
120 | |||
121 | mStore.sync(); | ||
122 | } | ||
123 | |||
124 | virtual void _sync() | ||
125 | { | ||
126 | Bu::ReadWriteMutex::WriteLocker wl( rwStore ); | ||
127 | if( !bStructureChanged ) | ||
128 | return; | ||
129 | |||
130 | Bu::MyriadStream ms = mStore.openStream( 1 ); | ||
131 | Bu::Archive ar( ms, Bu::Archive::save ); | ||
132 | ar << (uint8_t)0 << hIndex; | ||
133 | ar.close(); | ||
134 | ms.setSize( ms.tell() ); | ||
135 | |||
136 | bStructureChanged = false; | ||
137 | |||
138 | mStore.sync(); | ||
139 | } | ||
140 | |||
141 | private: | ||
142 | Bu::Stream &sStore; | ||
143 | Bu::Myriad mStore; | ||
144 | Bu::Hash<keytype, int> hIndex; | ||
145 | mutable Bu::ReadWriteMutex rwStore; | ||
146 | bool bStructureChanged; | ||
147 | }; | ||
148 | } | ||
149 | |||
150 | #endif | ||