1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use std::time::Duration;
use crate::error::Result;
use crate::models::distribution::Distribution as JdkDistribution;
use crate::models::metadata::JdkMetadata;
use crate::models::package::PackageType;
use crate::version::parser::ParsedVersionRequest;
use super::models::{PlatformFilter, SearchResult, VersionSearchType};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MetadataCache {
pub version: u32,
pub last_updated: DateTime<Utc>,
pub distributions: HashMap<String, DistributionCache>,
/// Maps distribution synonyms to their canonical api_parameter names
#[serde(default)]
pub synonym_map: HashMap<String, String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DistributionCache {
pub distribution: JdkDistribution,
pub display_name: String,
pub packages: Vec<JdkMetadata>,
}
impl MetadataCache {
pub fn new() -> Self {
Self {
version: 1,
last_updated: Utc::now(),
distributions: HashMap::new(),
synonym_map: HashMap::new(),
}
}
}
impl Default for MetadataCache {
fn default() -> Self {
Self::new()
}
}
impl MetadataCache {
/// Check if the cache is stale based on the given maximum age
pub fn is_stale(&self, max_age: Duration) -> bool {
let now = Utc::now();
let elapsed = now.signed_duration_since(self.last_updated);
// Convert chrono::Duration to std::time::Duration for comparison
match elapsed.to_std() {
Ok(std_duration) => std_duration > max_age,
Err(_) => true, // If time went backwards or conversion failed, consider stale
}
}
pub fn has_version(&self, version: &str) -> bool {
for dist in self.distributions.values() {
for package in &dist.packages {
if package.version.to_string() == version {
return true;
}
}
}
false
}
pub fn save(&self, path: &Path) -> Result<()> {
super::storage::save_cache(self, path)
}
/// Get the canonical name for a distribution from the synonym map
/// Returns None if not found
pub fn get_canonical_name(&self, name: &str) -> Option<&str> {
self.synonym_map.get(name).map(|s| s.as_str())
}
/// Search for packages matching the given request
pub fn search(
&self,
request: &ParsedVersionRequest,
version_type: VersionSearchType,
) -> Result<Vec<SearchResult>> {
let platform_filter = PlatformFilter::default();
let mut results = Vec::new();
// Pre-compute version string if needed to avoid repeated conversions
let version_str = request.version.as_ref().map(|v| v.to_string());
// Determine actual version type to use
let actual_version_type = match version_type {
VersionSearchType::Auto => {
if let Some(ref v_str) = version_str {
Self::detect_version_type(v_str)
} else {
VersionSearchType::JavaVersion
}
}
other => other,
};
for (dist_name, dist_cache) in &self.distributions {
// Filter by distribution if specified
if let Some(ref target_dist) = request.distribution {
if dist_cache.distribution != *target_dist {
continue;
}
}
if request.latest {
// For "latest" requests, find the highest version per distribution
let mut latest_package: Option<&JdkMetadata> = None;
for package in &dist_cache.packages {
// Apply package type filter if specified
if let Some(ref package_type) = request.package_type {
if package.package_type != *package_type {
continue;
}
}
// Apply platform filters
if !self.matches_package(
package,
request,
version_str.as_deref(),
actual_version_type,
&platform_filter,
) {
continue;
}
// Track the latest version
match latest_package {
None => latest_package = Some(package),
Some(current_latest) => {
if package.version > current_latest.version {
latest_package = Some(package);
}
}
}
}
if let Some(package) = latest_package {
results.push(SearchResult {
distribution: dist_name.to_string(),
display_name: dist_cache.display_name.clone(),
package: package.clone(),
});
}
} else {
// Regular search - include all matching versions
for package in &dist_cache.packages {
if !self.matches_package(
package,
request,
version_str.as_deref(),
actual_version_type,
&platform_filter,
) {
continue;
}
results.push(SearchResult {
distribution: dist_name.to_string(),
display_name: dist_cache.display_name.clone(),
package: package.clone(),
});
}
}
}
// Sort by distribution and version
results.sort_by(|a, b| match a.distribution.cmp(&b.distribution) {
std::cmp::Ordering::Equal => b.package.version.cmp(&a.package.version),
other => other,
});
Ok(results)
}
/// Auto-detect whether to search by java_version or distribution_version
pub fn detect_version_type(version_str: &str) -> VersionSearchType {
// If the version has 4+ components, likely a distribution_version
let component_count = version_str.split('.').count();
if component_count >= 4 {
return VersionSearchType::DistributionVersion;
}
// If it contains non-numeric build identifiers after +, likely distribution_version
if let Some(plus_pos) = version_str.find('+') {
let build_part = &version_str[plus_pos + 1..];
// Check if build part contains non-numeric characters or multiple components
if build_part.contains('.') || build_part.chars().any(|c| !c.is_ascii_digit()) {
return VersionSearchType::DistributionVersion;
}
}
// Default to java_version for standard formats
VersionSearchType::JavaVersion
}
/// Look up a specific package by distribution, version, and platform
pub fn lookup(
&self,
distribution: &JdkDistribution,
version: &str,
architecture: &str,
operating_system: &str,
package_type: Option<&PackageType>,
javafx_bundled: Option<bool>,
) -> Option<JdkMetadata> {
use crate::models::package::ArchiveType;
// Look up distribution by its API name, resolving synonyms
let canonical_name = self
.get_canonical_name(distribution.id())
.unwrap_or(distribution.id());
let dist_cache = self.distributions.get(canonical_name)?;
// Find exact match, filtering for supported archive types only
dist_cache
.packages
.iter()
.find(|pkg| {
pkg.version.matches_pattern(version)
&& pkg.architecture.to_string() == architecture
&& pkg.operating_system.to_string() == operating_system
&& (package_type.is_none() || Some(&pkg.package_type) == package_type)
&& (javafx_bundled.is_none() || Some(pkg.javafx_bundled) == javafx_bundled)
&& self.matches_platform_libc(&pkg.lib_c_type)
&& matches!(pkg.archive_type, ArchiveType::TarGz | ArchiveType::Zip)
})
.cloned()
}
/// Check if the package's lib_c_type is compatible with the current platform
fn matches_platform_libc(&self, lib_c_type: &Option<String>) -> bool {
match lib_c_type {
None => true, // If no lib_c_type specified, assume it's compatible
Some(libc) => crate::platform::matches_foojay_libc_type(libc),
}
}
fn matches_package(
&self,
package: &JdkMetadata,
request: &ParsedVersionRequest,
version_str: Option<&str>,
version_type: VersionSearchType,
platform_filter: &PlatformFilter,
) -> bool {
// Check version match if version is specified
if let Some(version_pattern) = version_str {
let matches = match version_type {
VersionSearchType::JavaVersion => package.version.matches_pattern(version_pattern),
VersionSearchType::DistributionVersion => {
// Use Version's matches_pattern method for distribution_version
package
.distribution_version
.matches_pattern(version_pattern)
}
VersionSearchType::Auto => {
// This shouldn't happen as Auto is resolved earlier, but handle it
package.version.matches_pattern(version_pattern)
}
};
if !matches {
return false;
}
}
// Check package type if specified
if let Some(ref package_type) = request.package_type {
if package.package_type != *package_type {
return false;
}
}
// Apply platform filters if set
if let Some(ref arch) = platform_filter.architecture {
if package.architecture.to_string() != *arch {
return false;
}
}
if let Some(ref os) = platform_filter.operating_system {
if package.operating_system.to_string() != *os {
return false;
}
}
if let Some(ref lib_c) = platform_filter.lib_c_type {
if let Some(ref pkg_lib_c) = package.lib_c_type {
if pkg_lib_c != lib_c {
return false;
}
} else {
// Package doesn't specify lib_c_type, skip it if we're filtering
return false;
}
} else {
// No explicit lib_c_type filter, but we should still check platform compatibility
if !self.matches_platform_libc(&package.lib_c_type) {
return false;
}
}
true
}
}