namespace cache::FileCache and use unique_ptr

This commit is contained in:
Zach Pomerantz 2017-03-01 20:58:00 -05:00
parent c71e614dd5
commit febeeeca3a
4 changed files with 32 additions and 18 deletions

View file

@ -13,6 +13,9 @@
#include <ktx/KTX.h>
using File = cache::File;
using FilePointer = cache::FilePointer;
KTXFilePointer KTXCache::writeFile(Data data) {
return std::static_pointer_cast<KTXFile>(FileCache::writeFile(data.key, data.data, data.length, (void*)&data));
}
@ -35,19 +38,22 @@ KTXFilePointer KTXCache::getFile(const QUrl& url) {
return file;
}
File* KTXCache::createFile(const Key& key, const std::string& filepath, size_t length, void* extra) {
const QUrl& url = reinterpret_cast<Data*>(extra)->url;
std::unique_ptr<File> KTXCache::createKTXFile(const Key& key, const std::string& filepath, size_t length, const QUrl& url) {
Lock lock(_urlMutex);
_urlMap[url] = key;
qCInfo(file_cache) << "Wrote KTX" << key.c_str() << url;
return new KTXFile(key, filepath, length, url);
return std::unique_ptr<File>(new KTXFile(key, filepath, length, url));
}
File* KTXCache::loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) {
std::unique_ptr<File> KTXCache::createFile(const Key& key, const std::string& filepath, size_t length, void* extra) {
const QUrl& url = reinterpret_cast<Data*>(extra)->url;
qCInfo(file_cache) << "Wrote KTX" << key.c_str() << url;
return createKTXFile(key, filepath, length, url);
}
std::unique_ptr<File> KTXCache::loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) {
const QUrl url = QString(metadata.c_str());
_urlMap[url] = key;
qCInfo(file_cache) << "Loaded KTX" << key.c_str() << url;
return new KTXFile(key, filepath, length, url);
return createKTXFile(key, filepath, length, url);
}
void KTXCache::evictedFile(const FilePointer& file) {

View file

@ -23,7 +23,7 @@ namespace ktx {
class KTXFile;
using KTXFilePointer = std::shared_ptr<KTXFile>;
class KTXCache : public FileCache {
class KTXCache : public cache::FileCache {
Q_OBJECT
public:
@ -42,11 +42,13 @@ public:
KTXFilePointer getFile(const QUrl& url);
protected:
File* createFile(const Key& key, const std::string& filepath, size_t length, void* extra) override final;
File* loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) override final;
void evictedFile(const FilePointer& file) override final;
std::unique_ptr<cache::File> createFile(const Key& key, const std::string& filepath, size_t length, void* extra) override final;
std::unique_ptr<cache::File> loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) override final;
void evictedFile(const cache::FilePointer& file) override final;
private:
std::unique_ptr<cache::File> createKTXFile(const Key& key, const std::string& filepath, size_t length, const QUrl& url);
using Mutex = std::mutex;
using Lock = std::lock_guard<Mutex>;
struct QUrlHasher { std::size_t operator()(QUrl const& url) const { return qHash(url); } };
@ -55,7 +57,7 @@ private:
Mutex _urlMutex;
};
class KTXFile : public File {
class KTXFile : public cache::File {
Q_OBJECT
public:

View file

@ -22,6 +22,8 @@
Q_LOGGING_CATEGORY(file_cache, "hifi.file_cache")
using namespace cache;
static const std::string MANIFEST_NAME = "manifest";
void FileCache::setUnusedFileCacheSize(size_t unusedFilesMaxSize) {
@ -85,7 +87,7 @@ void FileCache::initialize() {
const std::string filepath = dir.filePath(filename).toStdString();
const size_t length = std::ifstream(filepath, std::ios::binary | std::ios::ate).tellg();
FilePointer file(loadFile(key, filepath, length, metadata), &fileDeleter);
FilePointer file(loadFile(key, filepath, length, metadata).release(), &fileDeleter);
file->_cache = this;
_files[key] = file;
_numTotalFiles += 1;
@ -119,7 +121,7 @@ FilePointer FileCache::writeFile(const Key& key, const char* data, size_t length
// write the new file
FILE* saveFile = fopen(filepath.c_str(), "wb");
if (saveFile != nullptr && fwrite(data, length, 1, saveFile) && fclose(saveFile) == 0) {
file.reset(createFile(key, filepath, length, extra), &fileDeleter);
file.reset(createFile(key, filepath, length, extra).release(), &fileDeleter);
file->_cache = this;
_files[key] = file;
_numTotalFiles += 1;

View file

@ -22,6 +22,8 @@
Q_DECLARE_LOGGING_CATEGORY(file_cache)
namespace cache {
class File;
using FilePointer = std::shared_ptr<File>;
@ -80,9 +82,9 @@ protected:
FilePointer getFile(const Key& key);
/// create a file (ex.: create a class derived from File and store it in a secondary map with extra->url)
virtual File* createFile(const Key& key, const std::string& filepath, size_t length, void* extra) = 0;
virtual std::unique_ptr<File> createFile(const Key& key, const std::string& filepath, size_t length, void* extra) = 0;
/// load a file
virtual File* loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) = 0;
virtual std::unique_ptr<File> loadFile(const Key& key, const std::string& filepath, size_t length, const std::string& metadata) = 0;
/// take action when a file is evicted from the cache (ex.: evict it from a secondary map)
virtual void evictedFile(const FilePointer& file) = 0;
@ -130,6 +132,8 @@ public:
Key getKey() const { return _key; }
size_t getLength() const { return _length; }
// the destructor should handle unlinking of the actual filepath
virtual ~File();
// overrides should call File::deleter to maintain caching behavior
virtual void deleter();
@ -137,8 +141,6 @@ protected:
// when constructed, the file has already been created/written
File(const Key& key, const std::string& filepath, size_t length) :
_filepath(filepath), _key(key), _length(length) {}
// the destructor should handle unlinking of the actual filepath
virtual ~File();
/// get metadata to store with a file between instances (ex.: return the url of a hash)
virtual std::string getMetadata() const = 0;
@ -157,4 +159,6 @@ private:
bool _shouldPersist { false };
};
}
#endif // hifi_FileCache_h