You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: frameworks/Rust/ntex/src/db.rs
+22-40Lines changed: 22 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,8 @@
1
-
#![allow(clippy::uninit_vec)]
2
-
use std::{borrow::Cow, cell::RefCell, fmt::WriteasFmtWrite};
1
+
use std::{borrow::Cow, cell::RefCell};
3
2
4
3
use nanorand::{Rng,WyRand};
5
4
use ntex::util::{Bytes,BytesMut};
6
-
usetokio_postgres::types::ToSql;
5
+
usesmallvec::SmallVec;
7
6
use tokio_postgres::{connect,Client,Statement};
8
7
use yarte::TemplateBytesTrait;
9
8
@@ -33,7 +32,7 @@ pub struct PgConnection {
33
32
fortune:Statement,
34
33
world:Statement,
35
34
rng:WyRand,
36
-
updates:Vec<Statement>,
35
+
updates:Statement,
37
36
buf:RefCell<BytesMut>,
38
37
fbuf:RefCell<Vec<Fortune>>,
39
38
}
@@ -48,24 +47,7 @@ impl PgConnection {
48
47
});
49
48
50
49
let fortune = cl.prepare("SELECT * FROM fortune").await.unwrap();
51
-
letmut updates = Vec::new();
52
-
for num in1..=500u16{
53
-
letmut pl:u16 = 1;
54
-
letmut q = String::new();
55
-
q.push_str("UPDATE world SET randomnumber = CASE id ");
56
-
for _ in1..=num {
57
-
let _ = write!(&mut q,"when ${} then ${} ", pl, pl + 1);
58
-
pl += 2;
59
-
}
60
-
q.push_str("ELSE randomnumber END WHERE id IN (");
61
-
for _ in1..=num {
62
-
let _ = write!(&mut q,"${},", pl);
63
-
pl += 1;
64
-
}
65
-
q.pop();
66
-
q.push(')');
67
-
updates.push(cl.prepare(&q).await.unwrap());
68
-
}
50
+
let updates = cl.prepare("UPDATE world w SET randomnumber = u.new_val FROM (SELECT unnest($1::int[]) as id, unnest($2::int[]) as new_val) u WHERE w.id = u.id").await.unwrap();
69
51
let world = cl.prepare("SELECT * FROM world WHERE id=$1").await.unwrap();
70
52
71
53
PgConnection{
@@ -124,30 +106,30 @@ impl PgConnection {
124
106
125
107
pubasyncfnupdate(&self,num:usize) -> Bytes{
126
108
letmut rng = nanorand::tls_rng();
127
-
letmut queries = Vec::with_capacity(num);
109
+
letmut ids = Vec::with_capacity(num);
110
+
letmut numbers = Vec::with_capacity(num);
111
+
letmut worlds = SmallVec::<[_;32]>::new();
112
+
letmut queries = SmallVec::<[_;32]>::new();
113
+
128
114
(0..num).for_each(|_| {
129
115
let w_id = (rng.generate::<u32>() % 10_000 + 1)asi32;
0 commit comments