Identify files in object cache by filename and optional provided options.
Objects with the same filename may be different from others based on the provided plugin options. Using filename *and* the provided options as object cache key helps to avoid fetching the wrong object.
This commit is contained in:
committed by
Robert Osfield
parent
3816e4c76e
commit
85cd1c456f
@@ -12,6 +12,7 @@
|
||||
*/
|
||||
|
||||
#include <osgDB/ObjectCache>
|
||||
#include <osgDB/Options>
|
||||
|
||||
using namespace osgDB;
|
||||
|
||||
@@ -39,34 +40,48 @@ void ObjectCache::addObjectCache(ObjectCache* objectCache)
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock1(_objectCacheMutex);
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock2(objectCache->_objectCacheMutex);
|
||||
|
||||
// OSG_NOTICE<<"Inserting objects to main ObjectCache "<<objectCache->_objectCache.size()<<std::endl;
|
||||
OSG_DEBUG<<"Inserting objects to main ObjectCache "<<objectCache->_objectCache.size()<<std::endl;
|
||||
|
||||
_objectCache.insert(objectCache->_objectCache.begin(), objectCache->_objectCache.end());
|
||||
}
|
||||
|
||||
|
||||
void ObjectCache::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp)
|
||||
void ObjectCache::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp, const Options *options)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
_objectCache[filename]=ObjectTimeStampPair(object,timestamp);
|
||||
_objectCache[FileNameOptionsPair(filename, osg::clone(options))] = ObjectTimeStampPair(object,timestamp);
|
||||
OSG_DEBUG<<"Adding "<<filename<<" with options '"<<(options ? options->getOptionString() : "")<<"' to ObjectCache "<<this<<std::endl;
|
||||
}
|
||||
|
||||
osg::Object* ObjectCache::getFromObjectCache(const std::string& fileName)
|
||||
osg::Object* ObjectCache::getFromObjectCache(const std::string& fileName, const Options *options)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
||||
if (itr!=_objectCache.end()) return itr->second.first.get();
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(FileNameOptionsPair(fileName, options));
|
||||
if (itr!=_objectCache.end())
|
||||
{
|
||||
osg::ref_ptr<const osgDB::Options> o = itr->first.second;
|
||||
if (o.valid())
|
||||
OSG_DEBUG<<"Found "<<fileName<<" with options '"<< o->getOptionString()<< "' in ObjectCache "<<this<<std::endl;
|
||||
else
|
||||
OSG_DEBUG<<"Found "<<fileName<<" in ObjectCache "<<this<<std::endl;
|
||||
return itr->second.first.get();
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Object> ObjectCache::getRefFromObjectCache(const std::string& fileName)
|
||||
osg::ref_ptr<osg::Object> ObjectCache::getRefFromObjectCache(const std::string& fileName, const Options *options)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
||||
ObjectCacheMap::iterator itr;
|
||||
itr = _objectCache.find(FileNameOptionsPair(fileName, options));
|
||||
if (itr!=_objectCache.end())
|
||||
{
|
||||
// OSG_NOTICE<<"Found "<<fileName<<" in ObjectCache "<<this<<std::endl;
|
||||
return itr->second.first;
|
||||
osg::ref_ptr<const osgDB::Options> o = itr->first.second;
|
||||
if (o.valid())
|
||||
OSG_DEBUG<<"Found "<<fileName<<" with options '"<< o->getOptionString()<< "' in ObjectCache "<<this<<std::endl;
|
||||
else
|
||||
OSG_DEBUG<<"Found "<<fileName<<" in ObjectCache "<<this<<std::endl;
|
||||
return itr->second.first.get();
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
@@ -108,10 +123,10 @@ void ObjectCache::removeExpiredObjectsInCache(double expiryTime)
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectCache::removeFromObjectCache(const std::string& fileName)
|
||||
void ObjectCache::removeFromObjectCache(const std::string& fileName, const Options *options)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(FileNameOptionsPair(fileName, options));
|
||||
if (itr!=_objectCache.end()) _objectCache.erase(itr);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,3 +56,17 @@ void Options::parsePluginStringData(const std::string& str, char separator1, cha
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Options::operator <(const Options &rhs) const
|
||||
{
|
||||
// TODO add better compare
|
||||
//OSG_DEBUG << "comparing <'" << _str << "' with '" << rhs._str << "'" << std::endl;
|
||||
return _str.compare(rhs._str) < 0;
|
||||
}
|
||||
|
||||
bool Options::operator ==(const Options &rhs) const
|
||||
{
|
||||
// TODO add better compare
|
||||
//OSG_DEBUG << "comparing == '" << _str << "' with '" << rhs._str << "'" << std::endl;
|
||||
return _str.compare(rhs._str) == 0;
|
||||
}
|
||||
|
||||
@@ -1250,9 +1250,9 @@ ReaderWriter::ReadResult Registry::readImplementation(const ReadFunctor& readFun
|
||||
if (useObjectCache)
|
||||
{
|
||||
// search for entry in the object cache.
|
||||
osg::ref_ptr<osg::Object> object = optionsCache ? optionsCache->getRefFromObjectCache(file) : 0;
|
||||
osg::ref_ptr<osg::Object> object = optionsCache ? optionsCache->getRefFromObjectCache(file, options) : 0;
|
||||
|
||||
if (!object && _objectCache.valid()) object = _objectCache->getRefFromObjectCache(file);
|
||||
if (!object && _objectCache.valid()) object = _objectCache->getRefFromObjectCache(file, options);
|
||||
|
||||
if (object.valid())
|
||||
{
|
||||
@@ -1264,7 +1264,7 @@ ReaderWriter::ReadResult Registry::readImplementation(const ReadFunctor& readFun
|
||||
if (rr.validObject())
|
||||
{
|
||||
// search AGAIN for entry in the object cache.
|
||||
object = _objectCache->getRefFromObjectCache(file);
|
||||
object = _objectCache->getRefFromObjectCache(file, options);
|
||||
if (object.valid())
|
||||
{
|
||||
if (readFunctor.isValid(object.get())) return ReaderWriter::ReadResult(object.get(), ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE);
|
||||
@@ -1275,8 +1275,8 @@ ReaderWriter::ReadResult Registry::readImplementation(const ReadFunctor& readFun
|
||||
}
|
||||
|
||||
// update cache with new entry.
|
||||
if (optionsCache) optionsCache->addEntryToObjectCache(file, rr.getObject(), 0.0);
|
||||
else if (_objectCache.valid()) _objectCache->addEntryToObjectCache(file, rr.getObject(), 0.0);
|
||||
if (optionsCache) optionsCache->addEntryToObjectCache(file, rr.getObject(), 0.0, options);
|
||||
else if (_objectCache.valid()) _objectCache->addEntryToObjectCache(file, rr.getObject(), 0.0, options);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1597,19 +1597,19 @@ ReaderWriter::WriteResult Registry::writeScriptImplementation(const Script& imag
|
||||
return result;
|
||||
}
|
||||
|
||||
void Registry::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp)
|
||||
void Registry::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp, Options *options)
|
||||
{
|
||||
if (_objectCache.valid()) _objectCache->addEntryToObjectCache(filename, object, timestamp);
|
||||
if (_objectCache.valid()) _objectCache->addEntryToObjectCache(filename, object, timestamp, options);
|
||||
}
|
||||
|
||||
osg::Object* Registry::getFromObjectCache(const std::string& filename)
|
||||
osg::Object* Registry::getFromObjectCache(const std::string& filename, Options *options)
|
||||
{
|
||||
return _objectCache.valid() ? _objectCache->getFromObjectCache(filename) : 0;
|
||||
return _objectCache.valid() ? _objectCache->getFromObjectCache(filename, options) : 0;
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Object> Registry::getRefFromObjectCache(const std::string& filename)
|
||||
osg::ref_ptr<osg::Object> Registry::getRefFromObjectCache(const std::string& filename, Options *options)
|
||||
{
|
||||
return _objectCache.valid() ? _objectCache->getRefFromObjectCache(filename) : 0;
|
||||
return _objectCache.valid() ? _objectCache->getRefFromObjectCache(filename, options) : 0;
|
||||
}
|
||||
|
||||
void Registry::updateTimeStampOfObjectsInCacheWithExternalReferences(const osg::FrameStamp& frameStamp)
|
||||
@@ -1623,9 +1623,9 @@ void Registry::removeExpiredObjectsInCache(const osg::FrameStamp& frameStamp)
|
||||
if (_objectCache.valid()) _objectCache->removeExpiredObjectsInCache(expiryTime);
|
||||
}
|
||||
|
||||
void Registry::removeFromObjectCache(const std::string& filename)
|
||||
void Registry::removeFromObjectCache(const std::string& filename, Options *options)
|
||||
{
|
||||
if (_objectCache.valid()) _objectCache->removeFromObjectCache(filename);
|
||||
if (_objectCache.valid()) _objectCache->removeFromObjectCache(filename, options);
|
||||
}
|
||||
|
||||
void Registry::clearObjectCache()
|
||||
|
||||
Reference in New Issue
Block a user