Skip to content

Commit

Permalink
introduce an in memory tile cache
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed Feb 10, 2024
1 parent 4bb7535 commit 96ae8fc
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 25 deletions.
1 change: 1 addition & 0 deletions provider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ set(SRC
src/RenderHelper.cpp
src/SettingsManager.cpp
src/TokenHandler.cpp
src/TileCache.cpp
src/s52/S52Data.cpp
src/s52/S52Symbols.cpp
src/s52/S52CondRules.cpp
Expand Down
11 changes: 7 additions & 4 deletions provider/include/ChartManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class ChartManager : public StatusCollector{
STATE_READY
} ;
using RunFunction=std::function<void (void)>;
using SetChangeFunction=std::function<void(const String &setKey)>;
using SettingsChangeFunction=std::function<void(s52::S52Data::ConstPtr newData)>;
ChartManager(FontFileHolder::Ptr fontFile, IBaseSettings::ConstPtr bs, RenderSettings::ConstPtr rs, IChartFactory::Ptr chartFactory,String s57dataDir, unsigned int memLimitKb, int numOpeners);
/**
* really start reading the charts
Expand Down Expand Up @@ -129,8 +131,7 @@ class ChartManager : public StatusCollector{
bool CloseChart(const String &setName, const String &chartName);
int HandleCharts(const StringVector &dirsAndFiles,bool setsOnly, bool canDelete=false);
WeightedChartList FindChartsForTile(RenderSettings::ConstPtr renderSettingsPtr,const TileInfo &tile, bool allLower=false);
using ExtentList=std::vector<Coord::Extent>;
ExtentList GetChartSetExtents(const String &chartSetKey,bool includeSet);
ChartSet::ExtentList GetChartSetExtents(const String &chartSetKey,bool includeSet);
/**
* add mappings to shorten the chart set names for known directories
*/
Expand All @@ -145,6 +146,8 @@ class ChartManager : public StatusCollector{
bool HasOpeners() const;
s52::S52Data::ConstPtr GetS52Data() const;
Chart::ChartType GetChartType(const String &fileName) const;
void registerSetChagend(SetChangeFunction f);
void registerSettingsChanged(SettingsChangeFunction f);
private:
class HouseKeeper : public Thread{
ChartCache::Ptr cache;
Expand Down Expand Up @@ -193,8 +196,8 @@ class ChartManager : public StatusCollector{
HouseKeeper::Ptr houseKeeper;
int numOpeners;
FontFileHolder::Ptr fontFile;


SetChangeFunction setChanged;
SettingsChangeFunction settingsChanged;
};

#endif /* CHARTMANAGER_H */
Expand Down
16 changes: 11 additions & 5 deletions provider/include/ChartSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ class CacheReaderWriter;
class ChartList;
class ChartSet : public StatusCollector{
public:
typedef std::vector<ChartInfo::Ptr> InfoList;
typedef std::shared_ptr<ChartSet> Ptr;
typedef std::shared_ptr<const ChartSet> ConstPtr;
using InfoList=std::vector<ChartInfo::Ptr>;
using Ptr=std::shared_ptr<ChartSet>;
using ConstPtr=std::shared_ptr<const ChartSet>;
class ExtentList: public std::vector<Coord::Extent>{
public:
using std::vector<Coord::Extent>::vector;
String setHash;
int setSequence=0;
};
const int MAX_ERRORS_RETRY=10; //stop retrying after that many errors
typedef enum{
STATE_INIT,
Expand All @@ -66,7 +72,7 @@ class ChartSet : public StatusCollector{
ChartSetInfo::Ptr info;
ChartSet(ChartSetInfo::Ptr info, bool canDelete=false);
virtual ~ChartSet(){}
const String GetKey();
const String GetKey() const;
bool IsActive(){return (state == STATE_READY && (numValidCharts>0) && ! DisabledByErrors());}
/**
* change the enabled/disabled state
Expand All @@ -93,7 +99,7 @@ class ChartSet : public StatusCollector{
StringVector GetFailedChartNames(int maxErrors=-1);
ExtentInfo GetExtent();
int RemoveUnverified();
void FillChartExtents(std::vector<Coord::Extent> &extents);
void FillChartExtents(ExtentList &extents);



Expand Down
6 changes: 4 additions & 2 deletions provider/include/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "S52Data.h"
#include "FontManager.h"
#include <memory>
#include "TileCache.h"
class Renderer
{
public:
Expand All @@ -47,7 +48,6 @@ class Renderer
class RenderResult
{
public:
typedef std::shared_ptr<DataVector> DataPtr;
DataPtr result;
Timer::Measure timer;
RenderResult(){
Expand All @@ -63,16 +63,18 @@ class Renderer
public:
String pngType="fpng";
};
Renderer(ChartManager::Ptr m, bool debug){
Renderer(ChartManager::Ptr m,TileCache::Ptr tc, bool debug){
chartManager=m;
renderDebug=debug;
cache=tc;
}
virtual ~Renderer(){}
virtual void renderTile(const TileInfo &tile,const RenderInfo &info,RenderResult &result);
virtual ObjectList featureInfo(const TileInfo &info, const Coord::TileBox &box, bool overview);
virtual ChartManager::Ptr getManager(){return chartManager;}
protected:
ChartManager::Ptr chartManager;
TileCache::Ptr cache;
bool renderDebug=false;

};
Expand Down
39 changes: 31 additions & 8 deletions provider/src/ChartManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ int ChartManager::computeActiveSets(){
}
set->SetEnabled(enabled,disabledBy);
if (enabled) numEnabled++;
else{
if (setChanged) setChanged(set->GetKey());
}
}
return numEnabled;
}
Expand Down Expand Up @@ -488,6 +491,7 @@ bool ChartManager::DeleteChartSet(const String &key){
computeActiveSets();
chartCache->CloseBySet(key);
});
if (setChanged) setChanged(key);
return true;
}

Expand Down Expand Up @@ -612,9 +616,9 @@ static void fillChartList(WeightedChartList &chartList,ChartSet::Ptr chartSet,co
chartList.add(*it);
}
}
ChartManager::ExtentList ChartManager::GetChartSetExtents(const String &chartSetKey, bool includeSet)
ChartSet::ExtentList ChartManager::GetChartSetExtents(const String &chartSetKey, bool includeSet)
{
ExtentList rt;
ChartSet::ExtentList rt;
Synchronized l(lock);
auto it = chartSets.find(chartSetKey);
if (it == chartSets.end())
Expand Down Expand Up @@ -843,14 +847,23 @@ bool ChartManager::Stop(){
typedef std::unordered_set<ChartInfo::Ptr> ChartInfoSet;
void ChartManager::CloseDisabled(){
LOG_INFO("ChartManager::CloseDisabled");
Synchronized l(lock);
StringVector closed;
int numClosed=0;
for (auto it=chartSets.begin();it!=chartSets.end();it++){
ChartSet::Ptr set=it->second;
if (set->IsEnabled()) continue;
numClosed+=chartCache->CloseBySet(set->GetKey());
{
Synchronized l(lock);
for (auto it=chartSets.begin();it!=chartSets.end();it++){
ChartSet::Ptr set=it->second;
if (set->IsEnabled()) continue;
numClosed+=chartCache->CloseBySet(set->GetKey());
closed.push_back(set->GetKey());
}
}
LOG_INFO("ChartManager::CloseDisabled finished and closed %d charts",numClosed);
if (setChanged){
for (auto &&cs:closed){
setChanged(cs);
}
}
}

Chart::ConstPtr ChartManager::OpenChart(s52::S52Data::ConstPtr s52data,ChartInfo::Ptr chartInfo,bool doWait){
Expand Down Expand Up @@ -1143,6 +1156,16 @@ bool ChartManager::buildS52Data(RenderSettings::ConstPtr s){
s52data=newS52Data;
AddItem("s52data",s52data);
}
if (hasOld && chartCache) chartCache->CloseByMD5(oldMd5);
if (hasOld && chartCache) chartCache->CloseByMD5(oldMd5);
if (settingsChanged){
settingsChanged(s52data);
}
return true;
}

void ChartManager::registerSetChagend(ChartManager::SetChangeFunction f){
setChanged=f;
}
void ChartManager::registerSettingsChanged(ChartManager::SettingsChangeFunction f){
settingsChanged=f;
}
5 changes: 3 additions & 2 deletions provider/src/ChartSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ChartSet::ChartSet(ChartSetInfo::Ptr info, bool canDelete)
numValidCharts = 0;
}

const String ChartSet::GetKey()
const String ChartSet::GetKey() const
{
ChartSetInfo::Ptr ip = info; // avoid lock
if (!ip)
Expand Down Expand Up @@ -207,9 +207,10 @@ ChartSet::ExtentInfo ChartSet::GetExtent()
rt.minScale = minScale;
return rt;
}
void ChartSet::FillChartExtents(std::vector<Coord::Extent> &extents)
void ChartSet::FillChartExtents(ChartSet::ExtentList &extents)
{
Synchronized l(lock);
extents.setHash=hash.ToString();
for (const auto &info : chartList)
{
extents.push_back(info->GetExtent());
Expand Down
14 changes: 13 additions & 1 deletion provider/src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,21 @@ void Renderer::renderTile(const TileInfo &tile, const RenderInfo &info, RenderRe
context.s52Data=chartManager->GetS52Data();
RenderSettings::ConstPtr renderSettings=context.s52Data->getSettings();
result.timer.add("settings");
ChartManager::ExtentList extents=chartManager->GetChartSetExtents(tile.chartSetKey,true);
ChartSet::ExtentList extents=chartManager->GetChartSetExtents(tile.chartSetKey,true);
if (extents.size() < 1){
throw RenderException(tile,"internal error: no chart set extent");
}
TileCache::CacheDescription cd;
cd.settingsSequence=context.s52Data->getSequence();
cd.setHash=extents.setHash;
cd.setSequence=extents.setSequence;
TileCache::Png tileFromCache=cache->getTile(cd,tile);
if (tileFromCache){
result.timer.add("cache");
LOG_DEBUG("tile %s from cache",tile.ToString());
result.result=tileFromCache;
return;
}
Coord::TileBox tileBox=Coord::tileToBox(tile);
WeightedChartList renderCharts = chartManager->FindChartsForTile(renderSettings, tile);
result.timer.add("find");
Expand Down Expand Up @@ -207,6 +218,7 @@ void Renderer::renderTile(const TileInfo &tile, const RenderInfo &info, RenderRe
encoder->setContext(drawing.get());
bool rt = encoder->encode(result.result);
result.timer.add("png");
cache->addTile(result.result,cd,tile);
LOG_DEBUG("%s",drawing->getStatistics());
}

Expand Down
14 changes: 11 additions & 3 deletions provider/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include "S52Data.h"
#include "FontManager.h"
#include "SystemHelper.h"

#include "TileCache.h"

#define CHART_TEMP_DIR "_TMP"
using std::cout;
Expand Down Expand Up @@ -214,8 +214,16 @@ int mainFunction(int argc, char **argv,bool *stopFlag=NULL)
//for our normal chart dir we use an empty prefix
//to get short names
chartManager->AddKnownDirectory(chartDir,"");
Renderer::Ptr trender=std::make_shared<TestRenderer>(chartManager,renderDebug);
Renderer::Ptr render=std::make_shared<Renderer>(chartManager,renderDebug);
TileCache::Ptr tileCache=std::make_shared<TileCache>(40960); //TODO config
chartManager->registerSetChagend([&tileCache](const String &key){
tileCache->clean(key);
});
chartManager->registerSettingsChanged([&tileCache](s52::S52Data::ConstPtr s52data){
tileCache->cleanBySettings(s52data->getSequence());
});
collector.AddItem("tileCache",tileCache);
Renderer::Ptr trender=std::make_shared<TestRenderer>(chartManager,tileCache,renderDebug);
Renderer::Ptr render=std::make_shared<Renderer>(chartManager,tileCache,renderDebug);
TokenHandler::Ptr tokenHandler=std::make_shared<TokenHandler>("all");
tokenHandler->start();
HTTPServer server(port,5);
Expand Down

0 comments on commit 96ae8fc

Please sign in to comment.