Commit 8efe7b48 authored by Paal Kvamme's avatar Paal Kvamme
Browse files

Cache the result of filestats() when the file is opened read only.

parent 3b2a85ac
......@@ -318,7 +318,7 @@ public:
hh.bins() + hh.bincount()));
}
virtual FileStatistics filestats() const override
FileStatistics filestats_nocache() const
{
using InternalZGY::LookupTable;
using InternalZGY::BrickStatus;
......@@ -443,6 +443,19 @@ public:
return result;
}
/**
* This method should be overwridden in ZgyReader and ZgyWriter.
* If it is ok to have ZgyMeta be an abstract type then this
* implementation can simply be removed.
*
* Calling this generic version of filestats() method will not
* populate the file size and will not do any caching.
*/
virtual FileStatistics filestats() const override
{
return filestats_nocache();
}
};
/**
......@@ -523,6 +536,8 @@ class ZgyReader : public ZgyMetaAndTools, virtual public IZgyReader
private:
std::shared_ptr<InternalZGY::FileADT> _fd;
std::shared_ptr<const InternalZGY::ZgyInternalBulk> _accessor;
std::shared_ptr<const FileStatistics> _filestats;
mutable std::mutex _filestats_mutex;
public:
/**
......@@ -694,14 +709,26 @@ public:
}
/**
* filestats() needs to be overridden in ZgyReader and ZgyWriter
* if we want to make the physical file size accessible.
* Get the file statistics of a file currently opened for read.
* The result does not change while the file is open, so it can
* be cached here.
*/
virtual FileStatistics filestats() const override
{
FileStatistics result = ZgyMeta::filestats();
result._file_size = _fd->xx_eof();
return result;
if (!_filestats) {
FileStatistics result = filestats_nocache();
// The base class has no _fd member so I need to set the size here.
result._file_size = _fd->xx_eof();
// The cache is semantically const.
std::shared_ptr<const FileStatistics> stats(new FileStatistics(result));
{
// Too bad there is no proper atomic_shared_ptr yet.
std::lock_guard<std::mutex> lk(_filestats_mutex);
const_cast<ZgyReader*>(this)->_filestats = stats;
}
}
std::lock_guard<std::mutex> lk(_filestats_mutex);
return *_filestats;
}
};
......@@ -1121,12 +1148,16 @@ public:
}
/**
* filestats() needs to be overridden in ZgyReader and ZgyWriter
* if we want to make the physical file size accessible.
* Get the file statistics of a file currently opened for write.
* There is no caching because the result will change whenever data
* is written. If this is a problem then implement a cache as done
* in ZgyReader and arrange for it to be cleared every time the
* metadata is touched.
*/
virtual FileStatistics filestats() const override
{
FileStatistics result = ZgyMeta::filestats();
FileStatistics result = filestats_nocache();
// The base class has no _fd member so I need to set the size here.
result._file_size = _fd->xx_eof();
return result;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment