11#[ path = "./db_util.rs" ]
22mod db_util;
33
4- use core:: cell:: RefCell ;
5-
6- use xitca_io:: bytes:: BytesMut ;
7- use xitca_postgres:: { Execute , iter:: AsyncLendingIterator , pipeline:: Pipeline , pool:: Pool } ;
4+ use xitca_postgres:: { Execute , iter:: AsyncLendingIterator , pool:: Pool } ;
85
96use super :: {
107 ser:: { Fortune , Fortunes , World } ,
@@ -15,21 +12,21 @@ use db_util::{FORTUNE_STMT, UPDATE_STMT, WORLD_STMT, not_found};
1512
1613pub struct Client {
1714 pool : Pool ,
18- shared : RefCell < ( Rand , BytesMut ) > ,
15+ rng : core :: cell :: RefCell < Rand > ,
1916}
2017
2118pub async fn create ( ) -> HandleResult < Client > {
2219 Ok ( Client {
2320 pool : Pool :: builder ( DB_URL ) . capacity ( 1 ) . build ( ) ?,
24- shared : Default :: default ( ) ,
21+ rng : Default :: default ( ) ,
2522 } )
2623}
2724
2825impl Client {
2926 pub async fn get_world ( & self ) -> HandleResult < World > {
3027 let mut conn = self . pool . get ( ) . await ?;
3128 let stmt = WORLD_STMT . execute ( & mut conn) . await ?;
32- let id = self . shared . borrow_mut ( ) . 0 . gen_id ( ) ;
29+ let id = self . rng . borrow_mut ( ) . gen_id ( ) ;
3330 let mut res = stmt. bind ( [ id] ) . query ( & conn. consume ( ) ) . await ?;
3431 let row = res. try_next ( ) . await ?. ok_or_else ( not_found) ?;
3532 Ok ( World :: new ( row. get ( 0 ) , row. get ( 1 ) ) )
@@ -39,19 +36,21 @@ impl Client {
3936 let mut conn = self . pool . get ( ) . await ?;
4037 let stmt = WORLD_STMT . execute ( & mut conn) . await ?;
4138
42- let mut res = {
43- let ( ref mut rng, ref mut buf) = * self . shared . borrow_mut ( ) ;
44- let mut pipe = Pipeline :: with_capacity_from_buf ( num as _ , buf) ;
45- rng. gen_multi ( )
46- . take ( num as _ )
47- . try_for_each ( |id| stmt. bind ( [ id] ) . query ( & mut pipe) ) ?;
48- pipe. query ( & conn. consume ( ) ) ?
49- } ;
39+ let get = self
40+ . rng
41+ . borrow_mut ( )
42+ . gen_multi ( )
43+ . take ( num as _ )
44+ . map ( |id| stmt. bind ( [ id] ) . query ( & conn) )
45+ . collect :: < Vec < _ > > ( ) ;
46+
47+ drop ( conn) ;
5048
5149 let mut worlds = Vec :: with_capacity ( num as _ ) ;
5250
53- while let Some ( mut item) = res. try_next ( ) . await ? {
54- let row = item. try_next ( ) . await ?. ok_or_else ( not_found) ?;
51+ for get in get {
52+ let mut res = get. await ?;
53+ let row = res. try_next ( ) . await ?. ok_or_else ( not_found) ?;
5554 worlds. push ( World :: new ( row. get ( 0 ) , row. get ( 1 ) ) ) ;
5655 }
5756
@@ -63,32 +62,32 @@ impl Client {
6362 let world_stmt = WORLD_STMT . execute ( & mut conn) . await ?;
6463 let update_stmt = UPDATE_STMT . execute ( & mut conn) . await ?;
6564
66- let ( mut res, worlds) = {
67- let ( ref mut rng, ref mut buf) = * self . shared . borrow_mut ( ) ;
68- let mut pipe = Pipeline :: with_capacity_from_buf ( ( num + 1 ) as _ , buf) ;
69-
65+ let ( get, update, worlds) = {
66+ let mut rng = self . rng . borrow_mut ( ) ;
7067 let mut ids = rng. gen_multi ( ) . take ( num as _ ) . collect :: < Vec < _ > > ( ) ;
7168 ids. sort ( ) ;
7269
73- let ( rngs, worlds) = ids
70+ let ( get , rngs, worlds) = ids
7471 . iter ( )
7572 . cloned ( )
7673 . zip ( rng. gen_multi ( ) )
7774 . map ( |( id, rand) | {
78- world_stmt. bind ( [ id] ) . query ( & mut pipe ) ? ;
79- HandleResult :: Ok ( ( rand, World :: new ( id, rand) ) )
75+ let get = world_stmt. bind ( [ id] ) . query ( & conn ) ;
76+ ( get , rand, World :: new ( id, rand) )
8077 } )
81- . collect :: < HandleResult < ( Vec < _ > , Vec < _ > ) > > ( ) ?;
82- update_stmt. bind ( [ & ids, & rngs] ) . query ( & mut pipe) ?;
83- ( pipe. query ( & conn. consume ( ) ) ?, worlds)
78+ . collect :: < ( Vec < _ > , Vec < _ > , Vec < _ > ) > ( ) ;
79+
80+ let update = update_stmt. bind ( [ & ids, & rngs] ) . query ( & conn. consume ( ) ) ;
81+
82+ ( get, update, worlds)
8483 } ;
8584
86- while let Some ( mut item) = res. try_next ( ) . await ? {
87- while let Some ( row) = item. try_next ( ) . await ? {
88- let _rand = row. get :: < i32 > ( 1 ) ;
89- }
85+ for fut in get {
86+ let _rand = fut. await ?. try_next ( ) . await ?. ok_or_else ( not_found) ?. get :: < i32 > ( 1 ) ;
9087 }
9188
89+ update. await ?;
90+
9291 Ok ( worlds)
9392 }
9493
0 commit comments