SyncItem

Struct SyncItem 

Source
pub struct SyncItem {
Show 13 fields pub object_id: String, pub version: u64, pub updated_at: i64, pub content_type: ContentType, pub batch_id: Option<String>, pub trace_parent: Option<String>, pub trace_state: Option<String>, pub content_hash: String, pub last_accessed: u64, pub access_count: u64, pub content: Vec<u8>, pub home_instance_id: Option<String>, pub state: String, /* private fields */
}
Expand description

A wrapper struct that separates metadata from content.

§Binary-First Design

sync-engine is a dumb storage layer - it stores your bytes and routes them to L1/L2/L3 based on SubmitOptions. The content field is opaque Vec<u8> that we never interpret.

§Example

use sync_engine::SyncItem;
use serde_json::json;

// JSON content (serialize to bytes yourself)
let json_bytes = serde_json::to_vec(&json!({"name": "John Doe"})).unwrap();
let item = SyncItem::new("uk.nhs.patient.12345".into(), json_bytes);

assert_eq!(item.object_id, "uk.nhs.patient.12345");
assert_eq!(item.version, 1);

Fields§

§object_id: String

Reverse DNS style ID (e.g., uk.nhs.patient.record.1234567890)

§version: u64

Version number (monotonically increasing within this item)

§updated_at: i64

Last update timestamp (epoch millis)

§content_type: ContentType

Content type (json or binary) - determines storage format JSON → Redis HSET (searchable), SQL JSON column Binary → Redis SET, SQL BLOB column

§batch_id: Option<String>

Batch ID for tracking batch writes (UUID, set during batch flush)

§trace_parent: Option<String>

W3C Trace Context traceparent header (for cross-item trace linking) Format: “00-{trace_id}-{span_id}-{flags}” This is NOT for in-process tracing (that flows via Span::current()), but for linking related operations across items/time.

§trace_state: Option<String>

W3C Trace Context tracestate header (optional vendor-specific data)

§content_hash: String

SHA256 hash of the content (hex-encoded). Computed eagerly on creation for CDC dedup and integrity checks.

§last_accessed: u64

Timestamp of last access (epoch millis)

§access_count: u64

Number of times accessed

§content: Vec<u8>

The actual payload (opaque binary, caller handles serialization)

§home_instance_id: Option<String>

Optional guest data owner ID (for routing engine)

§state: String

Arbitrary state tag for caller-defined grouping (e.g., “delta”, “base”, “pending”). Indexed in SQL and tracked via Redis SETs for fast state-based queries. Default: “default”

Implementations§

Source§

impl SyncItem

Source

pub fn new(object_id: String, content: Vec<u8>) -> Self

Create a new SyncItem with binary content.

The content type is auto-detected: if the bytes are valid JSON, content_type will be Json, otherwise Binary. This enables intelligent storage routing (HSET vs SET in Redis, JSON vs BLOB in SQL).

§Example
use sync_engine::{SyncItem, ContentType};

// From raw bytes (detected as Binary)
let item = SyncItem::new("id".into(), vec![1, 2, 3]);
assert_eq!(item.content_type, ContentType::Binary);

// From JSON bytes (detected as Json)
let json = serde_json::to_vec(&serde_json::json!({"key": "value"})).unwrap();
let item = SyncItem::new("id".into(), json);
assert_eq!(item.content_type, ContentType::Json);
Source

pub fn from_json(object_id: String, value: Value) -> Self

Create a new SyncItem from a JSON value (convenience method).

This serializes the JSON to bytes and sets content_type to Json. For binary formats (MessagePack, Cap’n Proto), use new.

Source

pub fn from_serializable<T: Serialize>( object_id: String, value: &T, ) -> Result<Self, Error>

Create a new SyncItem from any serializable type.

This avoids creating an intermediate serde_json::Value if you have a struct. This is more efficient than from_json if you already have a typed object.

Source

pub fn with_options(self, options: SubmitOptions) -> Self

Set submit options for this item (builder pattern).

These options control where the item is stored (Redis, SQL) and how it’s compressed. Options travel with the item through the batch pipeline.

§Example
use sync_engine::{SyncItem, SubmitOptions, CacheTtl};

let item = SyncItem::new("cache.key".into(), b"data".to_vec())
    .with_options(SubmitOptions::cache(CacheTtl::Minute));
Source

pub fn with_state(self, state: impl Into<String>) -> Self

Set state tag for this item (builder pattern).

State is an arbitrary string for caller-defined grouping. Common uses: “delta”/“base” for CRDTs, “pending”/“approved” for workflows.

§Example
use sync_engine::SyncItem;

let item = SyncItem::new("crdt.123".into(), b"data".to_vec())
    .with_state("delta");
Source

pub fn effective_options(&self) -> SubmitOptions

Get the effective submit options (returns default if not set).

Source

pub fn content_as_json(&self) -> Option<Value>

Try to parse content as JSON.

Returns None if content is not valid JSON.

Trait Implementations§

Source§

impl BatchableItem for SyncItem

Source§

fn id(&self) -> &str

Source§

impl Clone for SyncItem

Source§

fn clone(&self) -> SyncItem

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SyncItem

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SyncItem

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for SyncItem

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl SizedItem for SyncItem

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,