mediagit-storage 0.2.0

Unified async storage backend trait and implementations
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
// MediaGit - Git for Media Files
// Copyright (C) 2025 MediaGit Contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

//! In-memory mock storage backend for testing
//!
//! Provides a thread-safe, in-memory implementation of [`StorageBackend`](crate::StorageBackend)
//! using `Arc<RwLock<HashMap>>` for concurrent access.
//!
//! # Examples
//!
//! ```rust,no_run
//! use mediagit_storage::{StorageBackend, mock::MockBackend};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let storage = MockBackend::new();
//!
//!     // Store data
//!     storage.put("test.bin", b"hello world").await?;
//!
//!     // Retrieve data
//!     let data = storage.get("test.bin").await?;
//!     assert_eq!(data, b"hello world");
//!
//!     // Check existence
//!     assert!(storage.exists("test.bin").await?);
//!
//!     // List objects
//!     let objects = storage.list_objects("test").await?;
//!     assert_eq!(objects.len(), 1);
//!
//!     // Delete object
//!     storage.delete("test.bin").await?;
//!     assert!(!storage.exists("test.bin").await?);
//!
//!     Ok(())
//! }
//! ```

use crate::StorageBackend;
use async_trait::async_trait;
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use tokio::sync::RwLock;

/// In-memory mock storage backend for testing
///
/// Thread-safe implementation suitable for unit and integration tests.
/// Uses `Arc<RwLock<HashMap>>` to allow concurrent read/write operations.
///
/// # Thread Safety
///
/// This implementation is `Send + Sync` and can be safely shared across threads
/// and async tasks.
#[derive(Clone)]
pub struct MockBackend {
    store: Arc<RwLock<HashMap<String, Vec<u8>>>>,
}

impl MockBackend {
    /// Create a new empty mock storage backend
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use mediagit_storage::mock::MockBackend;
    ///
    /// let storage = MockBackend::new();
    /// ```
    pub fn new() -> Self {
        MockBackend {
            store: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create a mock storage backend with initial data
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use mediagit_storage::mock::MockBackend;
    ///
    /// let mut initial_data = std::collections::HashMap::new();
    /// initial_data.insert("key1".to_string(), vec![1, 2, 3]);
    /// initial_data.insert("key2".to_string(), vec![4, 5, 6]);
    ///
    /// let storage = MockBackend::with_data(initial_data);
    /// ```
    pub fn with_data(initial_data: HashMap<String, Vec<u8>>) -> Self {
        MockBackend {
            store: Arc::new(RwLock::new(initial_data)),
        }
    }

    /// Get the current number of objects stored
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use mediagit_storage::mock::MockBackend;
    /// # #[tokio::main]
    /// # async fn main() {
    /// let storage = MockBackend::new();
    /// assert_eq!(storage.len().await, 0);
    /// # }
    /// ```
    pub async fn len(&self) -> usize {
        self.store.read().await.len()
    }

    /// Check if the storage is empty
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use mediagit_storage::mock::MockBackend;
    /// # #[tokio::main]
    /// # async fn main() {
    /// let storage = MockBackend::new();
    /// assert!(storage.is_empty().await);
    /// # }
    /// ```
    pub async fn is_empty(&self) -> bool {
        self.store.read().await.is_empty()
    }

    /// Clear all stored objects
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use mediagit_storage::mock::MockBackend;
    /// # #[tokio::main]
    /// # async fn main() {
    /// let storage = MockBackend::new();
    /// storage.clear().await;
    /// assert!(storage.is_empty().await);
    /// # }
    /// ```
    pub async fn clear(&self) {
        self.store.write().await.clear();
    }

    /// Get a copy of all stored keys
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use mediagit_storage::mock::MockBackend;
    /// # use mediagit_storage::StorageBackend;
    /// # #[tokio::main]
    /// # async fn main() -> anyhow::Result<()> {
    /// let storage = MockBackend::new();
    /// storage.put("a", b"data").await?;
    /// storage.put("b", b"data").await?;
    ///
    /// let keys = storage.keys().await;
    /// assert_eq!(keys.len(), 2);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn keys(&self) -> Vec<String> {
        self.store.read().await.keys().cloned().collect()
    }
}

impl Default for MockBackend {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Debug for MockBackend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MockBackend").finish()
    }
}

