ngdp-cache 0.4.3

Transparent caching layer with TTL support for all NGDP operations
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! CDN content cache implementation
//!
//! This module caches all CDN content following the CDN path structure:
//! - `{cdn_path}/config/{first2}/{next2}/{hash}` - Configuration files
//! - `{cdn_path}/data/{first2}/{next2}/{hash}` - Data files and archives
//! - `{cdn_path}/patch/{first2}/{next2}/{hash}` - Patch files
//!
//! Where `{cdn_path}` is the path provided by the CDN (e.g., "tpr/wow").
//! Archives and indices are stored in the data directory with `.index` extension for indices.

use std::path::PathBuf;
use tracing::{debug, trace};

use crate::{Result, ensure_dir, get_cache_dir};

/// Cache for CDN content following the standard CDN directory structure
pub struct CdnCache {
    /// Base directory for CDN cache
    base_dir: PathBuf,
    /// CDN path prefix (e.g., "tpr/wow")
    cdn_path: Option<String>,
}

impl CdnCache {
    /// Create a new CDN cache
    pub async fn new() -> Result<Self> {
        let base_dir = get_cache_dir()?.join("cdn");
        ensure_dir(&base_dir).await?;

        debug!("Initialized CDN cache at: {:?}", base_dir);

        Ok(Self {
            base_dir,
            cdn_path: None,
        })
    }

    /// Create a CDN cache for a specific product
    pub async fn for_product(product: &str) -> Result<Self> {
        let base_dir = get_cache_dir()?.join("cdn").join(product);
        ensure_dir(&base_dir).await?;

        debug!(
            "Initialized CDN cache for product '{}' at: {:?}",
            product, base_dir
        );

        Ok(Self {
            base_dir,
            cdn_path: None,
        })
    }

    /// Create a CDN cache with a custom base directory
    pub async fn with_base_dir(base_dir: PathBuf) -> Result<Self> {
        ensure_dir(&base_dir).await?;

        debug!("Initialized CDN cache at: {:?}", base_dir);

        Ok(Self {
            base_dir,
            cdn_path: None,
        })
    }

    /// Create a CDN cache with a specific CDN path
    pub async fn with_cdn_path(cdn_path: &str) -> Result<Self> {
        let base_dir = get_cache_dir()?.join("cdn");
        ensure_dir(&base_dir).await?;

        debug!(
            "Initialized CDN cache with path '{}' at: {:?}",
            cdn_path, base_dir
        );

        Ok(Self {
            base_dir,
            cdn_path: Some(cdn_path.to_string()),
        })
    }

    /// Set the CDN path for this cache
    pub fn set_cdn_path(&mut self, cdn_path: Option<String>) {
        self.cdn_path = cdn_path;
    }

    /// Get the effective base directory including CDN path
    fn effective_base_dir(&self) -> PathBuf {
        if let Some(ref cdn_path) = self.cdn_path {
            self.base_dir.join(cdn_path)
        } else {
            self.base_dir.clone()
        }
    }

    /// Get the config cache directory
    pub fn config_dir(&self) -> PathBuf {
        let base = self.effective_base_dir();
        let path_str = base.to_string_lossy();

        // Check if the path already ends with "config" or contains "configs"
        if path_str.ends_with("/config") || path_str.ends_with("\\config") {
            // Path already has /config suffix, don't add another
            base
        } else if path_str.contains("configs/") || path_str.contains("configs\\") {
            // For paths like "tpr/configs/data", don't add "config"
            base
        } else {
            // For paths like "tpr/wow", add "config"
            base.join("config")
        }
    }

    /// Get the data cache directory
    pub fn data_dir(&self) -> PathBuf {
        self.effective_base_dir().join("data")
    }

    /// Get the patch cache directory
    pub fn patch_dir(&self) -> PathBuf {
        self.effective_base_dir().join("patch")
    }

    /// Construct a config cache path from a hash
    ///
    /// Follows CDN structure: config/{first2}/{next2}/{hash}
    pub fn config_path(&self, hash: &str) -> PathBuf {
        if hash.len() >= 4 {
            self.config_dir()
                .join(&hash[..2])
                .join(&hash[2..4])
                .join(hash)
        } else {
            self.config_dir().join(hash)
        }
    }

    /// Construct a data cache path from a hash
    ///
    /// Follows CDN structure: data/{first2}/{next2}/{hash}
    pub fn data_path(&self, hash: &str) -> PathBuf {
        if hash.len() >= 4 {
            self.data_dir()
                .join(&hash[..2])
                .join(&hash[2..4])
                .join(hash)
        } else {
            self.data_dir().join(hash)
        }
    }

    /// Construct a patch cache path from a hash
    ///
    /// Follows CDN structure: patch/{first2}/{next2}/{hash}
    pub fn patch_path(&self, hash: &str) -> PathBuf {
        if hash.len() >= 4 {
            self.patch_dir()
                .join(&hash[..2])
                .join(&hash[2..4])
                .join(hash)
        } else {
            self.patch_dir().join(hash)
        }
    }

