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