Add singletone class RasterCache to handle global cache data.

This commit is contained in:
Tomonobu Saito 2019-10-09 13:02:59 +09:00
parent f36707d1fb
commit 542c3ba872
2 changed files with 38 additions and 27 deletions

36
include/extractor/raster_source.hpp Normal file → Executable file
View File

@ -126,14 +126,7 @@ class RasterSource
class RasterContainer class RasterContainer
{ {
public: public:
RasterContainer() { ++count; } RasterContainer() = default;
~RasterContainer() {
--count;
if (0 == count) {
LoadedSources.clear();
LoadedSourcePaths.clear();
}
}
int LoadRasterSource(const std::string &path_string, int LoadRasterSource(const std::string &path_string,
double xmin, double xmin,
@ -148,9 +141,30 @@ class RasterContainer
RasterDatum GetRasterInterpolateFromSource(unsigned int source_id, double lon, double lat); RasterDatum GetRasterInterpolateFromSource(unsigned int source_id, double lon, double lat);
private: private:
static std::vector<RasterSource> LoadedSources; };
static std::unordered_map<std::string, int> LoadedSourcePaths;
static int count; // << singletone >> RasterCache
class RasterCache
{
public:
// class method to get the instance
static RasterCache& getInstance() {
if (NULL == g_instance) {
g_instance = new RasterCache();
}
return *g_instance;
}
// get reference of cache
std::vector<RasterSource>& getLoadedSources() { return LoadedSources; }
std::unordered_map<std::string, int>& getLoadedSourcePaths() { return LoadedSourcePaths; }
private:
// constructor
RasterCache() = default;
// member
std::vector<RasterSource> LoadedSources;
std::unordered_map<std::string, int> LoadedSourcePaths;
// the instance
static RasterCache *g_instance;
}; };
} }
} }

View File

