1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Types for interacting with raw event data in PostgreSQL event store.

use crate::{error::LoadError, util::Sequence};
use cqrs_core::{EventNumber, Since};
use fallible_iterator::FallibleIterator;
use postgres::Connection;

/// An owned, raw view of event data.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct RawEvent {
    /// The event id.
    pub event_id: EventNumber,
    /// The aggregate type.
    pub aggregate_type: String,
    /// The entity id.
    pub entity_id: String,
    /// The sequence number of this event in the entity's event stream.
    pub sequence: EventNumber,
    /// The event type.
    pub event_type: String,
    /// The raw event payload.
    pub payload: Vec<u8>,
}

/// An owned, raw view of event data.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct BorrowedRawEvent<'row> {
    /// The event id.
    pub event_id: EventNumber,
    /// The aggregate type.
    pub aggregate_type: &'row str,
    /// The entity id.
    pub entity_id: &'row str,
    /// The sequence number of this event in the entity's event stream.
    pub sequence: EventNumber,
    /// The event type.
    pub event_type: &'row str,
    /// The raw event payload.
    pub payload: &'row [u8],
}

/// A connection to a PostgreSQL storage backend that is not specific to any aggregate.
#[derive(Clone, Copy, Debug)]
pub struct RawPostgresStore<'conn> {
    conn: &'conn Connection,
}

impl<'conn> RawPostgresStore<'conn> {
    /// Reads all events from the event stream, starting with events after `since`,
    pub fn read_all_events(
        self,
        since: Since,
        max_count: u64,
    ) -> Result<Vec<RawEvent>, postgres::Error> {
        let last_sequence = match since {
            Since::BeginningOfStream => 0,
            Since::Event(x) => x.get(),
        } as i64;

        let trans = self
            .conn
            .transaction_with(postgres::transaction::Config::default().read_only(true))?;

        let handle_row = |row: postgres::rows::Row| {
            let event_id: Sequence = row.get(0);
            let aggregate_type = row.get(1);
            let entity_id = row.get(2);
            let sequence: Sequence = row.get(3);
            let event_type = row.get(4);
            let payload = row.get_bytes(5).unwrap();
            log::trace!(
                "entity {}/{}: loaded event; sequence: {}, type: {}",
                aggregate_type,
                entity_id,
                sequence.0,
                event_type,
            );
            RawEvent {
                event_id: event_id.0,
                aggregate_type,
                entity_id,
                sequence: sequence.0,
                event_type,
                payload: payload.to_owned(),
            }
        };

        let events: Vec<RawEvent>;
        {
            let stmt = trans.prepare_cached(
                "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                 FROM events \
                 WHERE event_id > $1 \
                 ORDER BY event_id ASC \
                 LIMIT $2",
            )?;
            let rows = stmt.lazy_query(
                &trans,
                &[
                    &last_sequence,
                    &(max_count.min(i64::max_value() as u64) as i64),
                ],
                100,
            )?;

            events = rows
                .iterator()
                .map(|row_result| row_result.map(handle_row))
                .collect::<Result<_, postgres::Error>>()?;
        }

        trans.commit()?;

        log::trace!("read {} events", events.len(),);

        Ok(events)
    }

    /// Reads all events from the event stream, starting with events after `since`,
    pub fn read_all_events_with<E: cqrs_core::CqrsError>(
        self,
        since: Since,
        max_count: u64,
        mut f: impl for<'row> FnMut(BorrowedRawEvent<'row>) -> Result<(), E>,
    ) -> Result<(), LoadError<E>> {
        let last_sequence = match since {
            Since::BeginningOfStream => 0,
            Since::Event(x) => x.get(),
        } as i64;

        let trans = self
            .conn
            .transaction_with(postgres::transaction::Config::default().read_only(true))?;

        let mut handle_row = |row: postgres::rows::Row| -> Result<(), LoadError<E>> {
            let event_id: Sequence = row.get(0);
            let aggregate_type = std::str::from_utf8(row.get_bytes(1).unwrap()).unwrap();
            let entity_id = std::str::from_utf8(row.get_bytes(2).unwrap()).unwrap();
            let sequence: Sequence = row.get(3);
            let event_type = std::str::from_utf8(row.get_bytes(4).unwrap()).unwrap();
            let payload = row.get_bytes(5).unwrap();
            log::trace!(
                "entity {}/{}: loaded event; sequence: {}, type: {}",
                aggregate_type,
                entity_id,
                sequence.0,
                event_type,
            );
            f(BorrowedRawEvent {
                event_id: event_id.0,
                aggregate_type,
                entity_id,
                sequence: sequence.0,
                event_type,
                payload,
            })
            .map_err(LoadError::DeserializationError)
        };

        let events: Vec<()>;
        {
            let stmt = trans.prepare_cached(
                "SELECT event_id, aggregate_type, entity_id, sequence, event_type, payload \
                 FROM events \
                 WHERE event_id > $1 \
                 ORDER BY event_id ASC \
                 LIMIT $2",
            )?;
            let rows = stmt.lazy_query(
                &trans,
                &[
                    &last_sequence,
                    &(max_count.min(i64::max_value() as u64) as i64),
                ],
                100,
            )?;

            events = rows
                .iterator()
                .map(|row_result| {
                    row_result
                        .map_err(LoadError::from)
                        .and_then(|row| handle_row(row))
                })
                .collect::<Result<_, LoadError<E>>>()?;
        }

        trans.commit()?;

        log::trace!("read {} events", events.len(),);

        Ok(())
    }
}