Skip to content

Commit 45f8cd7

Browse files
authored
models/download: Add a slim Version model to reduce data loading (#10574)
1 parent 490a95e commit 45f8cd7

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/controllers/krate/downloads.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
use crate::app::AppState;
77
use crate::controllers::krate::CratePath;
8-
use crate::models::{Version, VersionDownload};
8+
use crate::models::download::Version;
9+
use crate::models::VersionDownload;
910
use crate::schema::{version_downloads, versions};
1011
use crate::util::errors::AppResult;
1112
use crate::views::EncodableVersionDownload;
@@ -42,7 +43,7 @@ pub async fn get_crate_downloads(state: AppState, path: CratePath) -> AppResult<
4243
.load(&mut conn)
4344
.await?;
4445

45-
versions.sort_by_cached_key(|version| cmp::Reverse(semver::Version::parse(&version.num).ok()));
46+
versions.sort_unstable_by(|a, b| b.num.cmp(&a.num));
4647
let (latest_five, rest) = versions.split_at(cmp::min(5, versions.len()));
4748

4849
let downloads = VersionDownload::belonging_to(latest_five)

src/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub mod crate_owner_invitation;
2626
pub mod default_versions;
2727
mod deleted_crate;
2828
pub mod dependency;
29-
mod download;
29+
pub mod download;
3030
mod email;
3131
mod follow;
3232
mod keyword;

src/models/download.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
use crate::models::Version;
2-
use crate::schema::version_downloads;
1+
use crate::models::Version as FullVersion;
2+
use crate::schema::{version_downloads, versions};
33
use chrono::NaiveDate;
4+
use crates_io_diesel_helpers::SemverVersion;
45

56
#[derive(Queryable, Identifiable, Associations, Debug, Clone, Copy)]
6-
#[diesel(primary_key(version_id, date), belongs_to(Version))]
7+
#[diesel(
8+
primary_key(version_id, date),
9+
belongs_to(FullVersion, foreign_key=version_id),
10+
belongs_to(Version),
11+
)]
712
pub struct VersionDownload {
813
pub version_id: i32,
914
pub downloads: i32,
1015
pub counted: i32,
1116
pub date: NaiveDate,
1217
pub processed: bool,
1318
}
19+
20+
/// A subset of the columns of the `versions` table.
21+
///
22+
/// This struct is used to load all versions of a crate from the database,
23+
/// without loading the additional data that is unnecessary for download version resolution.
24+
///
25+
#[derive(Queryable, Selectable, Identifiable)]
26+
#[diesel(table_name = versions)]
27+
pub struct Version {
28+
pub id: i32,
29+
#[diesel(deserialize_as = SemverVersion)]
30+
pub num: semver::Version,
31+
}

0 commit comments

Comments
 (0)