Commit 5cdd5d1d authored by Paal Kvamme's avatar Paal Kvamme
Browse files

Merge branch 'kvamme62/smart-filestats' into 'master'

filestats() changed to return a smart pointer.

See merge request !53
parents 3b2a85ac 4e5cdfc5
Pipeline #31118 passed with stages
in 8 minutes and 2 seconds
......@@ -318,7 +318,7 @@ public:
hh.bins() + hh.bincount()));
}
virtual FileStatistics filestats() const override
std::shared_ptr<const FileStatistics> filestats_nocache() const
{
using InternalZGY::LookupTable;
using InternalZGY::BrickStatus;
......@@ -441,7 +441,20 @@ public:
// result._used_size + (result._file_size-(result._used_if_uncompressed)) / (double)result._file_size:
// 1);
return result;
return std::shared_ptr<const FileStatistics>(new FileStatistics(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 std::shared_ptr<const 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
virtual std::shared_ptr<const FileStatistics> filestats() const override
{
FileStatistics result = ZgyMeta::filestats();
result._file_size = _fd->xx_eof();
return result;
if (!_filestats) {
std::shared_ptr<FileStatistics> result
(new FileStatistics(*filestats_nocache()));
// The base class has no _fd member so I need to set the size here.
result->_file_size = _fd->xx_eof();
{
// Too bad there is no proper atomic_shared_ptr yet.
std::lock_guard<std::mutex> lk(_filestats_mutex);
// The cache is semantically const.
const_cast<ZgyReader*>(this)->_filestats = result;
}
}
std::lock_guard<std::mutex> lk(_filestats_mutex);
return _filestats;
}
};
......@@ -1121,13 +1148,18 @@ 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
virtual std::shared_ptr<const FileStatistics> filestats() const override
{
FileStatistics result = ZgyMeta::filestats();
result._file_size = _fd->xx_eof();
std::shared_ptr<FileStatistics> result
(new FileStatistics(*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;
}
};
......
......@@ -916,7 +916,7 @@ public:
virtual void dump(std::ostream&) const = 0; /**< \brief Output in human readable form for debugging. */
virtual SampleStatistics statistics() const = 0; /**< \brief Statistics of all sample values on the file. */
virtual SampleHistogram histogram() const = 0; /**< \brief Histogram of all sample values on the file. */
virtual FileStatistics filestats() const = 0; /**< \brief For display purposes only. */
virtual std::shared_ptr<const FileStatistics> filestats() const = 0; /**< \brief For display purposes only. */
};
/**
......
......@@ -235,7 +235,7 @@ public:
return writer_->histogram();
}
virtual FileStatistics filestats() const override
virtual std::shared_ptr<const FileStatistics> filestats() const override
{
std::lock_guard<std::mutex> lk(mutex_);
return writer_->filestats();
......
......@@ -70,7 +70,7 @@ public:
virtual void dump(std::ostream&) const {throw std::runtime_error("dump() has not been mocked");}
virtual SampleStatistics statistics() const {throw std::runtime_error("statistics() has not been mocked");}
virtual SampleHistogram histogram() const {throw std::runtime_error("histogram() has not been mocked");}
virtual FileStatistics filestats() const {throw std::runtime_error("filestats() has not been mocked");}
virtual std::shared_ptr<const FileStatistics> filestats() const {throw std::runtime_error("filestats() has not been mocked");}
// Functions from IZgyTools
virtual void transform(const corners_t& from, const corners_t& to, std::vector<std::array<float64_t,2>>&) const {throw std::runtime_error("coord handling has not been mocked");}
virtual std::array<float64_t,2> transform1(const corners_t& from, const corners_t& to, const std::array<float64_t,2>&) const {throw std::runtime_error("coord handling has not been mocked");}
......
......@@ -122,7 +122,7 @@ static void dump_api(std::shared_ptr<OpenZGY::IZgyReader> rr, std::ostream& out)
std::streamsize oldprec = std::cout.precision();
std::ios_base::fmtflags oldflags = std::cout.flags();
out << "File format and version = " << r.datatype()
<< " ZGY version " << r.filestats().fileVersion() << "\n";
<< " ZGY version " << r.filestats()->fileVersion() << "\n";
out << "Size I,J,K = " << r.size() << "\n";
out << "Brick size I,J,K = " << r.bricksize() << "\n";
out << "Number of bricks I,J,K = " << r.brickcount()[0] << "\n";
......@@ -151,7 +151,7 @@ static void dump_api(std::shared_ptr<OpenZGY::IZgyReader> rr, std::ostream& out)
<< r.zunitfactor() << " '"
<< r.zunitname() << "'\n";
out << "Ordered Corner Points Legend = [ <i>, <j>] { <inline>, <xline>} ( <easting>, <northing>)" << "\n";
r.filestats().dump(out);
r.filestats()->dump(out);
for (int ii=0; ii<4; ++ii)
out << "Ordered Corner Point " << ii << " = ["
<< std::fixed << std::setprecision(0)
......@@ -197,7 +197,7 @@ void test_readmeta()
if (verbose()) {
std::cout << "\n";
dump_api(reader, std::cout);
reader->filestats().dump(std::cout, "filestats: ");
reader->filestats()->dump(std::cout, "filestats: ");
}
const OpenZGY::IZgyReader& r(*reader);
......@@ -233,13 +233,13 @@ void test_readmeta()
TEST_CHECK(r.zunitdim() == UnitDimension::unknown);
TEST_CHECK(r.zunitfactor() == 1.0);
TEST_CHECK(r.zunitname() == "");
const FileStatistics filestats = r.filestats();
TEST_CHECK(filestats.fileVersion() == 3);
TEST_CHECK(filestats.fileSize() == 12320768);
TEST_CHECK(filestats.alphaNormalCount() == 5);
TEST_CHECK(filestats.alphaNormalSizePerEntry() == 64*64);
TEST_CHECK(filestats.brickNormalCount() == 45);
TEST_CHECK(filestats.brickNormalSizePerEntry() == 64*64*64);
std::shared_ptr<const FileStatistics>filestats = r.filestats();
TEST_CHECK(filestats->fileVersion() == 3);
TEST_CHECK(filestats->fileSize() == 12320768);
TEST_CHECK(filestats->alphaNormalCount() == 5);
TEST_CHECK(filestats->alphaNormalSizePerEntry() == 64*64);
TEST_CHECK(filestats->brickNormalCount() == 45);
TEST_CHECK(filestats->brickNormalSizePerEntry() == 64*64*64);
// actual
const IZgyReader::corners_t& index = r.indexcorners();
......@@ -280,7 +280,7 @@ void test_readcmeta()
if (verbose()) {
std::cout << "\n";
dump_api(reader, std::cout);
reader->filestats().dump(std::cout, "filestats: ");
reader->filestats()->dump(std::cout, "filestats: ");
}
}
......
......@@ -276,14 +276,14 @@ dump_basic(std::shared_ptr<OpenZGY::IZgyReader> r, const std::string& filename,
const SampleStatistics stat = r->statistics();
const SampleHistogram hist = r->histogram();
const FileStatistics filestats = r->filestats();
std::shared_ptr<const FileStatistics> filestats = r->filestats();
// Note that file size in bytes, ZGY version, and projection system
// are not available in the API. They might not be useful anyway.
os << "File name = '" << filename << "'\n"
<< "File size (bytes) = " << filestats.fileSize() << "\n"
<< "File format and version = " << r->datatype() << " ZGY version " << filestats.fileVersion() << "\n"
<< "File size (bytes) = " << filestats->fileSize() << "\n"
<< "File format and version = " << r->datatype() << " ZGY version " << filestats->fileVersion() << "\n"
//<< "Data identifier = " << r->dataid() << "\n"
<< "Current data Version = " << r->verid() << "\n"
//<< "Previous data version = " << r->previd() << "\n"
......@@ -326,7 +326,7 @@ dump_summary_brick_offsets(std::shared_ptr<OpenZGY::IZgyReader> r, std::ostream&
{
// TBD -- need access to internals
// See all_brick(), all_alpha(), summary_brick_offsets() in the Python version.
r->filestats().dump(os);
r->filestats()->dump(os);
}
void
......
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