Skip to main content

KeyRow

Trait KeyRow 

Source
pub trait KeyRow {
    type Key: KeyColumn + 'static;

    // Required method
    fn partition_key(&self) -> &Self::Key;
}
Expand description

The row’s partition key, which a table described by hand reads off its row where its description sets InboxSpec::partition_key.

§Examples

use chrono::{DateTime, Utc};
use ruststream::runtime::{Input, SoloCarried};
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::prelude::*;
use ruststream_sqlx::spec::{Key, Lease};
use ruststream_sqlx::{InboxSpec, InboxTable, KeyRow};
use sqlx::{Sqlite, SqlitePool};

#[derive(Debug, Clone, sqlx::FromRow)]
pub struct Invoice {
    id: i64,
    customer: String,
    total: i64,
}

impl InboxTable for Invoice {
    type Id = i64;
    type Table = InboxSpec<(Lease<DateTime<Utc>>, Key)>;
    const TABLE: Self::Table = InboxSpec::new("invoices", Column::new("id").generated())
        .lease(Column::new("locked_until"))
        .partition_key(Column::new("customer"))
        .data(&[Column::new("total")]);

    fn id(&self) -> &i64 {
        &self.id
    }
}

// Row mode: the handler takes the row itself.
impl Input for Invoice {
    type Axis = SoloCarried<Self>;
}

impl KeyRow for Invoice {
    type Key = String;

    fn partition_key(&self) -> &String {
        &self.customer
    }
}

// One customer's invoices settle in order, each customer on a lane of its own.
#[subscriber(InboxQueue::<Invoice>::new("invoices"))]
async fn bill(invoice: &Invoice) -> HandlerOutcome {
    if invoice.total >= 0 {
        HandlerOutcome::ack()
    } else {
        HandlerOutcome::drop()
    }
}

pub fn app(pool: SqlitePool) -> RustStream {
    RustStream::new(AppInfo::new("billing", "1.0.0")).with_broker(
        SqlxBroker::<Sqlite>::new(pool),
        |b| {
            b.include(bill);
        },
    )
}

Required Associated Types§

Source

type Key: KeyColumn + 'static

The key column’s type: text or bytes, optional or not.

Required Methods§

Source

fn partition_key(&self) -> &Self::Key

The key field.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§