use crate::bytes::{get_u32, get_u64, put_u32, put_u64};
use super::{ObjectHeader, ParseError};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ObjectMap {
pub header: ObjectHeader,
pub flags: u32,
pub snapshot_count: u32,
pub tree_type: u32,
pub snapshot_tree_type: u32,
pub tree_oid: u64,
pub snapshot_tree_oid: u64,
pub most_recent_snapshot: u64,
pub pending_revert_min: u64,
pub pending_revert_max: u64,
}
pub const MAP_VALUE_DELETED: u32 = 0x1;
pub const MAP_CRYPTO_CHANGING: u32 = 0x2 | 0x4 | 0x8;
pub const CRYPTO_GENERATION: u32 = 0x10;
impl ObjectMap {
pub const SIZE: usize = 88;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("object map", Self::SIZE, buf)?;
Ok(Self {
header: ObjectHeader::read_from(buf)?,
flags: get_u32(buf, 32),
snapshot_count: get_u32(buf, 36),
tree_type: get_u32(buf, 40),
snapshot_tree_type: get_u32(buf, 44),
tree_oid: get_u64(buf, 48),
snapshot_tree_oid: get_u64(buf, 56),
most_recent_snapshot: get_u64(buf, 64),
pending_revert_min: get_u64(buf, 72),
pending_revert_max: get_u64(buf, 80),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"an object map needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
self.header.write_to(buf);
put_u32(buf, 32, self.flags);
put_u32(buf, 36, self.snapshot_count);
put_u32(buf, 40, self.tree_type);
put_u32(buf, 44, self.snapshot_tree_type);
put_u64(buf, 48, self.tree_oid);
put_u64(buf, 56, self.snapshot_tree_oid);
put_u64(buf, 64, self.most_recent_snapshot);
put_u64(buf, 72, self.pending_revert_min);
put_u64(buf, 80, self.pending_revert_max);
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct MapKey {
pub oid: u64,
pub xid: u64,
}
impl MapKey {
pub const SIZE: usize = 16;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("object map key", Self::SIZE, buf)?;
Ok(Self {
oid: get_u64(buf, 0),
xid: get_u64(buf, 8),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(buf.len() >= Self::SIZE, "an object map key needs 16 bytes");
put_u64(buf, 0, self.oid);
put_u64(buf, 8, self.xid);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct MapValue {
pub flags: u32,
pub size: u32,
pub address: u64,
}
impl MapValue {
pub const SIZE: usize = 16;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("object map value", Self::SIZE, buf)?;
Ok(Self {
flags: get_u32(buf, 0),
size: get_u32(buf, 4),
address: get_u64(buf, 8),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"an object map value needs 16 bytes"
);
put_u32(buf, 0, self.flags);
put_u32(buf, 4, self.size);
put_u64(buf, 8, self.address);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_map_header_round_trips() {
let map = ObjectMap {
header: ObjectHeader {
checksum: 1,
oid: 300,
xid: 4,
type_word: 0x4000_000B,
subtype: 0,
},
flags: 1,
snapshot_count: 2,
tree_type: 0x4000_0002,
snapshot_tree_type: 0x4000_0002,
tree_oid: 301,
snapshot_tree_oid: 302,
most_recent_snapshot: 40,
pending_revert_min: 0,
pending_revert_max: 0,
};
let mut buf = [0u8; ObjectMap::SIZE];
map.write_to(&mut buf);
assert_eq!(ObjectMap::read_from(&buf), Ok(map));
assert_eq!(get_u64(&buf, 48), 301, "the tree at 48");
}
#[test]
fn a_record_round_trips_and_keys_sort_by_identifier_then_transaction() {
let key = MapKey { oid: 1026, xid: 7 };
let value = MapValue {
flags: 0,
size: 4096,
address: 900,
};
let (mut k, mut v) = ([0u8; 16], [0u8; 16]);
key.write_to(&mut k);
value.write_to(&mut v);
assert_eq!(MapKey::read_from(&k), Ok(key));
assert_eq!(MapValue::read_from(&v), Ok(value));
assert!(MapKey { oid: 1, xid: 9 } < MapKey { oid: 2, xid: 1 });
assert!(MapKey { oid: 2, xid: 1 } < MapKey { oid: 2, xid: 3 });
}
}