aboutsummaryrefslogtreecommitdiff
path: root/src/cachestorenids.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/cachestorenids.h')
-rw-r--r--src/cachestorenids.h158
1 files changed, 0 insertions, 158 deletions
diff --git a/src/cachestorenids.h b/src/cachestorenids.h
deleted file mode 100644
index 293f521..0000000
--- a/src/cachestorenids.h
+++ /dev/null
@@ -1,158 +0,0 @@
1/*
2 * Copyright (C) 2007-2010 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_NIDS_H
9#define BU_CACHE_STORE_NIDS_H
10
11#include "bu/fstring.h"
12#include "bu/stream.h"
13#include "bu/nids.h"
14#include "bu/nidsstream.h"
15#include "bu/cachestore.h"
16
17#include "bu/file.h"
18#include "bu/archive.h"
19
20namespace Bu
21{
22 template<class keytype, class obtype>
23 keytype __cacheGetKey( const obtype *pObj );
24
25 template<class keytype, class obtype>
26 obtype *__cacheStoreNidsAlloc( const keytype &key )
27 {
28 return new obtype();
29 }
30
31 template<class keytype, class obtype>
32 void __cacheStoreNidsStore( Bu::Stream &s, obtype &rObj,
33 const keytype & )
34 {
35 Bu::Archive ar( s, Bu::Archive::save );
36 ar << rObj;
37 }
38
39 template<class keytype, class obtype>
40 obtype *__cacheStoreNidsLoad( Bu::Stream &s, const keytype &key )
41 {
42 obtype *pObj = __cacheStoreNidsAlloc<keytype, obtype>( key );
43 Bu::Archive ar( s, Bu::Archive::load );
44 ar >> (*pObj);
45 return pObj;
46 }
47
48 template<class keytype, class obtype>
49 class CacheStoreNids : public CacheStore<keytype, obtype>
50 {
51 public:
52 CacheStoreNids( Bu::Stream &sArch,
53 int iBlockSize=1024, int iPreAllocate=1 ) :
54 nStore( sArch )
55 {
56 try
57 {
58 nStore.initialize();
59 NidsStream ns = nStore.openStream( 0 );
60 Bu::Archive ar( ns, Bu::Archive::load );
61 ar >> hId;
62 }
63 catch( Bu::NidsException &e )
64 {
65 nStore.initialize( iBlockSize, iPreAllocate );
66 int iStream = nStore.createStream();
67 if( iStream != 0 )
68 throw Bu::ExceptionBase("That's...horrible...id = %d.\n\n", iStream );
69 NidsStream ns = nStore.openStream( 0 );
70 Bu::Archive ar( ns, Bu::Archive::save );
71 ar << hId;
72 }
73 }
74
75 virtual ~CacheStoreNids()
76 {
77 NidsStream ns = nStore.openStream( 0 );
78 Bu::Archive ar( ns, Bu::Archive::save );
79 ar << hId;
80 }
81
82 virtual obtype *load( const keytype &key )
83 {
84 int iStream = hId.get( key );
85 NidsStream ns = nStore.openStream( iStream );
86 obtype *pOb = __cacheStoreNidsLoad<keytype, obtype>( ns, key );
87 return pOb;
88 }
89
90 virtual void unload( obtype *pObj, const keytype &key )
91 {
92 delete pObj;
93 }
94
95 virtual keytype create( obtype *pSrc )
96 {
97 keytype key = __cacheGetKey<keytype, obtype>( pSrc );
98 int iStream = nStore.createStream();
99 hId.insert( key, iStream );
100 NidsStream ns = nStore.openStream( iStream );
101 __cacheStoreNidsStore<keytype, obtype>( ns, *pSrc, key );
102 return key;
103 }
104
105 virtual void sync()
106 {
107 NidsStream ns = nStore.openStream( 0 );
108 Bu::Archive ar( ns, Bu::Archive::save );
109 ar << hId;
110 }
111
112 virtual void sync( obtype *pSrc, const keytype &key )
113 {
114 int iStream = hId.get( key );
115 NidsStream ns = nStore.openStream( iStream );
116 __cacheStoreNidsStore<keytype, obtype>( ns, *pSrc, key );
117 }
118
119 virtual void destroy( obtype *pObj, const keytype &key )
120 {
121 int iStream = hId.get( key );
122 nStore.deleteStream( iStream );
123 hId.erase( key );
124 delete pObj;
125 sync();
126 }
127
128 virtual void destroy( const keytype &key )
129 {
130 int iStream = hId.get( key );
131 nStore.deleteStream( iStream );
132 hId.erase( key );
133 sync();
134 }
135
136 virtual bool has( const keytype &key )
137 {
138 return hId.has( key );
139 }
140
141 virtual Bu::List<keytype> getKeys()
142 {
143 return hId.getKeys();
144 }
145
146 virtual int getSize()
147 {
148 return hId.getSize();
149 }
150
151 private:
152 Nids nStore;
153 typedef Bu::Hash<keytype, long> NidHash;
154 NidHash hId;
155 };
156};
157
158#endif