Reduce mem size of SQL Model by converting to an enum

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-06 17:02:44 +01:00
parent 1e49a00840
commit 4d072a7f6c
3 changed files with 51 additions and 75 deletions

View file

@ -70,24 +70,26 @@ impl Model {
chn: &Sender<(Option<Thing>, Value)>,
) -> Result<(), Error> {
if ctx.is_ok() {
if let Some(c) = self.count {
for _ in 0..c {
Thing {
tb: self.table.to_string(),
id: Id::rand(),
match self {
Model::Count(tb, c) => {
for _ in 0..c {
Thing {
tb: tb.to_string(),
id: Id::rand(),
}
.process(ctx, opt, stm, txn, chn)
.await?;
}
.process(ctx, opt, stm, txn, chn)
.await?;
}
}
if let Some(r) = self.range {
for x in r.0..=r.1 {
Thing {
tb: self.table.to_string(),
id: Id::from(x),
Model::Range(tb, b, e) => {
for x in b..=e {
Thing {
tb: tb.to_string(),
id: Id::from(x),
}
.process(ctx, opt, stm, txn, chn)
.await?;
}
.process(ctx, opt, stm, txn, chn)
.await?;
}
}
}

View file

@ -69,24 +69,26 @@ impl Model {
ite: &mut Iterator,
) -> Result<(), Error> {
if ctx.is_ok() {
if let Some(c) = self.count {
for _ in 0..c {
Thing {
tb: self.table.to_string(),
id: Id::rand(),
match self {
Model::Count(tb, c) => {
for _ in 0..c {
Thing {
tb: tb.to_string(),
id: Id::rand(),
}
.iterate(ctx, opt, txn, ite)
.await?;
}
.iterate(ctx, opt, txn, ite)
.await?;
}
}
if let Some(r) = self.range {
for x in r.0..=r.1 {
Thing {
tb: self.table.to_string(),
id: Id::from(x),
Model::Range(tb, b, e) => {
for x in b..=e {
Thing {
tb: tb.to_string(),
id: Id::from(x),
}
.iterate(ctx, opt, txn, ite)
.await?;
}
.iterate(ctx, opt, txn, ite)
.await?;
}
}
}

View file

@ -8,24 +8,24 @@ use nom::character::complete::char;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Model {
pub table: String,
pub count: Option<u64>,
pub range: Option<(u64, u64)>,
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum Model {
Count(String, u64),
Range(String, u64, u64),
}
impl fmt::Display for Model {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref c) = self.count {
let t = escape(&self.table, &val_char, "`");
write!(f, "|{}:{}|", t, c)?;
match self {
Model::Count(tb, c) => {
let t = escape(tb, &val_char, "`");
write!(f, "|{}:{}|", t, c)
}
Model::Range(tb, b, e) => {
let t = escape(tb, &val_char, "`");
write!(f, "|{}:{}..{}|", t, b, e)
}
}
if let Some((ref b, ref e)) = self.range {
let t = escape(&self.table, &val_char, "`");
write!(f, "|{}:{}..{}|", t, b, e)?;
}
Ok(())
}
}
@ -39,14 +39,7 @@ fn model_count(i: &str) -> IResult<&str, Model> {
let (i, _) = char(':')(i)?;
let (i, c) = take_u64(i)?;
let (i, _) = char('|')(i)?;
Ok((
i,
Model {
table: t,
count: Some(c),
range: None,
},
))
Ok((i, Model::Count(t, c)))
}
fn model_range(i: &str) -> IResult<&str, Model> {
@ -58,14 +51,7 @@ fn model_range(i: &str) -> IResult<&str, Model> {
let (i, _) = char('.')(i)?;
let (i, e) = take_u64(i)?;
let (i, _) = char('|')(i)?;
Ok((
i,
Model {
table: t,
count: None,
range: Some((b, e)),
},
))
Ok((i, Model::Range(t, b, e)))
}
#[cfg(test)]
@ -80,14 +66,7 @@ mod tests {
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("|test:1000|", format!("{}", out));
assert_eq!(
out,
Model {
table: String::from("test"),
count: Some(1000),
range: None,
}
);
assert_eq!(out, Model::Count(String::from("test"), 1000));
}
#[test]
@ -97,13 +76,6 @@ mod tests {
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("|test:1..1000|", format!("{}", out));
assert_eq!(
out,
Model {
table: String::from("test"),
count: None,
range: Some((1, 1000)),
}
);
assert_eq!(out, Model::Range(String::from("test"), 1, 1000));
}
}