vrecko
virtual reality framework
|
Caching that persists multiple application sessions. More...
#include <PersistentCache.h>
Classes | |
class | OpenedCacheFile |
Public Member Functions | |
PersistentCache () | |
~PersistentCache () | |
void | setEnable (bool bNewEnableCache) |
bool | getEnable () |
const char * | getCacheDir () |
osg::Node * | loadModel (const char *relatedFileName, vrecko::EnvironmentObject *obj) |
bool | saveModel (const char *relatedFileName, osg::Node *node, vrecko::EnvironmentObject *obj) |
bool | openCacheFileForReading (const char *dataName, const char *relatedFileName, unsigned long version, OpenedCacheFile **cacheData, vrecko::BufferedFile **bfile, unsigned long ulWaitTimeout=0) |
void | closeCacheFile (OpenedCacheFile *cacheData, vrecko::BufferedFile *bfile) |
bool | openCacheFileForWriting (const char *dataName, const char *relatedFileName, unsigned long version, OpenedCacheFile **cacheData, vrecko::BufferedFileWrite **bfile, unsigned long ulWaitTimeout=0) |
void | closeCacheFile (OpenedCacheFile *cacheData, vrecko::BufferedFileWrite *bfile) |
Protected Member Functions | |
void | translateName (char *outTranslatedName, const char *inDataName, const char *inRelatedFileName) |
int | internalFindAndLock (const char *originalFileName, const char *translatedFileName, unsigned long version, const char *desiredExtension, bool bReading, unsigned long ulWaitTimeout) |
bool | internalOpenReadWrite (const char *dataName, const char *relatedFileName, unsigned long version, OpenedCacheFile **cacheData, vrecko::BufferedFile **bfile, vrecko::BufferedFileWrite **bfileWrite, unsigned long ulWaitTimeout) |
void | internalDecrementReaderWriter (OpenedCacheFile *cacheData, bool bReading) |
bool | testVersionIsAtLeast (const char *fileName, int minVersion) |
void | constructModelDataName (char *outDataName, vrecko::EnvironmentObject *obj) |
Protected Attributes | |
bool | bEnableCache |
DynamicArray< OpenedCacheFile * > | openedFiles |
MTLock | filesLock |
CHAR | baseDir [1000] |
CHAR | cacheDir [1000] |
Caching that persists multiple application sessions.
Persistent cache with data saving into files (into the "cache" directory). The class is multithread safe, multiple threads can read from the same file. Only one writer can write into a file - readers will be blocked and everything should be correctly managed automatically, i.e. from the time the writer requests writing, the next requesting readers will be blocked and remaining readers will finish their reading. Then the writer will be allowed to write (all this happen if the [Timeout] is sufficient).
IDEAS: 0. Most of the data are related to files - i.e. we will check whether the specific file hasn't changed.
This second name is composed of [the translated original file name] + [data name] + [timestamp] + [extension]
vrecko::PersistentCache::PersistentCache | ( | ) |
vrecko::PersistentCache::~PersistentCache | ( | ) |
void vrecko::PersistentCache::closeCacheFile | ( | OpenedCacheFile * | cacheData, |
vrecko::BufferedFile * | bfile | ||
) |
The [cacheData] + [bfile] structures will be NO LONGER VALID after calling this method.
void vrecko::PersistentCache::closeCacheFile | ( | OpenedCacheFile * | cacheData, |
vrecko::BufferedFileWrite * | bfile | ||
) |
The [cacheData] + [bfile] structures will be NO LONGER VALID after calling this method.
|
protected |
Used mainly in the loadModel() and saveModel() methods. The [obj] is necessary for getting attributes related to the model, i.e. Is it optimized? Is it smoothed? etc. - all produces different cache file The [obj] might be NULL in which case the default parameters are used.
|
inline |
|
inline |
Is caching enabled?
|
protected |
Effectively UNLOCK a FILE for reading/writing. The [cacheData] MIGHT BE INVALID after calling this method. It might get deleted if noone else uses that file. The real file should be close BEFORE calling this method.
|
protected |
Note: The function will acquire the [filesLock] itself. This function will find correct entry in [openedFiles] OR will create a new one if it doesn't exist. If the entry exist, the function will check if the file itself is readable/writable and in case it is not it will wait for [ulWaitTimeout] ms (may be also 0 or INFINITE). Result is a 0 or positive number (if the entry was found/created) and the file can be processed, or -1 in case of blockage. If the result is 0 or positive number, the [filesLock] will REMAIN LOCKED AT THE END OF THIS FUNCTION. The function employs a busy-waiting (with 1 milisecond Sleep() ). The [ulWaitTimeout] is probably only approximate and not a precise number. The [desiredExtension] is used only if writing.
|
protected |
osg::Node * vrecko::PersistentCache::loadModel | ( | const char * | relatedFileName, |
vrecko::EnvironmentObject * | obj | ||
) |
The [obj] is necessary for getting attributes related to the model, i.e. Is it optimized? Is it smoothed? etc. - all produces different cache file The [obj] might be NULL in which case the default parameters are used.
|
inline |
NOTE: [version] = 0 is NOT RECOMMENDED In case of success the [bfile] will hold a pointer to the existing file opened for reading and [cacheData] will hold proper structure which acts as a handle. Note: [ulWaitTimeout] can be INFINITE (= Windows' numeric constant). The [cacheData] may be NULL.
|
inline |
NOTE: [version] = 0 is NOT RECOMMENDED In case of success the [bfile] will hold a pointer to the new empty file opened for writing and [cacheData] will hold proper structure which acts as a handle. Note: [ulWaitTimeout] can be INFINITE (= Windows' numeric constant).
bool vrecko::PersistentCache::saveModel | ( | const char * | relatedFileName, |
osg::Node * | node, | ||
vrecko::EnvironmentObject * | obj | ||
) |
The [obj] is necessary for getting attributes related to the model, i.e. Is it optimized? Is it smoothed? etc. - all produces different cache file The [obj] might be NULL in which case the default parameters are used.
void vrecko::PersistentCache::setEnable | ( | bool | bNewEnableCache | ) |
Can be used to disable and enable the whole caching system.
|
protected |
Test whether the file name contains version at the end (returning "false" if not) and the version is at least [minVersion].
|
protected |
Translates original file name into the safe name ending with '$', the date part and the final extension of the resulting file will be missing. The file path will be translated into a path relative to the main vrecko directory.
|
protected |
Main program directory. Ending with backslash.
|
protected |
|
protected |
Cache directory. Ending with backslash.
|
protected |
Lock for access to the [openedFiles] structure. ( It doesn't block (accesses to multiple files at once) or (multiple read accesses to a single file)
|
protected |
MUST be array of pointers and NOT array of structures -> we will send these pointers as parameters and this would be screwed if the array was reallocated.