11use crate :: * ;
22
3- #[ inline]
43pub async fn get_db_connection ( ) -> DbPoolConnection {
54 if let Some ( db_pool) = DB . get ( ) {
65 return db_pool. clone ( ) ;
@@ -11,7 +10,6 @@ pub async fn get_db_connection() -> DbPoolConnection {
1110 db_pool
1211}
1312
14- #[ inline]
1513#[ cfg( feature = "dev" ) ]
1614pub async fn create_database ( ) {
1715 let db_pool: DbPoolConnection = get_db_connection ( ) . await ;
@@ -20,7 +18,6 @@ pub async fn create_database() {
2018 . await ;
2119}
2220
23- #[ inline]
2421#[ cfg( feature = "dev" ) ]
2522pub async fn create_table ( ) {
2623 let db_pool: DbPoolConnection = get_db_connection ( ) . await ;
@@ -42,7 +39,6 @@ pub async fn create_table() {
4239 . await ;
4340}
4441
45- #[ inline]
4642#[ cfg( feature = "dev" ) ]
4743pub async fn insert_records ( ) {
4844 let db_pool: DbPoolConnection = get_db_connection ( ) . await ;
@@ -80,7 +76,6 @@ pub async fn insert_records() {
8076 let _ = query ( & sql) . execute ( & db_pool) . await ;
8177}
8278
83- #[ inline]
8479pub async fn init_cache ( ) {
8580 let mut res: Vec < QueryRow > = Vec :: with_capacity ( RANDOM_MAX as usize ) ;
8681 let db_pool: DbPoolConnection = get_db_connection ( ) . await ;
@@ -98,7 +93,6 @@ pub async fn init_cache() {
9893 let _ = CACHE . set ( res) ;
9994}
10095
101- #[ inline]
10296pub async fn connection_db ( ) -> DbPoolConnection {
10397 let db_url: & str = match option_env ! ( "POSTGRES_URL" ) {
10498 Some ( it) => it,
@@ -112,7 +106,7 @@ pub async fn connection_db() -> DbPoolConnection {
112106 DATABASE_NAME
113107 ) ,
114108 } ;
115- let pool_size: u32 = ( get_thread_count ( ) >> 2 ) . max ( 10 ) . min ( 100 ) as u32 ;
109+ let pool_size: u32 = ( get_thread_count ( ) << 2 ) . max ( 10 ) . min ( 100 ) as u32 ;
116110 let max_pool_size: u32 = option_env ! ( "POSTGRES_MAX_POOL_SIZE" )
117111 . unwrap_or ( & pool_size. to_string ( ) )
118112 . parse :: < u32 > ( )
@@ -133,7 +127,6 @@ pub async fn connection_db() -> DbPoolConnection {
133127 pool
134128}
135129
136- #[ inline]
137130pub async fn get_update_data (
138131 limit : Queries ,
139132) -> ( String , Vec < QueryRow > , Vec < Queries > , Vec < Queries > ) {
@@ -165,7 +158,6 @@ pub async fn get_update_data(
165158 ( sql, query_res_list, id_list, random_numbers)
166159}
167160
168- #[ inline]
169161pub async fn init_db ( ) {
170162 get_db_connection ( ) . await ;
171163 #[ cfg( feature = "dev" ) ]
@@ -177,13 +169,11 @@ pub async fn init_db() {
177169 init_cache ( ) . await ;
178170}
179171
180- #[ inline]
181172pub async fn random_world_row ( db_pool : & DbPoolConnection ) -> QueryRow {
182173 let random_id: Queries = get_random_id ( ) ;
183174 query_world_row ( db_pool, random_id) . await
184175}
185176
186- #[ inline]
187177pub async fn query_world_row ( db_pool : & DbPoolConnection , id : Queries ) -> QueryRow {
188178 let sql: String = format ! (
189179 "SELECT id, randomNumber FROM {} WHERE id = {} LIMIT 1" ,
@@ -196,7 +186,6 @@ pub async fn query_world_row(db_pool: &DbPoolConnection, id: Queries) -> QueryRo
196186 return QueryRow :: new ( id as i32 , 1 ) ;
197187}
198188
199- #[ inline]
200189pub async fn update_world_rows ( limit : Queries ) -> Vec < QueryRow > {
201190 let db_pool: DbPoolConnection = get_db_connection ( ) . await ;
202191 let ( sql, data, id_list, random_numbers) = get_update_data ( limit) . await ;
@@ -211,21 +200,19 @@ pub async fn update_world_rows(limit: Queries) -> Vec<QueryRow> {
211200 data
212201}
213202
214- #[ inline]
215203pub async fn all_world_row ( ) -> Vec < PgRow > {
216204 let db_pool: DbPoolConnection = get_db_connection ( ) . await ;
217205 let sql: String = format ! ( "SELECT id, message FROM {}" , TABLE_NAME_FORTUNE ) ;
218206 let res: Vec < PgRow > = query ( & sql) . fetch_all ( & db_pool) . await . unwrap_or_default ( ) ;
219207 return res;
220208}
221209
222- #[ inline]
223210pub async fn get_some_row_id ( limit : Queries , db_pool : & DbPoolConnection ) -> Vec < QueryRow > {
224- let mut res : Vec < QueryRow > = Vec :: with_capacity ( limit as usize ) ;
225- for _ in 0 ..limit {
226- let id: i32 = get_random_id ( ) ;
227- let tem : QueryRow = query_world_row ( db_pool, id) . await ;
228- res . push ( tem ) ;
229- }
230- res
211+ let futures : Vec < _ > = ( 0 .. limit)
212+ . map ( |_| async {
213+ let id: i32 = get_random_id ( ) ;
214+ query_world_row ( db_pool, id) . await
215+ } )
216+ . collect ( ) ;
217+ join_all ( futures ) . await
231218}
0 commit comments