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
38
39namespace casacore { //# NAMESPACE CASACORE - BEGIN
40
41// <summary>
42// Define the type of the static read and write function.
43// </summary>
44// <use visibility=export>
45// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
46// </reviewed>
47
48// <synopsis>
49// The BucketCache class needs a way to convert its data from local
50// to canonical format and vice-versa. This is done by callback
51// functions defined at construction time.
52// <p>
53// The ToLocal callback function has to allocate a buffer of the correct
54// size and to copy/convert the canonical data in the input buffer to
55// this buffer. The pointer this newly allocated buffer has to be returned.
56// The BucketCache class keeps this pointer in the cache block.
57// <p>
58// The FromLocal callback function has to copy/convert the data from the
59// buffer in local format to the buffer in canonical format. It should
60// NOT delete the buffer; that has to be done by the DeleteBuffer function.
61// <p>
62// The AddBuffer callback function has to create (and initialize) a
63// buffer to be added to the file and cache.
64// When the file gets extended, BucketCache only registers the new size,
65// but does not werite anything. When a bucket is read between the
66// actual file size and the new file size, the AddBuffer callback function
67// is called to create a buffer and possibly initialize it.
68// <p>
69// The DeleteBuffer callback function has to delete the buffer
70// allocated by the ToLocal function.
71// <p>
72// The functions get a pointer to the owner object, which was provided
73// at construction time. The callback function has to cast this to the
74// correct type and can use it thereafter.
75// <br>
76// C++ supports pointers to members, but it is a bit hard. Therefore pointers
77// to static members are used (which are simple pointers to functions).
78// A pointer to the owner object is also passed to let the static function
79// call the correct member function (when needed).
80// </synopsis>
81//
82// <example>
83// See class <linkto class=BucketCache>BucketCache</linkto>.
84// </example>
85
86// <group name=BucketCache_CallBack>
87typedef char* (*BucketCacheToLocal) (void* ownerObject, const char* canonical);
88typedef void (*BucketCacheFromLocal) (void* ownerObject, char* canonical,
89 const char* local);
90typedef char* (*BucketCacheAddBuffer) (void* ownerObject);
91typedef void (*BucketCacheDeleteBuffer) (void* ownerObject, char* buffer);
92// </group>
93
94
95
96// <summary>
97// Cache for buckets in a part of a file
98// </summary>
99
100// <use visibility=export>
101
102// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
103// </reviewed>
104
105// <prerequisite>
106//# Classes you should understand before using this one.
107// <li> <linkto class=BucketFile>BucketFile</linkto>
108// </prerequisite>
109
110// <etymology>
111// BucketCache implements a cache for buckets in (a part of) a file.
112// </etymology>
113
114// <synopsis>
115// A cache may allow more efficient quasi-random IO.
116// It can, for instance, be used when a limited number of blocks
117// in a file have to be accessed again and again.
118// <p>
119// The class BucketCache provides such a cache. It can be used on a
120// consecutive part of a file as long as that part is not simultaneously
121// accessed in another way (including another BucketCache object).
122// <p>
123// BucketCache stores the data as given.
124// It uses <linkto group=BucketCache_CallBack>callback functions</linkto>
125// to allocate/delete buffers and to convert the data to/from local format.
126// <p>
127// When a new bucket is needed and all slots in the cache are used,
128// BucketCache will remove the least recently used bucket from the
129// cache. When the dirty flag is set, it will first be written.
130// <p>
131// BucketCache maintains a list of free buckets. Initially this list is
132// empty. When a bucket is removed, it is added to the free list.
133// AddBucket will take buckets from the free list before extending the file.
134// <p>
135// Since it is possible to handle only a part of a file by a BucketCache
136// object, it is also possible to have multiple BucketCache objects on
137// the same file (as long as they access disjoint parts of the file).
138// Each BucketCache object can have its own bucket size. This can,
139// for example, be used to have tiled arrays with different tile shapes
140// in the same file.
141// <p>
142// Statistics are kept to know how efficient the cache is working.
143// It is possible to initialize and show the statistics.
144// </synopsis>
145
146// <motivation>
147// A cache may reduce IO traffix considerably.
148// Furthermore it is more efficient to keep a cache in local format.
149// In that way conversion to/from local only have to be done when
150// data gets read/written. It also allows for precalculations.
151// </motivation>
152
153// <example>
154// <srcblock>
155// // Define the callback function for reading a bucket.
156// char* bToLocal (void*, const char* data)
157// {
158// char* ptr = new char[32768];
159// memcpy (ptr, data, 32768);
160// return ptr;
161// }
162// // Define the callback function for writing a bucket.
163// void bFromLocal (void*, char* data, const char* local)
164// {
165// memcpy (data, local, 32768);
166// }
167// // Define the callback function for initializing a new bucket.
168// char* bAddBuffer (void*)
169// {
170// char* ptr = new char[32768];
171// for (uInt i=0; i++; i<32768) {
172// ptr[i] = 0;
173// }
174// return ptr;
175// }
176// // Define the callback function for deleting a bucket.
177// void bDeleteBuffer (void*, char* buffer)
178// {
179// delete [] buffer;
180// }
181//
182// void someFunc()
183// {
184// // Open the filebuf.
185// BucketFile file(...);
186// file.open();
187// uInt i;
188// // Create a cache for the part of the file starting at offset 512
189// // consisting of 1000 buckets. The cache consists of 10 buckets.
190// // Each bucket is 32768 bytes.
191// BucketCache cache (&file, 512, 32768, 1000, 10, 0,
192// bToLocal, bFromLocal, bAddBuffer, bDeleteBuffer);
193// // Write all buckets into the file.
194// for (i=0; i<100; i++) {
195// char* buf = new char[32768];
196// cache.addBucket (buf);
197// }
198// Flush the cache to write all buckets in it.
199// cache.flush();
200// // Read all buckets from the file.
201// for (i=0; i<1000; i++) {
202// char* buf = cache.getBucket(i);
203// ...
204// }
205// cout << cache.nBucket() << endl;
206// }
207// </srcblock>
208// </example>
209
210// <todo asof="$DATE:$">
211// <li> When ready, use HashMap for the internal maps.
212// </todo>
213
214
216{
217public:
218
219 // Create the cache for (a part of) a file.
220 // The file part used starts at startOffset. Its length is
221 // bucketSize*nrOfBuckets bytes.
222 // When the file is smaller, the remainder is indicated as an extension
223 // similarly to the behaviour of function extend.
224 BucketCache (BucketFile* file, Int64 startOffset, uInt bucketSize,
225 uInt nrOfBuckets, uInt cacheSize,
226 void* ownerObject,
227 BucketCacheToLocal readCallBack,
228 BucketCacheFromLocal writeCallBack,
229 BucketCacheAddBuffer addCallBack,
230 BucketCacheDeleteBuffer deleteCallBack);
231
233
234 // Flush the cache from the given slot on.
235 // By default the entire cache is flushed.
236 // When the entire cache is flushed, possible remaining uninitialized
237 // buckets will be initialized first.
238 // A True status is returned when buckets had to be written.
239 Bool flush (uInt fromSlot = 0);
240
241 // Clear the cache from the given slot on.
242 // By default the entire cache is cleared.
243 // It will remove the buckets in the cleared part.
244 // If wanted and needed, the buckets are flushed to the file
245 // before removing them.
246 // It can be used to enforce rereading buckets from the file.
247 void clear (uInt fromSlot = 0, Bool doFlush = True);
248
249 // Resize the cache.
250 // When the cache gets smaller, the latter buckets are cached out.
251 // It does not take "least recently used" into account.
253
254 // Resynchronize the object (after another process updated the file).
255 // It clears the cache (so all data will be reread) and sets
256 // the new sizes.
257 void resync (uInt nrBucket, uInt nrOfFreeBucket, Int firstFreeBucket);
258
259 // Get the current nr of buckets in the file.
260 uInt nBucket() const;
261
262 // Get the current cache size (in buckets).
263 uInt cacheSize() const;
264
265 // Set the dirty bit for the current bucket.
266 void setDirty();
267
268 // Make another bucket current.
269 // When no more cache slots are available, the one least recently
270 // used is flushed.
271 // The data in the bucket is converted using the ToLocal callback
272 // function. When the bucket does not exist yet in the file, it
273 // gets added and initialized using the AddBuffer callback function.
274 // A pointer to the data in converted format is returned.
275 char* getBucket (uInt bucketNr);
276
277 // Extend the file with the given number of buckets.
278 // The buckets get initialized when they are acquired
279 // (using getBucket) for the first time.
280 void extend (uInt nrBucket);
281
282 // Add a bucket to the file and make it the current one.
283 // When no more cache slots are available, the one least recently
284 // used is flushed.
285 // <br> When no free buckets are available, the file will be
286 // extended with one bucket. It returns the new bucket number.
287 // The buffer must have been allocated on the heap.
288 // It will get part of the cache; its contents are not copied.
289 // Thus the buffer should hereafter NOT be used for other purposes.
290 // It will be deleted later via the DeleteBuffer callback function.
291 // The data is copied into the bucket. A pointer to the data in
292 // local format is returned.
293 uInt addBucket (char* data);
294
295 // Remove the current bucket; i.e. add it to the beginning of the
296 // free bucket list.
298
299 // Get a part from the file outside the cached area.
300 // It is checked if that part is indeed outside the cached file area.
301 void get (char* buf, uInt length, Int64 offset);
302
303 // Put a part from the file outside the cached area.
304 // It is checked if that part is indeed outside the cached file area.
305 void put (const char* buf, uInt length, Int64 offset);
306
307 // Get the bucket number of the first free bucket.
308 // -1 = no free buckets.
309 Int firstFreeBucket() const;
310
311 // Get the number of free buckets.
312 uInt nFreeBucket() const;
313
314 // (Re)initialize the cache statistics.
316
317 // Show the statistics.
318 void showStatistics (ostream& os) const;
319
320private:
321 // The file used.
323 // The owner object.
325 // The read callback function.
326 BucketCacheToLocal its_ReadCallBack;
327 // The write callback function.
328 BucketCacheFromLocal its_WriteCallBack;
329 // The add bucket callback function.
330 BucketCacheAddBuffer its_InitCallBack;
331 // The delete callback function.
332 BucketCacheDeleteBuffer its_DeleteCallBack;
333 // The starting offsets of the buckets in the file.
335 // The bucket size.
337 // The current nr of buckets in the file.
339 // The new nr of buckets in the file (after extension).
341 // The size of the cache (i.e. #buckets fitting in it).
343 // The nr of slots used in the cache.
345 // The cache itself.
347 // The cache slot actually used.
349 // The slot numbers of the buckets in the cache (-1 = not in cache).
351 // The buckets in the cache.
353 // Determine if a block is dirty (i.e. changed) (1=dirty).
355 // Determine when a block is used for the last time.
357 // The Least Recently Used counter.
359 // The internal buffer.
361 // The number of free buckets.
363 // The first free bucket (-1 = no free buckets).
365 // The statistics.
370
371
372 // Copy constructor is not possible.
374
375 // Assignment is not possible.
377
378 // Set the LRU information for the current slot.
379 void setLRU();
380
381 // Get a cache slot for the bucket.
382 void getSlot (uInt bucketNr);
383
384 // Write a bucket.
385 void writeBucket (uInt slotNr);
386
387 // Read a bucket.
388 void readBucket (uInt slotNr);
389
390 // Initialize the bucket buffer.
391 // The uninitialized buckets before this bucket are also initialized.
392 // It returns a pointer to the buffer.
393 void initializeBuckets (uInt bucketNr);
394
395 // Check if the offset of a non-cached part is correct.
396 void checkOffset (uInt length, Int64 offset) const;
397};
398
399
400
402 { return its_CacheSize; }
403
405 { return its_FirstFree; }
406
408 { return its_NrOfFree; }
409
410
411
412
413} //# NAMESPACE CASACORE - END
414
415#endif
simple 1-D array
Definition Block.h:198
Cache for buckets in a part of a file.
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.
uInt its_CacheSize
The size of the cache (i.e.
BucketCacheFromLocal its_WriteCallBack
The write callback function.
PtrBlock< char * > its_Cache
The cache itself.
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.
A drop-in replacement for Block<T*>.
Definition Block.h:812
this file contains all the compiler specific defines
Definition mainpage.dox:28
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:92
void(* BucketCacheFromLocal)(void *ownerObject, char *canonical, const char *local)
Definition BucketCache.h:89