Import type names instead of writing their paths (#355)
The style guide imports types and uses them unqualified. 222 places wrote a path such as std::collections::HashMap or std::time::Duration inline; each file now imports the type once, one use a module, and names it bare. Done with a script, file by file, only where the name is unambiguous there: - one path for the name throughout the file, and not a name the file already binds otherwise (its own types, other imports, a function's local use); - not a name generic enough to mislead bare (Error, Result, Value, Mutex, Client, Request, Response, Command, Event, Context, Map, Ok); - only code a normal build compiles, so no import is unused outside tests or names a crate only tests depend on. 193 paths stay, those cases above. No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,15 +7,19 @@ use crate::entity::{
|
||||
};
|
||||
use anyhow::{Context, Result};
|
||||
use sea_orm::sea_query::{Expr, Func};
|
||||
use sea_orm::sqlx::sqlite::SqliteSynchronous;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, Set,
|
||||
Statement,
|
||||
};
|
||||
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||
use std::path::Path;
|
||||
use std::time::SystemTime;
|
||||
use tracing::Instrument;
|
||||
|
||||
/// A pool of connections, where there was one connection behind a global lock.
|
||||
pub struct Db {
|
||||
orm: sea_orm::DatabaseConnection,
|
||||
orm: DatabaseConnection,
|
||||
/// A test's database file, removed when the test is done with it.
|
||||
#[cfg(test)]
|
||||
tmp: Option<std::path::PathBuf>,
|
||||
@@ -46,7 +50,7 @@ impl Drop for Db {
|
||||
/// here, as the old migrate() did for SQLite, or sea-orm-migration once there are several. A
|
||||
/// database from before 0.7 took its last columns from that migrate(), so it upgrades through
|
||||
/// a 0.7 release first.
|
||||
async fn create_missing(orm: &sea_orm::DatabaseConnection) -> Result<()> {
|
||||
async fn create_missing(orm: &DatabaseConnection) -> Result<()> {
|
||||
use crate::entity::*;
|
||||
use sea_orm::{ConnectionTrait, Schema};
|
||||
let schema = Schema::new(orm.get_database_backend());
|
||||
@@ -241,15 +245,15 @@ fn is_postgres(location: &str) -> bool {
|
||||
|
||||
/// sqlx's defaults for SQLite are what ipx wants, foreign keys on and a five-second wait for a
|
||||
/// lock, which is what rusqlite was set to, but for one.
|
||||
async fn connect(location: &str, keep_open: bool) -> Result<sea_orm::DatabaseConnection> {
|
||||
let mut opts = sea_orm::ConnectOptions::new(url_for(location));
|
||||
async fn connect(location: &str, keep_open: bool) -> Result<DatabaseConnection> {
|
||||
let mut opts = ConnectOptions::new(url_for(location));
|
||||
opts.sqlx_logging(false);
|
||||
// NORMAL, SQLite's own advice for WAL: synced at checkpoints, not after every commit. FULL,
|
||||
// the default, waited for the disk after nearly every statement, and a feed's items are
|
||||
// written a row at a time: 1.3s a feed on Tower's /tmp, where one synchronous write takes
|
||||
// 52ms, and 39s to subscribe an admin to 1,500 feeds (#135). A crash of ipx loses nothing;
|
||||
// only a power cut can lose the last transactions, and it never corrupts the file.
|
||||
opts.map_sqlx_sqlite_opts(|o| o.synchronous(sea_orm::sqlx::sqlite::SqliteSynchronous::Normal));
|
||||
opts.map_sqlx_sqlite_opts(|o| o.synchronous(SqliteSynchronous::Normal));
|
||||
// The same on Postgres, for ipx's own sessions: a commit waited for the WAL to reach the
|
||||
// disk, 12.8ms on the databases project's server, and at about fifty commits a feed a scan
|
||||
// took most of a second a feed (#140). A crash of the Postgres server can lose the last
|
||||
@@ -271,7 +275,7 @@ async fn connect(location: &str, keep_open: bool) -> Result<sea_orm::DatabaseCon
|
||||
if keep_open && is_postgres(location) {
|
||||
opts.min_connections(20);
|
||||
}
|
||||
sea_orm::Database::connect(opts).await.with_context(|| format!("opening {}", redact(location)))
|
||||
Database::connect(opts).await.with_context(|| format!("opening {}", redact(location)))
|
||||
}
|
||||
|
||||
/// The URL `connect` hands sqlx for a location: a SQLite file, created if missing, or a Postgres
|
||||
@@ -532,7 +536,7 @@ impl Db {
|
||||
let one = || sea_orm::Value::from(only.map(str::to_owned));
|
||||
// A step a query: the feed list's trace was 290 ms with nothing under it.
|
||||
let counts = |of: &'static str, sql: &'static str, args: Vec<sea_orm::Value>| async move {
|
||||
tracing::Instrument::instrument(self.rows(sql, args), step(of))
|
||||
Instrument::instrument(self.rows(sql, args), step(of))
|
||||
.await?
|
||||
.iter()
|
||||
.map(|r| Ok((r.try_get::<String>("", "feed_id")?, r.try_get::<i64>("", "n")?)))
|
||||
@@ -568,7 +572,7 @@ impl Db {
|
||||
),
|
||||
vec![user_id.into(), one()],
|
||||
);
|
||||
let blocked = tracing::Instrument::instrument(
|
||||
let blocked = Instrument::instrument(
|
||||
blocklists::Entity::find().filter(blocklists::Column::UserId.eq(user_id)).all(&self.orm),
|
||||
step("blocked"),
|
||||
);
|
||||
@@ -576,7 +580,7 @@ impl Db {
|
||||
if let Some(id) = only {
|
||||
rows = rows.filter(feeds::Column::Id.eq(id));
|
||||
}
|
||||
let rows = tracing::Instrument::instrument(rows.all(&self.orm), step("feeds"));
|
||||
let rows = Instrument::instrument(rows.all(&self.orm), step("feeds"));
|
||||
let (entries, downloaded, unread, blocked, rows) = tokio::try_join!(
|
||||
entries,
|
||||
downloaded,
|
||||
@@ -819,7 +823,7 @@ impl Db {
|
||||
|
||||
/// Returns true when this enclosure URL is new. False means we have downloaded it
|
||||
/// before, or deliberately reaped it -- either way it is not fetched again.
|
||||
pub async fn mark_downloaded(&self, url: &str, path: &std::path::Path, bytes: u64) -> Result<()> {
|
||||
pub async fn mark_downloaded(&self, url: &str, path: &Path, bytes: u64) -> Result<()> {
|
||||
self.exec(
|
||||
"UPDATE enclosures SET state = 'done', path = $2, bytes_done = $3,
|
||||
downloaded_at = $4, last_error = NULL WHERE url = $1",
|
||||
@@ -1002,7 +1006,7 @@ impl Db {
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter_map(|e| Some((e.id, e.path?)))
|
||||
.filter(|(_, p)| !std::path::Path::new(p).exists())
|
||||
.filter(|(_, p)| !Path::new(p).exists())
|
||||
.collect())
|
||||
}
|
||||
|
||||
@@ -3086,7 +3090,7 @@ impl Db {
|
||||
}
|
||||
|
||||
pub fn now() -> i64 {
|
||||
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs() as i64).unwrap_or(0)
|
||||
SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs() as i64).unwrap_or(0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user