ferrite_cache/
manager.rs

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
use std::{path::PathBuf, sync::Arc, thread};

use crate::{
    types::{CacheConfig, CacheHandle, CacheRequest, CacheState, ImageData},
    CacheError,
    CacheResult,
    ImageLoadError,
};
use image::GenericImageView;
use tokio::{
    runtime::Runtime,
    sync::{mpsc, oneshot, RwLock},
};
use tracing::{debug, info, warn};
pub struct CacheManager {
    config:         CacheConfig,
    state:          Arc<RwLock<CacheState>>,
    runtime_handle: Arc<Runtime>,
    _shutdown_tx:   oneshot::Sender<()>,
}

impl CacheManager {
    pub fn new(config: CacheConfig) -> CacheHandle {
        let (request_tx, mut request_rx) = mpsc::unbounded_channel();
        let (shutdown_tx, shutdown_rx) = oneshot::channel();

        let state = Arc::new(RwLock::new(CacheState::new()));

        thread::spawn(move || {
            let runtime = Arc::new(
                tokio::runtime::Builder::new_multi_thread()
                    .worker_threads(config.thread_count)
                    .enable_all()
                    .build()
                    .expect("Failed to create Tokio runtime"),
            );

            let manager = Arc::new(Self {
                config,
                state: state.clone(),
                runtime_handle: runtime.clone(),
                _shutdown_tx: shutdown_tx,
            });

            runtime.block_on(async {
                let shutdown_future = shutdown_rx;
                tokio::pin!(shutdown_future);

                loop {
                    tokio::select! {
                        _ = &mut shutdown_future => {
                            debug!("Received shutdown signal");
                            break;
                        }
                        Some(request) = request_rx.recv() => {
                            let manager = manager.clone();
                            match request {
                                CacheRequest::GetImage { path, response_tx } => {
                                    runtime.spawn(async move {
                                        let result = manager.get_image_internal(path).await;
                                        let _ = response_tx.send(result);
                                    });
                                }
                                CacheRequest::CacheImage { path, response_tx } => {
                                    runtime.spawn(async move {
                                        let result = manager.load_and_cache(path).await;
                                        let _ = response_tx.send(result);
                                    });
                                }
                            }
                        }
                        else => break,
                    }
                }
                debug!("Cache manager event loop terminated");
            });
        });

        CacheHandle::new(request_tx)
    }

    // Internal method to handle image retrieval
    async fn get_image_internal(
        &self,
        path: PathBuf,
    ) -> CacheResult<Arc<ImageData>> {
        let start_time = std::time::Instant::now();
        debug!(path = ?path, "Image requested from cache");

        if let Some(image) = self.lookup_image(&path).await {
            let duration = start_time.elapsed();
            debug!(path = ?path, duration = ?duration, "Cache hit");
            return Ok(image);
        }

        debug!(path = ?path, "Cache miss, loading from disk");
        let image = self.load_and_cache(path.clone()).await?;
        let duration = start_time.elapsed();
        debug!(path = ?path, duration = ?duration, "Total cache miss time");
        Ok(image)
    }

    pub async fn cache_image(
        &self,
        path: PathBuf,
    ) -> CacheResult<Arc<ImageData>> {
        let file_size = tokio::fs::metadata(&path)
            .await
            .map_err(|e| CacheError::ImageLoad {
                path:   path.clone(),
                source: ImageLoadError::Io(e),
            })?
            .len();

        debug!(
            path = ?path,
            size = file_size,
            "Loading image from filesystem"
        );

        // Read the file contents using tokio's async file IO
        let image_data = tokio::fs::read(&path).await.map_err(|e| {
            CacheError::ImageLoad {
                path:   path.clone(),
                source: ImageLoadError::Io(e),
            }
        })?;

        // Get the image dimensions by decoding the header only
        let dimensions =
            image::io::Reader::new(std::io::Cursor::new(&image_data))
                .with_guessed_format()
                .map_err(|e| CacheError::ImageLoad {
                    path:   path.clone(),
                    source: ImageLoadError::Format(e.to_string()),
                })?
                .into_dimensions()
                .map_err(|e| CacheError::ImageLoad {
                    path:   path.clone(),
                    source: ImageLoadError::Format(e.to_string()),
                })?;

        let image_data = ImageData::new(image_data, dimensions);

        // Update cache state
        let mut state = self.state.write().await;

        // Check if we need to evict images to make space
        if state.entries.len() >= self.config.max_image_count {
            // Get the least recently used image path
            if let Some(oldest_path) = state.lru_list.first().cloned() {
                info!(
                    path = ?oldest_path,
                    "Evicting least recently used image"
                );
                state.entries.remove(&oldest_path);
                state.lru_list.remove(0);
            }
        }

        // Update LRU list - remove if exists and add to end
        if let Some(pos) = state.lru_list.iter().position(|p| p == &path) {
            state.lru_list.remove(pos);
        }
        state.lru_list.push(path.clone());

        // Store the image data
        let image_data = Arc::new(image_data);
        state
            .entries
            .insert(path.clone(), (*image_data).clone());

        debug!(
            path = ?path,
            cache_size = state.entries.len(),
            "Image cached successfully"
        );

        Ok(image_data)
    }

