aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/myriadcache.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/myriadcache.h')
-rw-r--r--src/unstable/myriadcache.h140
1 files changed, 0 insertions, 140 deletions
diff --git a/src/unstable/myriadcache.h b/src/unstable/myriadcache.h
deleted file mode 100644
index 24002b0..0000000
--- a/src/unstable/myriadcache.h
+++ /dev/null
@@ -1,140 +0,0 @@
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
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 Bu::CacheBase<keytype,obtype>::sync();
54 }
55
56 using typename Bu::CacheBase<keytype,obtype>::KeyList;
57 using typename Bu::CacheBase<keytype,obtype>::ObjectType;
58
59 virtual typename Bu::CacheBase<keytype,obtype>::KeyList getKeys() const
60 {
61 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
62 return hIndex.getKeys();
63 }
64
65 virtual int getSize() const
66 {
67 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
68 return hIndex.getSize();
69 }
70
71 protected:
72 virtual bool _has( const keytype &key )
73 {
74 Bu::ReadWriteMutex::ReadLocker rl( rwStore );
75 return hIndex.has( key );
76 }
77
78 virtual void _create( const obtype *o )
79 {
80 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
81 hIndex.insert( o->getKey(), mStore.createStream() );
82 _save( o );
83
84 bStructureChanged = true;
85 }
86
87 virtual void _erase( const keytype &k )
88 {
89 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
90 mStore.deleteStream( hIndex.get( k ) );
91 hIndex.erase( k );
92
93 bStructureChanged = true;
94 }
95
96 virtual obtype *_load(
97 typename Bu::CacheObject<keytype, obtype>::Initializer &initObj,
98 const keytype &k
99 )
100 {
101 Bu::MyriadStream ms = mStore.openStream( hIndex.get( k ) );
102 return _cacheObjectLoad<keytype, obtype>( initObj, k, ms );
103 }
104
105 virtual void _save( const obtype *o )
106 {
107 Bu::MyriadStream ms = mStore.openStream( hIndex.get( o->getKey() ) );
108 _cacheObjectSave( ms, o );
109 ms.setSize( ms.tell() );
110
111 mStore.sync();
112 }
113
114 virtual void _sync()
115 {
116 Bu::ReadWriteMutex::WriteLocker wl( rwStore );
117 if( !bStructureChanged )
118 return;
119
120 Bu::MyriadStream ms = mStore.openStream( 1 );
121 Bu::Archive ar( ms, Bu::Archive::save );
122 ar << (uint8_t)0 << hIndex;
123 ar.close();
124 ms.setSize( ms.tell() );
125
126 bStructureChanged = false;
127
128 mStore.sync();
129 }
130
131 private:
132 Bu::Stream &sStore;
133 Bu::Myriad mStore;
134 Bu::Hash<keytype, int> hIndex;
135 mutable Bu::ReadWriteMutex rwStore;
136 bool bStructureChanged;
137 };
138}
139
140#endif