-
Notifications
You must be signed in to change notification settings - Fork 753
/
dictionary.rs
220 lines (195 loc) · 6.89 KB
/
dictionary.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
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use core::fmt;
use std::collections::BTreeMap;
use std::fmt::Display;
use std::fmt::Formatter;
use std::sync::Arc;
use chrono::DateTime;
use chrono::Utc;
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use databend_common_expression::TableSchema;
use super::dictionary_name_ident::DictionaryNameIdent;
use crate::tenant::Tenant;
use crate::tenant::ToTenant;
use crate::KeyWithTenant;
/// Represents the metadata of a dictionary within the system.
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct DictionaryMeta {
/// The source of the dictionary, which specifies where the dictionary data comes from, like `MySQL`.
pub source: String,
/// Specify the configuration related to the data source in the form of key-value pairs.
/// For example, `host='localhost' user='root' password='1234'`
pub options: BTreeMap<String, String>,
/// Schema refers to an external table that corresponds to the dictionary.
/// This is typically used to understand the layout and types of data within the dictionary.
pub schema: Arc<TableSchema>,
/// A set of key-value pairs is used to represent the annotations for each field in the dictionary, the key being column_id.
/// For example, if we have `id, address` fields, then field_comments could be `[ '1=student's number','2=home address']`
pub field_comments: BTreeMap<u32, String>,
/// A list of primary column IDs.
/// For example, vec![1, 2] indicating the first and second columns are the primary keys.
pub primary_column_ids: Vec<u32>,
/// A general comment string that can be used to provide additional notes or information about the dictionary.
pub comment: String,
/// The timestamp indicating when the dictionary was created, in Coordinated Universal Time (UTC).
pub created_on: DateTime<Utc>,
/// if used in CreateDictionaryReq,
/// `updated_on` MUST set to None.
pub updated_on: Option<DateTime<Utc>>,
}
impl Display for DictionaryMeta {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"Source: {}={:?}, Schema: {:?}, Primary_Column_Id: {:?}, CreatedOn: {:?}",
self.source, self.options, self.schema, self.primary_column_ids, self.created_on
)
}
}
impl Default for DictionaryMeta {
fn default() -> Self {
DictionaryMeta {
source: "".to_string(),
options: BTreeMap::new(),
schema: Arc::new(TableSchema::empty()),
primary_column_ids: Vec::new(),
created_on: Utc::now(),
updated_on: None,
comment: "".to_string(),
field_comments: BTreeMap::new(),
}
}
}
impl DictionaryMeta {
pub fn build_sql_connection_url(&self) -> Result<String> {
let username = self
.options
.get("username")
.ok_or_else(|| ErrorCode::BadArguments("Miss option `username`"))?;
let password = self
.options
.get("password")
.ok_or_else(|| ErrorCode::BadArguments("Miss option `password`"))?;
let host = self
.options
.get("host")
.ok_or_else(|| ErrorCode::BadArguments("Miss option `host`"))?;
let port = self
.options
.get("port")
.ok_or_else(|| ErrorCode::BadArguments("Miss option `port`"))?;
let db = self
.options
.get("db")
.ok_or_else(|| ErrorCode::BadArguments("Miss option `db`"))?;
Ok(format!(
"mysql://{}:{}@{}:{}/{}",
username, password, host, port, db
))
}
pub fn build_redis_connection_url(&self) -> Result<String> {
let host = self
.options
.get("host")
.ok_or_else(|| ErrorCode::BadArguments("Miss option `host`"))?;
let port = self
.options
.get("port")
.ok_or_else(|| ErrorCode::BadArguments("Miss option `port`"))?;
Ok(format!("tcp://{}:{}", host, port))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CreateDictionaryReq {
pub dictionary_ident: DictionaryNameIdent,
pub dictionary_meta: DictionaryMeta,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct CreateDictionaryReply {
pub dictionary_id: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GetDictionaryReply {
pub dictionary_id: u64,
pub dictionary_meta: DictionaryMeta,
/// Any change to a dictionary causes the seq to increment
pub dictionary_meta_seq: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListDictionaryReq {
pub tenant: Tenant,
pub db_id: u64,
}
impl ListDictionaryReq {
pub fn new(tenant: impl ToTenant, db_id: u64) -> ListDictionaryReq {
ListDictionaryReq {
tenant: tenant.to_tenant(),
db_id,
}
}
pub fn db_id(&self) -> u64 {
self.db_id
}
pub fn tenant(&self) -> String {
self.tenant.tenant_name().to_string()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpdateDictionaryReq {
pub dictionary_meta: DictionaryMeta,
pub dictionary_ident: DictionaryNameIdent,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UpdateDictionaryReply {
pub dictionary_id: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RenameDictionaryReq {
pub name_ident: DictionaryNameIdent,
pub new_name_ident: DictionaryNameIdent,
}
impl RenameDictionaryReq {
pub fn tenant(&self) -> &Tenant {
self.name_ident.tenant()
}
pub fn db_id(&self) -> u64 {
self.name_ident.db_id()
}
pub fn dictionary_name(&self) -> String {
self.name_ident.dict_name().clone()
}
pub fn new_db_id(&self) -> u64 {
self.new_name_ident.db_id()
}
pub fn new_dictionary_name(&self) -> String {
self.new_name_ident.dict_name().clone()
}
}
impl Display for RenameDictionaryReq {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"rename_dictionary:{}/{}-{}=>{}-{}",
self.tenant().tenant_name(),
self.db_id(),
self.dictionary_name(),
self.new_db_id(),
self.new_dictionary_name(),
)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RenameDictionaryReply {}