Skip to main content

StreamingEncoder

Struct StreamingEncoder 

Source
pub struct StreamingEncoder<W>
where W: Write,
{ /* private fields */ }
Expand description

A streaming encoder that writes Tensogram frames progressively to a sink.

Unlike crate::encode::encode, which builds the entire message in memory, StreamingEncoder writes each data object frame immediately. This allows encoding to a socket or pipe without buffering the full message.

The trade-off is that header-based index and hash frames are not possible; instead, these are written as footer frames when finish is called.

§Example

use std::io::BufWriter;
use std::fs::File;
use tensogram::streaming::StreamingEncoder;
use tensogram::{GlobalMetadata, EncodeOptions};

let file = BufWriter::new(File::create("output.tgm").unwrap());
let meta = GlobalMetadata::default();
let mut enc = StreamingEncoder::new(file, &meta, &EncodeOptions::default()).unwrap();
// enc.write_object(&desc, &data).unwrap();
// enc.finish().unwrap();

Implementations§

Source§

impl<W> StreamingEncoder<W>
where W: Write,

Source

pub fn new( writer: W, global_meta: &GlobalMetadata, options: &EncodeOptions, ) -> Result<StreamingEncoder<W>, TensogramError>

Begin a new streaming message.

Writes the preamble (with total_length = 0 for streaming mode) and a header metadata frame containing the global metadata.

Source

pub fn write_preceder( &mut self, metadata: BTreeMap<String, Value>, ) -> Result<(), TensogramError>

Write a PrecederMetadata frame for the next data object.

The metadata map becomes base[0] in a GlobalMetadata CBOR wrapper. Must be followed by exactly one write_object or write_object_pre_encoded call before another write_preceder or finish.

Source

pub fn write_object( &mut self, desc: &DataObjectDescriptor, data: &[u8], ) -> Result<(), TensogramError>

Encode and write a single data object frame.

The descriptor’s encoding/filter/compression pipeline is applied, the payload is hashed (if configured), and the frame is written immediately — no buffering.

When EncodeOptions.threads > 0 was passed to StreamingEncoder::new, the pipeline call may use up to that many threads internally (axis B). Axis A is not available in streaming mode — each write_object is a caller-paced event with no cross-object parallelism opportunity.

Source

pub fn write_object_pre_encoded( &mut self, descriptor: &DataObjectDescriptor, pre_encoded_bytes: &[u8], ) -> Result<(), TensogramError>

Write a pre-encoded data object frame directly.

Unlike write_object, this method does not run the encoding pipeline — pre_encoded_bytes are written to the stream as-is. The descriptor must accurately describe the encoding that was already applied (encoding, filter, compression, params) so that decoders can reconstruct the original payload.

This method participates in the same preceder consumption logic as write_object and can be freely intermixed with it.

§Errors

Returns an error if the descriptor is invalid or the frame cannot be written to the underlying writer.

Source

pub fn finish(self) -> Result<W, TensogramError>

Finalize the streaming message.

Writes footer frames (payload metadata + hash + index) and the postamble. Consumes the encoder and returns the underlying writer.

Source

pub fn object_count(&self) -> usize

Returns the number of data objects written so far.

Source

pub fn bytes_written(&self) -> u64

Returns the total bytes written so far.

Auto Trait Implementations§

§

impl<W> Freeze for StreamingEncoder<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for StreamingEncoder<W>
where W: RefUnwindSafe,

§

impl<W> Send for StreamingEncoder<W>
where W: Send,

§

impl<W> Sync for StreamingEncoder<W>
where W: Sync,

§

impl<W> Unpin for StreamingEncoder<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for StreamingEncoder<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for StreamingEncoder<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more