casacore
Loading...
Searching...
No Matches
BucketCache.h
Go to the documentation of this file.
1// # BucketCache.h: Cache for buckets in a part of a file
2// # Copyright (C) 1994,1995,1996,1999,2000,2001
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_BUCKETCACHE_H
27#define CASA_BUCKETCACHE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/IO/BucketFile.h>
32#include <casacore/casa/Containers/Block.h>
33#include <casacore/casa/OS/CanonicalConversion.h>
34
35// # Forward clarations
36#include <casacore/casa/iosfwd.h>
37
38namespace casacore { // # NAMESPACE CASACORE - BEGIN
39
40// <summary>
41// Define the type of the static read and write function.
42// </summary>
43// <use visibility=export>
44// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
45// </reviewed>
46
47// <synopsis>
48// The BucketCache class needs a way to convert its data from local
49// to canonical format and vice-versa. This is done by callback
50// functions defined at construction time.
51// <p>
52// The ToLocal callback function has to allocate a buffer of the correct
53// size and to copy/convert the canonical data in the input buffer to
54// this buffer. The pointer this newly allocated buffer has to be returned.
55// The BucketCache class keeps this pointer in the cache block.
56// <p>
57// The FromLocal callback function has to copy/convert the data from the
58// buffer in local format to the buffer in canonical format. It should
59// NOT delete the buffer; that has to be done by the DeleteBuffer function.
60// <p>
61// The AddBuffer callback function has to create (and initialize) a
62// buffer to be added to the file and cache.
63// When the file gets extended, BucketCache only registers the new size,
64// but does not werite anything. When a bucket is read between the
65// actual file size and the new file size, the AddBuffer callback function
66// is called to create a buffer and possibly initialize it.
67// <p>
68// The DeleteBuffer callback function has to delete the buffer
69// allocated by the ToLocal function.
70// <p>
71// The functions get a pointer to the owner object, which was provided
72// at construction time. The callback function has to cast this to the
73// correct type and can use it thereafter.
74// <br>
75// C++ supports pointers to members, but it is a bit hard. Therefore pointers
76// to static members are used (which are simple pointers to functions).
77// A pointer to the owner object is also passed to let the static function
78// call the correct member function (when needed).
79// </synopsis>
80//
81// <example>
82// See class <linkto class=BucketCache>BucketCache</linkto>.
83// </example>
84
85// <group name=BucketCache_CallBack>
86typedef char* (*BucketCacheToLocal)(void* ownerObject, const char* canonical);
87typedef void (*BucketCacheFromLocal)(void* ownerObject, char* canonical, const char* local);
88typedef char* (*BucketCacheAddBuffer)(void* ownerObject);
89typedef void (*BucketCacheDeleteBuffer)(void* ownerObject, char* buffer);
90// </group>
91
92// <summary>
93// Cache for buckets in a part of a file
94// </summary>
95
96// <use visibility=export>
97
98// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
99// </reviewed>
100
101// <prerequisite>
102// # Classes you should understand before using this one.
103// <li> <linkto class=BucketFile>BucketFile</linkto>
104// </prerequisite>
105
106// <etymology>
107// BucketCache implements a cache for buckets in (a part of) a file.
108// </etymology>
109
110// <synopsis>
111// A cache may allow more efficient quasi-random IO.
112// It can, for instance, be used when a limited number of blocks
113// in a file have to be accessed again and again.
114// <p>
115// The class BucketCache provides such a cache. It can be used on a
116// consecutive part of a file as long as that part is not simultaneously
117// accessed in another way (including another BucketCache object).
118// <p>
119// BucketCache stores the data as given.
120// It uses <linkto group=BucketCache_CallBack>callback functions</linkto>
121// to allocate/delete buffers and to convert the data to/from local format.
122// <p>
123// When a new bucket is needed and all slots in the cache are used,
124// BucketCache will remove the least recently used bucket from the
125// cache. When the dirty flag is set, it will first be written.
126// <p>
127// BucketCache maintains a list of free buckets. Initially this list is
128// empty. When a bucket is removed, it is added to the free list.
129// AddBucket will take buckets from the free list before extending the file.
130// <p>
131// Since it is possible to handle only a part of a file by a BucketCache
132// object, it is also possible to have multiple BucketCache objects on
133// the same file (as long as they access disjoint parts of the file).
134// Each BucketCache object can have its own bucket size. This can,
135// for example, be used to have tiled arrays with different tile shapes
136// in the same file.
137// <p>
138// Statistics are kept to know how efficient the cache is working.
139// It is possible to initialize and show the statistics.
140// </synopsis>
141
142// <motivation>
143// A cache may reduce IO traffix considerably.
144// Furthermore it is more efficient to keep a cache in local format.
145// In that way conversion to/from local only have to be done when
146// data gets read/written. It also allows for precalculations.
147// </motivation>
148
149// <example>
150// <srcblock>
151// // Define the callback function for reading a bucket.
152// char* bToLocal (void*, const char* data)
153// {
154// char* ptr = new char[32768];
155// memcpy (ptr, data, 32768);
156// return ptr;
157// }
158// // Define the callback function for writing a bucket.
159// void bFromLocal (void*, char* data, const char* local)
160// {
161// memcpy (data, local, 32768);
162// }
163// // Define the callback function for initializing a new bucket.
164// char* bAddBuffer (void*)
165// {
166// char* ptr = new char[32768];
167// for (uInt i=0; i++; i<32768) {
168// ptr[i] = 0;
169// }
170// return ptr;
171// }
172// // Define the callback function for deleting a bucket.
173// void bDeleteBuffer (void*, char* buffer)
174// {
175// delete [] buffer;
176// }
177//
178// void someFunc()
179// {
180// // Open the filebuf.
181// BucketFile file(...);
182// file.open();
183// uInt i;
184// // Create a cache for the part of the file starting at offset 512
185// // consisting of 1000 buckets. The cache consists of 10 buckets.
186// // Each bucket is 32768 bytes.
187// BucketCache cache (&file, 512, 32768, 1000, 10, 0,
188// bToLocal, bFromLocal, bAddBuffer, bDeleteBuffer);
189// // Write all buckets into the file.
190// for (i=0; i<100; i++) {
191// char* buf = new char[32768];
192// cache.addBucket (buf);
193// }
194// Flush the cache to write all buckets in it.
195// cache.flush();
196// // Read all buckets from the file.
197// for (i=0; i<1000; i++) {
198// char* buf = cache.getBucket(i);
199// ...
200// }
201// cout << cache.nBucket() << endl;
202// }
203// </srcblock>
204// </example>
205
206// <todo asof="$DATE:$">
207// <li> When ready, use HashMap for the internal maps.
208// </todo>
209
211 public:
212 // Create the cache for (a part of) a file.
213 // The file part used starts at startOffset. Its length is
214 // bucketSize*nrOfBuckets bytes.
215 // When the file is smaller, the remainder is indicated as an extension
216 // similarly to the behaviour of function extend.
217 BucketCache(BucketFile* file, Int64 startOffset, uInt bucketSize, uInt nrOfBuckets,
218 uInt cacheSize, void* ownerObject, BucketCacheToLocal readCallBack,
219 BucketCacheFromLocal writeCallBack, BucketCacheAddBuffer addCallBack,
220 BucketCacheDeleteBuffer deleteCallBack);
221
223
224 // Flush the cache from the given slot on.
225 // By default the entire cache is flushed.
226 // When the entire cache is flushed, possible remaining uninitialized
227 // buckets will be initialized first.
228 // A True status is returned when buckets had to be written.
229 Bool flush(uInt fromSlot = 0);
230
231 // Clear the cache from the given slot on.
232 // By default the entire cache is cleared.
233 // It will remove the buckets in the cleared part.
234 // If wanted and needed, the buckets are flushed to the file
235 // before removing them.
236 // It can be used to enforce rereading buckets from the file.
237 void clear(uInt fromSlot = 0, Bool doFlush = True);
238
239 // Resize the cache.
240 // When the cache gets smaller, the latter buckets are cached out.
241 // It does not take "least recently used" into account.
243
244 // Resynchronize the object (after another process updated the file).
245 // It clears the cache (so all data will be reread) and sets
246 // the new sizes.
247 void resync(uInt nrBucket, uInt nrOfFreeBucket, Int firstFreeBucket);
248
249 // Get the current nr of buckets in the file.
250 uInt nBucket() const;
251
252 // Get the current cache size (in buckets).
253 uInt cacheSize() const;
254
255 // Set the dirty bit for the current bucket.
256 void setDirty();
257
258 // Make another bucket current.
259 // When no more cache slots are available, the one least recently
260 // used is flushed.
261 // The data in the bucket is converted using the ToLocal callback
262 // function. When the bucket does not exist yet in the file, it
263 // gets added and initialized using the AddBuffer callback function.
264 // A pointer to the data in converted format is returned.
265 char* getBucket(uInt bucketNr);
266
267 // Extend the file with the given number of buckets.
268 // The buckets get initialized when they are acquired
269 // (using getBucket) for the first time.
270 void extend(uInt nrBucket);
271
272 // Add a bucket to the file and make it the current one.
273 // When no more cache slots are available, the one least recently
274 // used is flushed.
275 // <br> When no free buckets are available, the file will be
276 // extended with one bucket. It returns the new bucket number.
277 // The buffer must have been allocated on the heap.
278 // It will get part of the cache; its contents are not copied.
279 // Thus the buffer should hereafter NOT be used for other purposes.
280 // It will be deleted later via the DeleteBuffer callback function.
281 // The data is copied into the bucket. A pointer to the data in
282 // local format is returned.
283 uInt addBucket(char* data);
284
285 // Remove the current bucket; i.e. add it to the beginning of the
286 // free bucket list.
288
289 // Get a part from the file outside the cached area.
290 // It is checked if that part is indeed outside the cached file area.
291 void get(char* buf, uInt length, Int64 offset);
292
293 // Put a part from the file outside the cached area.
294 // It is checked if that part is indeed outside the cached file area.
295 void put(const char* buf, uInt length, Int64 offset);
296
297 // Get the bucket number of the first free bucket.
298 // -1 = no free buckets.
299 Int firstFreeBucket() const;
300
301 // Get the number of free buckets.
302 uInt nFreeBucket() const;
303
304 // (Re)initialize the cache statistics.
306
307 // Show the statistics.
308 void showStatistics(ostream& os) const;
309
310 private:
311 // The file used.
313 // The owner object.
315 // The read callback function.
316 BucketCacheToLocal its_ReadCallBack;
317 // The write callback function.
318 BucketCacheFromLocal its_WriteCallBack;
319 // The add bucket callback function.
320 BucketCacheAddBuffer its_InitCallBack;
321 // The delete callback function.
322 BucketCacheDeleteBuffer its_DeleteCallBack;
323 // The starting offsets of the buckets in the file.
325 // The bucket size.
327 // The current nr of buckets in the file.
329 // The new nr of buckets in the file (after extension).
331 // The size of the cache (i.e. #buckets fitting in it).
333 // The nr of slots used in the cache.
335 // The cache itself.
337 // The cache slot actually used.
339 // The slot numbers of the buckets in the cache (-1 = not in cache).
341 // The buckets in the cache.
343 // Determine if a block is dirty (i.e. changed) (1=dirty).
345 // Determine when a block is used for the last time.
347 // The Least Recently Used counter.
349 // The internal buffer.
351 // The number of free buckets.
353 // The first free bucket (-1 = no free buckets).
355 // The statistics.
360
361 // Copy constructor is not possible.
363
364 // Assignment is not possible.
366
367 // Set the LRU information for the current slot.
368 void setLRU();
369
370 // Get a cache slot for the bucket.
371 void getSlot(uInt bucketNr);
372
373 // Write a bucket.
374 void writeBucket(uInt slotNr);
375
376 // Read a bucket.
377 void readBucket(uInt slotNr);
378
379 // Initialize the bucket buffer.
380 // The uninitialized buckets before this bucket are also initialized.
381 // It returns a pointer to the buffer.
382 void initializeBuckets(uInt bucketNr);
383
384 // Check if the offset of a non-cached part is correct.
386};
387
388inline uInt BucketCache::cacheSize() const { return its_CacheSize; }
389
391
393
394} // namespace casacore
395
396#endif
uInt its_ActualSlot
The cache slot actually used.
Int64 its_StartOffset
The starting offsets of the buckets in the file.
uInt its_LRUCounter
The Least Recently Used counter.
BucketCache(BucketFile *file, Int64 startOffset, uInt bucketSize, uInt nrOfBuckets, uInt cacheSize, void *ownerObject, BucketCacheToLocal readCallBack, BucketCacheFromLocal writeCallBack, BucketCacheAddBuffer addCallBack, BucketCacheDeleteBuffer deleteCallBack)
Create the cache for (a part of) a file.
Block< uInt > its_LRU
Determine when a block is used for the last time.
Block< uInt > its_Dirty
Determine if a block is dirty (i.e.
void initializeBuckets(uInt bucketNr)
Initialize the bucket buffer.
void setDirty()
Set the dirty bit for the current bucket.
Bool flush(uInt fromSlot=0)
Flush the cache from the given slot on.
uInt its_NrOfFree
The number of free buckets.
Block< uInt > its_BucketNr
The buckets in the cache.
void resize(uInt cacheSize)
Resize the cache.
void removeBucket()
Remove the current bucket; i.e.
uInt naccess_p
The statistics.
uInt nBucket() const
Get the current nr of buckets in the file.
void resync(uInt nrBucket, uInt nrOfFreeBucket, Int firstFreeBucket)
Resynchronize the object (after another process updated the file).
void showStatistics(ostream &os) const
Show the statistics.
void writeBucket(uInt slotNr)
Write a bucket.
BucketCacheAddBuffer its_InitCallBack
The add bucket callback function.
uInt its_NewNrOfBuckets
The new nr of buckets in the file (after extension).
Int firstFreeBucket() const
Get the bucket number of the first free bucket.
void readBucket(uInt slotNr)
Read a bucket.
BucketCache(const BucketCache &)
Copy constructor is not possible.
void initStatistics()
(Re)initialize the cache statistics.
char * getBucket(uInt bucketNr)
Make another bucket current.
BucketCacheDeleteBuffer its_DeleteCallBack
The delete callback function.
void extend(uInt nrBucket)
Extend the file with the given number of buckets.
BucketCacheToLocal its_ReadCallBack
The read callback function.
BucketCache & operator=(const BucketCache &)
Assignment is not possible.
Block< Int > its_SlotNr
The slot numbers of the buckets in the cache (-1 = not in cache).
uInt cacheSize() const
Get the current cache size (in buckets).
void checkOffset(uInt length, Int64 offset) const
Check if the offset of a non-cached part is correct.
Block< char * > its_Cache
The cache itself.
uInt its_CacheSize
The size of the cache (i.e.
BucketCacheFromLocal its_WriteCallBack
The write callback function.
void setLRU()
Set the LRU information for the current slot.
void * its_Owner
The owner object.
char * its_Buffer
The internal buffer.
Int its_FirstFree
The first free bucket (-1 = no free buckets).
uInt its_CurNrOfBuckets
The current nr of buckets in the file.
uInt nFreeBucket() const
Get the number of free buckets.
void clear(uInt fromSlot=0, Bool doFlush=True)
Clear the cache from the given slot on.
void getSlot(uInt bucketNr)
Get a cache slot for the bucket.
BucketFile * its_file
The file used.
uInt addBucket(char *data)
Add a bucket to the file and make it the current one.
uInt its_BucketSize
The bucket size.
void put(const char *buf, uInt length, Int64 offset)
Put a part from the file outside the cached area.
uInt its_CacheSizeUsed
The nr of slots used in the cache.
void get(char *buf, uInt length, Int64 offset)
Get a part from the file outside the cached area.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
int offset(int, int) const
compute a linear offset from array indicies
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
void(* BucketCacheDeleteBuffer)(void *ownerObject, char *buffer)
Definition BucketCache.h:90
void(* BucketCacheFromLocal)(void *ownerObject, char *canonical, const char *local)
Definition BucketCache.h:88