ella_engine/registry/
transactions.rs

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
use arrow_schema::SchemaRef;

use crate::{
    table::{
        info::{TableInfo, TopicInfo, ViewInfo},
        topic::ShardInfo,
    },
    Path,
};

use super::id::*;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct PartitionId {
    pub key: String,
    pub value: String,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum PartitionValue {
    Int(i64),
    String(String),
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CreateCatalog {
    pub uuid: TransactionId,
    pub id: CatalogId<'static>,
    pub path: Path,
}

impl CreateCatalog {
    pub fn new(id: CatalogId<'static>, root: &Path) -> Self {
        let path = root.join(id.as_ref());
        Self {
            uuid: TransactionId::new(),
            id,
            path,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CreateSchema {
    pub uuid: TransactionId,
    pub id: SchemaId<'static>,
    pub path: Path,
}

impl CreateSchema {
    pub fn new(id: SchemaId<'static>, root: &Path) -> Self {
        let path = root.join(id.catalog.as_ref()).join(id.schema.as_ref());
        Self {
            uuid: TransactionId::new(),
            id,
            path,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CreateTable {
    pub uuid: TransactionId,
    pub id: TableId<'static>,
    pub info: TableInfo,
}

impl CreateTable {
    pub fn topic(id: TableId<'static>, info: TopicInfo) -> Self {
        Self {
            uuid: TransactionId::new(),
            id,
            info: info.into(),
        }
    }

    pub fn view(id: TableId<'static>, info: ViewInfo) -> Self {
        Self {
            uuid: TransactionId::new(),
            id,
            info: info.into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TableKind {
    Topic(TopicInfo),
    View(ViewInfo),
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CreateShard {
    pub uuid: TransactionId,
    pub table: TableId<'static>,
    pub shard: ShardId,
    pub file_schema: SchemaRef,
    pub path: Path,
}

impl CreateShard {
    pub fn new(table: TableId<'static>, file_schema: SchemaRef, root: &Path) -> Self {
        let shard = ShardId::new();
        let path = shard.encode_path(root, "parquet");
        Self {
            uuid: TransactionId::new(),
            table,
            shard,
            file_schema,
            path,
        }
    }
}

impl From<CreateShard> for ShardInfo {
    fn from(tsn: CreateShard) -> Self {
        ShardInfo::new(tsn.shard, tsn.table, tsn.file_schema, tsn.path)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CloseShard {
    pub uuid: TransactionId,
    pub table: TableId<'static>,
    pub shard: ShardId,
    pub rows: usize,
}

impl CloseShard {
    pub fn new(table: TableId<'static>, shard: ShardId, rows: usize) -> Self {
        Self {
            uuid: TransactionId::new(),
            table,
            shard,
            rows,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DeleteShard {
    pub uuid: TransactionId,
    pub table: TableId<'static>,
    pub shard: ShardId,
}

impl DeleteShard {
    pub fn new(table: TableId<'static>, shard: ShardId) -> Self {
        Self {
            uuid: TransactionId::new(),
            table,
            shard,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CompactShards {
    pub uuid: TransactionId,
    pub table: TableId<'static>,
    pub src: Vec<ShardId>,
    pub dst: ShardId,
    pub file_schema: SchemaRef,
    pub path: Path,
}

impl CompactShards {
    pub fn new(
        table: TableId<'static>,
        src: Vec<ShardId>,
        file_schema: SchemaRef,
        root: &Path,
    ) -> Self {
        let dst = ShardId::generate_from(src.first().expect("cannot compact empty shard list"));
        let path = dst.encode_path(root, "parquet");
        Self {
            uuid: TransactionId::new(),
            table,
            src,
            dst,
            file_schema,
            path,
        }
    }
}

impl From<CompactShards> for ShardInfo {
    fn from(tsn: CompactShards) -> Self {
        ShardInfo::new(tsn.dst, tsn.table, tsn.file_schema, tsn.path)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DropTable {
    pub uuid: TransactionId,
    pub id: TableId<'static>,
}

impl DropTable {
    pub fn new(id: TableId<'static>) -> Self {
        Self {
            uuid: TransactionId::new(),
            id,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DropSchema {
    pub uuid: TransactionId,
    pub id: SchemaId<'static>,
}

impl DropSchema {
    pub fn new(id: SchemaId<'static>) -> Self {
        Self {
            uuid: TransactionId::new(),
            id,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DropCatalog {
    pub uuid: TransactionId,
    pub id: CatalogId<'static>,
}

impl DropCatalog {
    pub fn new(id: CatalogId<'static>) -> Self {
        Self {
            uuid: TransactionId::new(),
            id,
        }
    }
}

#[derive(
    Debug,
    Clone,
    PartialEq,
    Eq,
    serde::Serialize,
    serde::Deserialize,
    derive_more::From,
    strum::Display,
)]
#[strum(serialize_all = "snake_case")]
pub enum Transaction {
    CreateCatalog(CreateCatalog),
    CreateSchema(CreateSchema),
    CreateTable(CreateTable),
    CreateShard(CreateShard),
    CloseShard(CloseShard),
    DeleteShard(DeleteShard),
    CompactShards(CompactShards),
    DropTable(DropTable),
    DropSchema(DropSchema),
    DropCatalog(DropCatalog),
}

impl Transaction {
    pub fn uuid(&self) -> TransactionId {
        use Transaction::*;
        match self {
            CreateCatalog(t) => t.uuid,
            CreateSchema(t) => t.uuid,
            CreateTable(t) => t.uuid,
            CreateShard(t) => t.uuid,
            CloseShard(t) => t.uuid,
            DeleteShard(t) => t.uuid,
            CompactShards(t) => t.uuid,
            DropTable(t) => t.uuid,
            DropSchema(t) => t.uuid,
            DropCatalog(t) => t.uuid,
        }
    }

    pub fn kind(&self) -> String {
        self.to_string()
    }
}