    /// Construct an index cache path from a hash
    ///
    /// Follows CDN structure: data/{first2}/{next2}/{hash}.index
    pub fn index_path(&self, hash: &str) -> PathBuf {
        let mut path = self.data_path(hash);
        path.set_extension("index");
        path
    }

    /// Check if a config exists in cache
    pub async fn has_config(&self, hash: &str) -> bool {
        tokio::fs::metadata(self.config_path(hash)).await.is_ok()
    }

    /// Check if data exists in cache
    pub async fn has_data(&self, hash: &str) -> bool {
        tokio::fs::metadata(self.data_path(hash)).await.is_ok()
    }

    /// Check if a patch exists in cache
    pub async fn has_patch(&self, hash: &str) -> bool {
        tokio::fs::metadata(self.patch_path(hash)).await.is_ok()
    }

    /// Check if an index exists in cache
    pub async fn has_index(&self, hash: &str) -> bool {
        tokio::fs::metadata(self.index_path(hash)).await.is_ok()
    }

    /// Write config data to cache
    pub async fn write_config(&self, hash: &str, data: &[u8]) -> Result<()> {
        let path = self.config_path(hash);

        if let Some(parent) = path.parent() {
            ensure_dir(parent).await?;
        }

        trace!("Writing {} bytes to config cache: {}", data.len(), hash);
        tokio::fs::write(&path, data).await?;

        Ok(())
    }

    /// Write data to cache
    pub async fn write_data(&self, hash: &str, data: &[u8]) -> Result<()> {
        let path = self.data_path(hash);

        if let Some(parent) = path.parent() {
            ensure_dir(parent).await?;
        }

        trace!("Writing {} bytes to data cache: {}", data.len(), hash);
        tokio::fs::write(&path, data).await?;

        Ok(())
    }

    /// Write patch data to cache
    pub async fn write_patch(&self, hash: &str, data: &[u8]) -> Result<()> {
        let path = self.patch_path(hash);

        if let Some(parent) = path.parent() {
            ensure_dir(parent).await?;
        }

        trace!("Writing {} bytes to patch cache: {}", data.len(), hash);
        tokio::fs::write(&path, data).await?;

        Ok(())
    }

    /// Write index to cache
    pub async fn write_index(&self, hash: &str, data: &[u8]) -> Result<()> {
        let path = self.index_path(hash);

        if let Some(parent) = path.parent() {
            ensure_dir(parent).await?;
        }

        trace!("Writing {} bytes to index cache: {}", data.len(), hash);
        tokio::fs::write(&path, data).await?;

        Ok(())
    }

    /// Read config from cache
    pub async fn read_config(&self, hash: &str) -> Result<Vec<u8>> {
        let path = self.config_path(hash);
        trace!("Reading config from cache: {}", hash);
        Ok(tokio::fs::read(&path).await?)
    }

    /// Read data from cache
    pub async fn read_data(&self, hash: &str) -> Result<Vec<u8>> {
        let path = self.data_path(hash);
        trace!("Reading data from cache: {}", hash);
        Ok(tokio::fs::read(&path).await?)
    }

    /// Stream data from cache to writer (memory-efficient)
    pub async fn read_data_to_writer<W>(&self, hash: &str, mut writer: W) -> Result<u64>
    where
        W: tokio::io::AsyncWrite + Unpin,
    {
        let path = self.data_path(hash);
        trace!("Streaming data from cache: {}", hash);

        let mut file = tokio::fs::File::open(&path).await?;
        let bytes_copied = tokio::io::copy(&mut file, &mut writer).await?;

        Ok(bytes_copied)
    }

    /// Read patch from cache
    pub async fn read_patch(&self, hash: &str) -> Result<Vec<u8>> {
        let path = self.patch_path(hash);
        trace!("Reading patch from cache: {}", hash);
        Ok(tokio::fs::read(&path).await?)
    }

    /// Read index from cache
    pub async fn read_index(&self, hash: &str) -> Result<Vec<u8>> {
        let path = self.index_path(hash);
        trace!("Reading index from cache: {}", hash);
        Ok(tokio::fs::read(&path).await?)
    }

    /// Stream index from cache to writer (memory-efficient)
    pub async fn read_index_to_writer<W>(&self, hash: &str, mut writer: W) -> Result<u64>
    where
        W: tokio::io::AsyncWrite + Unpin,
    {
        let path = self.index_path(hash);
        trace!("Streaming index from cache: {}", hash);

        let mut file = tokio::fs::File::open(&path).await?;
        let bytes_copied = tokio::io::copy(&mut file, &mut writer).await?;

        Ok(bytes_copied)
    }

    /// Stream read data from cache
    ///
    /// Returns a file handle for efficient streaming
    pub async fn open_data(&self, hash: &str) -> Result<tokio::fs::File> {
        let path = self.data_path(hash);
        trace!("Opening data for streaming: {}", hash);
        Ok(tokio::fs::File::open(&path).await?)
    }

    /// Get data size without reading it
    pub async fn data_size(&self, hash: &str) -> Result<u64> {
        let path = self.data_path(hash);
        let metadata = tokio::fs::metadata(&path).await?;
        Ok(metadata.len())
    }

