Commit 87f10dde authored by Paal Kvamme's avatar Paal Kvamme
Browse files

Store file disposition only one place.

parent a5b52d6d
......@@ -169,6 +169,8 @@ private:
void do_write_one(const void* const data, const std::int64_t blocknum, const std::int64_t size, const bool overwrite);
void do_write_many(const void* const data, const std::int64_t blocknum, const std::int64_t size, const std::int64_t blobsize, const bool overwrite);
public:
// Needed by SeismicStoreFileDelayedWrite.
OpenMode _mode() const;
// The raw SDGenericDataset is needed by SeismicStoreFileDelayedWrite
// when opening a file for update.
std::shared_ptr<SDGenericDatasetWrapper> datasetwrapper() const {return _dataset;}
......@@ -209,7 +211,6 @@ private:
RawList _split_by_segment(const ReadList& requests);
void _cached_read(/*TODO-Low: seg, offset, view*/);
private:
OpenMode _mode;
// TODO-Low: To improve isolation, the user visible context should
// be copied into an equivalent InternalZGY::SeismicStoreConfig.
// The downside is that it gets more tedious to maintain.
......@@ -274,7 +275,6 @@ SeismicStoreFile::LoggerFn SeismicStoreFile::_loggerfn;
*/
class SeismicStoreFileDelayedWrite : public FileADT
{
OpenMode _mode;
std::shared_ptr<OpenZGY::SeismicStoreIOContext> _config;
std::shared_ptr<SeismicStoreFile> _relay;
std::vector<char> _open_segment;
......@@ -735,7 +735,9 @@ public:
return manager_;
}
OpenMode disposition() const {
return disposition_; // immutable, so no lock.
// This is immutable, except in close() which is not threadsafe
// anyway, so no lock is needed.
return disposition_;
}
std::shared_ptr<const DatasetInformation> info() {
std::lock_guard<std::mutex> lk(mutex_);
......@@ -756,6 +758,15 @@ public:
return info_;
}
void close()
{
auto victim = dataset_;
dataset_.reset();
disposition_ = OpenMode::Closed;
victim->close();
victim.reset(); // Any throw from SDGenericDataset dtor happens here.
}
void updateDataset(std::shared_ptr<seismicdrive::SDGenericDataset> dataset) {
std::lock_guard<std::mutex> lk(mutex_);
dataset_ = dataset;
......@@ -957,7 +968,6 @@ FileUtilsSeismicStore::~FileUtilsSeismicStore()
SeismicStoreFile::SeismicStoreFile(const std::string& filename, OpenMode mode, const IOContext *iocontext)
: FileUtilsSeismicStore()
, _mode(mode)
, _config()
{
_rtimer.reset(new SummaryPrintingTimerEx(mode == OpenMode::ReadWrite || mode == OpenMode::Truncate ? "Cloud.reread" : "Cloud.read"));
......@@ -1057,9 +1067,9 @@ SeismicStoreFile::~SeismicStoreFile()
// the manager. It might not be safe to invoke the callback any
// more. And do a blind catch of any exception because if we don't
// the application will crash.
if (_mode != OpenMode::Closed && _dataset && _dataset->dataset()) {
if (_dataset && _dataset->dataset() && _dataset->disposition() != OpenMode::Closed) {
try {
_dataset->dataset()->close();
_dataset->close();
}
catch (const std::exception& ex) {
_logger(0, "EXCEPTION closing file: " + std::string(ex.what()));
......@@ -1069,7 +1079,6 @@ SeismicStoreFile::~SeismicStoreFile()
}
}
_dataset.reset();
_mode = OpenMode::Closed;
}
/**
......@@ -1137,7 +1146,7 @@ SeismicStoreFile::xx_make_instance(const std::string& filename, OpenMode mode, c
void
SeismicStoreFile::xx_read(void *data, std::int64_t offset, std::int64_t size, UsageHint usagehint)
{
this->_validate_read(data, offset, size, this->xx_eof(), this->_mode);
this->_validate_read(data, offset, size, this->xx_eof(), this->_mode());
this->_dataset->reAuthorizeManager();
ReadRequest request(offset, size, nullptr);
RawList split = this->_split_by_segment(ReadList{request});
......@@ -1211,7 +1220,7 @@ SeismicStoreFile::xx_readv(const ReadList& requests, bool parallel_ok, bool immu
// likely to be very rare, is detected.
std::int64_t current_eof = SeismicStoreFile::xx_eof(); // exclude open segment
_validate_readv(requests, current_eof, this->_mode);
_validate_readv(requests, current_eof, this->_mode());
this->_dataset->reAuthorizeManager();
// For debugging / logging only
......@@ -1317,7 +1326,7 @@ void
SeismicStoreFile::xx_write(const void* data, std::int64_t offset, std::int64_t size, UsageHint usagehint)
{
SimpleTimerEx tt(*_wtimer);
this->_validate_write(data, offset, size, this->_mode);
this->_validate_write(data, offset, size, this->_mode());
this->_dataset->reAuthorizeManager();
std::int64_t current_eof = SeismicStoreFile::xx_eof(); // MUST be nonvirtual
if (_logger(5, ""))
......@@ -1386,6 +1395,12 @@ SeismicStoreFile::xx_write(const void* data, std::int64_t offset, std::int64_t s
size, size, 1, this->xx_segments(true));
}
OpenMode
SeismicStoreFile::_mode() const
{
return _dataset ? _dataset->disposition() : OpenMode::Closed;
}
/**
* This is the final part of xx_write, in the case where we want to
* write just a simgle SDAPI blob.
......@@ -1516,7 +1531,7 @@ SeismicStoreFile::do_write_many(
void
SeismicStoreFile::xx_close()
{
if (!_dataset ||_mode == OpenMode::Closed) {
if (!_dataset || _dataset->disposition() == OpenMode::Closed) {
// Note: I might "be nice" to the application and simply ignore a duplicate
// close or a close on a file that was never open in the first place.
// In that case I should probably check using an atomic_flag.
......@@ -1527,7 +1542,7 @@ SeismicStoreFile::xx_close()
}
if (_dataset) {
switch (_mode) {
switch (_dataset->disposition()) {
case OpenMode::Closed:
break;
case OpenMode::ReadOnly:
......@@ -1535,13 +1550,12 @@ SeismicStoreFile::xx_close()
case OpenMode::Truncate:
if (_dataset->dataset()) {
this->_dataset->reAuthorizeManager();
_dataset->dataset()->close();
_dataset->close();
}
break;
}
}
_dataset.reset();
_mode = OpenMode::Closed;
_rtimer.reset();
_wtimer.reset();
}
......@@ -1717,7 +1731,6 @@ SeismicStoreFile::_cached_read(/*TODO-Low: seg, offset, view*/)
SeismicStoreFileDelayedWrite::SeismicStoreFileDelayedWrite(const std::string& filename, OpenMode mode, const IOContext *iocontext)
: FileADT()
, _mode(mode)
, _config(nullptr)
, _relay()
, _open_segment()
......@@ -1773,7 +1786,7 @@ SeismicStoreFileDelayedWrite::xx_make_instance(const std::string& filename, Open
void
SeismicStoreFileDelayedWrite::xx_read(void *data, std::int64_t offset, std::int64_t size, UsageHint usagehint)
{
this->_validate_read(data, offset, size, this->xx_eof(), this->_mode);
this->_validate_read(data, offset, size, this->xx_eof(), this->_relay->_mode());
const std::int64_t closed_size =
std::max(std::int64_t(0), std::min(size, this->_relay->xx_eof() - offset));
const std::int64_t opened_size =
......@@ -1917,7 +1930,7 @@ SeismicStoreFileDelayedWrite::xx_write(const void* data, std::int64_t offset, st
// The reason I deferred this validation is that if forwarding directly
// to the relay it isn't needed. Because the relay does the same check.
this->_validate_write(data, offset, size, this->_mode);
this->_validate_write(data, offset, size, this->_relay->_mode());
if (offset > written)
throw OpenZGY::Errors::ZgyUserError("Data must be written sequentially.");
......
Markdown is supported
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