10 #include "aliceVision/numeric/numeric.hpp"
13 #include <unordered_map>
14 #include <boost/multi_index_container.hpp>
15 #include <boost/multi_index/hashed_index.hpp>
16 #include <boost/multi_index/identity.hpp>
17 #include <boost/multi_index/sequenced_index.hpp>
18 #include <boost/multi_index/member.hpp>
28 class TileCacheManager;
40 using weak_pointer = std::weak_ptr<CachedTile>;
41 using smart_pointer = std::shared_ptr<CachedTile>;
46 CachedTile(
const std::shared_ptr<TileCacheManager>& manager,
55 _tileWidth(tileWidth),
56 _tileHeight(tileHeight),
57 _requiredWidth(width),
58 _requiredHeight(height),
62 _requiredWidth = std::min(_requiredWidth, _tileWidth);
63 _requiredHeight = std::min(_requiredHeight, _tileHeight);
68 size_t getUid()
const {
return _uid; }
70 size_t getTileWidth()
const {
return _tileWidth; }
72 size_t getTileHeight()
const {
return _tileHeight; }
74 size_t getRequiredWidth()
const {
return _requiredWidth; }
76 size_t getRequiredHeight()
const {
return _requiredHeight; }
78 size_t getDepth()
const {
return _depth; }
92 void setData(std::unique_ptr<unsigned char>&& data) { _data = std::move(data); }
112 std::unique_ptr<unsigned char>
getData() {
return std::move(_data); }
115 std::unique_ptr<unsigned char> _data =
nullptr;
116 std::weak_ptr<TileCacheManager> _manager;
121 size_t _requiredWidth;
122 size_t _requiredHeight;
132 using IndexedStoragePaths = std::unordered_map<size_t, std::string>;
133 using IndexedFreeBlocks = std::unordered_map<size_t, std::list<size_t>>;
153 using MemoryMap = std::map<size_t, MemoryItem>;
159 using MRUType = boost::multi_index::multi_index_container<
161 boost::multi_index::indexed_by<boost::multi_index::sequenced<>,
162 boost::multi_index::hashed_unique<boost::multi_index::member<MRUItem, size_t, &MRUItem::objectId>>>>;
174 CacheManager(
const std::string& pathStorage,
size_t blockSize,
size_t maxBlocksPerIndex);
203 bool acquireObject(std::unique_ptr<unsigned char>& data,
size_t objectId);
212 std::string getPathForIndex(
size_t indexId);
213 void deleteIndexFiles();
216 bool prepareBlockGroup(
size_t startBlockId,
size_t blocksCount);
217 std::unique_ptr<unsigned char> load(
size_t startBlockId,
size_t blocksCount);
218 bool save(std::unique_ptr<unsigned char>&& data,
size_t startBlockId,
size_t blockCount);
219 bool saveObject(std::unique_ptr<unsigned char>&& data,
size_t objectId);
221 virtual void onRemovedFromMRU(
size_t objectId) = 0;
223 void addFreeBlock(
size_t blockId,
size_t blockCount);
224 size_t getFreeBlockId(
size_t blockCount);
227 size_t _blockSize{0};
228 size_t _incoreBlockUsageCount{0};
229 size_t _incoreBlockUsageMax{0};
230 size_t _blockCountPerIndex{0};
231 size_t _nextStartBlockId{0};
232 size_t _nextObjectId{0};
234 std::string _basePathStorage;
235 IndexedStoragePaths _indexPaths;
236 IndexedFreeBlocks _freeBlocks;
239 MemoryMap _memoryMap;
249 using shared_ptr = std::shared_ptr<TileCacheManager>;
250 using MapCachedTile = std::map<size_t, CachedTile::weak_pointer>;
264 static std::shared_ptr<TileCacheManager>
create(
const std::string& pathStorage,
size_t tileWidth,
size_t tileHeight,
size_t maxTilesPerIndex);
287 std::shared_ptr<CachedTile>
requireNewCachedTile(
size_t width,
size_t height,
size_t blockCount);
308 TileCacheManager(
const std::string& pathStorage,
size_t tileWidth,
size_t tileHeight,
size_t maxTilesPerIndex);
310 virtual void onRemovedFromMRU(
size_t objectId);
316 MapCachedTile _objectMap;