Skip to content

Commit

Permalink
Add ability to delete cache files from tiler
Browse files Browse the repository at this point in the history
- To allow the system to reset after subsequent runs, it can be useful
to have the system automatically delete cache tiles when starting a new
run.
- This is added as a command line option to allow enabling/disabling of
the behavior
- This is preparation for allowing a job to resume (sans old keypoints)
after a crash
  • Loading branch information
zthorson committed Aug 16, 2021
1 parent cf6a492 commit 062a04d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
8 changes: 6 additions & 2 deletions modules/realm_ortho/include/realm_ortho/tile_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class TileCache : public WorkerThreadBase
void flushAll();
void loadAll();

void deleteCache();
void deleteCache(std::string layer);

private:

bool m_has_init_directories;
Expand All @@ -64,6 +67,7 @@ class TileCache : public WorkerThreadBase

std::mutex m_mutex_cache;
std::map<int, CacheElementGrid> m_cache;
std::mutex m_mutex_file_write;

std::mutex m_mutex_do_update;
bool m_do_update;
Expand All @@ -79,9 +83,9 @@ class TileCache : public WorkerThreadBase
void reset() override;

void load(const CacheElement::Ptr &element) const;
void write(const CacheElement::Ptr &element) const;
void write(const CacheElement::Ptr &element);

void flush(const CacheElement::Ptr &element) const;
void flush(const CacheElement::Ptr &element);

bool isCached(const CacheElement::Ptr &element) const;

Expand Down
30 changes: 27 additions & 3 deletions modules/realm_ortho/src/tile_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,27 @@ void TileCache::loadAll()
}
}

void TileCache::deleteCache()
{
std::lock_guard<std::mutex> lock(m_mutex_file_write);
// Remove all cache items
flushAll();
m_has_init_directories = false;
auto files = io::getFileList(m_dir_toplevel, "");
for (auto & file : files) {
if (!file.empty()) io::removeFileOrDirectory(file);
}
}

void TileCache::deleteCache(std::string layer)
{
std::lock_guard<std::mutex> lock(m_mutex_file_write);
// Attempt to remove the specific layer name
flushAll();
m_has_init_directories = false;
io::removeFileOrDirectory(m_dir_toplevel + "/" + layer);
}

void TileCache::load(const CacheElement::Ptr &element) const
{
for (const auto &meta : element->layer_meta)
Expand Down Expand Up @@ -303,7 +324,7 @@ void TileCache::load(const CacheElement::Ptr &element) const
}
}

void TileCache::write(const CacheElement::Ptr &element) const
void TileCache::write(const CacheElement::Ptr &element)
{
for (const auto &meta : element->layer_meta)
{
Expand Down Expand Up @@ -335,13 +356,16 @@ void TileCache::write(const CacheElement::Ptr &element) const
throw(std::invalid_argument("Error writing tile: data type unknown!"));
}

io::saveImage(data, filename);
{
std::lock_guard<std::mutex> lock(m_mutex_file_write);
io::saveImage(data, filename);
}

element->was_written = true;
}
}

void TileCache::flush(const CacheElement::Ptr &element) const
void TileCache::flush(const CacheElement::Ptr &element)
{
if (!element->was_written)
write(element);
Expand Down
1 change: 1 addition & 0 deletions modules/realm_stages/include/realm_stages/stage_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class TileingSettings : public StageSettings
add("tms_tiles", Parameter_t<int>{1, "Generate output tiles using TMS standard. If false, Google/OSM standard is used."});
add("min_zoom", Parameter_t<int>{11, "The minimum tile zoom to generate for the output tiles."});
add("max_zoom", Parameter_t<int>{20, "The maximum tile zoom to generate for the output tiles. (Set to -1 for maximum zoom based on GSD)"});
add("delete_cache_on_init", Parameter_t<int>{0, "If there are leftover cache items in the cache folder, delete them before starting the stage."});
}
};

Expand Down
6 changes: 6 additions & 0 deletions modules/realm_stages/include/realm_stages/tileing.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Tileing : public StageBase
bool process() override;
void saveAll();

void deleteCache();
void deleteCache(std::string layer);

private:
std::deque<Frame::Ptr> m_buffer;
std::mutex m_mutex_buffer;
Expand All @@ -78,6 +81,9 @@ class Tileing : public StageBase
/// The maximum zoom to generate. May not be generated if GSD isn't sufficient
int m_max_tile_zoom;

/// Indicates we should wipe the cache directory when starting or resetting the stage
bool m_delete_cache_on_init;

/// The directory to store the output map tiles in, defaults to log directory
std::string m_cache_path;

Expand Down
17 changes: 17 additions & 0 deletions modules/realm_stages/src/tileing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Tileing::Tileing(const StageSettings::Ptr &stage_set, double rate)
m_generate_tms_tiles((*stage_set)["tms_tiles"].toInt() > 0),
m_min_tile_zoom((*stage_set)["min_zoom"].toInt()),
m_max_tile_zoom((*stage_set)["max_zoom"].toInt()),
m_delete_cache_on_init((*stage_set)["delete_cache_on_init"].toInt() > 0),
m_utm_reference(nullptr),
m_map_tiler(nullptr),
m_tile_cache(nullptr),
Expand Down Expand Up @@ -246,6 +247,19 @@ bool Tileing::process()
return has_processed;
}

void Tileing::deleteCache() {
if (m_tile_cache)
{
m_tile_cache->deleteCache();
}
}

void Tileing::deleteCache(std::string layer) {
if (m_tile_cache) {
m_tile_cache->deleteCache(layer);
}
}

Tile::Ptr Tileing::merge(const Tile::Ptr &t1, const Tile::Ptr &t2)
{
if (t2->data()->empty())
Expand Down Expand Up @@ -390,6 +404,9 @@ void Tileing::initStageCallback()
{
m_map_tiler = std::make_shared<MapTiler>(true, m_generate_tms_tiles);
m_tile_cache = std::make_shared<TileCache>("tile_cache", 500, m_cache_path, false);
if (m_delete_cache_on_init) {
deleteCache();
}
m_tile_cache->start();
}
}
Expand Down

0 comments on commit 062a04d

Please sign in to comment.