#[async_trait]
impl StorageBackend for MockBackend {
    async fn get(&self, key: &str) -> anyhow::Result<Vec<u8>> {
        if key.is_empty() {
            return Err(anyhow::anyhow!("key cannot be empty"));
        }

        let store = self.store.read().await;
        store
            .get(key)
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("object not found: {}", key))
    }

    async fn put(&self, key: &str, data: &[u8]) -> anyhow::Result<()> {
        if key.is_empty() {
            return Err(anyhow::anyhow!("key cannot be empty"));
        }

        let mut store = self.store.write().await;
        store.insert(key.to_string(), data.to_vec());
        Ok(())
    }

    async fn exists(&self, key: &str) -> anyhow::Result<bool> {
        if key.is_empty() {
            return Err(anyhow::anyhow!("key cannot be empty"));
        }

        let store = self.store.read().await;
        Ok(store.contains_key(key))
    }

    async fn delete(&self, key: &str) -> anyhow::Result<()> {
        if key.is_empty() {
            return Err(anyhow::anyhow!("key cannot be empty"));
        }

        let mut store = self.store.write().await;
        store.remove(key);
        Ok(())
    }

    async fn list_objects(&self, prefix: &str) -> anyhow::Result<Vec<String>> {
        let store = self.store.read().await;
        let mut results: Vec<String> = store
            .keys()
            .filter(|k| k.starts_with(prefix))
            .cloned()
            .collect();
        results.sort();
        Ok(results)
    }
}

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

    #[tokio::test]
    async fn test_new() {
        let backend = MockBackend::new();
        assert!(backend.is_empty().await);
        assert_eq!(backend.len().await, 0);
    }

    #[tokio::test]
    async fn test_put_and_get() {
        let backend = MockBackend::new();
        let data = b"test data";

        backend.put("key1", data).await.unwrap();
        assert_eq!(backend.len().await, 1);

        let retrieved = backend.get("key1").await.unwrap();
        assert_eq!(retrieved, data);
    }

    #[tokio::test]
    async fn test_get_nonexistent() {
        let backend = MockBackend::new();
        let result = backend.get("nonexistent").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_empty_key_operations() {
        let backend = MockBackend::new();

        assert!(backend.put("", b"data").await.is_err());
        assert!(backend.get("").await.is_err());
        assert!(backend.exists("").await.is_err());
        assert!(backend.delete("").await.is_err());
    }

    #[tokio::test]
    async fn test_exists() {
        let backend = MockBackend::new();

        backend.put("key1", b"data").await.unwrap();
        assert!(backend.exists("key1").await.unwrap());
        assert!(!backend.exists("key2").await.unwrap());
    }

    #[tokio::test]
    async fn test_delete() {
        let backend = MockBackend::new();

        backend.put("key1", b"data").await.unwrap();
        assert!(backend.exists("key1").await.unwrap());

        backend.delete("key1").await.unwrap();
        assert!(!backend.exists("key1").await.unwrap());
        assert_eq!(backend.len().await, 0);
    }

    #[tokio::test]
    async fn test_delete_nonexistent() {
        let backend = MockBackend::new();
        // Should not error, just do nothing
        backend.delete("nonexistent").await.unwrap();
    }

    #[tokio::test]
    async fn test_list_objects() {
        let backend = MockBackend::new();

        backend.put("images/photo1.jpg", b"data1").await.unwrap();
        backend.put("images/photo2.jpg", b"data2").await.unwrap();
        backend.put("videos/video1.mp4", b"data3").await.unwrap();
        backend.put("audio/song1.mp3", b"data4").await.unwrap();

        let images = backend.list_objects("images/").await.unwrap();
        assert_eq!(images.len(), 2);
        assert!(images.contains(&"images/photo1.jpg".to_string()));
        assert!(images.contains(&"images/photo2.jpg".to_string()));

        let videos = backend.list_objects("videos/").await.unwrap();
        assert_eq!(videos.len(), 1);

        let empty = backend.list_objects("nonexistent/").await.unwrap();
        assert_eq!(empty.len(), 0);
    }

    #[tokio::test]
    async fn test_list_objects_sorting() {
        let backend = MockBackend::new();

        backend.put("z_file", b"data").await.unwrap();
        backend.put("a_file", b"data").await.unwrap();
        backend.put("m_file", b"data").await.unwrap();

        let objects = backend.list_objects("").await.unwrap();
        assert_eq!(objects, vec!["a_file", "m_file", "z_file"]);
    }

    #[tokio::test]
    async fn test_overwrite() {
        let backend = MockBackend::new();

        backend.put("key1", b"old data").await.unwrap();
        assert_eq!(backend.get("key1").await.unwrap(), b"old data");

        backend.put("key1", b"new data").await.unwrap();
        assert_eq!(backend.get("key1").await.unwrap(), b"new data");
        assert_eq!(backend.len().await, 1);
    }

    #[tokio::test]
    async fn test_with_data() {
        let mut initial = HashMap::new();
        initial.insert("key1".to_string(), vec![1, 2, 3]);
        initial.insert("key2".to_string(), vec![4, 5, 6]);

        let backend = MockBackend::with_data(initial);
        assert_eq!(backend.len().await, 2);
        assert_eq!(backend.get("key1").await.unwrap(), vec![1, 2, 3]);
        assert_eq!(backend.get("key2").await.unwrap(), vec![4, 5, 6]);
    }

    #[tokio::test]
    async fn test_clear() {
        let backend = MockBackend::new();

        backend.put("key1", b"data").await.unwrap();
        backend.put("key2", b"data").await.unwrap();
        assert_eq!(backend.len().await, 2);

        backend.clear().await;
        assert!(backend.is_empty().await);
        assert_eq!(backend.len().await, 0);
    }

    #[tokio::test]
    async fn test_concurrent_access() {
        let backend = MockBackend::new();

        // Simulate concurrent writes
        let backend1 = backend.clone();
        let handle1 = tokio::spawn(async move {
            for i in 0..10 {
                let key = format!("task1_key{}", i);
                backend1.put(&key, b"data").await.unwrap();
            }
        });

        let backend2 = backend.clone();
        let handle2 = tokio::spawn(async move {
            for i in 0..10 {
                let key = format!("task2_key{}", i);
                backend2.put(&key, b"data").await.unwrap();
            }
        });

        handle1.await.unwrap();
        handle2.await.unwrap();

        assert_eq!(backend.len().await, 20);
    }

    #[tokio::test]
    async fn test_clone_shares_state() {
        let backend1 = MockBackend::new();
        backend1.put("key1", b"data").await.unwrap();

        let backend2 = backend1.clone();
        assert_eq!(backend2.len().await, 1);
        assert_eq!(backend2.get("key1").await.unwrap(), b"data");

        backend2.put("key2", b"data").await.unwrap();
        assert_eq!(backend1.len().await, 2);
    }

    #[tokio::test]
    async fn test_keys() {
        let backend = MockBackend::new();

        backend.put("apple", b"data").await.unwrap();
        backend.put("banana", b"data").await.unwrap();
        backend.put("cherry", b"data").await.unwrap();

        let keys = backend.keys().await;
        assert_eq!(keys.len(), 3);
        assert!(keys.contains(&"apple".to_string()));
        assert!(keys.contains(&"banana".to_string()));
        assert!(keys.contains(&"cherry".to_string()));
    }

    #[tokio::test]
    async fn test_debug_impl() {
        let backend = MockBackend::new();
        let debug_str = format!("{:?}", backend);
        assert!(debug_str.contains("MockBackend"));
    }
}