@ -78,11 +78,6 @@ RasterDatum RasterSource::GetRasterInterpolate(const int lon, const int lat) con
raster_data(right, bottom) * (fromLeft * fromTop))}; raster_data(right, bottom) * (fromLeft * fromTop))};
} }
// static member of Raster Container
std::vector<RasterSource> RasterContainer::LoadedSources;
std::unordered_map<std::string, int> RasterContainer::LoadedSourcePaths;
int RasterContainer::count = 0;
// Load raster source into memory // Load raster source into memory
int RasterContainer::LoadRasterSource(const std::string &path_string, int RasterContainer::LoadRasterSource(const std::string &path_string,
double xmin, double xmin,
@ -97,15 +92,15 @@ int RasterContainer::LoadRasterSource(const std::string &path_string,
const auto _ymin = static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{ymin})); const auto _ymin = static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{ymin}));
const auto _ymax = static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{ymax})); const auto _ymax = static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{ymax}));
const auto itr = LoadedSourcePaths.find(path_string); const auto itr = RasterCache::getInstance().getLoadedSourcePaths().find(path_string);
if (itr != LoadedSourcePaths.end()) if (itr != RasterCache::getInstance().getLoadedSourcePaths().end())
{ {
util::Log() << "[source loader] Already loaded source '" << path_string << "' at source_id " util::Log() << "[source loader] Already loaded source '" << path_string << "' at source_id "
<< itr->second; << itr->second;
return itr->second; return itr->second;
} }
int source_id = static_cast<int>(LoadedSources.size()); int source_id = static_cast<int>(RasterCache::getInstance().getLoadedSources().size());
util::Log() << "[source loader] Loading from " << path_string << " ... "; util::Log() << "[source loader] Loading from " << path_string << " ... ";
TIMER_START(loading_source); TIMER_START(loading_source);
@ -121,8 +116,8 @@ int RasterContainer::LoadRasterSource(const std::string &path_string,
RasterSource source{std::move(rasterData), ncols, nrows, _xmin, _xmax, _ymin, _ymax}; RasterSource source{std::move(rasterData), ncols, nrows, _xmin, _xmax, _ymin, _ymax};
TIMER_STOP(loading_source); TIMER_STOP(loading_source);
LoadedSourcePaths.emplace(path_string, source_id); RasterCache::getInstance().getLoadedSourcePaths().emplace(path_string, source_id);
LoadedSources.push_back(std::move(source)); RasterCache::getInstance().getLoadedSources().push_back(std::move(source));
util::Log() << "[source loader] ok, after " << TIMER_SEC(loading_source) << "s"; util::Log() << "[source loader] ok, after " << TIMER_SEC(loading_source) << "s";
@ -132,10 +127,10 @@ int RasterContainer::LoadRasterSource(const std::string &path_string,
// External function for looking up nearest data point from a specified source // External function for looking up nearest data point from a specified source
RasterDatum RasterContainer::GetRasterDataFromSource(unsigned int source_id, double lon, double lat) RasterDatum RasterContainer::GetRasterDataFromSource(unsigned int source_id, double lon, double lat)
{ {
if (LoadedSources.size() < source_id + 1) if (RasterCache::getInstance().getLoadedSources().size() < source_id + 1)
{ {
throw util::exception("Attempted to access source " + std::to_string(source_id) + throw util::exception("Attempted to access source " + std::to_string(source_id) +
", but there are only " + std::to_string(LoadedSources.size()) + ", but there are only " + std::to_string(RasterCache::getInstance().getLoadedSources().size()) +
" loaded" + SOURCE_REF); " loaded" + SOURCE_REF);
} }
@ -144,7 +139,7 @@ RasterDatum RasterContainer::GetRasterDataFromSource(unsigned int source_id, dou
BOOST_ASSERT(lon < 180); BOOST_ASSERT(lon < 180);
BOOST_ASSERT(lon > -180); BOOST_ASSERT(lon > -180);
const auto &found = LoadedSources[source_id]; const auto &found = RasterCache::getInstance().getLoadedSources()[source_id];
return found.GetRasterData(static_cast<std::int32_t>(util::toFixed(util::FloatLongitude{lon})), return found.GetRasterData(static_cast<std::int32_t>(util::toFixed(util::FloatLongitude{lon})),
static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{lat}))); static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{lat})));
} }
@ -153,10 +148,10 @@ RasterDatum RasterContainer::GetRasterDataFromSource(unsigned int source_id, dou
RasterDatum RasterDatum
RasterContainer::GetRasterInterpolateFromSource(unsigned int source_id, double lon, double lat) RasterContainer::GetRasterInterpolateFromSource(unsigned int source_id, double lon, double lat)
{ {
if (LoadedSources.size() < source_id + 1) if (RasterCache::getInstance().getLoadedSources().size() < source_id + 1)
{ {
throw util::exception("Attempted to access source " + std::to_string(source_id) + throw util::exception("Attempted to access source " + std::to_string(source_id) +
", but there are only " + std::to_string(LoadedSources.size()) + ", but there are only " + std::to_string(RasterCache::getInstance().getLoadedSources().size()) +
" loaded" + SOURCE_REF); " loaded" + SOURCE_REF);
} }
@ -165,10 +160,12 @@ RasterContainer::GetRasterInterpolateFromSource(unsigned int source_id, double l
BOOST_ASSERT(lon < 180); BOOST_ASSERT(lon < 180);
BOOST_ASSERT(lon > -180); BOOST_ASSERT(lon > -180);
const auto &found = LoadedSources[source_id]; const auto &found = RasterCache::getInstance().getLoadedSources()[source_id];
return found.GetRasterInterpolate( return found.GetRasterInterpolate(
static_cast<std::int32_t>(util::toFixed(util::FloatLongitude{lon})), static_cast<std::int32_t>(util::toFixed(util::FloatLongitude{lon})),
static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{lat}))); static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{lat})));
} }
RasterCache *RasterCache::g_instance = NULL;
} }
} }