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
use std::fmt;
use std::str::FromStr;
use anyhow::{anyhow, Error};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Id {
Local(LocalId),
Global(GlobalId),
LocalBareSource,
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Id::Local(id) => id.fmt(f),
Id::Global(id) => id.fmt(f),
Id::LocalBareSource => write!(f, "(bare source for this source)"),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct LocalId(u64);
impl LocalId {
pub fn new(v: u64) -> LocalId {
LocalId(v)
}
}
impl fmt::Display for LocalId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "l{}", self.0)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum GlobalId {
System(u64),
User(u64),
Transient(u64),
Explain,
}
impl GlobalId {
pub fn is_system(&self) -> bool {
matches!(self, GlobalId::System(_))
}
pub fn is_user(&self) -> bool {
matches!(self, GlobalId::User(_))
}
pub fn is_transient(&self) -> bool {
matches!(self, GlobalId::Transient(_))
}
}
impl FromStr for GlobalId {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() < 2 {
return Err(anyhow!("couldn't parse id {}", s));
}
let val: u64 = s[1..].parse()?;
match s.chars().next().unwrap() {
's' => Ok(GlobalId::System(val)),
'u' => Ok(GlobalId::User(val)),
't' => Ok(GlobalId::Transient(val)),
_ => Err(anyhow!("couldn't parse id {}", s)),
}
}
}
impl fmt::Display for GlobalId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GlobalId::System(id) => write!(f, "s{}", id),
GlobalId::User(id) => write!(f, "u{}", id),
GlobalId::Transient(id) => write!(f, "t{}", id),
GlobalId::Explain => write!(f, "Explained Query"),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct SourceInstanceId {
pub source_id: GlobalId,
pub dataflow_id: usize,
}
impl fmt::Display for SourceInstanceId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/{}", self.source_id, self.dataflow_id)
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum PartitionId {
Kafka(i32),
None,
}
impl fmt::Display for PartitionId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PartitionId::Kafka(id) => write!(f, "{}", id),
PartitionId::None => write!(f, "none"),
}
}
}
impl From<&PartitionId> for Option<String> {
fn from(pid: &PartitionId) -> Option<String> {
match pid {
PartitionId::None => None,
_ => Some(pid.to_string()),
}
}
}
impl FromStr for PartitionId {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(PartitionId::None),
s => {
let val: i32 = s.parse()?;
Ok(PartitionId::Kafka(val))
}
}
}
}