mqdb-core 0.5.1

Core types, storage, schema, and protocol for MQDB
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2025-2026 LabOverWire. All rights reserved.
// SPDX-License-Identifier: AGPL-3.0-only

use crate::error::Result;
use crate::events::ChangeEvent;
use crate::storage::{BatchWriter, Storage};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

const OUTBOX_PREFIX: &[u8] = b"_outbox/";
const DEAD_LETTER_PREFIX: &[u8] = b"_dead_letter/";

#[derive(Debug, Clone, Serialize, Deserialize)]
struct StoredOutboxEntry {
    events: Vec<ChangeEvent>,
    retry_count: u32,
    created_at: u64,
    #[serde(default)]
    dispatched_count: usize,
}

pub struct Outbox {
    storage: Arc<Storage>,
}

impl Outbox {
    #[allow(clippy::must_use_candidate)]
    pub fn new(storage: Arc<Storage>) -> Self {
        Self { storage }
    }

    pub fn enqueue_event(&self, batch: &mut BatchWriter, operation_id: &str, event: &ChangeEvent) {
        self.enqueue_events(batch, operation_id, std::slice::from_ref(event));
    }

    pub fn enqueue_events(
        &self,
        batch: &mut BatchWriter,
        operation_id: &str,
        events: &[ChangeEvent],
    ) {
        let key = format!("_outbox/{operation_id}");
        let stored = StoredOutboxEntry {
            events: events.to_vec(),
            retry_count: 0,
            created_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0),
            dispatched_count: 0,
        };
        let value = serde_json::to_vec(&stored).unwrap_or_default();
        batch.insert(key.into_bytes(), value);
    }

    /// # Errors
    /// Returns an error if reading from storage fails.
    pub fn pending_events(&self) -> Result<Vec<OutboxEntry>> {
        self.scan_entries(OUTBOX_PREFIX, "_outbox/")
    }

    fn scan_entries(&self, prefix: &[u8], strip_prefix: &str) -> Result<Vec<OutboxEntry>> {
        let items = self.storage.prefix_scan(prefix)?;
        let mut entries = Vec::new();

        for (key, value) in items {
            let key_str = String::from_utf8_lossy(&key);
            let operation_id = key_str
                .strip_prefix(strip_prefix)
                .unwrap_or(&key_str)
                .to_string();

            if let Ok(stored) = serde_json::from_slice::<StoredOutboxEntry>(&value) {
                entries.push(OutboxEntry {
                    operation_id,
                    events: stored.events,
                    retry_count: stored.retry_count,
                    created_at: stored.created_at,
                    dispatched_count: stored.dispatched_count,
                });
            } else if let Ok(event) = serde_json::from_slice::<ChangeEvent>(&value) {
                entries.push(OutboxEntry {
                    operation_id,
                    events: vec![event],
                    retry_count: 0,
                    created_at: 0,
                    dispatched_count: 0,
                });
            } else if let Ok(events) = serde_json::from_slice::<Vec<ChangeEvent>>(&value) {
                entries.push(OutboxEntry {
                    operation_id,
                    events,
                    retry_count: 0,
                    created_at: 0,
                    dispatched_count: 0,
                });
            }
        }

        Ok(entries)
    }

    /// # Errors
    /// Returns an error if removing from storage fails.
    pub fn mark_delivered(&self, operation_id: &str) -> Result<()> {
        let key = format!("_outbox/{operation_id}");
        self.storage.remove(key.as_bytes())
    }

    /// # Errors
    /// Returns an error if reading from storage fails.
    pub fn pending_count(&self) -> Result<usize> {
        let items = self.storage.prefix_scan(OUTBOX_PREFIX)?;
        Ok(items.len())
    }

    /// # Errors
    /// Returns an error if reading or writing to storage fails.
    pub fn increment_retry(&self, operation_id: &str) -> Result<()> {
        let key = format!("_outbox/{operation_id}");
        if let Some(value) = self.storage.get(key.as_bytes())?
            && let Ok(mut stored) = serde_json::from_slice::<StoredOutboxEntry>(&value)
        {
            stored.retry_count += 1;
            let new_value = serde_json::to_vec(&stored).unwrap_or_default();
            self.storage.insert(key.as_bytes(), &new_value)?;
        }
        Ok(())
    }

    /// # Errors
    /// Returns an error if reading or writing to storage fails.
    pub fn update_dispatched_count(&self, operation_id: &str, count: usize) -> Result<()> {
        let key = format!("_outbox/{operation_id}");
        if let Some(value) = self.storage.get(key.as_bytes())?
            && let Ok(mut stored) = serde_json::from_slice::<StoredOutboxEntry>(&value)
        {
            stored.dispatched_count = count;
            stored.retry_count += 1;
            let new_value = serde_json::to_vec(&stored).unwrap_or_default();
            self.storage.insert(key.as_bytes(), &new_value)?;
        }
        Ok(())
    }

    /// # Errors
    /// Returns an error if reading or writing to storage fails.
    pub fn move_to_dead_letter(&self, operation_id: &str) -> Result<()> {
        let outbox_key = format!("_outbox/{operation_id}");
        if let Some(value) = self.storage.get(outbox_key.as_bytes())? {
            let dead_letter_key = format!("_dead_letter/{operation_id}");
            let mut batch = self.storage.batch();
            batch.remove(outbox_key.into_bytes());
            batch.insert(dead_letter_key.into_bytes(), value);
            batch.commit()?;
        }
        Ok(())
    }

    /// # Errors
    /// Returns an error if reading from storage fails.
    pub fn dead_letter_entries(&self) -> Result<Vec<OutboxEntry>> {
        self.scan_entries(DEAD_LETTER_PREFIX, "_dead_letter/")
    }

    /// # Errors
    /// Returns an error if reading from storage fails.
    pub fn dead_letter_count(&self) -> Result<usize> {
        let items = self.storage.prefix_scan(DEAD_LETTER_PREFIX)?;
        Ok(items.len())
    }

    /// # Errors
    /// Returns an error if removing from storage fails.
    pub fn remove_dead_letter(&self, operation_id: &str) -> Result<()> {
        let key = format!("_dead_letter/{operation_id}");
        self.storage.remove(key.as_bytes())
    }
}

