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
//! In‑memory object store – the simplest [`ObjectStore`] implementation.
//!
//! [`MemoryStore`] implements [`ObjectStore`] using a `HashMap`. It is
//! useful for testing, prototyping, and as a reference for building
//! real backends.
//!
//! # Design
//!
//! The store is **content‑addressable**: objects are stored by their
//! [`Hash`] and retrieved by the same hash. The hash is never verified
//! against the content – that responsibility lies with the caller.
//!
//! # Performance
//!
//! - **put**: O(1) average – inserts into the `HashMap`.
//! - **get**: O(1) average – looks up and clones the value.
//! - **delete**: O(1) average – removes from the `HashMap`.
//! - **exists**: O(1) average – checks for key presence.
//!
//! The `Clone` in `get` means you receive an independent copy of the data.
//! This is safe but can be expensive for large objects. A production
//! backend might return a reference or use a copy‑on‑write strategy.
//!
//! # Memory usage
//!
//! The store holds a copy of every object. There is no garbage collection
//! or deduplication beyond what the caller does (e.g., storing the same
//! data twice under different hashes will consume twice the memory).
//!
//! # Idempotency
//!
//! Storing the same `(hash, data)` pair multiple times is safe and does
//! not fail. It simply overwrites the previous entry. This matches the
//! behaviour required by the trait contract.
//!
//! # Examples
//!
//! ```rust
//! use libvctrl_core::store::MemoryStore;
//! use libvctrl_handler::{Hash, ObjectStore, HASH_LENGTH};
//!
//! let mut store = MemoryStore::new();
//! let hash = Hash::from_bytes(&[0u8; HASH_LENGTH]).unwrap();
//!
//! // Store data
//! store.put(&hash, b"hello").unwrap();
//! assert!(store.exists(&hash).unwrap());
//!
//! // Retrieve data
//! let data = store.get(&hash).unwrap();
//! assert_eq!(data, b"hello");
//!
//! // Delete data
//! store.delete(&hash).unwrap();
//! assert!(!store.exists(&hash).unwrap());
//! ```
use ;
use HashMap;
/// An in‑memory object store backed by a [`HashMap`].
///
/// # Characteristics
/// - **Fast**: all operations are O(1) average.
/// - **Not thread‑safe**: wrap in `Arc<Mutex<…>>` for shared access.
/// - **Not persistent**: data is lost when the store is dropped.
///
/// # Examples
/// ```
/// use libvctrl_core::store::MemoryStore;
/// use libvctrl_handler::{Hash, ObjectStore, HASH_LENGTH};
///
/// let mut store = MemoryStore::new();
/// let hash = Hash::from_bytes(&[0u8; HASH_LENGTH]).unwrap();
/// store.put(&hash, b"hello").unwrap();
/// assert!(store.exists(&hash).unwrap());
/// assert_eq!(store.get(&hash).unwrap(), b"hello");
/// ```