Skip to content

Commit

Permalink
update: load deleted torrent status from database on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Roardom committed Jan 13, 2025
1 parent 636f076 commit b1981fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/tracker/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ impl Map {
});
});

// TODO: deleted_at column still needs added to unit3d. Until then, no
// torrents are considered deleted.
// Load one torrent per info hash. If multiple are found, prefer
// undeleted torrents. If multiple are still found, prefer approved
// torrents. If multiple are still found, prefer the oldest.
let torrents: Vec<DBImportTorrent> = sqlx::query_as!(
DBImportTorrent,
r#"
Expand All @@ -67,12 +68,23 @@ impl Map {
torrents.leechers as `leechers: u32`,
torrents.times_completed as `times_completed: u32`,
100 - LEAST(torrents.free, 100) as `download_factor: u8`,
IF(doubleup, 200, 100) as `upload_factor: u8`,
0 as `is_deleted: bool`
IF(torrents.doubleup, 200, 100) as `upload_factor: u8`,
torrents.deleted_at IS NOT NULL as `is_deleted: bool`
FROM
torrents
WHERE
torrents.deleted_at IS NULL
JOIN (
SELECT
COALESCE(
MIN(CASE WHEN deleted_at IS NULL AND status = 1 THEN id END),
MIN(CASE WHEN deleted_at IS NULL AND status != 1 THEN id END),
MIN(CASE WHEN deleted_at IS NOT NULL THEN id END)
) AS id
FROM
torrents
GROUP BY
info_hash
) AS distinct_torrents
ON distinct_torrents.id = torrents.id
"#
)
.fetch_all(db)
Expand Down
18 changes: 16 additions & 2 deletions src/tracker/torrent/infohash2id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ impl Map {
}

pub async fn from_db(db: &MySqlPool) -> Result<Map> {
// Load one torrent per info hash. If multiple are found, prefer
// undeleted torrents. If multiple are still found, prefer approved
// torrents. If multiple are still found, prefer the oldest.
let info_hash2ids = sqlx::query_as!(
InfoHash2Id,
r#"
Expand All @@ -36,8 +39,19 @@ impl Map {
torrents.info_hash as `info_hash: InfoHash`
FROM
torrents
WHERE
torrents.deleted_at IS NULL
JOIN (
SELECT
COALESCE(
MIN(CASE WHEN deleted_at IS NULL AND status = 1 THEN id END),
MIN(CASE WHEN deleted_at IS NULL AND status != 1 THEN id END),
MIN(CASE WHEN deleted_at IS NOT NULL THEN id END)
) AS id
FROM
torrents
GROUP BY
info_hash
) AS distinct_torrents
ON distinct_torrents.id = torrents.id
"#
)
.fetch_all(db)
Expand Down

0 comments on commit b1981fb

Please sign in to comment.