|
| 1 | +use std::fmt::Write; |
| 2 | +use std::io; |
| 3 | + |
| 4 | +use futures_util::{stream::FuturesUnordered, TryFutureExt, TryStreamExt}; |
| 5 | +use nanorand::{Rng, WyRand}; |
| 6 | +use tokio_postgres::{connect, types::ToSql, Client, NoTls, Statement}; |
| 7 | +use viz::{Error, IntoResponse, Response, StatusCode}; |
| 8 | + |
| 9 | +use crate::models::{Fortune, World}; |
| 10 | + |
| 11 | +/// Postgres Error |
| 12 | +#[derive(Debug, thiserror::Error)] |
| 13 | +pub enum PgError { |
| 14 | + #[error("connect to database was failed")] |
| 15 | + Connect, |
| 16 | + #[error(transparent)] |
| 17 | + Io(#[from] io::Error), |
| 18 | + #[error(transparent)] |
| 19 | + Pg(#[from] tokio_postgres::Error), |
| 20 | +} |
| 21 | + |
| 22 | +impl From<PgError> for Error { |
| 23 | + fn from(e: PgError) -> Self { |
| 24 | + Error::Responder(e.into_response()) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl IntoResponse for PgError { |
| 29 | + fn into_response(self) -> Response { |
| 30 | + (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Postgres interface |
| 35 | +pub struct PgConnection { |
| 36 | + rng: WyRand, |
| 37 | + client: Client, |
| 38 | + world: Statement, |
| 39 | + fortune: Statement, |
| 40 | + updates: Vec<Statement>, |
| 41 | +} |
| 42 | + |
| 43 | +impl PgConnection { |
| 44 | + pub async fn connect(db_url: &str) -> PgConnection { |
| 45 | + let (client, conn) = connect(db_url, NoTls) |
| 46 | + .await |
| 47 | + .expect("can not connect to postgresql"); |
| 48 | + |
| 49 | + // Spawn connection |
| 50 | + tokio::spawn(async move { |
| 51 | + if let Err(error) = conn.await { |
| 52 | + eprintln!("Connection error: {}", error); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + let fortune = client.prepare("SELECT * FROM fortune").await.unwrap(); |
| 57 | + let mut updates = Vec::new(); |
| 58 | + |
| 59 | + for num in 1..=500u16 { |
| 60 | + let mut pl = 1; |
| 61 | + let mut q = String::new(); |
| 62 | + |
| 63 | + q.push_str("UPDATE world SET randomnumber = CASE id "); |
| 64 | + |
| 65 | + for _ in 1..=num { |
| 66 | + let _ = write!(q, "when ${} then ${} ", pl, pl + 1); |
| 67 | + pl += 2; |
| 68 | + } |
| 69 | + |
| 70 | + q.push_str("ELSE randomnumber END WHERE id IN ("); |
| 71 | + |
| 72 | + for _ in 1..=num { |
| 73 | + let _ = write!(q, "${},", pl); |
| 74 | + pl += 1; |
| 75 | + } |
| 76 | + |
| 77 | + q.pop(); |
| 78 | + q.push(')'); |
| 79 | + |
| 80 | + updates.push(client.prepare(&q).await.unwrap()); |
| 81 | + } |
| 82 | + |
| 83 | + let world = client |
| 84 | + .prepare("SELECT * FROM world WHERE id = $1") |
| 85 | + .await |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + PgConnection { |
| 89 | + rng: WyRand::new(), |
| 90 | + world, |
| 91 | + client, |
| 92 | + fortune, |
| 93 | + updates, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl PgConnection { |
| 99 | + async fn query_one_world(&self, id: i32) -> Result<World, PgError> { |
| 100 | + self.client |
| 101 | + .query_one(&self.world, &[&id]) |
| 102 | + .await |
| 103 | + .map(|row| World { |
| 104 | + id: row.get(0), |
| 105 | + randomnumber: row.get(1), |
| 106 | + }) |
| 107 | + .map_err(PgError::Pg) |
| 108 | + } |
| 109 | + |
| 110 | + pub async fn get_world(&self) -> Result<World, PgError> { |
| 111 | + let random_id = (self.rng.clone().generate::<u32>() % 10_000 + 1) as i32; |
| 112 | + self.query_one_world(random_id).await |
| 113 | + } |
| 114 | + |
| 115 | + pub async fn get_worlds(&self, num: u16) -> Result<Vec<World>, PgError> { |
| 116 | + let mut rng = self.rng.clone(); |
| 117 | + (0..num) |
| 118 | + .map(|_| { |
| 119 | + let id = (rng.generate::<u32>() % 10_000 + 1) as i32; |
| 120 | + self.query_one_world(id) |
| 121 | + }) |
| 122 | + .collect::<FuturesUnordered<_>>() |
| 123 | + .try_collect() |
| 124 | + .await |
| 125 | + } |
| 126 | + |
| 127 | + pub async fn get_worlds_by_limit(&self, limit: i64) -> Result<Vec<World>, PgError> { |
| 128 | + self.client |
| 129 | + .query("SELECT * FROM world LIMIT $1", &[&limit]) |
| 130 | + .await |
| 131 | + .map(|rows| { |
| 132 | + rows.iter() |
| 133 | + .map(|row| World { |
| 134 | + id: row.get(0), |
| 135 | + randomnumber: row.get(1), |
| 136 | + }) |
| 137 | + .collect() |
| 138 | + }) |
| 139 | + .map_err(PgError::Pg) |
| 140 | + } |
| 141 | + |
| 142 | + pub async fn update(&self, num: u16) -> Result<Vec<World>, PgError> { |
| 143 | + let mut rng = self.rng.clone(); |
| 144 | + |
| 145 | + let worlds: Vec<World> = (0..num) |
| 146 | + .map(|_| { |
| 147 | + let id = (rng.generate::<u32>() % 10_000 + 1) as i32; |
| 148 | + let rid = (rng.generate::<u32>() % 10_000 + 1) as i32; |
| 149 | + self.query_one_world(id).map_ok(move |mut world| { |
| 150 | + world.randomnumber = rid; |
| 151 | + world |
| 152 | + }) |
| 153 | + }) |
| 154 | + .collect::<FuturesUnordered<_>>() |
| 155 | + .try_collect() |
| 156 | + .await?; |
| 157 | + |
| 158 | + let mut params: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(num as usize * 3); |
| 159 | + |
| 160 | + for w in &worlds { |
| 161 | + params.push(&w.id); |
| 162 | + params.push(&w.randomnumber); |
| 163 | + } |
| 164 | + |
| 165 | + for w in &worlds { |
| 166 | + params.push(&w.id); |
| 167 | + } |
| 168 | + |
| 169 | + let st = self.updates[(num as usize) - 1].clone(); |
| 170 | + |
| 171 | + self.client.query(&st, ¶ms[..]).await?; |
| 172 | + |
| 173 | + Ok(worlds) |
| 174 | + } |
| 175 | + |
| 176 | + pub async fn tell_fortune(&self) -> Result<Vec<Fortune>, PgError> { |
| 177 | + let mut items = Vec::with_capacity(32); |
| 178 | + |
| 179 | + items.push(Fortune { |
| 180 | + id: 0, |
| 181 | + message: "Additional fortune added at request time.".to_string(), |
| 182 | + }); |
| 183 | + |
| 184 | + self.client |
| 185 | + .query(&self.fortune, &[]) |
| 186 | + .await? |
| 187 | + .iter() |
| 188 | + .for_each(|row| { |
| 189 | + items.push(Fortune { |
| 190 | + id: row.get(0), |
| 191 | + message: row.get(1), |
| 192 | + }) |
| 193 | + }); |
| 194 | + |
| 195 | + items.sort_by(|it, next| it.message.cmp(&next.message)); |
| 196 | + |
| 197 | + Ok(items) |
| 198 | + } |
| 199 | +} |
0 commit comments