summaryrefslogtreecommitdiff
path: root/src/cachestoremyriad.h
blob: 19288406069e04157a92a1274ae5e1275991e16f (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
/*
 * Copyright (C) 2007-2010 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_MYRIAD_H
#define BU_CACHE_STORE_MYRIAD_H

#include "bu/string.h"
#include "bu/stream.h"
#include "bu/myriad.h"
#include "bu/cachestore.h"
#include "bu/myriadstream.h"

#include "bu/archive.h"

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

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

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

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

	template<class keytype, class obtype>
	class CacheStoreMyriad : public CacheStore<keytype, obtype>
	{
	public:
		CacheStoreMyriad( Bu::Stream &sArch,
			int iBlockSize=512, int iPreAllocate=8 ) :
			mStore( sArch, iBlockSize, iPreAllocate )
		{
			try
			{
				MyriadStream ns = mStore.openStream( 1 );
				Bu::Archive ar( ns, Bu::Archive::load );
				ar >> hId;
			}
			catch( Bu::MyriadException &e )
			{
				int iStream = mStore.createStream();
				if( iStream != 1 )
					throw Bu::ExceptionBase("That's...horrible...id = %d.\n\n",
							iStream );
				MyriadStream ns = mStore.openStream( 1 );
				Bu::Archive ar( ns, Bu::Archive::save );
				ar << hId;
			}
		}

		virtual ~CacheStoreMyriad()
		{
			MyriadStream ns = mStore.openStream( 1 );
			Bu::Archive ar( ns, Bu::Archive::save );
			ar << hId;
		}
		
		virtual obtype *load( const keytype &key )
		{
			int iStream = hId.get( key );
			MyriadStream ns = mStore.openStream( iStream );
			obtype *pOb = __cacheStoreMyriadLoad<keytype, obtype>( ns, key );
			return pOb;
		}

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

		virtual keytype create( obtype *pSrc )
		{
			keytype key = __cacheGetKey<keytype, obtype>( pSrc );
			int iStream = mStore.createStream();
			hId.insert( key, iStream );
			MyriadStream ns = mStore.openStream( iStream );
			__cacheStoreMyriadStore<keytype, obtype>( ns, *pSrc, key );
			ns.setSize( ns.tell() );
			return key;
		}

		virtual void sync()
		{
			MyriadStream ns = mStore.openStream( 1 );
			Bu::Archive ar( ns, Bu::Archive::save );
			ar << hId;
			ns.setSize( ns.tell() );
			mStore.sync();
		}

		virtual void sync( obtype *pSrc, const keytype &key )
		{
			int iStream = hId.get( key );
			MyriadStream ns = mStore.openStream( iStream );
			__cacheStoreMyriadStore<keytype, obtype>( ns, *pSrc, key );
			ns.setSize( ns.tell() );
		}

		virtual void destroy( obtype *pObj, const keytype &key )
		{
			int iStream = hId.get( key );
			mStore.deleteStream( iStream );
			hId.erase( key );
			delete pObj;
		}

		virtual void destroy( const keytype &key )
		{
			int iStream = hId.get( key );
			mStore.deleteStream( iStream );
			hId.erase( key );
		}

		virtual bool has( const keytype &key )
		{
			return hId.has( key );
		}

		virtual Bu::List<keytype> getKeys()
		{
			return hId.getKeys();
		}

		virtual int getSize()
		{
			return hId.getSize();
		}

	private:
		Myriad mStore;
		typedef Bu::Hash<keytype, long> StreamHash;
		StreamHash hId;
	};
};

#endif