diff options
Diffstat (limited to '')
-rw-r--r-- | src/experimental/cachestorefiles.h | 358 |
1 files changed, 179 insertions, 179 deletions
diff --git a/src/experimental/cachestorefiles.h b/src/experimental/cachestorefiles.h index d3a6894..0805e7b 100644 --- a/src/experimental/cachestorefiles.h +++ b/src/experimental/cachestorefiles.h | |||
@@ -23,188 +23,188 @@ | |||
23 | 23 | ||
24 | namespace Bu | 24 | namespace Bu |
25 | { | 25 | { |
26 | template<class keytype, class obtype> | 26 | template<class keytype, class obtype> |
27 | keytype __cacheGetKey( const obtype *pObj ); | 27 | keytype __cacheGetKey( const obtype *pObj ); |
28 | 28 | ||
29 | template<class keytype, class obtype> | 29 | template<class keytype, class obtype> |
30 | obtype *__cacheStoreFilesAlloc( const keytype &key ) | 30 | obtype *__cacheStoreFilesAlloc( const keytype &key ) |
31 | { | 31 | { |
32 | return new obtype(); | 32 | return new obtype(); |
33 | } | 33 | } |
34 | 34 | ||
35 | template<class keytype, class obtype> | 35 | template<class keytype, class obtype> |
36 | void __cacheStoreFilesStore( Bu::Stream &s, obtype &rObj, | 36 | void __cacheStoreFilesStore( Bu::Stream &s, obtype &rObj, |
37 | const keytype & ) | 37 | const keytype & ) |
38 | { | 38 | { |
39 | Bu::Archive ar( s, Bu::Archive::save ); | 39 | Bu::Archive ar( s, Bu::Archive::save ); |
40 | ar << rObj; | 40 | ar << rObj; |
41 | } | 41 | } |
42 | 42 | ||
43 | template<class keytype, class obtype> | 43 | template<class keytype, class obtype> |
44 | obtype *__cacheStoreFilesLoad( Bu::Stream &s, const keytype &key ) | 44 | obtype *__cacheStoreFilesLoad( Bu::Stream &s, const keytype &key ) |
45 | { | 45 | { |
46 | obtype *pObj = __cacheStoreFilesAlloc<keytype, obtype>( key ); | 46 | obtype *pObj = __cacheStoreFilesAlloc<keytype, obtype>( key ); |
47 | Bu::Archive ar( s, Bu::Archive::load ); | 47 | Bu::Archive ar( s, Bu::Archive::load ); |
48 | ar >> (*pObj); | 48 | ar >> (*pObj); |
49 | return pObj; | 49 | return pObj; |
50 | } | 50 | } |
51 | 51 | ||
52 | template<class keytype, class obtype> | 52 | template<class keytype, class obtype> |
53 | class CacheStoreFiles : public CacheStore<keytype, obtype> | 53 | class CacheStoreFiles : public CacheStore<keytype, obtype> |
54 | { | 54 | { |
55 | public: | 55 | public: |
56 | CacheStoreFiles( const Bu::String &sPrefix ) : | 56 | CacheStoreFiles( const Bu::String &sPrefix ) : |
57 | sPrefix( sPrefix ) | 57 | sPrefix( sPrefix ) |
58 | { | 58 | { |
59 | if( access( sPrefix.getStr(), W_OK|R_OK|X_OK ) ) | 59 | if( access( sPrefix.getStr(), W_OK|R_OK|X_OK ) ) |
60 | { | 60 | { |
61 | #ifdef WIN32 | 61 | #ifdef WIN32 |
62 | mkdir( sPrefix.getStr() ); | 62 | mkdir( sPrefix.getStr() ); |
63 | #else | 63 | #else |
64 | mkdir( sPrefix.getStr(), 0755 ); | 64 | mkdir( sPrefix.getStr(), 0755 ); |
65 | #endif | 65 | #endif |
66 | } | 66 | } |
67 | } | 67 | } |
68 | 68 | ||
69 | virtual ~CacheStoreFiles() | 69 | virtual ~CacheStoreFiles() |
70 | { | 70 | { |
71 | } | 71 | } |
72 | 72 | ||
73 | virtual obtype *load( const keytype &key ) | 73 | virtual obtype *load( const keytype &key ) |
74 | { | 74 | { |
75 | // try | 75 | // try |
76 | // { | 76 | // { |
77 | Bu::MemBuf mb; | 77 | Bu::MemBuf mb; |
78 | Bu::Formatter f( mb ); | 78 | Bu::Formatter f( mb ); |
79 | f << sPrefix << "/" << key; | 79 | f << sPrefix << "/" << key; |
80 | Bu::File fIn( mb.getString(), Bu::File::Read ); | 80 | Bu::File fIn( mb.getString(), Bu::File::Read ); |
81 | obtype *pOb = __cacheStoreFilesLoad<keytype, obtype>( fIn, key ); | 81 | obtype *pOb = __cacheStoreFilesLoad<keytype, obtype>( fIn, key ); |
82 | return pOb; | 82 | return pOb; |
83 | // } | 83 | // } |
84 | // catch( std::exception &e ) | 84 | // catch( std::exception &e ) |
85 | // { | 85 | // { |
86 | // throw Bu::HashException( e.what() ); | 86 | // throw Bu::HashException( e.what() ); |
87 | // } | 87 | // } |
88 | } | 88 | } |
89 | 89 | ||
90 | virtual void unload( obtype *pObj, const keytype & ) | 90 | virtual void unload( obtype *pObj, const keytype & ) |
91 | { | 91 | { |
92 | delete pObj; | 92 | delete pObj; |
93 | } | 93 | } |
94 | 94 | ||
95 | virtual keytype create( obtype *pSrc ) | 95 | virtual keytype create( obtype *pSrc ) |
96 | { | 96 | { |
97 | keytype key = __cacheGetKey<keytype, obtype>( pSrc ); | 97 | keytype key = __cacheGetKey<keytype, obtype>( pSrc ); |
98 | Bu::MemBuf mb; | 98 | Bu::MemBuf mb; |
99 | Bu::Formatter f( mb ); | 99 | Bu::Formatter f( mb ); |
100 | f << sPrefix << "/" << key; | 100 | f << sPrefix << "/" << key; |
101 | 101 | ||
102 | Bu::File fTouch( mb.getString(), Bu::File::WriteNew ); | 102 | Bu::File fTouch( mb.getString(), Bu::File::WriteNew ); |
103 | 103 | ||
104 | return key; | 104 | return key; |
105 | } | 105 | } |
106 | 106 | ||
107 | virtual void sync() | 107 | virtual void sync() |
108 | { | 108 | { |
109 | } | 109 | } |
110 | 110 | ||
111 | virtual void sync( obtype *pSrc, const keytype &key ) | 111 | virtual void sync( obtype *pSrc, const keytype &key ) |
112 | { | 112 | { |
113 | Bu::MemBuf mb; | 113 | Bu::MemBuf mb; |
114 | Bu::Formatter f( mb ); | 114 | Bu::Formatter f( mb ); |
115 | f << sPrefix << "/" << key; | 115 | f << sPrefix << "/" << key; |
116 | 116 | ||
117 | Bu::File fOut( mb.getString(), Bu::File::WriteNew ); | 117 | Bu::File fOut( mb.getString(), Bu::File::WriteNew ); |
118 | __cacheStoreFilesStore<keytype, obtype>( fOut, *pSrc, key ); | 118 | __cacheStoreFilesStore<keytype, obtype>( fOut, *pSrc, key ); |
119 | } | 119 | } |
120 | 120 | ||
121 | virtual void destroy( obtype *pObj, const keytype &key ) | 121 | virtual void destroy( obtype *pObj, const keytype &key ) |
122 | { | 122 | { |
123 | Bu::MemBuf mb; | 123 | Bu::MemBuf mb; |
124 | Bu::Formatter f( mb ); | 124 | Bu::Formatter f( mb ); |
125 | f << sPrefix << "/" << key; | 125 | f << sPrefix << "/" << key; |
126 | 126 | ||
127 | unlink( mb.getString().getStr() ); | 127 | unlink( mb.getString().getStr() ); |
128 | delete pObj; | 128 | delete pObj; |
129 | } | 129 | } |
130 | 130 | ||
131 | virtual void destroy( const keytype &key ) | 131 | virtual void destroy( const keytype &key ) |
132 | { | 132 | { |
133 | Bu::MemBuf mb; | 133 | Bu::MemBuf mb; |
134 | Bu::Formatter f( mb ); | 134 | Bu::Formatter f( mb ); |
135 | f << sPrefix << "/" << key; | 135 | f << sPrefix << "/" << key; |
136 | 136 | ||
137 | unlink( mb.getString().getStr() ); | 137 | unlink( mb.getString().getStr() ); |
138 | } | 138 | } |
139 | 139 | ||
140 | virtual bool has( const keytype &key ) | 140 | virtual bool has( const keytype &key ) |
141 | { | 141 | { |
142 | Bu::MemBuf mb; | 142 | Bu::MemBuf mb; |
143 | Bu::Formatter f( mb ); | 143 | Bu::Formatter f( mb ); |
144 | f << sPrefix << "/"; | 144 | f << sPrefix << "/"; |
145 | Bu::String sBase = mb.getString(); | 145 | Bu::String sBase = mb.getString(); |
146 | f << key; | 146 | f << key; |
147 | 147 | ||
148 | if( sBase == mb.getString() ) | 148 | if( sBase == mb.getString() ) |
149 | return false; | 149 | return false; |
150 | 150 | ||
151 | return access( mb.getString().getStr(), F_OK ) == 0; | 151 | return access( mb.getString().getStr(), F_OK ) == 0; |
152 | } | 152 | } |
153 | 153 | ||
154 | virtual Bu::List<keytype> getKeys() | 154 | virtual Bu::List<keytype> getKeys() |
155 | { | 155 | { |
156 | DIR *dir = opendir( sPrefix.getStr() ); | 156 | DIR *dir = opendir( sPrefix.getStr() ); |
157 | struct dirent *de; | 157 | struct dirent *de; |
158 | Bu::List<keytype> lKeys; | 158 | Bu::List<keytype> lKeys; |
159 | 159 | ||
160 | while( (de = readdir( dir ) ) ) | 160 | while( (de = readdir( dir ) ) ) |
161 | { | 161 | { |
162 | if( de->d_type != DT_REG ) | 162 | if( de->d_type != DT_REG ) |
163 | continue; | 163 | continue; |
164 | 164 | ||
165 | keytype tmp; | 165 | keytype tmp; |
166 | Bu::MemBuf mb( de->d_name ); | 166 | Bu::MemBuf mb( de->d_name ); |
167 | Bu::Formatter f( mb ); | 167 | Bu::Formatter f( mb ); |
168 | try | 168 | try |
169 | { | 169 | { |
170 | Fmt fm; | 170 | Fmt fm; |
171 | fm.tokenize( false ); | 171 | fm.tokenize( false ); |
172 | f << fm; | 172 | f << fm; |
173 | f >> tmp; | 173 | f >> tmp; |
174 | } | 174 | } |
175 | catch( Bu::ExceptionBase &e ) | 175 | catch( Bu::ExceptionBase &e ) |
176 | { | 176 | { |
177 | Bu::sio << "Parse error in dir-scan: " << e.what() | 177 | Bu::sio << "Parse error in dir-scan: " << e.what() |
178 | << Bu::sio.nl; | 178 | << Bu::sio.nl; |
179 | } | 179 | } |
180 | lKeys.append( tmp ); | 180 | lKeys.append( tmp ); |
181 | } | 181 | } |
182 | closedir( dir ); | 182 | closedir( dir ); |
183 | 183 | ||
184 | return lKeys; | 184 | return lKeys; |
185 | } | 185 | } |
186 | 186 | ||
187 | virtual int getSize() | 187 | virtual int getSize() |
188 | { | 188 | { |
189 | DIR *dir = opendir( sPrefix.getStr() ); | 189 | DIR *dir = opendir( sPrefix.getStr() ); |
190 | struct dirent *de; | 190 | struct dirent *de; |
191 | int iCount = 0; | 191 | int iCount = 0; |
192 | 192 | ||
193 | while( (de = readdir( dir ) ) ) | 193 | while( (de = readdir( dir ) ) ) |
194 | { | 194 | { |
195 | if( de->d_type != DT_REG ) | 195 | if( de->d_type != DT_REG ) |
196 | continue; | 196 | continue; |
197 | 197 | ||
198 | iCount++; | 198 | iCount++; |
199 | } | 199 | } |
200 | closedir( dir ); | 200 | closedir( dir ); |
201 | 201 | ||
202 | return iCount; | 202 | return iCount; |
203 | } | 203 | } |
204 | 204 | ||
205 | private: | 205 | private: |
206 | Bu::String sPrefix; | 206 | Bu::String sPrefix; |
207 | }; | 207 | }; |
208 | 208 | ||
209 | }; | 209 | }; |
210 | 210 | ||