-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.rs
More file actions
157 lines (118 loc) · 3.86 KB
/
index.rs
File metadata and controls
157 lines (118 loc) · 3.86 KB
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
use bytes::{BufMut, BytesMut};
use uuid::Uuid;
use crate::error::RisklessError;
#[derive(Debug, Clone)]
pub struct Index {
pub object_key: Uuid,
pub offset: u64,
pub size: u32,
}
impl Index {
pub fn new(object_key: uuid::Uuid, offset: u64, size: u32) -> Self {
Self {
object_key,
offset,
size,
}
}
#[inline]
pub const fn packed_size() -> usize {
16 + std::mem::size_of::<u64>() + std::mem::size_of::<u32>()
}
pub fn write(self, buf: &mut BytesMut) {
buf.put_slice(self.object_key.as_bytes());
buf.put_u64(self.offset);
buf.put_u32(self.size);
}
}
impl TryFrom<&[u8]> for Index {
type Error = RisklessError;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
if value.len() < Self::packed_size() {
return Err(RisklessError::Unknown); // TODO make an error for this.;
}
tracing::info!("{:#?}", &value[0..16]);
let object_key = Uuid::from_slice(&value[0..16])?;
tracing::info!("{:#?}", &value[17..24]);
let offset = u64::from_be_bytes(value[16..24].try_into()?);
let size = u32::from_be_bytes(value[24..28].try_into()?);
Ok(Self {
object_key,
offset,
size,
})
}
}
impl From<Index> for BytesMut {
fn from(val: Index) -> Self {
let mut bytes = BytesMut::with_capacity(Index::packed_size());
val.write(&mut bytes);
bytes
}
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::BytesMut;
use uuid::Uuid;
#[test]
fn test_index_new() {
let uuid = Uuid::new_v4();
let offset = 12345;
let size = 67890;
let index = Index::new(uuid, offset, size);
assert_eq!(index.object_key, uuid);
assert_eq!(index.offset, offset);
assert_eq!(index.size, size);
}
#[test]
fn test_index_write() {
let uuid = Uuid::new_v4();
let offset = 12345;
let size = 67890;
let index = Index::new(uuid, offset, size);
let mut buf = BytesMut::new();
index.write(&mut buf);
// Check the written bytes
assert_eq!(buf.len(), 16 + 8 + 4); // UUID + u64 + u32
assert_eq!(&buf[0..16], uuid.as_bytes());
assert_eq!(u64::from_be_bytes(buf[16..24].try_into().unwrap()), offset);
assert_eq!(u32::from_be_bytes(buf[24..28].try_into().unwrap()), size);
}
#[test]
fn test_try_from_slice_success() {
let uuid: Uuid = Uuid::new_v4();
let offset: u64 = 12345;
let size: u32 = 67890;
let mut bytes = Vec::new();
bytes.extend_from_slice(uuid.as_bytes());
bytes.extend_from_slice(&offset.to_be_bytes());
bytes.extend_from_slice(&size.to_be_bytes());
tracing::info!("{:#?}", bytes);
let index = Index::try_from(bytes.as_slice()).unwrap();
assert_eq!(index.object_key, uuid);
assert_eq!(index.offset, offset);
assert_eq!(index.size, size);
}
#[test]
fn test_try_from_slice_too_short() {
let bytes = [0u8; 15]; // Less than needed (16 + 8 + 4 = 28 bytes)
let result = Index::try_from(bytes.as_slice());
tracing::info!("{:#?}", result);
assert!(result.is_err());
}
#[test]
fn test_round_trip() {
let uuid: Uuid = Uuid::new_v4();
let offset: u64 = 12345;
let size: u32 = 67890;
let original = Index::new(uuid, offset, size);
let original_clone = original.clone();
let mut buf = BytesMut::new();
original_clone.write(&mut buf);
let reconstructed = Index::try_from(buf.as_ref()).unwrap();
assert_eq!(reconstructed.object_key, original.object_key);
assert_eq!(reconstructed.offset, original.offset);
assert_eq!(reconstructed.size, original.size);
}
}