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)>, chn: &Sender<(Option<Thing>, Value)>,
) -> Result<(), Error> { ) -> Result<(), Error> {
if ctx.is_ok() { if ctx.is_ok() {
if let Some(c) = self.count { match self {
for _ in 0..c { Model::Count(tb, c) => {
Thing { for _ in 0..c {
tb: self.table.to_string(), Thing {
id: Id::rand(), tb: tb.to_string(),
id: Id::rand(),
}
.process(ctx, opt, stm, txn, chn)
.await?;
} }
.process(ctx, opt, stm, txn, chn)
.await?;
} }
} Model::Range(tb, b, e) => {
if let Some(r) = self.range { for x in b..=e {
for x in r.0..=r.1 { Thing {
Thing { tb: tb.to_string(),
tb: self.table.to_string(), id: Id::from(x),
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, ite: &mut Iterator,
) -> Result<(), Error> { ) -> Result<(), Error> {
if ctx.is_ok() { if ctx.is_ok() {
if let Some(c) = self.count { match self {
for _ in 0..c { Model::Count(tb, c) => {
Thing { for _ in 0..c {
tb: self.table.to_string(), Thing {
id: Id::rand(), tb: tb.to_string(),
id: Id::rand(),
}
.iterate(ctx, opt, txn, ite)
.await?;
} }
.iterate(ctx, opt, txn, ite)
.await?;
} }
} Model::Range(tb, b, e) => {
if let Some(r) = self.range { for x in b..=e {
for x in r.0..=r.1 { Thing {
Thing { tb: tb.to_string(),
tb: self.table.to_string(), id: Id::from(x),
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 serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Model { pub enum Model {
pub table: String, Count(String, u64),
pub count: Option<u64>, Range(String, u64, u64),
pub range: Option<(u64, u64)>,
} }
impl fmt::Display for Model { impl fmt::Display for Model {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref c) = self.count { match self {
let t = escape(&self.table, &val_char, "`"); Model::Count(tb, c) => {
write!(f, "|{}:{}|", t, 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, _) = char(':')(i)?;
let (i, c) = take_u64(i)?; let (i, c) = take_u64(i)?;
let (i, _) = char('|')(i)?; let (i, _) = char('|')(i)?;
Ok(( Ok((i, Model::Count(t, c)))
i,
Model {
table: t,
count: Some(c),
range: None,
},
))
} }
fn model_range(i: &str) -> IResult<&str, Model> { 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, _) = char('.')(i)?;
let (i, e) = take_u64(i)?; let (i, e) = take_u64(i)?;
let (i, _) = char('|')(i)?; let (i, _) = char('|')(i)?;
Ok(( Ok((i, Model::Range(t, b, e)))
i,
Model {
table: t,
count: None,
range: Some((b, e)),
},
))
} }
#[cfg(test)] #[cfg(test)]
@ -80,14 +66,7 @@ mod tests {
assert!(res.is_ok()); assert!(res.is_ok());
let out = res.unwrap().1; let out = res.unwrap().1;
assert_eq!("|test:1000|", format!("{}", out)); assert_eq!("|test:1000|", format!("{}", out));
assert_eq!( assert_eq!(out, Model::Count(String::from("test"), 1000));
out,
Model {
table: String::from("test"),
count: Some(1000),
range: None,
}
);
} }
#[test] #[test]
@ -97,13 +76,6 @@ mod tests {
assert!(res.is_ok()); assert!(res.is_ok());
let out = res.unwrap().1; let out = res.unwrap().1;
assert_eq!("|test:1..1000|", format!("{}", out)); assert_eq!("|test:1..1000|", format!("{}", out));
assert_eq!( assert_eq!(out, Model::Range(String::from("test"), 1, 1000));
out,
Model {
table: String::from("test"),
count: None,
range: Some((1, 1000)),
}
);
} }
} }