aboutsummaryrefslogtreecommitdiff
path: root/src/cachestorefiles.h
blob: 10b2c1bfa7a6cbb10cb06add7812711e3eb87691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_CACHE_STORE_FILES_H
#define BU_CACHE_STORE_FILES_H

#include "bu/string.h"
#include "bu/file.h"
#include "bu/cachestore.h"
#include "bu/archive.h"
#include "bu/membuf.h"
#include "bu/formatter.h"
#include "bu/sio.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

namespace Bu
{
	template<class keytype, class obtype>
	keytype __cacheGetKey( const obtype *pObj );

	template<class keytype, class obtype>
	obtype *__cacheStoreFilesAlloc( const keytype &key )
	{
		return new obtype();
	}

	template<class keytype, class obtype>
	void __cacheStoreFilesStore( Bu::Stream &s, obtype &rObj,
			const keytype & )
	{
		Bu::Archive ar( s, Bu::Archive::save );
		ar << rObj;
	}

	template<class keytype, class obtype>
	obtype *__cacheStoreFilesLoad( Bu::Stream &s, const keytype &key )
	{
		obtype *pObj = __cacheStoreFilesAlloc<keytype, obtype>( key );
		Bu::Archive ar( s, Bu::Archive::load );
		ar >> (*pObj);
		return pObj;
	}

	template<class keytype, class obtype>
	class CacheStoreFiles : public CacheStore<keytype, obtype>
	{
	public:
		CacheStoreFiles( const Bu::String &sPrefix ) :
			sPrefix( sPrefix )
		{
			if( access( sPrefix.getStr(), W_OK|R_OK|X_OK ) )
			{
				mkdir( sPrefix.getStr(), 0755 );
			}
		}

		virtual ~CacheStoreFiles()
		{
		}
		
		virtual obtype *load( const keytype &key )
		{
//			try
//			{
				Bu::MemBuf mb;
				Bu::Formatter f( mb );
				f << sPrefix << "/" << key;
				Bu::File fIn( mb.getString(), Bu::File::Read );
				obtype *pOb = __cacheStoreFilesLoad<keytype, obtype>( fIn, key );
				return pOb;
//			}
//			catch( std::exception &e )
//			{
//				throw Bu::HashException( e.what() );
//			}
		}

		virtual void unload( obtype *pObj, const keytype & )
		{
			delete pObj;
		}

		virtual keytype create( obtype *pSrc )
		{
			keytype key = __cacheGetKey<keytype, obtype>( pSrc );
			Bu::MemBuf mb;
			Bu::Formatter f( mb );
			f << sPrefix << "/" << key;

			Bu::File fTouch( mb.getString(), Bu::File::WriteNew );

			return key;
		}

		virtual void sync()
		{
		}

		virtual void sync( obtype *pSrc, const keytype &key )
		{
			Bu::MemBuf mb;
			Bu::Formatter f( mb );
			f << sPrefix << "/" << key;

			Bu::File fOut( mb.getString(), Bu::File::WriteNew );
			__cacheStoreFilesStore<keytype, obtype>( fOut, *pSrc, key );
		}

		virtual void destroy( obtype *pObj, const keytype &key )
		{
			Bu::MemBuf mb;
			Bu::Formatter f( mb );
			f << sPrefix << "/" << key;

			unlink( mb.getString().getStr() );
			delete pObj;
		}

		virtual void destroy( const keytype &key )
		{
			Bu::MemBuf mb;
			Bu::Formatter f( mb );
			f << sPrefix << "/" << key;

			unlink( mb.getString().getStr() );
		}

		virtual bool has( const keytype &key )
		{
			Bu::MemBuf mb;
			Bu::Formatter f( mb );
			f << sPrefix << "/";
			Bu::String sBase = mb.getString();
			f << key;

			if( sBase == mb.getString() )
				return false;

			return access( mb.getString().getStr(), F_OK ) == 0;
		}

		virtual Bu::List<keytype> getKeys()
		{
			DIR *dir = opendir( sPrefix.getStr() );
			struct dirent *de;
			Bu::List<keytype> lKeys;

			while( (de = readdir( dir ) ) )
			{
				if( de->d_type != DT_REG )
					continue;

				keytype tmp;
				Bu::MemBuf mb( de->d_name );
				Bu::Formatter f( mb );
				try
				{
					Fmt fm;
					fm.tokenize( false );
					f << fm;
					f >> tmp;
				}
				catch( Bu::ExceptionBase &e )
				{
					Bu::sio << "Parse error in dir-scan: " << e.what()
						<< Bu::sio.nl;
				}
				lKeys.append( tmp );
			}
			closedir( dir );

			return lKeys;
		}

		virtual int getSize()
		{
			DIR *dir = opendir( sPrefix.getStr() );
			struct dirent *de;
			int iCount = 0;

			while( (de = readdir( dir ) ) )
			{
				if( de->d_type != DT_REG )
					continue;

				iCount++;
			}
			closedir( dir );

			return iCount;
		}

	private:
		Bu::String sPrefix;
	};

};

#endif