about summary refs log tree commit diff
path: root/finito-postgres/src/lib.rs
blob: 6e1815a69fa1984f3ce59111e6f227d21a27cdff (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! PostgreSQL-backed persistence for Finito state machines
//!
//! This module implements ... TODO when I can write again.
//!
//! TODO: events & actions should have `SERIAL` keys

#[macro_use] extern crate postgres;
#[macro_use] extern crate postgres_derive;

extern crate chrono;
extern crate finito;
extern crate serde;
extern crate serde_json;
extern crate uuid;

mod error;
pub use error::{Result, Error};

use chrono::prelude::{DateTime, Utc};
use finito::FSM;
use postgres::GenericConnection;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::fmt;
use std::marker::PhantomData;
use uuid::Uuid;

/// This struct represents rows in the database table in which
/// machines (i.e. the current state of a Finito state machine) are
/// persisted.
#[derive(Debug, ToSql, FromSql)]
struct MachineT {
    /// ID of the persisted state machine.
    id: Uuid,

    /// Time at which the FSM was first created.
    created: DateTime<Utc>,

    /// Name of the type of FSM represented by this state.
    fsm: String,

    /// Current state of the FSM (TODO: Can the serialised FSM type be
    /// used?)
    state: Value,
}

/// This struct represents rows in the database table in which events
/// are persisted.
#[derive(Debug, ToSql, FromSql)]
struct EventT {
    /// ID of the persisted event.
    id: Uuid,

    /// Timestamp at which the event was stored.
    created: DateTime<Utc>,

    /// Name of the type of FSM that this state belongs to.
    fsm: String,

    /// ID of the state machine belonging to this event.
    fsm_id: Uuid,

    /// Serialised content of the event.
    event: Value,
}

/// This enum represents the possible statuses an action can be in.
#[derive(Debug, ToSql, FromSql)]
enum ActionStatus {
    /// The action was requested but has not run yet.
    Pending,

    /// The action completed successfully.
    Completed,

    /// The action failed to run. Information about the error will
    /// have been persisted in Postgres.
    Failed,
}

/// This struct represents rows in the database table in which actions
/// are persisted.
#[derive(Debug, ToSql, FromSql)]
struct ActionT {
    /// ID of the persisted event.
    id: Uuid,

    /// Timestamp at which the event was stored.
    created: DateTime<Utc>,

    /// Name of the type of FSM that this state belongs to.
    fsm: String,

    /// ID of the state machine belonging to this event.
    fsm_id: Uuid,

    /// ID of the event that resulted in this action.
    event_id: Uuid,

    /// Serialised content of the action.
    #[postgres(name = "content")] // renamed because 'action' is a keyword in PG
    action: Value,

    /// Current status of the action.
    status: ActionStatus,

    /// Serialised error representation, if an error occured during
    /// processing. TODO: Use some actual error type. Maybe failure
    /// has serialisation support?
    error: Option<Value>,
}

// The following functions implement the public interface of
// `finito-postgres`.

/// This type is used as a type-safe wrapper around the ID of a state
/// machine. It carries information about the FSM type and is intended
/// to add a layer of checking to prevent IDs from being mixed up.
#[derive(Clone)]
pub struct MachineId<S: FSM> {
    uuid: Uuid,
    phantom: PhantomData<S>,
}

/// Custom debug implementation to format machine IDs using the name
/// of the FSM and their UUID.
impl <S: FSM> fmt::Debug for MachineId<S> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}:{}", S::FSM_NAME, self.uuid.hyphenated())
    }
}

impl <S: FSM> MachineId<S> {
    /// Convert a UUID into a strongly typed machine ID.
    pub fn from_uuid(uuid: Uuid) -> Self {
        MachineId {
            uuid,
            phantom: PhantomData,
        }
    }

    /// Return the UUID contained in a machine ID.
    pub fn to_uuid(&self) -> Uuid {
        self.uuid
    }
}

/// Insert a single state-machine into the database and return its
/// newly allocated, random UUID.
pub fn insert_machine<C, S>(conn: &C, initial: S) -> Result<MachineId<S>> where
    C: GenericConnection,
    S: FSM + Serialize {
    let query = r#"
      INSERT INTO machines (id, fsm, state)
      VALUES ($1, $2, $3)
    "#;

    let id = Uuid::new_v4();
    let fsm = S::FSM_NAME.to_string();
    let state = serde_json::to_value(initial).expect("TODO");

    conn.execute(query, &[&id, &fsm, &state]).expect("TODO");

    return Ok(MachineId::from_uuid(id));
}