    pub fn runtime(&self) -> Arc<Runtime> {
        self.runtime_handle.clone()
    }

    pub async fn get_image(
        &self,
        path: PathBuf,
    ) -> CacheResult<Arc<ImageData>> {
        let start_time = std::time::Instant::now();
        debug!(path = ?path, "Image requested from cache");

        if let Some(image) = self.lookup_image(&path).await {
            let duration = start_time.elapsed();
            debug!(path = ?path, duration = ?duration, "Cache hit");
            return Ok(image);
        }

        debug!(path = ?path, "Cache miss, loading from disk");
        let image = self.load_and_cache(path.clone()).await?;
        let duration = start_time.elapsed();
        debug!(path = ?path, duration = ?duration, "Total cache miss time");
        Ok(image)
    }

    async fn lookup_image(&self, path: &PathBuf) -> Option<Arc<ImageData>> {
        let mut state = self.state.write().await;

        if let Some(image) = state.entries.get(path) {
            debug!(path = ?path, "Found image in cache");
            let mut image_data = image.clone();
            image_data.touch();
            let copy_start = std::time::Instant::now();
            let _copied_data = image_data.simulate_copy();
            let copy_duration = copy_start.elapsed();
            debug!(path = ?path, duration = ?copy_duration, "Data copy completed");
            state
                .entries
                .insert(path.clone(), image_data.clone());
            self.update_lru(path, &mut state).await;
            return Some(Arc::new(image_data));
        }
        debug!(path = ?path, "Image not found in cache");
        None
    }

    async fn load_and_cache(
        &self,
        path: PathBuf,
    ) -> CacheResult<Arc<ImageData>> {
        let load_start = std::time::Instant::now();
        let path_clone = path.clone();

        let file_size = path_clone
            .metadata()
            .map(|m| m.len())
            .unwrap_or(0);
        debug!(path = ?path, size = file_size, "Loading image from filesystem");

        let rn = self.runtime();
        let image_data = rn.spawn(async move {
            tokio::fs::read(&path_clone).await
        }).await.map_err(|e| {
            warn!(path = ?path, error = ?e, "Failed to spawn image loading task");
            CacheError::ImageLoad {
                path: path.clone(),
                source: ImageLoadError::Io(std::io::Error::new(std::io::ErrorKind::Other, e)),
            }
        })?.map_err(|e| {
            warn!(path = ?path, error = ?e, "Failed to read image file");
            CacheError::ImageLoad {
                path: path.clone(),
                source: ImageLoadError::Io(e),
            }
        })?;

        let load_duration = load_start.elapsed();
        debug!(path = ?path, duration = ?load_duration, "File load completed");

        let decode_start = std::time::Instant::now();
        let image = image::load_from_memory(&image_data).map_err(|e| {
            warn!(path = ?path, error = ?e, "Failed to parse image data");
            CacheError::ImageLoad {
                path:   path.clone(),
                source: ImageLoadError::Format(e.to_string()),
            }
        })?;

        let decode_duration = decode_start.elapsed();
        let dimensions = image.dimensions();
        let memory_size = image.as_bytes().len();

        debug!(
            path = ?path,
            duration = ?decode_duration,
            width = dimensions.0,
            height = dimensions.1,
            memory = memory_size,
            "Image decoded"
        );

        let image_data = ImageData::new(image_data, dimensions);
        let mut state = self.state.write().await;

        if state.entries.len() >= self.config.max_image_count {
            if let Some(oldest) = state.lru_list.first().cloned() {
                info!(path = ?oldest, "Evicting least recently used image");
                state.entries.remove(&oldest);
                state.lru_list.remove(0);
            }
        }

        debug!(
            path = ?path,
            cache_size = state.entries.len(),
            "Adding image to cache"
        );

        state
            .entries
            .insert(path.clone(), image_data.clone());
        state.lru_list.push(path);

        Ok(Arc::new(image_data))
    }

    pub async fn get_total_memory_usage(&self) -> CacheResult<usize> {
        let state = self.state.read().await;
        Ok(state
            .entries
            .values()
            .map(|img| img.data().len())
            .sum())
    }

    async fn update_lru(&self, path: &PathBuf, state: &mut CacheState) {
        if let Some(pos) = state.lru_list.iter().position(|p| p == path) {
            state.lru_list.remove(pos);
        }
        state.lru_list.push(path.clone());
        debug!(
            path = ?path,
            list_size = state.lru_list.len(),
            "Updated LRU list"
        );
    }
}