Skip to content

Commit 0dcafc4

Browse files
committed
feat: db
1 parent ae191b8 commit 0dcafc4

File tree

3 files changed

+9
-21
lines changed

3 files changed

+9
-21
lines changed

frameworks/Rust/hyperlane/src/db.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub async fn insert_records() {
6060
let missing_count: i64 = limit - count;
6161
let mut values: Vec<String> = Vec::new();
6262
for _ in 0..missing_count {
63-
let random_number: i32 = get_random_id();
63+
let random_number: i32 = get_random_id().await;
6464
values.push(format!("(DEFAULT, {})", random_number));
6565
}
6666
let sql: String = format!(
@@ -71,7 +71,7 @@ pub async fn insert_records() {
7171
let _ = query(&sql).execute(&db_pool).await;
7272
let mut values: Vec<String> = Vec::new();
7373
for _ in 0..missing_count {
74-
let random_number: i32 = get_random_id();
74+
let random_number: i32 = get_random_id().await;
7575
values.push(format!("(DEFAULT, {})", random_number));
7676
}
7777
let sql: String = format!(
@@ -137,7 +137,7 @@ pub async fn get_update_data(limit: Queries) -> (String, Vec<QueryRow>) {
137137
let mut id_in_clause: String = format!("{}", rows[0].id);
138138
let last_idx: usize = rows.len() - 1;
139139
for (i, row) in rows.iter().enumerate() {
140-
let new_random_number: Queries = get_random_id();
140+
let new_random_number: Queries = get_random_id().await;
141141
let id: i32 = row.id;
142142
id_list.push(id);
143143
value_list.push_str(&format!("WHEN {} THEN {} ", id, new_random_number));
@@ -171,7 +171,7 @@ pub async fn init_db() {
171171

172172
#[inline]
173173
pub async fn random_world_row(db_pool: &DbPoolConnection) -> QueryRow {
174-
let random_id: Queries = get_random_id();
174+
let random_id: Queries = get_random_id().await;
175175
query_world_row(db_pool, random_id).await
176176
}
177177

@@ -207,8 +207,8 @@ pub async fn all_world_row() -> Vec<PgRow> {
207207
#[inline]
208208
pub async fn get_some_row_id(limit: Queries, db_pool: &DbPoolConnection) -> Vec<QueryRow> {
209209
let mut res: Vec<QueryRow> = Vec::with_capacity(limit as usize);
210-
let id_list: Vec<i32> = get_random_id_list(limit);
211-
for id in id_list {
210+
for _ in 0..limit {
211+
let id: i32 = get_random_id().await;
212212
let tem: QueryRow = query_world_row(db_pool, id).await;
213213
res.push(tem);
214214
}

frameworks/Rust/hyperlane/src/lazy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ use crate::*;
33
pub static DB: Lazy<ArcRwLock<Option<DbPoolConnection>>> =
44
Lazy::new(|| Arc::new(RwLock::new(None)));
55
pub static CACHE: Lazy<ArcRwLock<Vec<QueryRow>>> = Lazy::new(|| arc_rwlock(vec![]));
6+
pub static RAND: Lazy<ArcRwLock<WyRand>> = Lazy::new(|| arc_rwlock(WyRand::new()));

frameworks/Rust/hyperlane/src/utils.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,8 @@ pub fn escape_html(input: &str) -> String {
1717
}
1818

1919
#[inline]
20-
pub fn get_random_id() -> Queries {
21-
let mut rand: WyRand = WyRand::new();
20+
pub async fn get_random_id() -> Queries {
21+
let mut rand: RwLockWriteGuard<'_, WyRand> = RAND.write().await;
2222
let random_id: u32 = rand.generate::<u32>() % RANDOM_MAX as u32 + 1;
2323
random_id as Queries
2424
}
25-
26-
#[inline]
27-
pub fn get_random_id_list(limit: Queries) -> Vec<i32> {
28-
let mut id_list: Vec<i32> = (1..=limit).collect();
29-
let mut rand: WyRand = WyRand::new();
30-
let len: usize = id_list.len();
31-
for i in (1..len).rev() {
32-
let j: usize = (rand.generate::<u32>() as usize) % (i + 1);
33-
id_list.swap(i, j);
34-
}
35-
id_list.truncate(limit as usize);
36-
id_list
37-
}

0 commit comments

Comments
 (0)