/// Insert a single event into the database and return its UUID.
fn insert_event<C, S>(conn: &C,
                      fsm_id: &MachineId<S>,
                      event: &S::Event) -> Result<Uuid>
where
    C: GenericConnection,
    S: FSM,
    S::Event: Serialize {
    let query = r#"
      INSERT INTO events (id, fsm, fsm_id, event)
      VALUES ($1, $2, $3, $4)
    "#;

    let id = Uuid::new_v4();
    let fsm = S::FSM_NAME.to_string();
    let event_value = serde_json::to_value(event).expect("TODO");

    conn.execute(query, &[&id, &fsm, &fsm_id.to_uuid(), &event_value]).expect("TODO");
    return Ok(id)
}

/// Insert a single action into the database and return its UUID.
fn insert_action<C, S>(conn: &C,
                       fsm_id: &MachineId<S>,
                       event_id: Uuid,
                       action: &S::Action) -> Result<Uuid> where
    C: GenericConnection,
    S: FSM,
    S::Action: Serialize {
    let query = r#"
      INSERT INTO actions (id, fsm, fsm_id, event_id, action, status)
      VALUES ($1, $2, $3, $4, $5, $6)
    "#;

    let id = Uuid::new_v4();
    let fsm = S::FSM_NAME.to_string();
    let action_value = serde_json::to_value(action).expect("TODO");

    conn.execute(query, &[&id, &fsm, &fsm_id.to_uuid(), &event_id,
                          &action_value, &ActionStatus::Pending]).expect("TODO");
    return Ok(id)
}

/// Update the state of a specified machine.
fn update_state<C, S>(conn: &C,
                      fsm_id: &MachineId<S>,
                      state: &S) -> Result<()> where
    C: GenericConnection,
    S: FSM + Serialize {
    let query = r#"
      UPDATE machines SET state = $1 WHERE id = $2
    "#;

    let state_value = serde_json::to_value(state).expect("TODO");
    let res_count = conn.execute(query, &[&state_value, &fsm_id.to_uuid()])
        .expect("TODO");

    if res_count != 1 {
        // TODO: not found error!
        unimplemented!()
    } else {
        Ok(())
    }
}

/// Retrieve the current state of a state machine from the database,
/// optionally locking the machine state for the duration of some
/// enclosing transaction.
pub fn get_machine<C, S>(conn: &C,
                         id: &MachineId<S>,
                         for_update: bool) -> Result<S> where
    C: GenericConnection,
    S: FSM + DeserializeOwned {
    let query = r#"
      SELECT (id, created, fsm, state) FROM machines WHERE id = $1
    "#;

    // If the machine is being fetched in the context of a
    // transaction, with the intention to update it, the relevant
    // clause needs to be appended:
    let query = match for_update {
        false => query.to_string(),
        true  => format!("{} FOR UPDATE", query),
    };

    let rows = conn.query(&query, &[&id.to_uuid()]).expect("TODO");
    let mut machines = rows.into_iter().map(|row| MachineT {
        id: row.get(0),
        created: row.get(1),
        fsm: row.get(2),
        state: row.get(3),
    });

    if let Some(machine) = machines.next() {
        Ok(serde_json::from_value(machine.state).expect("TODO"))
    } else {
        // TODO: return appropriate not found error
        Err(Error::SomeError)
    }
}

/// Advance a persisted state machine by applying an event, and
/// storing the event as well as all resulting actions.
///
/// This function holds a database-lock on the state's row while
/// advancing the machine.
///
/// **Note**: This function returns the new state of the machine
/// immediately after applying the event, however this does not
/// necessarily equate to the state of the machine after all related
/// processing is finished as running actions may result in additional
/// transitions.
pub fn advance<C, S>(conn: &C,
                     id: &MachineId<S>,
                     event: S::Event) -> Result<S> where
    C: GenericConnection,
    S: FSM + Serialize + DeserializeOwned,
    S::Event: Serialize,
    S::Action: Serialize {
    let tx = conn.transaction().expect("TODO");
    let state = get_machine(&tx, id, true).expect("TODO");

    // Advancing the FSM consumes the event, so it is persisted first:
    let event_id = insert_event(&tx, id, &event).expect("TODO");

    // Core advancing logic is run:
    let (new_state, actions) = finito::advance(state, event);

    // Resulting actions are persisted (TODO: and interpreted)
    for action in actions {
        insert_action(&tx, id, event_id, &action).expect("TODO");
    }

    // And finally the state is updated:
    update_state(&tx, id, &new_state).expect("TODO");
    Ok(new_state)
}