#[derive(Debug, Clone)]
pub struct OutboxEntry {
    pub operation_id: String,
    pub events: Vec<ChangeEvent>,
    pub retry_count: u32,
    pub created_at: u64,
    pub dispatched_count: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::MemoryBackend;

    #[test]
    fn test_outbox_enqueue_single_event() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let event = ChangeEvent::create(
            "users".to_string(),
            "1".to_string(),
            serde_json::json!({"name": "Alice"}),
        );

        let mut batch = storage.batch();
        outbox.enqueue_event(&mut batch, "op-123", &event);
        batch.commit().unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].operation_id, "op-123");
        assert_eq!(pending[0].events.len(), 1);
        assert_eq!(pending[0].events[0].entity, "users");
    }

    #[test]
    fn test_outbox_enqueue_multiple_events() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let events = vec![
            ChangeEvent::delete("users".to_string(), "1".to_string(), serde_json::json!({})),
            ChangeEvent::delete("posts".to_string(), "10".to_string(), serde_json::json!({})),
            ChangeEvent::delete("posts".to_string(), "11".to_string(), serde_json::json!({})),
        ];

        let mut batch = storage.batch();
        outbox.enqueue_events(&mut batch, "cascade-456", &events);
        batch.commit().unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].events.len(), 3);
    }

    #[test]
    fn test_outbox_mark_delivered() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let event = ChangeEvent::create(
            "users".to_string(),
            "1".to_string(),
            serde_json::json!({"name": "Alice"}),
        );

        let mut batch = storage.batch();
        outbox.enqueue_event(&mut batch, "op-123", &event);
        batch.commit().unwrap();

        assert_eq!(outbox.pending_count().unwrap(), 1);

        outbox.mark_delivered("op-123").unwrap();

        assert_eq!(outbox.pending_count().unwrap(), 0);
    }

    #[test]
    fn test_outbox_atomic_with_data() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let event = ChangeEvent::create(
            "users".to_string(),
            "1".to_string(),
            serde_json::json!({"name": "Alice"}),
        );

        let mut batch = storage.batch();
        batch.insert(b"data/users/1".to_vec(), b"user data".to_vec());
        outbox.enqueue_event(&mut batch, "op-123", &event);
        batch.commit().unwrap();

        assert!(storage.get(b"data/users/1").unwrap().is_some());
        assert_eq!(outbox.pending_count().unwrap(), 1);
    }

    #[test]
    fn test_outbox_retry_count() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let event = ChangeEvent::create(
            "users".to_string(),
            "1".to_string(),
            serde_json::json!({"name": "Alice"}),
        );

        let mut batch = storage.batch();
        outbox.enqueue_event(&mut batch, "op-123", &event);
        batch.commit().unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending[0].retry_count, 0);

        outbox.increment_retry("op-123").unwrap();
        outbox.increment_retry("op-123").unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending[0].retry_count, 2);
    }

    #[test]
    fn test_outbox_dead_letter() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let event = ChangeEvent::create(
            "users".to_string(),
            "1".to_string(),
            serde_json::json!({"name": "Alice"}),
        );

        let mut batch = storage.batch();
        outbox.enqueue_event(&mut batch, "op-123", &event);
        batch.commit().unwrap();

        assert_eq!(outbox.pending_count().unwrap(), 1);
        assert_eq!(outbox.dead_letter_count().unwrap(), 0);

        outbox.move_to_dead_letter("op-123").unwrap();

        assert_eq!(outbox.pending_count().unwrap(), 0);
        assert_eq!(outbox.dead_letter_count().unwrap(), 1);

        let dead = outbox.dead_letter_entries().unwrap();
        assert_eq!(dead[0].operation_id, "op-123");
        assert_eq!(dead[0].events[0].entity, "users");
    }

    #[test]
    fn test_outbox_created_at() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let event = ChangeEvent::create(
            "users".to_string(),
            "1".to_string(),
            serde_json::json!({"name": "Alice"}),
        );

        let mut batch = storage.batch();
        outbox.enqueue_event(&mut batch, "op-123", &event);
        batch.commit().unwrap();

        let pending = outbox.pending_events().unwrap();
        assert!(pending[0].created_at > 0);
    }

    #[test]
    fn test_outbox_update_dispatched_count() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let events = vec![
            ChangeEvent::create("users".to_string(), "1".to_string(), serde_json::json!({})),
            ChangeEvent::create("users".to_string(), "2".to_string(), serde_json::json!({})),
            ChangeEvent::create("users".to_string(), "3".to_string(), serde_json::json!({})),
        ];

        let mut batch = storage.batch();
        outbox.enqueue_events(&mut batch, "op-partial", &events);
        batch.commit().unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending[0].dispatched_count, 0);
        assert_eq!(pending[0].retry_count, 0);

        outbox.update_dispatched_count("op-partial", 1).unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending[0].dispatched_count, 1);
        assert_eq!(pending[0].retry_count, 1);
        assert_eq!(pending[0].events.len(), 3);

        outbox.update_dispatched_count("op-partial", 2).unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending[0].dispatched_count, 2);
        assert_eq!(pending[0].retry_count, 2);
    }

    #[test]
    fn test_outbox_backward_compat_missing_dispatched_count() {
        let backend = Arc::new(MemoryBackend::new());
        let storage = Arc::new(Storage::with_backend(backend));
        let outbox = Outbox::new(Arc::clone(&storage));

        let legacy = serde_json::json!({
            "events": [{"sequence": 0, "entity": "users", "id": "1", "operation": "Create", "data": {}}],
            "retry_count": 3,
            "created_at": 1000
        });
        storage
            .insert(b"_outbox/legacy-op", &serde_json::to_vec(&legacy).unwrap())
            .unwrap();

        let pending = outbox.pending_events().unwrap();
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].dispatched_count, 0);
        assert_eq!(pending[0].retry_count, 3);
    }
}