Skip to content

Commit

Permalink
Assert correct file extension before writing indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisspyB committed Nov 26, 2024
1 parent 6f5a5ab commit cb4a1ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/gribjump/info/InfoCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void IndexFile::reload() {
load();
}

void IndexFile::encode(eckit::Stream& s) {
void IndexFile::encode(eckit::Stream& s) const {
std::lock_guard<std::mutex> lock(mutex_);

s << currentVersion_;
Expand Down Expand Up @@ -342,13 +342,15 @@ void IndexFile::decode(eckit::Stream& s) {
LOG_DEBUG_LIB(LibGribJump) << "Loaded " << count << " entries from stream" << std::endl;
}

void IndexFile::toNewFile(eckit::PathName path){
void IndexFile::toNewFile(const eckit::PathName& path) const {
ASSERT(path.extension() == file_ext);
eckit::FileStream s(path, "w");
encode(s);
s.close();
}

void IndexFile::appendToFile(eckit::PathName path) {
void IndexFile::appendToFile(const eckit::PathName& path) const {
ASSERT(path.extension() == file_ext);
// NB: Non-atomic. Gribjump does not support concurrent writes to the same file from different processes.
if (!path.exists()) {
toNewFile(path);
Expand All @@ -368,7 +370,7 @@ void IndexFile::appendToFile(eckit::PathName path) {
s.close();
}

void IndexFile::fromFile(eckit::PathName path) {
void IndexFile::fromFile(const eckit::PathName& path) {
eckit::FileStream s(path, "r");
decode(s);
s.close();
Expand All @@ -387,11 +389,13 @@ void IndexFile::merge(IndexFile& other) {
void IndexFile::write() {

// create a unique filename for the cache file before (atomically) moving it into place
eckit::PathName uniqPath = eckit::PathName::unique(path_);
eckit::PathName uniqPath = eckit::PathName::unique(path_) + file_ext;
toNewFile(uniqPath);

// atomically move the file into place
LOG_DEBUG_LIB(LibGribJump) << "IndexFile writing to file " << path_ << std::endl;

ASSERT(path_.extension() == file_ext);
eckit::PathName::rename(uniqPath, path_);
}

Expand Down
10 changes: 4 additions & 6 deletions src/gribjump/info/InfoCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ class InfoCache {
mutable std::mutex infomutex_;; //< mutex for infocache_
infocache_t infocache_;

bool persistentCache_ = true;

bool lazy_; //< if true, cache.get may construct JumpInfo on the fly

bool shadowCache_ = false; //< if true, cache files are persisted next to the original data files (e.g. in FDB)
Expand Down Expand Up @@ -117,7 +115,7 @@ class IndexFile {

private: // Methods are only intended to be called from InfoCache

void encode(eckit::Stream& s);
void encode(eckit::Stream& s) const;

void decode(eckit::Stream& s);

Expand All @@ -129,9 +127,9 @@ class IndexFile {

void insert(eckit::Offset offset, std::shared_ptr<JumpInfo> info);

void toNewFile(eckit::PathName path);
void appendToFile(eckit::PathName path);
void fromFile(eckit::PathName path);
void toNewFile(const eckit::PathName& path) const;
void appendToFile(const eckit::PathName& path) const;
void fromFile(const eckit::PathName& path);

// wrapper around map_.find()
std::shared_ptr<JumpInfo> find(eckit::Offset offset);
Expand Down

0 comments on commit cb4a1ec

Please sign in to comment.