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
use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use crate::activation_id::ActivationId;

#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EcbId {
    // FIXME we should extract the scheduler's HostIdx type into the common definitions
    host_idx: u64,
    activation_id: ActivationId,
}

impl EcbId {
    pub fn new(host_idx: u64, activation_id: ActivationId) -> Self {
        Self {
            host_idx,
            activation_id,
        }
    }

    pub fn get_host_idx(&self) -> u64 {
        self.host_idx
    }

    pub fn get_activation_id(&self) -> ActivationId {
        self.activation_id
    }

    pub fn get_root_id(&self) -> Self {
        Self {
            host_idx: self.host_idx,
            activation_id: ActivationId::new_for_txn(self.activation_id.txn_id()),
        }
    }
}

impl fmt::Debug for EcbId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!("{}:{}", self.host_idx, self.activation_id))
    }
}

impl fmt::Display for EcbId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!("{}:{}", self.host_idx, self.activation_id))
    }
}

impl FromStr for EcbId {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.split_once(':') {
            Some((host_idx_str, activation_id_str)) => Ok(Self {
                host_idx: host_idx_str
                    .parse()
                    .expect("failed to parse host idx in ecb id"),
                activation_id: ActivationId::from_str(activation_id_str)
                    .expect("failed to parse activation id"),
            }),
            None => Err(format!("invalid string passed for ecb id: {}", s)),
        }
    }
}