use std::sync::OnceLock;
use astrs_data::ipc::decode_payload;
use astrs_data::{ArrayRef, RecordBatch, SchemaHash};
use astrs_shm::Sample;
use crate::error::{NodeError, Result};
use crate::message::FromPayload;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct SlotLocation {
pub slot: u32,
pub sequence: u64,
pub address: usize,
pub layout_address: usize,
pub mapping_base: usize,
pub mapping_len: usize,
}
impl SlotLocation {
#[must_use]
pub const fn is_at_layout_offset(&self) -> bool {
self.address == self.layout_address
}
#[must_use]
pub const fn is_inside_mapping(&self) -> bool {
self.address >= self.mapping_base
&& self.address < self.mapping_base.saturating_add(self.mapping_len)
}
#[must_use]
pub const fn offset_in_mapping(&self) -> usize {
self.address.saturating_sub(self.mapping_base)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PayloadKind {
Inline,
ZeroCopy,
}
impl PayloadKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Inline => "inline",
Self::ZeroCopy => "zero_copy",
}
}
#[must_use]
pub const fn is_zero_copy(self) -> bool {
matches!(self, Self::ZeroCopy)
}
}
impl core::fmt::Display for PayloadKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug)]
enum PayloadSource {
Inline(Vec<u8>),
Shm(Box<Sample>),
}
#[derive(Debug)]
pub struct Payload {
source: PayloadSource,
decoded: OnceLock<RecordBatch>,
}
impl Payload {
#[must_use]
pub fn inline(bytes: Vec<u8>) -> Self {
Self {
source: PayloadSource::Inline(bytes),
decoded: OnceLock::new(),
}
}
#[must_use]
pub fn empty() -> Self {
Self::inline(Vec::new())
}
#[must_use]
pub fn zero_copy(sample: Sample) -> Self {
Self {
source: PayloadSource::Shm(Box::new(sample)),
decoded: OnceLock::new(),
}
}
pub fn from_batch(batch: RecordBatch) -> Result<Self> {
let bytes = astrs_data::ipc::encode_payload(&batch)?.to_vec();
let payload = Self::inline(bytes);
let _ = payload.decoded.set(batch);
Ok(payload)
}
#[must_use]
pub const fn kind(&self) -> PayloadKind {
match self.source {
PayloadSource::Inline(_) => PayloadKind::Inline,
PayloadSource::Shm(_) => PayloadKind::ZeroCopy,
}
}
#[must_use]
pub const fn is_zero_copy(&self) -> bool {
self.kind().is_zero_copy()
}
#[must_use]
pub fn bytes(&self) -> &[u8] {
match &self.source {
PayloadSource::Inline(bytes) => bytes,
PayloadSource::Shm(sample) => sample.payload(),
}
}
#[must_use]
pub fn len(&self) -> usize {
self.bytes().len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.bytes().is_empty()
}
#[must_use]
pub fn address(&self) -> usize {
self.bytes().as_ptr() as usize
}
#[must_use]
pub fn is_aligned_to(&self, alignment: usize) -> bool {
alignment != 0 && alignment.is_power_of_two() && self.address().is_multiple_of(alignment)
}
#[must_use]
pub fn to_vec(&self) -> Vec<u8> {
self.bytes().to_vec()
}
#[must_use]
pub fn detach(self) -> Self {
match self.source {
PayloadSource::Inline(_) => self,
PayloadSource::Shm(ref sample) => {
let bytes = sample.payload().to_vec();
let decoded = self.decoded;
Self {
source: PayloadSource::Inline(bytes),
decoded,
}
}
}
}
pub fn batch(&self) -> Result<&RecordBatch> {
if let Some(batch) = self.decoded.get() {
return Ok(batch);
}
let batch = decode_payload(self.bytes())?;
let _ = self.decoded.set(batch);
self.decoded
.get()
.ok_or(NodeError::Data(astrs_data::DataError::AllocationFailed {
bytes: 0,
}))
}
pub fn into_batch(self) -> Result<RecordBatch> {
let Self { source, decoded } = self;
if let Some(batch) = decoded.into_inner() {
return Ok(batch);
}
let bytes = match &source {
PayloadSource::Inline(bytes) => bytes.as_slice(),
PayloadSource::Shm(sample) => sample.payload(),
};
Ok(decode_payload(bytes)?)
}
pub fn column(&self) -> Result<&ArrayRef> {
let batch = self.batch()?;
batch
.payload_column()
.or_else(|| batch.column(0))
.ok_or(NodeError::Data(
astrs_data::DataError::ColumnCountMismatch {
fields: 1,
columns: 0,
},
))
}
pub fn num_rows(&self) -> Result<usize> {
Ok(self.batch()?.num_rows())
}
pub fn schema_hash(&self) -> Result<SchemaHash> {
Ok(SchemaHash::of(self.batch()?.schema()))
}
pub fn view<T: FromPayload>(&self) -> Result<T> {
Ok(T::from_batch(self.batch()?)?)
}
#[must_use]
pub fn sequence(&self) -> Option<u64> {
match &self.source {
PayloadSource::Inline(_) => None,
PayloadSource::Shm(sample) => Some(sample.seq()),
}
}
#[must_use]
pub fn slot(&self) -> Option<SlotLocation> {
match &self.source {
PayloadSource::Inline(_) => None,
PayloadSource::Shm(sample) => Some(SlotLocation {
slot: sample.slot_index(),
sequence: sample.seq(),
address: sample.payload_address(),
layout_address: sample.layout_payload_address(),
mapping_base: sample.mapping_base(),
mapping_len: sample.mapping_len(),
}),
}
}
#[must_use]
pub fn slot_metadata(&self) -> Option<&[u8]> {
match &self.source {
PayloadSource::Inline(_) => None,
PayloadSource::Shm(sample) => Some(sample.metadata()),
}
}
}
impl AsRef<[u8]> for Payload {
fn as_ref(&self) -> &[u8] {
self.bytes()
}
}
impl From<Vec<u8>> for Payload {
fn from(bytes: Vec<u8>) -> Self {
Self::inline(bytes)
}
}
impl From<Sample> for Payload {
fn from(sample: Sample) -> Self {
Self::zero_copy(sample)
}
}
impl core::fmt::Display for Payload {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{} byte(s) ({})", self.len(), self.kind())
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use astrs_data::array::{Float64Array, IntoArrayRef};
use astrs_data::ipc::encode_payload;
fn sample_batch() -> RecordBatch {
RecordBatch::from_payload(Float64Array::from_values([1.5, 2.5, 3.5]).into_array_ref())
}
fn sample_bytes() -> Vec<u8> {
encode_payload(&sample_batch()).unwrap().to_vec()
}
#[test]
fn an_inline_payload_reports_its_bytes_without_decoding() {
let bytes = sample_bytes();
let payload = Payload::inline(bytes.clone());
assert_eq!(payload.kind(), PayloadKind::Inline);
assert!(!payload.is_zero_copy());
assert_eq!(payload.bytes(), bytes.as_slice());
assert_eq!(payload.len(), bytes.len());
assert!(!payload.is_empty());
assert_eq!(payload.to_vec(), bytes);
assert_eq!(payload.sequence(), None);
assert_eq!(payload.slot_metadata(), None);
assert!(payload.to_string().contains("inline"));
}
#[test]
fn decoding_happens_once_and_is_cached() {
let payload = Payload::inline(sample_bytes());
let first = payload.batch().unwrap();
let first_address = std::ptr::from_ref(first) as usize;
let second = payload.batch().unwrap();
assert_eq!(std::ptr::from_ref(second) as usize, first_address);
assert_eq!(second.num_rows(), 3);
assert_eq!(payload.num_rows().unwrap(), 3);
}
#[test]
fn a_payload_built_from_a_batch_skips_the_first_decode() {
let payload = Payload::from_batch(sample_batch()).unwrap();
assert_eq!(payload.batch().unwrap(), &sample_batch());
assert_eq!(payload.bytes(), sample_bytes().as_slice());
assert_eq!(payload.into_batch().unwrap(), sample_batch());
}
#[test]
fn an_empty_payload_is_empty_and_does_not_decode() {
let payload = Payload::empty();
assert!(payload.is_empty());
assert_eq!(payload.len(), 0);
assert!(payload.batch().is_err(), "no bytes is not a valid stream");
}
#[test]
fn corrupt_bytes_are_reported_not_guessed() {
let mut bytes = sample_bytes();
bytes[4] ^= 0xFF;
let payload = Payload::inline(bytes);
let error = payload.batch().unwrap_err();
assert_eq!(error.kind_name(), "ipc");
}
#[test]
fn the_payload_column_is_the_data_column() {
let payload = Payload::inline(sample_bytes());
let column = payload.column().unwrap();
assert_eq!(column.len(), 3);
assert_eq!(column.data_type(), &astrs_data::DataType::Float64);
}
#[test]
fn detaching_an_inline_payload_is_a_no_op() {
let payload = Payload::inline(sample_bytes());
let bytes = payload.to_vec();
let detached = payload.detach();
assert_eq!(detached.to_vec(), bytes);
assert_eq!(detached.kind(), PayloadKind::Inline);
}
#[test]
fn schema_hashes_agree_for_identical_layouts() {
let left = Payload::inline(sample_bytes());
let right = Payload::from_batch(sample_batch()).unwrap();
assert_eq!(left.schema_hash().unwrap(), right.schema_hash().unwrap());
}
#[test]
fn conversions_and_as_ref_agree_with_bytes() {
let bytes = sample_bytes();
let payload: Payload = bytes.clone().into();
assert_eq!(payload.as_ref(), bytes.as_slice());
}
#[test]
fn alignment_is_reported_honestly() {
let payload = Payload::inline(sample_bytes());
assert!(payload.is_aligned_to(1));
assert!(!payload.is_aligned_to(0), "zero is not an alignment");
assert!(!payload.is_aligned_to(3), "only powers of two are");
}
#[test]
fn payload_kinds_describe_themselves() {
assert_eq!(PayloadKind::Inline.as_str(), "inline");
assert_eq!(PayloadKind::ZeroCopy.as_str(), "zero_copy");
assert!(PayloadKind::ZeroCopy.is_zero_copy());
assert!(!PayloadKind::Inline.is_zero_copy());
assert_eq!(PayloadKind::Inline.to_string(), "inline");
}
#[test]
fn payloads_cross_thread_boundaries() {
const fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Payload>();
}
}