Skip to content

Commit 57f8036

Browse files
committed
chore: Merge sql-client change
1 parent ac1c06a commit 57f8036

File tree

10 files changed

+103
-44
lines changed

10 files changed

+103
-44
lines changed

atcoder-problems-backend/sql-client/src/contest_problem.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,54 @@ pub trait ContestProblemClient {
1414
#[async_trait]
1515
impl ContestProblemClient for PgPool {
1616
async fn insert_contest_problem(&self, contest_problems: &[ContestProblem]) -> Result<()> {
17-
let (contest_ids, problem_ids): (Vec<&str>, Vec<&str>) = contest_problems
17+
let contest_ids: Vec<&str> = contest_problems
1818
.iter()
19-
.map(|c| (c.contest_id.as_str(), c.problem_id.as_str()))
20-
.unzip();
19+
.map(|c| c.contest_id.as_str())
20+
.collect();
21+
let problem_ids: Vec<&str> = contest_problems
22+
.iter()
23+
.map(|c| c.problem_id.as_str())
24+
.collect();
25+
let problem_indexes: Vec<&str> = contest_problems
26+
.iter()
27+
.map(|c| c.problem_index.as_str())
28+
.collect();
2129

2230
sqlx::query(
2331
r"
24-
INSERT INTO contest_problem (contest_id, problem_id)
32+
INSERT INTO contest_problem (contest_id, problem_id, problem_index)
2533
VALUES (
2634
UNNEST($1::VARCHAR(255)[]),
27-
UNNEST($2::VARCHAR(255)[])
35+
UNNEST($2::VARCHAR(255)[]),
36+
UNNEST($3::VARCHAR(255)[])
2837
)
2938
ON CONFLICT DO NOTHING
3039
",
3140
)
3241
.bind(contest_ids)
3342
.bind(problem_ids)
43+
.bind(problem_indexes)
3444
.execute(self)
3545
.await?;
3646

3747
Ok(())
3848
}
3949

4050
async fn load_contest_problem(&self) -> Result<Vec<ContestProblem>> {
41-
let problems = sqlx::query("SELECT contest_id, problem_id FROM contest_problem")
42-
.try_map(|row: PgRow| {
43-
let contest_id: String = row.try_get("contest_id")?;
44-
let problem_id: String = row.try_get("problem_id")?;
45-
Ok(ContestProblem {
46-
contest_id,
47-
problem_id,
51+
let problems =
52+
sqlx::query("SELECT contest_id, problem_id, problem_index FROM contest_problem")
53+
.try_map(|row: PgRow| {
54+
let contest_id: String = row.try_get("contest_id")?;
55+
let problem_id: String = row.try_get("problem_id")?;
56+
let problem_index: String = row.try_get("problem_index")?;
57+
Ok(ContestProblem {
58+
contest_id,
59+
problem_id,
60+
problem_index,
61+
})
4862
})
49-
})
50-
.fetch_all(self)
51-
.await?;
63+
.fetch_all(self)
64+
.await?;
5265

5366
Ok(problems)
5467
}

atcoder-problems-backend/sql-client/src/models.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct Contest {
1616
pub struct Problem {
1717
pub id: String,
1818
pub contest_id: String,
19+
pub problem_index: String,
20+
pub name: String,
1921
pub title: String,
2022
}
2123

@@ -97,6 +99,7 @@ pub struct UserSum {
9799
pub struct ContestProblem {
98100
pub contest_id: String,
99101
pub problem_id: String,
102+
pub problem_index: String,
100103
}
101104

102105
#[derive(PartialEq, Debug, Serialize)]

atcoder-problems-backend/sql-client/src/rated_point_sum.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ impl RatedPointSumClient for PgPool {
4040
.fetch_all(self);
4141

4242
let rated_problem_ids_fut =
43-
sqlx::query("SELECT contest_id, problem_id FROM contest_problem")
43+
sqlx::query("SELECT contest_id, problem_id, problem_index FROM contest_problem")
4444
.try_map(|row: PgRow| {
4545
let contest_id: String = row.try_get("contest_id")?;
4646
let problem_id: String = row.try_get("problem_id")?;
47+
let problem_index: String = row.try_get("problem_index")?;
4748
Ok(ContestProblem {
4849
contest_id,
4950
problem_id,
51+
problem_index,
5052
})
5153
})
5254
.fetch_all(self);

atcoder-problems-backend/sql-client/src/simple_client.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,36 @@ impl SimpleClient for PgPool {
6868
}
6969

7070
async fn insert_problems(&self, values: &[Problem]) -> Result<usize> {
71-
let (ids, contest_ids, titles) = values.iter().fold(
72-
(vec![], vec![], vec![]),
73-
|(mut ids, mut contest_ids, mut titles), cur| {
71+
let (ids, contest_ids, problem_indexes, names, titles) = values.iter().fold(
72+
(vec![], vec![], vec![], vec![], vec![]),
73+
|(mut ids, mut contest_ids, mut problem_indexes, mut names, mut titles), cur| {
7474
ids.push(cur.id.as_str());
7575
contest_ids.push(cur.contest_id.as_str());
76+
problem_indexes.push(cur.problem_index.as_str());
77+
names.push(cur.name.as_str());
7678
titles.push(cur.title.as_str());
77-
(ids, contest_ids, titles)
79+
(ids, contest_ids, problem_indexes, names, titles)
7880
},
7981
);
8082

8183
let result = sqlx::query(
8284
r"
8385
INSERT INTO problems
84-
(id, contest_id, title)
86+
(id, contest_id, problem_index, name, title)
8587
VALUES (
8688
UNNEST($1::VARCHAR(255)[]),
8789
UNNEST($2::VARCHAR(255)[]),
88-
UNNEST($3::VARCHAR(255)[])
90+
UNNEST($3::VARCHAR(255)[]),
91+
UNNEST($4::VARCHAR(255)[]),
92+
UNNEST($5::VARCHAR(255)[])
8993
)
9094
ON CONFLICT DO NOTHING
9195
",
9296
)
9397
.bind(ids)
9498
.bind(contest_ids)
99+
.bind(problem_indexes)
100+
.bind(names)
95101
.bind(titles)
96102
.execute(self)
97103
.await?;
@@ -100,19 +106,24 @@ impl SimpleClient for PgPool {
100106
}
101107

102108
async fn load_problems(&self) -> Result<Vec<Problem>> {
103-
let problems = sqlx::query("SELECT id, contest_id, title FROM problems")
104-
.try_map(|row: PgRow| {
105-
let id: String = row.try_get("id")?;
106-
let contest_id: String = row.try_get("contest_id")?;
107-
let title: String = row.try_get("title")?;
108-
Ok(Problem {
109-
id,
110-
contest_id,
111-
title,
109+
let problems =
110+
sqlx::query("SELECT id, contest_id, problem_index, name, title FROM problems")
111+
.try_map(|row: PgRow| {
112+
let id: String = row.try_get("id")?;
113+
let contest_id: String = row.try_get("contest_id")?;
114+
let problem_index: String = row.try_get("problem_index")?;
115+
let name: String = row.try_get("name")?;
116+
let title: String = row.try_get("title")?;
117+
Ok(Problem {
118+
id,
119+
contest_id,
120+
problem_index,
121+
name,
122+
title,
123+
})
112124
})
113-
})
114-
.fetch_all(self)
115-
.await?;
125+
.fetch_all(self)
126+
.await?;
116127
Ok(problems)
117128
}
118129

atcoder-problems-backend/sql-client/tests/test_contest_problem.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn create_problem(id: i32) -> ContestProblem {
77
ContestProblem {
88
contest_id: format!("contest{}", id),
99
problem_id: format!("problem{}", id),
10+
problem_index: format!("{}", id),
1011
}
1112
}
1213

atcoder-problems-backend/sql-client/tests/test_rated_point_sum.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,50 +88,60 @@ async fn setup_contest_problems(pool: &PgPool) {
8888
let problems = vec![
8989
ContestProblem {
9090
problem_id: "problem1".to_string(),
91+
problem_index: "1".to_string(),
9192
contest_id: RATED_CONTEST.to_string(),
9293
},
9394
ContestProblem {
9495
problem_id: "problem2".to_string(),
96+
problem_index: "2".to_string(),
9597
contest_id: UNRATED_CONTEST1.to_string(),
9698
},
9799
ContestProblem {
98100
problem_id: "problem3".to_string(),
101+
problem_index: "3".to_string(),
99102
contest_id: UNRATED_CONTEST1.to_string(),
100103
},
101104
ContestProblem {
102105
problem_id: "problem4".to_string(),
106+
problem_index: "4".to_string(),
103107
contest_id: RATED_CONTEST.to_string(),
104108
},
105109
ContestProblem {
106110
problem_id: "problem5".to_string(),
111+
problem_index: "5".to_string(),
107112
contest_id: SAME_CONTEST_RATED.to_string(),
108113
},
109114
ContestProblem {
110115
problem_id: "problem5".to_string(),
116+
problem_index: "5".to_string(),
111117
contest_id: SAME_CONTEST_UNRATED.to_string(),
112118
},
113119
ContestProblem {
114120
problem_id: "problem6".to_string(),
121+
problem_index: "6".to_string(),
115122
contest_id: SAME_CONTEST_RATED.to_string(),
116123
},
117124
ContestProblem {
118125
problem_id: "problem6".to_string(),
126+
problem_index: "6".to_string(),
119127
contest_id: SAME_CONTEST_UNRATED.to_string(),
120128
},
121129
ContestProblem {
122130
problem_id: "heuristic-problem".to_string(),
131+
problem_index: "1".to_string(),
123132
contest_id: HEURISTIC_CONTEST.to_string(),
124133
},
125134
];
126135

127136
for problem in problems {
128137
sqlx::query(
129138
r"
130-
INSERT INTO contest_problem (problem_id, contest_id)
131-
VALUES ($1, $2)
139+
INSERT INTO contest_problem (problem_id, problem_index, contest_id)
140+
VALUES ($1, $2, $3)
132141
",
133142
)
134143
.bind(problem.problem_id)
144+
.bind(problem.problem_index)
135145
.bind(problem.contest_id)
136146
.execute(pool)
137147
.await

atcoder-problems-backend/sql-client/tests/test_simple_client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ async fn test_insert_problems() {
3838
pool.insert_problems(&[Problem {
3939
id: "problem1".to_string(),
4040
contest_id: "".to_string(),
41+
problem_index: "".to_string(),
42+
name: "".to_string(),
4143
title: "".to_string(),
4244
}])
4345
.await
@@ -49,6 +51,8 @@ async fn test_insert_problems() {
4951
pool.insert_problems(&[Problem {
5052
id: "problem1".to_string(),
5153
contest_id: "".to_string(),
54+
problem_index: "".to_string(),
55+
name: "".to_string(),
5256
title: "".to_string(),
5357
}])
5458
.await

atcoder-problems-backend/src/bin/dump_json.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ async fn main() -> Result<()> {
124124
SELECT
125125
problems.id AS merged_problem_id,
126126
problems.contest_id AS merged_contest_id,
127+
problems.problem_index AS merged_problem_index,
128+
problems.name AS merged_problem_name,
127129
problems.title AS merged_problem_title,
128130
129131
shortest.submission_id AS shortest_submission_id,
@@ -158,6 +160,8 @@ async fn main() -> Result<()> {
158160
.map(|row: PgRow| {
159161
let id: String = row.get("merged_problem_id");
160162
let contest_id: String = row.get("merged_contest_id");
163+
let problem_index: String = row.get("merged_problem_index");
164+
let name: String = row.get("merged_problem_name");
161165
let title: String = row.get("merged_problem_title");
162166

163167
let shortest_submission_id: Option<i64> = row.get("shortest_submission_id");
@@ -180,6 +184,9 @@ async fn main() -> Result<()> {
180184
MergedProblem {
181185
id,
182186
contest_id,
187+
problem_index,
188+
name,
189+
183190
title,
184191
shortest_submission_id,
185192
shortest_contest_id,
@@ -236,6 +243,8 @@ struct UserStreak {
236243
struct MergedProblem {
237244
id: String,
238245
contest_id: String,
246+
problem_index: String,
247+
name: String,
239248
title: String,
240249
shortest_submission_id: Option<i64>,
241250
shortest_contest_id: Option<String>,

atcoder-problems-backend/src/crawler/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,19 @@ impl AtCoderFetcher for AtCoderClient {
7979
contest_id: &str,
8080
) -> Result<(Vec<Problem>, Vec<ContestProblem>)> {
8181
info!("Fetching problems from {} ...", contest_id);
82-
let problems = self.fetch_problem_list(contest_id).await?;
83-
let problems = problems
84-
.into_iter()
85-
.map(convert_problem)
86-
.collect::<Vec<_>>();
87-
let contest_problem = problems
82+
let atcoder_problems = self.fetch_problem_list(contest_id).await?;
83+
let contest_problem = atcoder_problems
8884
.iter()
8985
.map(|problem| ContestProblem {
90-
problem_id: problem.id.clone(),
9186
contest_id: problem.contest_id.clone(),
87+
problem_id: problem.id.clone(),
88+
problem_index: problem.position.clone(),
9289
})
9390
.collect::<Vec<_>>();
91+
let problems = atcoder_problems
92+
.into_iter()
93+
.map(convert_problem)
94+
.collect::<Vec<_>>();
9495
Ok((problems, contest_problem))
9596
}
9697
}
@@ -125,7 +126,9 @@ fn convert_problem(p: AtCoderProblem) -> Problem {
125126
Problem {
126127
id: p.id,
127128
contest_id: p.contest_id,
129+
problem_index: p.position.to_string(),
128130
title: p.position + ". " + p.title.as_str(),
131+
name: p.title.to_string(),
129132
}
130133
}
131134

config/database-definition.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ DROP TABLE IF EXISTS problems;
2626
CREATE TABLE problems (
2727
id VARCHAR(255) NOT NULL,
2828
contest_id VARCHAR(255) NOT NULL,
29+
problem_index VARCHAR(255) NOT NULL,
30+
name VARCHAR(255) NOT NULL,
2931
title VARCHAR(255) NOT NULL,
3032
PRIMARY KEY (id)
3133
);
@@ -112,7 +114,8 @@ DROP TABLE IF EXISTS contest_problem;
112114
CREATE TABLE contest_problem (
113115
contest_id VARCHAR(255) NOT NULL,
114116
problem_id VARCHAR(255) NOT NULL,
115-
PRIMARY KEY (contest_id, problem_id)
117+
problem_index VARCHAR(255) NOT NULL,
118+
PRIMARY KEY (contest_id, problem_id, problem_index)
116119
);
117120

118121
DROP TABLE IF EXISTS max_streaks;

0 commit comments

Comments
 (0)