Skip to content

Commit 9e610d3

Browse files
authored
feat: Shard Caching (#4174)
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
1 parent 04e3322 commit 9e610d3

File tree

3 files changed

+668
-49
lines changed

3 files changed

+668
-49
lines changed

libmamba/include/mamba/core/shards.hpp

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424

2525
namespace mamba
2626
{
27-
// Forward declaration
28-
class TemporaryFile;
29-
3027
/**
3128
* Handle repodata_shards.msgpack.zst and individual per-package shards.
3229
*
@@ -129,11 +126,31 @@ namespace mamba
129126
/** Cached resolved base_url for packages. */
130127
mutable std::optional<std::string> m_base_url_cache;
131128

129+
/** Root directory of the writable packages cache (e.g. first writable pkgs_dir). */
130+
fs::u8path m_pkgs_cache_root;
131+
132+
/** Directory for cached shard files: {pkgs_cache_root}/cache/shards */
133+
fs::u8path m_shard_cache_dir;
134+
132135
/**
133136
* Get the base URL where shards are stored.
134137
*/
135138
[[nodiscard]] auto shards_base_url() const -> std::string;
136139

140+
/**
141+
* Get the root directory used for shard caching.
142+
*
143+
* When constructed with an explicit cache root, that path is used.
144+
* Otherwise, this falls back to an environment-based default using
145+
* XDG_CACHE_HOME or util::user_cache_dir().
146+
*/
147+
[[nodiscard]] auto pkgs_cache_root() const -> fs::u8path;
148+
149+
/**
150+
* Get the shard cache directory: {pkgs_cache_root}/cache/shards
151+
*/
152+
[[nodiscard]] auto shard_cache_dir() const -> const fs::u8path&;
153+
137154
/**
138155
* Get the relative path for a shard (for use with download::Request).
139156
* Returns path relative to channel base.
@@ -160,16 +177,15 @@ namespace mamba
160177

161178
/**
162179
* Create download requests for shards with proper mirror handling.
180+
* Downloads go directly to the shard cache path.
163181
*/
164182
void create_download_requests(
165183
const std::map<std::string, std::string>& url_to_package,
166-
const std::string& cache_dir_str,
167184
download::mirror_map& extended_mirrors,
168185
download::MultiRequest& requests,
169186
std::vector<std::string>& cache_miss_urls,
170187
std::vector<std::string>& cache_miss_packages,
171-
std::map<std::string, fs::u8path>& package_to_artifact_path,
172-
std::vector<std::shared_ptr<TemporaryFile>>& artifacts
188+
std::map<std::string, fs::u8path>& package_to_cache_path
173189
) const;
174190

175191
/**
@@ -178,21 +194,46 @@ namespace mamba
178194
auto process_downloaded_shard(
179195
const std::string& package,
180196
const download::Success& success,
181-
const std::map<std::string, fs::u8path>& package_to_artifact_path
197+
const std::map<std::string, fs::u8path>& package_to_cache_path
182198
) -> expected_t<ShardDict>;
183199

184200
/**
185201
* Decompress zstd compressed shard data.
186202
*/
187-
auto decompress_zstd_shard(const std::vector<std::uint8_t>& compressed_data)
203+
auto decompress_zstd_shard(const std::vector<std::uint8_t>& compressed_data) const
188204
-> expected_t<std::vector<std::uint8_t>>;
189205

190206
/**
191207
* Parse msgpack data into ShardDict.
192208
*/
193-
auto
194-
parse_shard_msgpack(const std::vector<std::uint8_t>& decompressed_data, const std::string& package)
195-
-> expected_t<ShardDict>;
209+
auto parse_shard_msgpack(
210+
const std::vector<std::uint8_t>& decompressed_data,
211+
const std::string& package
212+
) const -> expected_t<ShardDict>;
213+
214+
/**
215+
* Get the cache path for a shard file.
216+
* Returns path: {cache_dir}/cache/shards/{hex_hash}.msgpack.zst
217+
*/
218+
[[nodiscard]] auto shard_cache_path(const std::string& package) const -> fs::u8path;
219+
220+
/**
221+
* Check if a shard is cached and valid (matches expected hash).
222+
*/
223+
[[nodiscard]] auto is_shard_cached(const std::string& package) const -> bool;
224+
225+
/**
226+
* Load and parse a shard from cache.
227+
*/
228+
auto load_shard_from_cache(const std::string& package) const -> expected_t<ShardDict>;
229+
230+
/** For unit testing. */
231+
friend auto test_process_downloaded_shard(
232+
Shards& shards,
233+
const std::string& package,
234+
const download::Success& success,
235+
const std::map<std::string, fs::u8path>& package_to_cache_path
236+
) -> expected_t<ShardDict>;
196237
};
197238

198239
}

0 commit comments

Comments
 (0)