Skip to content

Commit

Permalink
feat: add to_string impl for table options
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Jul 2, 2024
1 parent bb1748e commit 12157b4
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 76 deletions.
3 changes: 2 additions & 1 deletion src/catalog/src/information_schema/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,11 @@ impl InformationSchemaTablesBuilder {
self.update_time.push(None);
self.check_time.push(None);

// use mariadb default table version number here
self.version.push(Some(11));
self.table_comment.push(table_info.desc.as_deref());
self.create_options
.push(Some(format!("{:?}", table_info.meta.options).as_ref()));
.push(Some(table_info.meta.options.to_string().as_ref()));
self.create_time
.push(Some(table_info.meta.created_on.timestamp_millis().into()));

Expand Down
45 changes: 45 additions & 0 deletions src/table/src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! Table and TableEngine requests

use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use std::time::Duration;

Expand Down Expand Up @@ -128,6 +129,25 @@ impl TableOptions {
}
}

impl fmt::Display for TableOptions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut key_vals = vec![];
if let Some(size) = self.write_buffer_size {
key_vals.push(format!("{}={}", WRITE_BUFFER_SIZE_KEY, size));
}

if let Some(ttl) = self.ttl {
key_vals.push(format!("{}={}", TTL_KEY, humantime::Duration::from(ttl)));
}

for (k, v) in &self.extra_options {
key_vals.push(format!("{}={}", k, v));
}

write!(f, "{}", key_vals.join(" "))
}
}

impl From<&TableOptions> for HashMap<String, String> {
fn from(opts: &TableOptions) -> Self {
let mut res = HashMap::with_capacity(2 + opts.extra_options.len());
Expand Down Expand Up @@ -345,4 +365,29 @@ mod tests {
let serialized = TableOptions::try_from_iter(&serialized_map).unwrap();
assert_eq!(options, serialized);
}

#[test]
fn test_table_options_to_string() {
let options = TableOptions {
write_buffer_size: Some(ReadableSize::mb(128)),
ttl: Some(Duration::from_secs(1000)),
extra_options: HashMap::new(),
};

assert_eq!(
"write_buffer_size=128.0MiB ttl=16m 40s",
options.to_string()
);

let options = TableOptions {
write_buffer_size: Some(ReadableSize::mb(128)),
ttl: Some(Duration::from_secs(1000)),
extra_options: HashMap::from([("a".to_string(), "A".to_string())]),
};

assert_eq!(
"write_buffer_size=128.0MiB ttl=16m 40s a=A",
options.to_string()
);
}
}
Loading

0 comments on commit 12157b4

Please sign in to comment.