qualia_core_db/bundle/stream/runtime/jobs/
ticket.rs1use super::super::{CompletionKey, TransferTicket};
3use crate::net::qdnf::{errors::QdnfError, types::Generation};
4
5pub const MAX_STORAGE_JOBS: usize = 30;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub(super) enum JobState {
10 Vacant,
11 Submitted,
12 Cancelling,
13 Failed,
14}
15
16#[derive(Clone, Copy)]
17pub(super) struct JobSlot {
18 pub generation: Generation,
19 pub state: JobState,
20 pub key: CompletionKey,
21 pub reserved_bytes: u64,
22}
23
24impl JobSlot {
25 pub const fn empty() -> Self {
26 Self {
27 generation: Generation::ZERO,
28 state: JobState::Vacant,
29 key: CompletionKey {
30 owner: 0,
31 slot: 0,
32 generation: Generation::ZERO,
33 operation: crate::net::qdnf::types::OperationId::ZERO,
34 },
35 reserved_bytes: 0,
36 }
37 }
38 pub fn clear(&mut self) {
39 let generation = self.generation;
40 *self = Self::empty();
41 self.generation = generation;
42 }
43}
44
45#[must_use = "retain the job ticket through cancellation and actual I/O completion"]
48#[derive(Debug)]
49pub struct StorageJobTicket {
50 pub(super) key: CompletionKey,
51 pub(super) job_generation: Generation,
52 pub(super) job_slot: u8,
53 pub(super) reserved_bytes: u64,
54 pub(super) spent: bool,
55}
56
57impl StorageJobTicket {
58 pub const fn completion_key(&self) -> CompletionKey {
59 self.key
60 }
61 pub const fn reserved_bytes(&self) -> u64 {
62 self.reserved_bytes
63 }
64 pub(super) fn matches_transfer(&self, ticket: &TransferTicket) -> Result<(), QdnfError> {
65 if self.spent {
66 return Err(QdnfError::DoubleRelease);
67 }
68 if self.key != ticket.completion_key() {
69 return Err(QdnfError::StaleGeneration);
70 }
71 Ok(())
72 }
73}