Grok 20.3.2
DiskCache.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <cstdint>
21#include <cstdio>
22#include <filesystem>
23#ifdef _WIN32
24#include <process.h>
25#else
26#include <unistd.h>
27#endif
28#include <fstream>
29#include <mutex>
30#include <optional>
31#include <string>
32#include <unordered_map>
33#include <vector>
34
35namespace grk
36{
37
49{
50public:
55 explicit DiskCache(const std::string& basePath = "")
56 {
57 namespace fs = std::filesystem;
58 if(basePath.empty())
59 {
60 cacheDir_ = fs::temp_directory_path() / "grok_cache_XXXXXX";
61 // Create a unique directory
62 std::string tmpl = cacheDir_.string();
63#ifdef _WIN32
64 // mkdtemp not available on Windows; use PID-based name
65 cacheDir_ = fs::temp_directory_path() / ("grok_cache_" + std::to_string(_getpid()));
66#else
67 if(!mkdtemp(tmpl.data()))
68 {
69 // Fallback: use PID-based name
70 cacheDir_ = fs::temp_directory_path() / ("grok_cache_" + std::to_string(getpid()));
71 }
72 else
73 {
74 cacheDir_ = tmpl;
75 }
76#endif
77 }
78 else
79 {
80 cacheDir_ = basePath;
81 }
82 std::filesystem::create_directories(cacheDir_);
83 }
84
86 {
87 std::error_code ec;
88 std::filesystem::remove_all(cacheDir_, ec);
89 }
90
91 // non-copyable
92 DiskCache(const DiskCache&) = delete;
93 DiskCache& operator=(const DiskCache&) = delete;
94
98 void store(uint16_t tileIndex, const uint8_t* data, size_t size)
99 {
100 auto path = tilePath(tileIndex);
101 std::ofstream out(path, std::ios::binary | std::ios::trunc);
102 if(!out)
103 return;
104
105 out.write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(size));
106 out.close();
107
108 std::lock_guard<std::mutex> lock(mutex_);
109 index_[tileIndex] = size;
110 }
111
116 std::optional<std::vector<uint8_t>> load(uint16_t tileIndex)
117 {
118 {
119 std::lock_guard<std::mutex> lock(mutex_);
120 if(index_.find(tileIndex) == index_.end())
121 return std::nullopt;
122 }
123
124 auto path = tilePath(tileIndex);
125 std::ifstream in(path, std::ios::binary | std::ios::ate);
126 if(!in)
127 return std::nullopt;
128
129 auto fileSize = static_cast<size_t>(in.tellg());
130 if(fileSize == 0)
131 return std::nullopt;
132
133 in.seekg(0);
134
135 std::vector<uint8_t> result(fileSize);
136 in.read(reinterpret_cast<char*>(result.data()), static_cast<std::streamsize>(fileSize));
137
138 return result;
139 }
140
144 bool contains(uint16_t tileIndex) const
145 {
146 std::lock_guard<std::mutex> lock(mutex_);
147 return index_.find(tileIndex) != index_.end();
148 }
149
153 void clear()
154 {
155 std::lock_guard<std::mutex> lock(mutex_);
156 for(auto& [idx, _] : index_)
157 {
158 std::error_code ec;
159 std::filesystem::remove(tilePath(idx), ec);
160 }
161 index_.clear();
162 }
163
164 size_t size() const
165 {
166 std::lock_guard<std::mutex> lock(mutex_);
167 return index_.size();
168 }
169
170private:
171 std::filesystem::path tilePath(uint16_t tileIndex) const
172 {
173 return cacheDir_ / ("tile_" + std::to_string(tileIndex) + ".grk");
174 }
175
176 std::filesystem::path cacheDir_;
177 std::unordered_map<uint16_t, size_t> index_; // tileIndex → data size
178 mutable std::mutex mutex_;
179};
180
181} // namespace grk
bool contains(uint16_t tileIndex) const
Check if a tile is stored on disk.
Definition DiskCache.h:144
DiskCache(const std::string &basePath="")
Create a disk cache in a temporary directory.
Definition DiskCache.h:55
void clear()
Remove all cached entries from disk.
Definition DiskCache.h:153
size_t size() const
Definition DiskCache.h:164
std::filesystem::path tilePath(uint16_t tileIndex) const
Definition DiskCache.h:171
~DiskCache()
Definition DiskCache.h:85
DiskCache(const DiskCache &)=delete
std::optional< std::vector< uint8_t > > load(uint16_t tileIndex)
Load raw bytes from disk for a given tile.
Definition DiskCache.h:116
std::mutex mutex_
Definition DiskCache.h:178
void store(uint16_t tileIndex, const uint8_t *data, size_t size)
Store raw bytes to disk for a given tile.
Definition DiskCache.h:98
std::unordered_map< uint16_t, size_t > index_
Definition DiskCache.h:177
DiskCache & operator=(const DiskCache &)=delete
std::filesystem::path cacheDir_
Definition DiskCache.h:176
ResWindow.
Definition CompressedChunkCache.h:36