32 #include <boost/unordered_map.hpp> 46 file.open(filename.string(), std::ios::in | std::ios::binary);
47 file.seekg(0, std::ios::end);
48 std::streampos length(file.tellg());
49 if (length == std::streampos(-1) || !file.is_open()) {
52 file.seekg(0, std::ios::beg);
54 image->resize(static_cast<std::size_t>(length));
55 image->assign(std::istreambuf_iterator<char>(file),
56 std::istreambuf_iterator<char>());
60 void Cache::writeFile(shared_ptr<Tile> tile,
const boost::filesystem::path& filename) {
61 boost::filesystem::create_directories(filename.parent_path());
62 std::ofstream out(filename.string(), std::ios::out | std::ios::binary);
68 boost::filesystem::remove(filename);
71 auto size = png->size();
72 out.write((
const char*) png->data(), size);
81 std::stringstream path;
83 path << ti->getStylesheetPath() <<
"/";
84 path << ti->getZoom() <<
"/";
85 path << ti->getX() <<
"/";
87 path <<
"." << ti->getImageFormatString();
88 boost::filesystem::path file(path.str());
101 shared_ptr<CacheOfOneStylesheet> cache;
102 const string& stylesheet = ti->getStylesheetPath();
103 auto cacheIt =
AllCaches.find(stylesheet);
106 cache = cacheIt->second;
109 cache = boost::make_shared<CacheOfOneStylesheet>();
112 boost::filesystem::create_directories(dir);
113 LOG_SEV(cache_log, debug) <<
"Stylesheetcache " << stylesheet <<
" created.";
116 shared_ptr<Tile> tile;
117 auto tileIt = cache->find(*ti);
118 if (tileIt != cache->end()) {
120 tile = tileIt->second.first;
126 tile = boost::make_shared<Tile>(ti);
130 Tile::ImageType image = boost::make_shared<Tile::ImageType::element_type>();
133 tile->setImage(image);
135 LOG_SEV(cache_log, debug) <<
"readFile: Not found: " << path.string();
146 shared_ptr<TileIdentifier> tiToDelete = tileToDelete->getIdentifier();
147 boost::filesystem::path path =
getTilePath(tiToDelete);
151 LOG_SEV(cache_log, debug) <<
"WriteFile: Could not open file " << path.string();
154 LOG_SEV(cache_log, debug) <<
"WriteFile: Image not yet rendered " << *tile->getIdentifier();
156 cacheIt =
AllCaches.find(tileToDelete->getIdentifier()->getStylesheetPath());
158 cacheIt->second->erase(*tileToDelete->getIdentifier());
166 LOG_SEV(cache_log, debug) <<
"Deleting least recently used Tile." << *tileToDelete->getIdentifier();
167 cacheIt =
AllCaches.find(tileToDelete->getIdentifier()->getStylesheetPath());
169 cacheIt->second->erase(*tileToDelete->getIdentifier());
185 shared_ptr<TileIdentifier> ti = boost::make_shared<TileIdentifier>(-1, -1, -1,
"/", TileIdentifier::Format::PNG);
188 Tile::ImageType image = boost::make_shared<Tile::ImageType::element_type>();
191 DefaultTile->setImage(image);
208 boost::system::error_code ec;
209 boost::filesystem::remove_all(dir, ec);
210 if (ec.value() == 39) {
211 LOG_SEV(cache_log, warning) <<
"could not delete all tiles in folder.";
214 }
catch (std::out_of_range) {
215 LOG_SEV(cache_log, warning) <<
"trying to delete Tiles of non existant Stylesheet.";
void readFile(const Tile::ImageType &image, const boost::filesystem::path &filename)
Cache(const shared_ptr< Configuration > &config)
This file is part of alaCarte.
static const char * cache_size
Option to get the cache size (type: int)
const boost::filesystem::path getTilePath(const shared_ptr< TileIdentifier > &ti)
shared_ptr< Tile > DefaultTile
static const char * cache_keep_tile
Option to get the zoomlevel until tiles are kept on harddrive (type: int)
TileList RecentlyUsedList
#define LOG_SEV(log, lvl)
TESTABLE shared_ptr< Tile > getDefaultTile()
Get the default tile used for error and such.
Thrown if a file was not found.
TESTABLE void deleteTiles(const string path)
Deletes all cached Tiles of the Stylesheet with the given path.
static const char * path_to_default_tile
Path to the default tile (type: string)
static const char * config
Option to get the configuration filename (type: string)
shared_ptr< Configuration > Config
shared_ptr< std::vector< uint8_t > > ImageType
static const char * cache_path
Option to get the cache path (type: string)
std::pair< shared_ptr< Tile >, TileList::iterator > CacheElement
An element stored in the cache.
boost::mutex GlobalCacheLock
boost::error_info< struct TagFileName, string > InfoFileName
Use this to inform about a file name.
void writeFile(shared_ptr< Tile > tile, const boost::filesystem::path &filename)
TESTABLE shared_ptr< Tile > getTile(const shared_ptr< TileIdentifier > &tl)
Gets a Tile where the image data can be stored.