use std::cmp::Ordering;
use super::decode::{self, fixed, int_array_element_width};
use super::tag::*;
use super::{DEPTH_LIMIT, Error, read_int, read_uint, read_width, split_clen, utf8};
use crate::val::Value;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
None,
Null,
Bool,
Int,
Float,
Decimal,
BigDecimal,
I8,
U8,
I16,
U16,
I32,
U32,
U64,
F16,
F32,
String,
Duration,
Datetime,
Date,
Time,
Timestamp,
Interval,
Uuid,
Bytes,
Table,
RecordId,
File,
Regex,
Range,
Geometry,
Array,
Set,
Object,
Unknown,
}
impl Kind {
#[inline(always)]
pub const fn of(tag: u8) -> Self {
match tag {
NONE => Self::None,
NULL => Self::Null,
FALSE | TRUE => Self::Bool,
INT | 0x80..=0xFE => Self::Int,
FLOAT => Self::Float,
DECIMAL => Self::Decimal,
BIGDECIMAL => Self::BigDecimal,
I8 => Self::I8,
U8 => Self::U8,
I16 => Self::I16,
U16 => Self::U16,
I32 => Self::I32,
U32 => Self::U32,
U64 => Self::U64,
F16 => Self::F16,
F32 => Self::F32,
STRING => Self::String,
DURATION => Self::Duration,
DATETIME => Self::Datetime,
DATE => Self::Date,
TIME => Self::Time,
TIMESTAMP => Self::Timestamp,
INTERVAL => Self::Interval,
UUID => Self::Uuid,
BYTES => Self::Bytes,
TABLE => Self::Table,
RECORD_ID => Self::RecordId,
FILE => Self::File,
REGEX => Self::Regex,
RANGE => Self::Range,
GEOMETRY => Self::Geometry,
F64_ARRAY | F32_ARRAY | I8_ARRAY..=I64_ARRAY | 0x14..=0x16 => Self::Array,
0x17..=0x19 => Self::Set,
0x1A..=0x1C => Self::Object,
_ => Self::Unknown,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct ValueRef<'a> {
tag: u8,
payload: &'a [u8],
depth: usize,
}
impl PartialEq for ValueRef<'_> {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.tag == other.tag && self.payload == other.payload
}
}
impl Eq for ValueRef<'_> {}
impl<'a> ValueRef<'a> {
#[inline(always)]
pub fn new(bytes: &'a [u8]) -> Result<Self, Error> {
Self::new_nested(bytes, 0)
}
#[inline(always)]
pub fn new_nested(bytes: &'a [u8], depth: usize) -> Result<Self, Error> {
let (&tag, payload) = bytes.split_first().ok_or(Error::Truncated)?;
if depth > DEPTH_LIMIT {
return Err(Error::TooDeep(DEPTH_LIMIT));
}
if matches!(tag, NONE..=TRUE | SMALL_INT..=0xFE) && !payload.is_empty() {
return Err(Error::Malformed("a payload on a value that has none"));
}
Ok(Self {
tag,
payload,
depth,
})
}
pub fn bigdecimal(payload: &'a [u8]) -> Result<Self, Error> {
decode::bigdecimal_parts(payload)?;
Ok(Self {
tag: BIGDECIMAL,
payload,
depth: 0,
})
}
#[inline(always)]
pub const fn tag(&self) -> u8 {
self.tag
}
#[inline(always)]
pub const fn payload(&self) -> &'a [u8] {
self.payload
}
#[inline(always)]
pub const fn kind(&self) -> Kind {
Kind::of(self.tag)
}
#[inline]
pub fn write_to(&self, out: &mut Vec<u8>) {
out.push(self.tag);
out.extend_from_slice(self.payload);
}
#[inline]
pub fn decode(&self) -> Result<Value, Error> {
decode::parts::<decode::Build>(self.tag, self.payload, self.depth)
}
#[inline(always)]
pub fn as_bool(&self) -> Option<bool> {
match self.tag {
FALSE => Some(false),
TRUE => Some(true),
_ => None,
}
}
#[inline(always)]
pub fn as_int(&self) -> Result<Option<i64>, Error> {
match self.tag {
0x80..=0xFE => Ok(Some((self.tag - SMALL_INT) as i64)),
INT => read_int(self.payload).map(Some),
I8 => {
let [b] = fixed(self.payload)?;
Ok(Some(b as i8 as i64))
}
U8 => {
let [b] = fixed(self.payload)?;
Ok(Some(b as i64))
}
I16 => {
if self.payload.is_empty() || self.payload.len() > 2 {
return Err(Error::Malformed("an i16 of the wrong width"));
}
read_int(self.payload).map(|i| Some(i as i16 as i64))
}
U16 => {
if self.payload.is_empty() || self.payload.len() > 2 {
return Err(Error::Malformed("a u16 of the wrong width"));
}
read_uint(self.payload).map(|u| Some(u as u16 as i64))
}
I32 => {
if self.payload.is_empty() || self.payload.len() > 4 {
return Err(Error::Malformed("an i32 of the wrong width"));
}
read_int(self.payload).map(|i| Some(i as i32 as i64))
}
U32 => {
if self.payload.is_empty() || self.payload.len() > 4 {
return Err(Error::Malformed("a u32 of the wrong width"));
}
read_uint(self.payload).map(|u| Some(u as u32 as i64))
}
U64 => {
let u = read_uint(self.payload)?;
Ok(i64::try_from(u).ok())
}
_ => Ok(None),
}
}
#[inline(always)]
pub fn as_float(&self) -> Result<Option<f64>, Error> {
match self.tag {
FLOAT => decode::float(self.payload).map(Some),
F16 => {
let b = fixed(self.payload)?;
Ok(Some(half::f16::from_bits(u16::from_le_bytes(b)).to_f64()))
}
F32 => {
let b = fixed(self.payload)?;
Ok(Some(f32::from_le_bytes(b) as f64))
}
_ => Ok(None),
}
}
#[inline]
pub fn as_decimal(&self) -> Result<Option<rust_decimal::Decimal>, Error> {
match self.tag {
DECIMAL => decode::decimal(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_bigdecimal(&self) -> Result<Option<surrealdb_types::BigDecimal>, Error> {
match self.tag {
BIGDECIMAL => decode::bigdecimal(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline(always)]
pub fn as_str(&self) -> Result<Option<&'a str>, Error> {
match self.tag {
STRING => utf8(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline(always)]
pub fn as_bytes(&self) -> Option<&'a [u8]> {
match self.tag {
BYTES => Some(self.payload),
_ => None,
}
}
#[inline]
pub fn as_duration(&self) -> Result<Option<std::time::Duration>, Error> {
match self.tag {
DURATION => decode::duration(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_datetime(&self) -> Result<Option<chrono::DateTime<chrono::Utc>>, Error> {
match self.tag {
DATETIME => decode::datetime(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_date(&self) -> Result<Option<crate::val::Date>, Error> {
match self.tag {
DATE => decode::date(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_time(&self) -> Result<Option<crate::val::Time>, Error> {
match self.tag {
TIME => decode::time(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_timestamp(&self) -> Result<Option<crate::val::Timestamp>, Error> {
match self.tag {
TIMESTAMP => decode::timestamp(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_interval(&self) -> Result<Option<crate::val::Interval>, Error> {
match self.tag {
INTERVAL => decode::interval(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_uuid(&self) -> Result<Option<uuid::Uuid>, Error> {
match self.tag {
UUID => Ok(Some(uuid::Uuid::from_bytes(fixed(self.payload)?))),
_ => Ok(None),
}
}
#[inline]
pub fn as_table(&self) -> Result<Option<&'a str>, Error> {
match self.tag {
TABLE => utf8(self.payload).map(Some),
_ => Ok(None),
}
}
#[inline]
pub fn as_record_id(&self) -> Result<Option<(&'a str, ValueRef<'a>)>, Error> {
match self.tag {
RECORD_ID => {
let (table, key) = split_clen(self.payload)?;
Ok(Some((utf8(table)?, ValueRef::new_nested(key, self.depth + 1)?)))
}
_ => Ok(None),
}
}
#[inline(always)]
pub fn as_object(&self) -> Result<Option<ObjectRef<'a>>, Error> {
match self.tag {
0x1A..=0x1C => Ok(Some(ObjectRef {
c: Container::split(self.payload, width(self.tag, OBJECT))?,
depth: self.depth,
})),
_ => Ok(None),
}
}
#[inline]
pub fn as_ordered_object(&self) -> Result<Option<OrderedObjectRef<'a>>, Error> {
self.as_object()?.map(ObjectRef::ordered).transpose()
}
#[inline(always)]
pub fn as_array(&self) -> Result<Option<ArrayRef<'a>>, Error> {
ArrayRef::of(self.tag, self.payload, ARRAY, self.depth)
}
#[inline(always)]
pub fn as_set(&self) -> Result<Option<ArrayRef<'a>>, Error> {
ArrayRef::of(self.tag, self.payload, SET, self.depth)
}
}
#[derive(Clone, Copy, Debug)]
pub(super) struct Container<'a> {
pub w: usize,
pub n: usize,
pub table: &'a [u8],
pub data: &'a [u8],
}
impl<'a> Container<'a> {
#[inline(always)]
pub fn split(p: &'a [u8], w: usize) -> Result<Self, Error> {
let n = read_width(p, 0, w).ok_or(Error::Truncated)?;
let end = n
.checked_mul(w)
.and_then(|t| t.checked_add(w))
.ok_or(Error::Malformed("an impossible entry count"))?;
let table = p.get(w..end).ok_or(Error::Truncated)?;
let data = p.get(end..).ok_or(Error::Truncated)?;
if n == 0 && !data.is_empty() {
return Err(Error::Malformed("bytes after the end of an empty container"));
}
Ok(Self {
w,
n,
table,
data,
})
}
#[inline(always)]
fn offset(&self, i: usize) -> Result<usize, Error> {
let at = i.checked_mul(self.w).ok_or(Error::Malformed("an entry outside its container"))?;
read_width(self.table, at, self.w).ok_or(Error::Truncated)
}
#[inline(always)]
pub fn entry(&self, i: usize) -> Result<&'a [u8], Error> {
if i >= self.n {
return Err(Error::Malformed("an entry outside its container"));
}
let start = self.offset(i)?;
let end = if i + 1 < self.n {
self.offset(i + 1)?
} else {
self.data.len()
};
if i == 0 && start != 0 {
return Err(Error::Malformed("bytes before a container's first entry"));
}
match self.data.get(start..end) {
Some(e) if !e.is_empty() => Ok(e),
_ => Err(Error::Malformed("an entry outside its container")),
}
}
#[inline(always)]
fn split_entry(&self, i: usize) -> Result<(&'a [u8], &'a [u8]), Error> {
split_clen(self.entry(i)?)
}
}
#[derive(Clone, Copy, Debug)]
pub struct ObjectRef<'a> {
c: Container<'a>,
depth: usize,
}
impl<'a> ObjectRef<'a> {
#[inline(always)]
pub fn len(&self) -> usize {
self.c.n
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.c.n == 0
}
#[inline]
pub fn entry(&self, i: usize) -> Result<(&'a str, ValueRef<'a>), Error> {
let (k, v) = split_clen(self.c.entry(i)?)?;
Ok((utf8(k)?, ValueRef::new_nested(v, self.depth + 1)?))
}
#[inline(always)]
pub fn value_at(&self, i: usize) -> Result<ValueRef<'a>, Error> {
ValueRef::new_nested(split_clen(self.c.entry(i)?)?.1, self.depth + 1)
}
pub fn ordered(self) -> Result<OrderedObjectRef<'a>, Error> {
let mut prev: Option<&[u8]> = None;
for i in 0..self.c.n {
let (k, _) = self.c.split_entry(i)?;
if prev.is_some_and(|prev| prev >= k) {
return Err(Error::Malformed("object keys out of order"));
}
utf8(k)?;
prev = Some(k);
}
Ok(OrderedObjectRef {
obj: self,
})
}
#[inline(always)]
pub fn validated(self) -> OrderedObjectRef<'a> {
OrderedObjectRef {
obj: self,
}
}
#[inline]
pub fn key_bytes(&self, i: usize) -> Result<&'a [u8], Error> {
Ok(self.c.split_entry(i)?.0)
}
#[inline]
pub fn iter(&self) -> ObjectIter<'a> {
ObjectIter {
obj: *self,
i: 0,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct OrderedObjectRef<'a> {
obj: ObjectRef<'a>,
}
impl<'a> std::ops::Deref for OrderedObjectRef<'a> {
type Target = ObjectRef<'a>;
#[inline(always)]
fn deref(&self) -> &ObjectRef<'a> {
&self.obj
}
}
impl<'a> OrderedObjectRef<'a> {
#[inline]
pub fn get(&self, key: &str) -> Result<Option<ValueRef<'a>>, Error> {
let mut hint = u32::MAX;
self.get_hinted(key, &mut hint)
}
#[inline]
pub fn get_hinted(&self, key: &str, hint: &mut u32) -> Result<Option<ValueRef<'a>>, Error> {
match self.find(key.as_bytes(), hint)? {
Some(value) => ValueRef::new_nested(value, self.obj.depth + 1).map(Some),
None => Ok(None),
}
}
fn find(&self, needle: &[u8], hint: &mut u32) -> Result<Option<&'a [u8]>, Error> {
let n = self.obj.c.n;
let (mut lo, mut hi) = (0, n);
let h = *hint as usize;
if h < n {
let (k, v) = self.obj.c.split_entry(h)?;
match k.cmp(needle) {
Ordering::Equal => return Ok(Some(v)),
Ordering::Less => {
lo = h + 1;
if lo < n {
let (k, v) = self.obj.c.split_entry(lo)?;
match k.cmp(needle) {
Ordering::Equal => {
*hint = lo as u32;
return Ok(Some(v));
}
Ordering::Less => lo += 1,
Ordering::Greater => return Ok(None),
}
}
}
Ordering::Greater => hi = h,
}
}
while lo < hi {
let mid = lo + (hi - lo) / 2;
let (k, v) = self.obj.c.split_entry(mid)?;
match k.cmp(needle) {
Ordering::Less => lo = mid + 1,
Ordering::Greater => hi = mid,
Ordering::Equal => {
*hint = mid as u32;
return Ok(Some(v));
}
}
}
Ok(None)
}
}
pub struct ObjectIter<'a> {
obj: ObjectRef<'a>,
i: usize,
}
impl<'a> Iterator for ObjectIter<'a> {
type Item = Result<(&'a str, ValueRef<'a>), Error>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.i >= self.obj.len() {
return None;
}
let r = self.obj.entry(self.i);
self.i += 1;
Some(r)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = self.obj.len() - self.i;
(n, Some(n))
}
}
#[derive(Clone, Copy, Debug)]
enum ArrayRepr<'a> {
Values(Container<'a>),
F64(&'a [u8]),
F32(&'a [u8]),
Ints {
p: &'a [u8],
w: usize,
},
}
#[derive(Clone, Copy, Debug)]
pub struct ArrayRef<'a> {
repr: ArrayRepr<'a>,
depth: usize,
}
impl<'a> ArrayRef<'a> {
#[inline(always)]
fn of(tag: u8, payload: &'a [u8], base: u8, depth: usize) -> Result<Option<Self>, Error> {
let repr = match tag {
t if (base..base + 3).contains(&t) => {
ArrayRepr::Values(Container::split(payload, width(t, base))?)
}
F64_ARRAY if base == ARRAY => {
if payload.is_empty() || !payload.len().is_multiple_of(8) {
return Err(Error::Malformed("a float array of the wrong width"));
}
ArrayRepr::F64(payload)
}
F32_ARRAY if base == ARRAY => {
if payload.is_empty() || !payload.len().is_multiple_of(4) {
return Err(Error::Malformed("a float array of the wrong width"));
}
ArrayRepr::F32(payload)
}
I8_ARRAY..=I64_ARRAY if base == ARRAY => ArrayRepr::Ints {
p: payload,
w: int_array_element_width(tag, payload)?,
},
_ => return Ok(None),
};
Ok(Some(Self {
repr,
depth,
}))
}
#[inline(always)]
pub fn len(&self) -> usize {
match self.repr {
ArrayRepr::Values(c) => c.n,
ArrayRepr::F64(p) => p.len() / 8,
ArrayRepr::F32(p) => p.len() / 4,
ArrayRepr::Ints {
p,
w,
} => p.len() / w,
}
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn get(&self, i: usize) -> Result<Option<ValueRef<'a>>, Error> {
if i >= self.len() {
return Ok(None);
}
Ok(Some(match self.repr {
ArrayRepr::Values(c) => ValueRef::new_nested(c.entry(i)?, self.depth + 1)?,
ArrayRepr::F64(p) => ValueRef {
tag: FLOAT,
payload: p
.as_chunks::<8>()
.0
.get(i)
.ok_or(Error::Malformed("an element outside its array"))?,
depth: self.depth,
},
ArrayRepr::F32(p) => ValueRef {
tag: FLOAT,
payload: p
.as_chunks::<4>()
.0
.get(i)
.ok_or(Error::Malformed("an element outside its array"))?,
depth: self.depth,
},
ArrayRepr::Ints {
p,
w,
} => {
let start =
i.checked_mul(w).ok_or(Error::Malformed("an element outside its array"))?;
let end =
start.checked_add(w).ok_or(Error::Malformed("an element outside its array"))?;
ValueRef {
tag: INT,
payload: p
.get(start..end)
.ok_or(Error::Malformed("an element outside its array"))?,
depth: self.depth,
}
}
}))
}
#[inline]
pub fn ints(&self) -> Option<impl ExactSizeIterator<Item = i64> + 'a> {
match self.repr {
ArrayRepr::Ints {
p,
w,
} => Some(match w {
1 => IntIter::I8(p.as_chunks::<1>().0.iter()),
2 => IntIter::I16(p.as_chunks::<2>().0.iter()),
4 => IntIter::I32(p.as_chunks::<4>().0.iter()),
_ => IntIter::I64(p.as_chunks::<8>().0.iter()),
}),
ArrayRepr::Values(_) | ArrayRepr::F64(_) | ArrayRepr::F32(_) => None,
}
}
#[inline]
pub fn floats(&self) -> Option<impl ExactSizeIterator<Item = f64> + 'a> {
match self.repr {
ArrayRepr::F64(p) => Some(FloatIter::F64(p.as_chunks::<8>().0.iter())),
ArrayRepr::F32(p) => Some(FloatIter::F32(p.as_chunks::<4>().0.iter())),
ArrayRepr::Values(_)
| ArrayRepr::Ints {
..
} => None,
}
}
#[inline]
pub fn iter(&self) -> ArrayIter<'a> {
ArrayIter {
arr: *self,
i: 0,
}
}
}
enum FloatIter<'a> {
F64(std::slice::Iter<'a, [u8; 8]>),
F32(std::slice::Iter<'a, [u8; 4]>),
}
impl Iterator for FloatIter<'_> {
type Item = f64;
#[inline]
fn next(&mut self) -> Option<f64> {
match self {
Self::F64(i) => i.next().map(|b| f64::from_le_bytes(*b)),
Self::F32(i) => i.next().map(|b| f32::from_le_bytes(*b) as f64),
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self {
Self::F64(i) => i.size_hint(),
Self::F32(i) => i.size_hint(),
}
}
}
impl ExactSizeIterator for FloatIter<'_> {}
enum IntIter<'a> {
I8(std::slice::Iter<'a, [u8; 1]>),
I16(std::slice::Iter<'a, [u8; 2]>),
I32(std::slice::Iter<'a, [u8; 4]>),
I64(std::slice::Iter<'a, [u8; 8]>),
}
impl Iterator for IntIter<'_> {
type Item = i64;
#[inline]
fn next(&mut self) -> Option<i64> {
match self {
Self::I8(i) => i.next().map(|b| i8::from_le_bytes(*b) as i64),
Self::I16(i) => i.next().map(|b| i16::from_le_bytes(*b) as i64),
Self::I32(i) => i.next().map(|b| i32::from_le_bytes(*b) as i64),
Self::I64(i) => i.next().map(|b| i64::from_le_bytes(*b)),
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self {
Self::I8(i) => i.size_hint(),
Self::I16(i) => i.size_hint(),
Self::I32(i) => i.size_hint(),
Self::I64(i) => i.size_hint(),
}
}
}
impl ExactSizeIterator for IntIter<'_> {}
pub struct ArrayIter<'a> {
arr: ArrayRef<'a>,
i: usize,
}
impl<'a> Iterator for ArrayIter<'a> {
type Item = Result<ValueRef<'a>, Error>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let r = self.arr.get(self.i).transpose()?;
self.i += 1;
Some(r)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = self.arr.len().saturating_sub(self.i);
(n, Some(n))
}
}
#[cfg(test)]
mod depth_tests {
use super::ValueRef;
use crate::val::stored::tag::{OBJECT, SMALL_INT};
use crate::val::stored::{DEPTH_LIMIT, Error, decode};
fn wrap_in_object(inner: &[u8]) -> Vec<u8> {
[&[OBJECT, 0x01, 0x00, 0x01, b'k'][..], inner].concat()
}
fn nested_objects(levels: usize) -> Vec<u8> {
let mut b = vec![SMALL_INT + 7];
for _ in 0..levels {
b = wrap_in_object(&b);
}
b
}
fn descend(bytes: &[u8], levels: usize) -> Result<ValueRef<'_>, Error> {
let mut v = ValueRef::new(bytes)?;
for _ in 0..levels {
let obj = v.as_ordered_object()?.expect("an object");
v = obj.get("k")?.expect("a field k");
}
Ok(v)
}
#[test]
fn a_value_at_the_depth_limit_is_accepted_by_lookups_and_decode() {
let bytes = nested_objects(DEPTH_LIMIT);
assert!(decode(&bytes).is_ok());
let leaf = descend(&bytes, DEPTH_LIMIT).unwrap();
assert_eq!(leaf.as_int().unwrap(), Some(7));
assert!(leaf.decode().is_ok());
}
#[test]
fn a_value_past_the_depth_limit_is_refused_by_lookups_as_by_decode() {
let bytes = nested_objects(DEPTH_LIMIT + 1);
assert_eq!(decode(&bytes).err(), Some(Error::TooDeep(DEPTH_LIMIT)));
let reached = descend(&bytes, DEPTH_LIMIT + 1).and_then(|v| v.decode());
assert_eq!(reached.err(), Some(Error::TooDeep(DEPTH_LIMIT)));
}
#[test]
fn new_nested_refuses_a_depth_past_the_limit() {
assert!(ValueRef::new_nested(&[SMALL_INT], DEPTH_LIMIT).is_ok());
assert_eq!(
ValueRef::new_nested(&[SMALL_INT], DEPTH_LIMIT + 1).err(),
Some(Error::TooDeep(DEPTH_LIMIT))
);
}
}