parent
d04e0234f2
commit
8ad78387ea
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include "StitchStruct.h"
|
||||
//
|
||||
using KeyType = int;
|
||||
|
||||
|
||||
class FileCache
|
||||
{
|
||||
public:
|
||||
explicit FileCache(size_t max_size, const std::string& cache_dir = "./cache");
|
||||
|
||||
bool get(const KeyType& key, std::shared_ptr<FrameCache>& pData);
|
||||
void set(const KeyType& key, std::shared_ptr<FrameCache> pData);
|
||||
|
||||
size_t getMemSize();
|
||||
|
||||
private:
|
||||
size_t max_size_;
|
||||
std::string cache_dir_;
|
||||
std::unordered_map<KeyType, std::pair<std::shared_ptr<FrameCache>, std::list<KeyType>::iterator>> cache_;
|
||||
std::list<KeyType> lru_list_;
|
||||
std::mutex mutex_;
|
||||
|
||||
bool loadFromFile(const KeyType& key, FrameCache& frame);
|
||||
void saveToFile(const KeyType& key, const FrameCache& frame);
|
||||
void evictIfNeeded();
|
||||
|
||||
std::string getCacheFilePath(const KeyType& key) const;
|
||||
};
|
@ -1,179 +0,0 @@
|
||||
#include "FileCache.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
FileCache::FileCache(const std::string& cache_dir, size_t max_memory_items)
|
||||
: cache_dir_(cache_dir), max_memory_items_(max_memory_items)
|
||||
{
|
||||
fs::create_directories(cache_dir_);
|
||||
}
|
||||
|
||||
FileCache::~FileCache()
|
||||
{
|
||||
// Clean up memory-mapped files
|
||||
for (auto& [key, mapped_file] : mapped_files_) {
|
||||
if (mapped_file.data) {
|
||||
unmap_file(mapped_file.data, mapped_file.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string FileCache::hash_key(const std::string& key) const
|
||||
{
|
||||
// Simple hash function (replace with a better one if needed)
|
||||
std::hash<std::string> hasher;
|
||||
size_t hash_value = hasher(key);
|
||||
std::ostringstream hex_stream;
|
||||
hex_stream << std::hex << hash_value;
|
||||
return hex_stream.str();
|
||||
}
|
||||
|
||||
std::string FileCache::get_file_path(const std::string& key) const
|
||||
{
|
||||
std::string hashed = hash_key(key);
|
||||
std::string sub_dir = cache_dir_ + "/" + hashed.substr(0, 2);
|
||||
fs::create_directories(sub_dir);
|
||||
return sub_dir + "/" + hashed.substr(2) + ".cache";
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> FileCache::get(const std::string& key)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
if (memory_cache_.count(key)) {
|
||||
const auto& data = memory_cache_[key];
|
||||
return *reinterpret_cast<const T*>(data.data());
|
||||
}
|
||||
|
||||
if (mapped_files_.count(key)) {
|
||||
const auto& mapped_file = mapped_files_[key];
|
||||
return *reinterpret_cast<const T*>(mapped_file.data);
|
||||
}
|
||||
}
|
||||
|
||||
auto file_path = get_file_path(key);
|
||||
return load_from_disk<T>(file_path);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void FileCache::set(const std::string& key, const T& data)
|
||||
{
|
||||
auto serialized = std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(&data),
|
||||
reinterpret_cast<const uint8_t*>(&data) + sizeof(T));
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
memory_cache_[key] = serialized;
|
||||
|
||||
// Evict if necessary
|
||||
if (memory_cache_.size() > max_memory_items_)
|
||||
{
|
||||
memory_cache_.erase(memory_cache_.begin());
|
||||
}
|
||||
}
|
||||
|
||||
// Async save to disk
|
||||
auto file_path = get_file_path(key);
|
||||
async_tasks_.push_back(std::async(std::launch::async, [this, file_path, serialized]() {
|
||||
save_to_disk(file_path, serialized);
|
||||
}));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool FileCache::save_to_disk(const std::string& file_path, const T& data)
|
||||
{
|
||||
std::ofstream out(file_path, std::ios::binary);
|
||||
if (!out) return false;
|
||||
|
||||
out.write(reinterpret_cast<const char*>(&data), sizeof(data));
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> FileCache::load_from_disk(const std::string& file_path) const
|
||||
{
|
||||
size_t file_size = fs::file_size(file_path);
|
||||
void* mapped_data = map_file(file_path, file_size);
|
||||
if (!mapped_data)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Store the mapped file for later use
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
mapped_files_[file_path] = {mapped_data, file_size};
|
||||
|
||||
return *reinterpret_cast<const T*>(mapped_data);
|
||||
}
|
||||
|
||||
void FileCache::clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
memory_cache_.clear();
|
||||
for (const auto& [key, mapped_file] : mapped_files_) {
|
||||
if (mapped_file.data)
|
||||
{
|
||||
unmap_file(mapped_file.data, mapped_file.size);
|
||||
}
|
||||
}
|
||||
mapped_files_.clear();
|
||||
for (const auto& entry : fs::directory_iterator(cache_dir_))
|
||||
{
|
||||
fs::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* FileCache::map_file(const std::string& file_path, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
HANDLE file = CreateFileA(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
|
||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (file == INVALID_HANDLE_VALUE) return nullptr;
|
||||
|
||||
HANDLE mapping = CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
|
||||
if (!mapping)
|
||||
{
|
||||
CloseHandle(file);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size);
|
||||
CloseHandle(mapping);
|
||||
CloseHandle(file);
|
||||
return data;
|
||||
#else
|
||||
int fd = open(file_path.c_str(), O_RDONLY);
|
||||
if (fd == -1) return nullptr;
|
||||
|
||||
void* data = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
close(fd);
|
||||
|
||||
if (data == MAP_FAILED) {
|
||||
return nullptr;
|
||||
}
|
||||
return data;
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileCache::unmap_file(void* data, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
UnmapViewOfFile(data);
|
||||
#else
|
||||
munmap(data, size);
|
||||
#endif
|
||||
}
|
@ -0,0 +1,229 @@
|
||||
#include "FileCache.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <stdexcept>
|
||||
#include "StitchStruct.h"
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "openssl/sha.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
FileCache::FileCache(const std::string& cache_dir, size_t max_memory_items)
|
||||
: cache_dir_(cache_dir), max_memory_items_(max_memory_items)
|
||||
{
|
||||
fs::create_directories(cache_dir_);
|
||||
}
|
||||
|
||||
FileCache::~FileCache()
|
||||
{
|
||||
// Clean up memory-mapped files
|
||||
for (auto& [key, mapped_file] : mapped_files_)
|
||||
{
|
||||
unmap_file(&mapped_file, sizeof(FrameCache));
|
||||
}
|
||||
}
|
||||
|
||||
std::string FileCache::hash_key(const KeyType& key) const
|
||||
{
|
||||
// Convert key to a string representation
|
||||
std::string key_str = std::to_string(key);
|
||||
|
||||
// Compute SHA-256 hash
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
SHA256_CTX sha256;
|
||||
SHA256_Init(&sha256);
|
||||
SHA256_Update(&sha256, key_str.c_str(), key_str.size());
|
||||
SHA256_Final(hash, &sha256);
|
||||
|
||||
// Convert hash to a hex string
|
||||
std::ostringstream hex_stream;
|
||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
||||
hex_stream << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
|
||||
}
|
||||
|
||||
return hex_stream.str();
|
||||
}
|
||||
|
||||
std::string FileCache::get_file_path(const KeyType& key) const
|
||||
{
|
||||
std::string hashed = hash_key(key);
|
||||
return cache_dir_ + "/" + hashed.substr(2) + ".cache";
|
||||
}
|
||||
|
||||
|
||||
bool FileCache::get(KeyType key,FrameCache* pData)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
if (memory_cache_.count(key))
|
||||
{
|
||||
memcpy(pData,&memory_cache_[key],sizeof(FrameCache));
|
||||
std::cout << "get from memory_cache_:" << pData->_para.nFrmID << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (mapped_files_.count(key))
|
||||
{
|
||||
memcpy(pData,&mapped_files_[key],sizeof(FrameCache));
|
||||
std::cout << "get from mapped_files_:" << pData->_para.nFrmID << std::endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
auto file_path = get_file_path(key);
|
||||
return load_from_disk(file_path,key,pData);
|
||||
}
|
||||
|
||||
|
||||
void FileCache::set(KeyType key, FrameCache* pData)
|
||||
{
|
||||
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
|
||||
memcpy(&memory_cache_[key],pData,sizeof(FrameCache));
|
||||
|
||||
std::cout << "set memory_cache:" << memory_cache_[key]._para.nFrmID <<std::endl;
|
||||
|
||||
// Evict if necessary
|
||||
if (memory_cache_.size() > max_memory_items_)
|
||||
{
|
||||
memory_cache_.erase(memory_cache_.begin());
|
||||
}
|
||||
|
||||
|
||||
// Async save to disk
|
||||
auto file_path = get_file_path(key);
|
||||
|
||||
//save_to_disk(file_path, pData);
|
||||
async_tasks_.push_back(std::async(std::launch::async, [this, file_path, pData]() {
|
||||
save_to_disk(file_path, pData);
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
bool FileCache::save_to_disk(const std::string& file_path, FrameCache* data)
|
||||
{
|
||||
std::ofstream out(file_path, std::ios::binary);
|
||||
if (!out) return false;
|
||||
|
||||
auto nsize = sizeof(FrameCache);
|
||||
|
||||
//std::cout << "set file:" << data->_para.nFrmID <<std::endl;
|
||||
|
||||
out.write(reinterpret_cast<const char*>(data), sizeof(FrameCache));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool FileCache::load_from_disk(const std::string& file_path,const KeyType& key,FrameCache* pData)
|
||||
{
|
||||
if (!fs::exists(file_path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* str = file_path.c_str();
|
||||
|
||||
size_t file_size = fs::file_size(file_path);
|
||||
void* mapped_data = map_file(file_path, file_size);
|
||||
if (!mapped_data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store the mapped file for later use
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
|
||||
FrameCache* ptr = &mapped_files_[key];
|
||||
|
||||
// 存储到文件映射中
|
||||
memcpy(ptr,mapped_data,sizeof(FrameCache));
|
||||
|
||||
// 返回
|
||||
memcpy(pData,mapped_data,sizeof(FrameCache));
|
||||
|
||||
|
||||
|
||||
std::cout << "get from raw_files_:" << pData->_para.nFrmID << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileCache::clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
memory_cache_.clear();
|
||||
for (const auto& [key, mapped_file] : mapped_files_)
|
||||
{
|
||||
unmap_file((void*)&mapped_file, sizeof(FrameCache));
|
||||
}
|
||||
mapped_files_.clear();
|
||||
for (const auto& entry : fs::directory_iterator(cache_dir_))
|
||||
{
|
||||
fs::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
SINT32 FileCache::get_mem_size()
|
||||
{
|
||||
return memory_cache_.size();
|
||||
}
|
||||
|
||||
SINT32 FileCache::get_file_size()
|
||||
{
|
||||
return mapped_files_.size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* FileCache::map_file(const std::string& file_path, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
HANDLE file = CreateFileA(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
|
||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (file == INVALID_HANDLE_VALUE) return nullptr;
|
||||
|
||||
HANDLE mapping = CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
|
||||
if (!mapping)
|
||||
{
|
||||
CloseHandle(file);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size);
|
||||
CloseHandle(mapping);
|
||||
CloseHandle(file);
|
||||
return data;
|
||||
#else
|
||||
int fd = open(file_path.c_str(), O_RDONLY);
|
||||
if (fd == -1) return nullptr;
|
||||
|
||||
void* data = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
close(fd);
|
||||
|
||||
if (data == MAP_FAILED) {
|
||||
return nullptr;
|
||||
}
|
||||
return data;
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileCache::unmap_file(void* data, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
UnmapViewOfFile(data);
|
||||
#else
|
||||
munmap(data, size);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -1,55 +0,0 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
class FileCache
|
||||
{
|
||||
public:
|
||||
FileCache(const std::string& cache_dir = "./tile_cache", size_t max_memory_items = 1000);
|
||||
~FileCache();
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> get(const std::string& key);
|
||||
|
||||
template<typename T>
|
||||
void set(const std::string& key, const T& data);
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::string get_file_path(const std::string& key) const;
|
||||
std::string hash_key(const std::string& key) const;
|
||||
|
||||
template<typename T>
|
||||
bool save_to_disk(const std::string& file_path, const T& data);
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> load_from_disk(const std::string& file_path) const;
|
||||
|
||||
std::string cache_dir_;
|
||||
size_t max_memory_items_;
|
||||
|
||||
// Memory cache
|
||||
mutable std::mutex cache_mutex_;
|
||||
std::unordered_map<std::string, std::vector<uint8_t>> memory_cache_;
|
||||
|
||||
// Memory-mapped files
|
||||
struct MappedFile {
|
||||
void* data;
|
||||
size_t size;
|
||||
};
|
||||
std::unordered_map<std::string, MappedFile> mapped_files_;
|
||||
|
||||
// Async tasks
|
||||
std::vector<std::future<void>> async_tasks_;
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* map_file(const std::string& file_path, size_t size);
|
||||
void unmap_file(void* data, size_t size);
|
||||
};
|
@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include "StitchStruct.h"
|
||||
|
||||
typedef int KeyType;
|
||||
|
||||
class FileCache
|
||||
{
|
||||
public:
|
||||
FileCache(const std::string& cache_dir = "./tile_cache", size_t max_memory_items = 200);
|
||||
~FileCache();
|
||||
|
||||
|
||||
bool get(KeyType key,FrameCache* pData);
|
||||
|
||||
void set(KeyType key, FrameCache* pData);
|
||||
|
||||
void clear();
|
||||
|
||||
SINT32 get_mem_size();
|
||||
SINT32 get_file_size();
|
||||
|
||||
private:
|
||||
std::string get_file_path(const KeyType& key) const;
|
||||
std::string hash_key(const KeyType& key) const;
|
||||
|
||||
bool save_to_disk(const std::string& file_path, FrameCache* data);
|
||||
|
||||
|
||||
bool load_from_disk(const std::string& file_path,const KeyType& key,FrameCache* pData);
|
||||
|
||||
std::string cache_dir_;
|
||||
size_t max_memory_items_;
|
||||
|
||||
// Memory cache
|
||||
mutable std::mutex cache_mutex_;
|
||||
|
||||
std::unordered_map<KeyType, FrameCache> memory_cache_;
|
||||
|
||||
|
||||
std::unordered_map<KeyType, FrameCache> mapped_files_;
|
||||
|
||||
// Async tasks
|
||||
std::vector<std::future<void>> async_tasks_;
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* map_file(const std::string& file_path, size_t size);
|
||||
void unmap_file(void* data, size_t size);
|
||||
};
|
Loading…
Reference in new issue