use crate::error::RowProblem;
use crate::record::Descriptor;
use crate::row_address::RowAddress;
use crate::schema::{Column, DataType};
const LONG_MARKER: u8 = 0x80;
const WIDE_MARKER: u8 = 0xff;
const LINKED: u8 = 0x80;
const COMPRESSED: [u8; 2] = [0x40, 0xc0];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Form {
Fixed(usize),
Number,
Bytes {
linked: bool,
compressed: bool,
counts_prefix: bool,
},
Unknown,
}
impl Form {
pub(crate) fn of(column: &Column) -> Result<Self, RowProblem> {
Ok(match column.data_type() {
DataType::SmallInt | DataType::UnsignedSmallInt => Self::Fixed(2),
DataType::Integer | DataType::UnsignedInt | DataType::Float | DataType::Date => {
Self::Fixed(4)
}
DataType::BigInt
| DataType::UnsignedBigInt
| DataType::Double
| DataType::Time
| DataType::Timestamp => Self::Fixed(8),
DataType::TinyInt if column.width != 1 => {
return Err(RowProblem::Width {
column_id: column.id,
width: column.width,
});
}
DataType::TinyInt | DataType::Bit => Self::Fixed(1),
DataType::Numeric | DataType::Decimal => Self::Number,
DataType::Char | DataType::Binary => Self::Bytes {
linked: false,
compressed: false,
counts_prefix: true,
},
DataType::VarChar
| DataType::LongVarChar
| DataType::LongBinary
| DataType::VarBinary => Self::Bytes {
linked: true,
compressed: true,
counts_prefix: true,
},
DataType::LongVarBit => Self::Bytes {
linked: true,
compressed: false,
counts_prefix: false,
},
DataType::Unknown(_) => Self::Unknown,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum StoredField<'record> {
Bytes(&'record [u8]),
Number { control: u8, digits: &'record [u8] },
Long(Descriptor<'record>),
}
pub(crate) fn take<'record>(
rest: &mut &'record [u8],
form: Form,
column: &Column,
) -> Result<StoredField<'record>, RowProblem> {
match form {
Form::Fixed(size) => bytes(rest, size).map(StoredField::Bytes),
Form::Number => {
let [count, control] = array(rest)?;
let digits = bytes(rest, count.into())?;
Ok(StoredField::Number { control, digits })
}
Form::Bytes {
linked,
compressed,
counts_prefix,
} => {
let [marker] = array(rest)?;
if marker == WIDE_MARKER {
let length = u16::from_be_bytes(array(rest)?);
return bytes(rest, length.into()).map(StoredField::Bytes);
}
if marker < LONG_MARKER {
return bytes(rest, marker.into()).map(StoredField::Bytes);
}
let unknown = RowProblem::Descriptor {
column_id: column.id,
};
let prefix = bytes(rest, (marker & !LONG_MARKER).into())?;
let [tag] = array(rest)?;
let compressed = compressed && COMPRESSED.contains(&tag);
if tag != 0 && !compressed && !(linked && tag == LINKED) {
return Err(unknown);
}
let stored_length = u32::from_le_bytes(array(rest)?);
let page = u32::from_le_bytes(array(rest)?);
let _: [u8; 2] = array(rest)?;
let slot = u16::from_le_bytes(array(rest)?);
let decompressed_length = if compressed {
Some(u32::from_le_bytes(array(rest)?))
} else {
None
};
let length = decompressed_length.unwrap_or(stored_length);
let shorter = (length as usize) < prefix.len();
let empty = compressed && (length == 0 || stored_length == 0);
if empty || (counts_prefix && shorter) {
return Err(unknown);
}
Ok(StoredField::Long(Descriptor {
prefix,
stored_length,
first: RowAddress::new(page, slot),
decompressed_length,
}))
}
Form::Unknown => Err(RowProblem::UnsupportedDomain {
column_id: column.id,
domain: column.domain,
}),
}
}
fn bytes<'record>(rest: &mut &'record [u8], size: usize) -> Result<&'record [u8], RowProblem> {
let (taken, left) = rest.split_at_checked(size).ok_or(RowProblem::Truncated)?;
*rest = left;
Ok(taken)
}
fn array<const N: usize>(rest: &mut &[u8]) -> Result<[u8; N], RowProblem> {
let (taken, left) = rest.split_first_chunk().ok_or(RowProblem::Truncated)?;
*rest = left;
Ok(*taken)
}