    /// Get the base directory of this cache
    pub fn base_dir(&self) -> &PathBuf {
        &self.base_dir
    }

    /// Get the CDN path if set
    pub fn cdn_path(&self) -> Option<&str> {
        self.cdn_path.as_deref()
    }

    /// Write multiple config files in parallel
    pub async fn write_configs_batch(&self, entries: &[(String, Vec<u8>)]) -> Result<()> {
        use futures::future::try_join_all;

        let futures = entries
            .iter()
            .map(|(hash, data)| self.write_config(hash, data));

        try_join_all(futures).await?;
        Ok(())
    }

    /// Write multiple data files in parallel
    pub async fn write_data_batch(&self, entries: &[(String, Vec<u8>)]) -> Result<()> {
        use futures::future::try_join_all;

        let futures = entries
            .iter()
            .map(|(hash, data)| self.write_data(hash, data));

        try_join_all(futures).await?;
        Ok(())
    }

    /// Read multiple config files in parallel
    pub async fn read_configs_batch(&self, hashes: &[String]) -> Vec<Result<Vec<u8>>> {
        use futures::future::join_all;

        let futures = hashes.iter().map(|hash| self.read_config(hash));
        join_all(futures).await
    }

    /// Read multiple data files in parallel
    pub async fn read_data_batch(&self, hashes: &[String]) -> Vec<Result<Vec<u8>>> {
        use futures::future::join_all;

        let futures = hashes.iter().map(|hash| self.read_data(hash));
        join_all(futures).await
    }

    /// Check existence of multiple configs in parallel
    pub async fn has_configs_batch(&self, hashes: &[String]) -> Vec<bool> {
        use futures::future::join_all;

        let futures = hashes.iter().map(|hash| self.has_config(hash));
        join_all(futures).await
    }

    /// Check existence of multiple data files in parallel
    pub async fn has_data_batch(&self, hashes: &[String]) -> Vec<bool> {
        use futures::future::join_all;

        let futures = hashes.iter().map(|hash| self.has_data(hash));
        join_all(futures).await
    }

    /// Get sizes of multiple data files in parallel
    pub async fn data_sizes_batch(&self, hashes: &[String]) -> Vec<Result<u64>> {
        use futures::future::join_all;

        let futures = hashes.iter().map(|hash| self.data_size(hash));
        join_all(futures).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_cdn_cache_paths() {
        let cache = CdnCache::new().await.unwrap();

        let hash = "deadbeef1234567890abcdef12345678";

        let config_path = cache.config_path(hash);
        assert!(config_path.ends_with("config/de/ad/deadbeef1234567890abcdef12345678"));

        let data_path = cache.data_path(hash);
        assert!(data_path.ends_with("data/de/ad/deadbeef1234567890abcdef12345678"));

        let patch_path = cache.patch_path(hash);
        assert!(patch_path.ends_with("patch/de/ad/deadbeef1234567890abcdef12345678"));

        let index_path = cache.index_path(hash);
        assert!(index_path.ends_with("data/de/ad/deadbeef1234567890abcdef12345678.index"));
    }

    #[tokio::test]
    async fn test_cdn_cache_with_cdn_path() {
        let cache = CdnCache::with_cdn_path("tpr/wow").await.unwrap();

        let hash = "deadbeef1234567890abcdef12345678";

        let config_path = cache.config_path(hash);
        assert!(config_path.ends_with("tpr/wow/config/de/ad/deadbeef1234567890abcdef12345678"));

        let data_path = cache.data_path(hash);
        assert!(data_path.ends_with("tpr/wow/data/de/ad/deadbeef1234567890abcdef12345678"));

        let patch_path = cache.patch_path(hash);
        assert!(patch_path.ends_with("tpr/wow/patch/de/ad/deadbeef1234567890abcdef12345678"));
    }

    #[tokio::test]
    async fn test_cdn_product_cache() {
        let cache = CdnCache::for_product("wow").await.unwrap();
        assert!(cache.base_dir().ends_with("cdn/wow"));
    }

    #[tokio::test]
    async fn test_cdn_cache_operations() {
        let cache = CdnCache::for_product("test").await.unwrap();
        let hash = "test5678901234567890abcdef123456";
        let data = b"test data content";

        // Write and read data
        cache.write_data(hash, data).await.unwrap();
        assert!(cache.has_data(hash).await);

        let read_data = cache.read_data(hash).await.unwrap();
        assert_eq!(read_data, data);

        // Test size
        let size = cache.data_size(hash).await.unwrap();
        assert_eq!(size, data.len() as u64);

        // Test config
        let config_data = b"test config data";
        cache.write_config(hash, config_data).await.unwrap();
        assert!(cache.has_config(hash).await);

        let read_config = cache.read_config(hash).await.unwrap();
        assert_eq!(read_config, config_data);

        // Cleanup
        let _ = tokio::fs::remove_file(cache.data_path(hash)).await;
        let _ = tokio::fs::remove_file(cache.config_path(hash)).await;
    }
}