Refactoring du cache pour une meilleur gestion des metadonnées
This commit is contained in:
@@ -93,7 +93,7 @@ pub struct ErrorResponse {
|
||||
///
|
||||
/// Retourne la liste complète des entrées du cache triées par nombre d'accès décroissant.
|
||||
pub async fn list_items<C: CacheConfig>(State(cache): State<Arc<Cache<C>>>) -> impl IntoResponse {
|
||||
match cache.db.get_all() {
|
||||
match cache.db.get_all(true) {
|
||||
Ok(entries) => (StatusCode::OK, Json(entries)).into_response(),
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
@@ -113,7 +113,7 @@ pub async fn get_item_info<C: CacheConfig>(
|
||||
State(cache): State<Arc<Cache<C>>>,
|
||||
Path(pk): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
match cache.db.get(&pk) {
|
||||
match cache.db.get(&pk, true) {
|
||||
Ok(entry) => (StatusCode::OK, Json(entry)).into_response(),
|
||||
Err(_) => (
|
||||
StatusCode::NOT_FOUND,
|
||||
@@ -135,7 +135,7 @@ pub async fn get_download_status<C: CacheConfig>(
|
||||
Path(pk): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
// Vérifier que l'item existe dans la DB
|
||||
if cache.db.get(&pk).is_err() {
|
||||
if cache.db.get(&pk, false).is_err() {
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(ErrorResponse {
|
||||
@@ -222,7 +222,7 @@ pub async fn delete_item<C: CacheConfig>(
|
||||
Path(pk): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
// Vérifier que l'item existe
|
||||
if cache.db.get(&pk).is_err() {
|
||||
if cache.db.get(&pk, false).is_err() {
|
||||
return (
|
||||
StatusCode::NOT_FOUND,
|
||||
Json(ErrorResponse {
|
||||
|
||||
@@ -21,9 +21,7 @@ pub trait CacheConfig: Send + Sync {
|
||||
/// Extension des fichiers (ex: "webp", "flac")
|
||||
fn file_extension() -> &'static str;
|
||||
/// Nom de la table dans la base de données (ex: "covers", "audio")
|
||||
fn table_name() -> &'static str {
|
||||
"cached_items"
|
||||
}
|
||||
|
||||
/// Type de cache (ex: "audio", "image")
|
||||
fn cache_type() -> &'static str {
|
||||
"file"
|
||||
@@ -118,7 +116,7 @@ impl<C: CacheConfig> Cache<C> {
|
||||
) -> Result<Self> {
|
||||
let directory = PathBuf::from(dir);
|
||||
std::fs::create_dir_all(&directory)?;
|
||||
let db = DB::init(&directory.join("cache.db"), C::table_name())?;
|
||||
let db = DB::init(&directory.join("cache.db"))?;
|
||||
|
||||
Ok(Self {
|
||||
dir: directory,
|
||||
@@ -169,7 +167,7 @@ impl<C: CacheConfig> Cache<C> {
|
||||
tracing::debug!("Computed pk {} for URL {}", pk, url);
|
||||
|
||||
// 3. Vérifier si le fichier est déjà en cache
|
||||
if self.db.get(&pk).is_ok() {
|
||||
if self.db.get(&pk, false).is_ok() {
|
||||
let file_path = self.file_path(&pk);
|
||||
if file_path.exists() {
|
||||
// Déjà en cache, update timestamp et retour rapide
|
||||
@@ -202,8 +200,8 @@ impl<C: CacheConfig> Cache<C> {
|
||||
}
|
||||
|
||||
// Ajouter immédiatement à la DB
|
||||
self.db.add(&pk, url, collection)?;
|
||||
|
||||
self.db.add(&pk, None, collection)?;
|
||||
self.db.set_origin_url(&pk, url)?;
|
||||
// Appliquer la politique d'éviction LRU si nécessaire
|
||||
if let Err(e) = self.enforce_limit().await {
|
||||
tracing::warn!("Error enforcing cache limit: {}", e);
|
||||
@@ -264,7 +262,7 @@ impl<C: CacheConfig> Cache<C> {
|
||||
tracing::debug!("Computed pk {} for source_uri {}", pk, source_uri);
|
||||
|
||||
// 3. Vérifier si le fichier est déjà en cache
|
||||
if self.db.get(&pk).is_ok() {
|
||||
if self.db.get(&pk, false).is_ok() {
|
||||
let file_path = self.file_path(&pk);
|
||||
if file_path.exists() {
|
||||
// Déjà en cache, update timestamp et retour rapide
|
||||
@@ -301,7 +299,8 @@ impl<C: CacheConfig> Cache<C> {
|
||||
downloads.insert(pk.clone(), download.clone());
|
||||
}
|
||||
|
||||
self.db.add(&pk, source_uri, collection)?;
|
||||
self.db.add(&pk, None, collection)?;
|
||||
self.db.set_origin_url(&pk, source_uri);
|
||||
|
||||
if let Err(e) = self.enforce_limit().await {
|
||||
tracing::warn!("Error enforcing cache limit: {}", e);
|
||||
@@ -359,13 +358,59 @@ impl<C: CacheConfig> Cache<C> {
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_item(&self, pk: &str) -> Result<()> {
|
||||
// Vérifie l'existence pour signaler une erreur explicite si l'entrée est absente
|
||||
self.db.get(pk, false)?;
|
||||
|
||||
// Oublie un téléchargement en cours pour cette clé
|
||||
self.downloads.write().await.remove(pk);
|
||||
|
||||
// Supprime chaque fichier {pk}.{qualifier}.{ext} (ignorer si déjà absent)
|
||||
for path in self.get_file_paths(pk)? {
|
||||
if let Err(err) = tokio::fs::remove_file(&path).await {
|
||||
if err.kind() != std::io::ErrorKind::NotFound {
|
||||
return Err(err.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Efface l’entrée de la base (les métadonnées partent via ON DELETE CASCADE)
|
||||
self.db.delete(pk)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_collection(&self, collection: &str) -> Result<()> {
|
||||
let entries = self.db.get_by_collection(collection, false)?;
|
||||
|
||||
{
|
||||
let mut downloads = self.downloads.write().await;
|
||||
for entry in &entries {
|
||||
downloads.remove(&entry.pk);
|
||||
}
|
||||
}
|
||||
|
||||
for entry in &entries {
|
||||
for path in self.get_file_paths(&entry.pk)? {
|
||||
if let Err(err) = tokio::fs::remove_file(&path).await {
|
||||
if err.kind() != std::io::ErrorKind::NotFound {
|
||||
return Err(err.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.db.delete_collection(collection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Récupère le chemin d'un fichier dans le cache
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pk` - Clé primaire du fichier
|
||||
pub async fn get(&self, pk: &str) -> Result<PathBuf> {
|
||||
self.db.get(pk)?;
|
||||
self.db.get(pk, false)?;
|
||||
self.db.update_hit(pk)?;
|
||||
|
||||
let file_path = self.file_path(pk);
|
||||
@@ -376,13 +421,18 @@ impl<C: CacheConfig> Cache<C> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn touch(&self, pk: &str) -> Result<()> {
|
||||
self.db.update_hit(pk)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Récupère tous les fichiers d'une collection
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `collection` - Identifiant de la collection
|
||||
pub async fn get_collection(&self, collection: &str) -> Result<Vec<PathBuf>> {
|
||||
let entries = self.db.get_by_collection(collection)?;
|
||||
let entries = self.db.get_by_collection(collection, false)?;
|
||||
let mut paths = Vec::new();
|
||||
|
||||
for entry in entries {
|
||||
@@ -412,20 +462,26 @@ impl<C: CacheConfig> Cache<C> {
|
||||
/// Consolide le cache en supprimant les orphelins et en re-téléchargeant les fichiers manquants
|
||||
pub async fn consolidate(&self) -> Result<()> {
|
||||
// Récupérer la liste des entrées à traiter
|
||||
let entries = self.db.get_all()?;
|
||||
let entries = self.db.get_all(false)?;
|
||||
|
||||
// Supprimer les entrées sans fichiers correspondants
|
||||
for entry in entries {
|
||||
let file_path = self.file_path(&entry.pk);
|
||||
|
||||
if !file_path.exists() {
|
||||
// Re-télécharger le fichier manquant
|
||||
match self
|
||||
.add_from_url(&entry.source_url, entry.collection.as_deref())
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
// Si le téléchargement échoue, supprimer l'entrée DB
|
||||
match self.db.get_origin_url(&entry.pk)? {
|
||||
Some(url) => {
|
||||
if let Err(err) = self.add_from_url(&url, entry.collection.as_deref()).await
|
||||
{
|
||||
tracing::warn!(
|
||||
"Unable to redownload missing file for {}: {}",
|
||||
entry.pk,
|
||||
err
|
||||
);
|
||||
self.db.delete(&entry.pk)?;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.db.delete(&entry.pk)?;
|
||||
}
|
||||
}
|
||||
@@ -441,7 +497,7 @@ impl<C: CacheConfig> Cache<C> {
|
||||
// Format attendu: {pk}.{qualifier}.{EXT}
|
||||
// On extrait le pk (première partie avant le premier point)
|
||||
if let Some(pk) = file_name.split('.').next() {
|
||||
if self.db.get(pk).is_err() {
|
||||
if self.db.get(pk, false).is_err() {
|
||||
tokio::fs::remove_file(path).await?;
|
||||
}
|
||||
}
|
||||
@@ -601,6 +657,42 @@ impl<C: CacheConfig> Cache<C> {
|
||||
.join(format!("{}.{}.{}", pk, qualifier, C::file_extension()))
|
||||
}
|
||||
|
||||
/// Retourne tous les chemins de fichiers stockés pour une clé donnée,
|
||||
/// quel que soit le qualifier.
|
||||
///
|
||||
/// Format: `{pk}.*.{extension}`
|
||||
pub fn get_file_paths(&self, pk: &str) -> Result<Vec<PathBuf>> {
|
||||
let mut paths = Vec::new();
|
||||
let prefix = format!("{pk}.");
|
||||
let expected_ext = C::file_extension();
|
||||
|
||||
for entry in std::fs::read_dir(&self.dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
if !path.is_file() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_name = match entry.file_name().into_string() {
|
||||
Ok(name) => name,
|
||||
Err(_) => continue, // nom de fichier non UTF-8 : on l’ignore
|
||||
};
|
||||
|
||||
if !file_name.starts_with(&prefix) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !file_name.ends_with(expected_ext) {
|
||||
continue;
|
||||
}
|
||||
|
||||
paths.push(path);
|
||||
}
|
||||
|
||||
Ok(paths)
|
||||
}
|
||||
|
||||
/// Valide les données avant de les stocker
|
||||
/// Par défaut, accepte toutes les données
|
||||
pub fn validate_data(&self, data: &[u8]) -> Result<Vec<u8>> {
|
||||
|
||||
@@ -51,11 +51,6 @@ pub trait FileCache<C: CacheConfig>: Send + Sync {
|
||||
C::file_extension()
|
||||
}
|
||||
|
||||
/// Retourne le nom de la table
|
||||
fn table_name(&self) -> &'static str {
|
||||
C::table_name()
|
||||
}
|
||||
|
||||
/// Construit le chemin complet d'un fichier dans le cache
|
||||
///
|
||||
/// Format: `{pk}.{qualificatif}.{extension}`
|
||||
@@ -161,11 +156,11 @@ pub trait FileCache<C: CacheConfig>: Send + Sync {
|
||||
/// assert_eq!(pk.len(), 32); // 16 bytes = 32 hex chars
|
||||
/// ```
|
||||
pub fn pk_from_content_header(header: &[u8]) -> String {
|
||||
use sha2::{Sha256, Digest};
|
||||
use sha2::{Digest, Sha256};
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(header);
|
||||
let result = hasher.finalize();
|
||||
hex::encode(&result[..16]) // 16 octets = 32 caractères hex
|
||||
hex::encode(&result[..16]) // 16 octets = 32 caractères hex
|
||||
}
|
||||
|
||||
/// Génère une clé primaire à partir d'une URL (legacy)
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
//! des éléments en cache, avec tracking des accès et des statistiques.
|
||||
|
||||
use chrono::Utc;
|
||||
use rusqlite::{params, Connection};
|
||||
use rusqlite::{params, Connection, Error, OptionalExtension};
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Number, Value};
|
||||
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
@@ -21,7 +24,7 @@ pub struct CacheEntry {
|
||||
pub pk: String,
|
||||
/// URL source de l'élément
|
||||
#[cfg_attr(feature = "openapi", schema(example = "https://example.com/resource"))]
|
||||
pub source_url: String,
|
||||
pub id: String,
|
||||
/// Collection à laquelle appartient l'élément (optionnel)
|
||||
#[cfg_attr(feature = "openapi", schema(example = "album:123"))]
|
||||
pub collection: Option<String>,
|
||||
@@ -36,7 +39,7 @@ pub struct CacheEntry {
|
||||
feature = "openapi",
|
||||
schema(example = r#"{"title":"Track","artist":"Artist"}"#)
|
||||
)]
|
||||
pub metadata_json: Option<String>,
|
||||
pub metadata: Option<Value>,
|
||||
}
|
||||
|
||||
/// Base de données SQLite pour le cache
|
||||
@@ -48,7 +51,6 @@ pub struct CacheEntry {
|
||||
#[derive(Debug)]
|
||||
pub struct DB {
|
||||
conn: Mutex<Connection>,
|
||||
table_name: String,
|
||||
}
|
||||
|
||||
impl DB {
|
||||
@@ -67,42 +69,55 @@ impl DB {
|
||||
///
|
||||
/// let db = DB::init(Path::new("cache.db"), "my_cache").unwrap();
|
||||
/// ```
|
||||
pub fn init(path: &Path, table_name: &str) -> Result<Self, rusqlite::Error> {
|
||||
pub fn init(path: &Path) -> Result<Self, rusqlite::Error> {
|
||||
let conn = Connection::open(path)?;
|
||||
|
||||
let create_table_sql = format!(
|
||||
"CREATE TABLE IF NOT EXISTS ASSET (
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS asset (
|
||||
pk TEXT PRIMARY KEY,
|
||||
source_url TEXT,
|
||||
collection TEXT,
|
||||
id TEXT,
|
||||
hits INTEGER DEFAULT 0,
|
||||
last_used TEXT,
|
||||
metadata_json TEXT
|
||||
last_used TEXT
|
||||
)",
|
||||
table_name
|
||||
);
|
||||
|
||||
conn.execute(&create_table_sql, [])?;
|
||||
[],
|
||||
)?;
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS metadata (
|
||||
pk TEXT,
|
||||
key TEXT,
|
||||
value_type TEXT NOT NULL CHECK (value_type IN ('string','number','boolean','null')),
|
||||
value TEXT,
|
||||
PRIMARY KEY (pk, key),
|
||||
FOREIGN KEY (pk) REFERENCES asset (pk) ON DELETE CASCADE
|
||||
)"
|
||||
, [])?;
|
||||
|
||||
// Créer un index sur la collection pour les requêtes rapides
|
||||
let create_index_sql = format!(
|
||||
"CREATE INDEX IF NOT EXISTS idx_{}_collection ON {} (collection)",
|
||||
table_name, table_name
|
||||
);
|
||||
|
||||
conn.execute(&create_index_sql, [])?;
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_asset_collection
|
||||
ON ASSET (collection)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
// Créer un index composite pour optimiser la politique LRU (get_oldest)
|
||||
let create_lru_index_sql = format!(
|
||||
"CREATE INDEX IF NOT EXISTS idx_{}_lru ON {} (last_used ASC, hits ASC)",
|
||||
table_name, table_name
|
||||
);
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_asset_lru
|
||||
ON asset (last_used ASC, hits ASC)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
conn.execute(&create_lru_index_sql, [])?;
|
||||
// Crée un index composite pour rendre unique les ids si défini dans une collection
|
||||
conn.execute(
|
||||
"CREATE UNIQUE INDEX
|
||||
IF NOT EXISTS asset_collection_id_unique
|
||||
ON asset (collection, id)
|
||||
WHERE id IS NOT NULL;",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
conn: Mutex::new(conn),
|
||||
table_name: table_name.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -113,8 +128,13 @@ impl DB {
|
||||
/// * `pk` - Clé primaire de l'élément
|
||||
/// * `url` - URL source de l'élément
|
||||
/// * `collection` - Collection optionnelle à laquelle appartient l'élément
|
||||
pub fn add(&self, pk: &str, url: &str, collection: Option<&str>) -> rusqlite::Result<()> {
|
||||
self.add_with_metadata(pk, url, collection, None)
|
||||
pub fn add(
|
||||
&self,
|
||||
pk: &str,
|
||||
id: Option<&str>,
|
||||
collection: Option<&str>,
|
||||
) -> rusqlite::Result<()> {
|
||||
self.add_with_metadata(pk, id, collection, None)
|
||||
}
|
||||
|
||||
/// Ajoute ou met à jour une entrée avec métadonnées JSON optionnelles
|
||||
@@ -128,52 +148,317 @@ impl DB {
|
||||
pub fn add_with_metadata(
|
||||
&self,
|
||||
pk: &str,
|
||||
url: &str,
|
||||
id: Option<&str>,
|
||||
collection: Option<&str>,
|
||||
metadata_json: Option<&str>,
|
||||
metadata: Option<&Value>,
|
||||
) -> rusqlite::Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
"INSERT INTO {} (pk, source_url, collection, hits, last_used, metadata_json)
|
||||
VALUES (?1, ?2, ?3, 0, ?4, ?5)
|
||||
ON CONFLICT(pk) DO UPDATE SET
|
||||
source_url = excluded.source_url,
|
||||
collection = excluded.collection,
|
||||
last_used = excluded.last_used,
|
||||
metadata_json = excluded.metadata_json",
|
||||
self.table_name
|
||||
);
|
||||
|
||||
conn.execute(
|
||||
&sql,
|
||||
params![pk, url, collection, Utc::now().to_rfc3339(), metadata_json],
|
||||
"INSERT INTO asset (pk, id, collection, hits, last_used)
|
||||
VALUES (?1, ?2, ?3, 0, ?4)
|
||||
ON CONFLICT(pk) DO UPDATE SET
|
||||
id = excluded.id,
|
||||
collection = excluded.collection,
|
||||
last_used = excluded.last_used",
|
||||
params![pk, id, collection, Utc::now().to_rfc3339()],
|
||||
)?;
|
||||
|
||||
if metadata.is_some() {
|
||||
self.set_metadata(pk, metadata.unwrap())?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remplace toutes les métadonnées associées à une entrée.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pk` - Clé primaire de l'élément ciblé.
|
||||
/// * `metadata` - Objet JSON complet décrivant les nouvelles métadonnées.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Retourne une erreur si `metadata` n'est pas un objet JSON ou si l'écriture
|
||||
/// SQLite échoue.
|
||||
pub fn set_metadata(&self, pk: &str, metadata: &Value) -> rusqlite::Result<()> {
|
||||
let metadata_obj = metadata.as_object().ok_or_else(|| {
|
||||
Error::InvalidParameterName("metadata must be a JSON object".to_owned())
|
||||
})?;
|
||||
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
|
||||
let tx = conn.transaction()?;
|
||||
|
||||
tx.execute("DELETE FROM metadata WHERE pk = ?1", params![pk])?;
|
||||
|
||||
for (key, value) in metadata_obj.iter() {
|
||||
let (value_type, value_text): (&str, Option<String>) = match value {
|
||||
Value::Null => ("null", None),
|
||||
Value::Bool(b) => ("boolean", Some(b.to_string())),
|
||||
Value::Number(n) => ("number", Some(n.to_string())),
|
||||
Value::String(s) => ("string", Some(s.clone())),
|
||||
Value::Array(_) | Value::Object(_) => ("string", Some(value.to_string())),
|
||||
};
|
||||
|
||||
tx.execute(
|
||||
"INSERT INTO metadata (pk, key, value_type, value) VALUES (?1, ?2, ?3, ?4)",
|
||||
params![pk, key, value_type, value_text.as_deref()],
|
||||
)?;
|
||||
}
|
||||
|
||||
tx.commit()
|
||||
}
|
||||
|
||||
/// Insère ou met à jour une métadonnée individuelle.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pk` - Clé primaire de l'élément concerné.
|
||||
/// * `key` - Nom de la métadonnée à enregistrer.
|
||||
/// * `value` - Valeur JSON à stocker pour cette clé.
|
||||
pub fn set_a_metadata(&self, pk: &str, key: &str, value: Value) -> rusqlite::Result<()> {
|
||||
let (value_type, value_text): (&str, Option<String>) = match value {
|
||||
Value::Null => ("null", None),
|
||||
Value::Bool(b) => ("boolean", Some(b.to_string())),
|
||||
Value::Number(n) => ("number", Some(n.to_string())),
|
||||
Value::String(s) => ("string", Some(s)),
|
||||
Value::Array(arr) => ("string", Some(Value::Array(arr).to_string())),
|
||||
Value::Object(map) => ("string", Some(Value::Object(map).to_string())),
|
||||
};
|
||||
|
||||
let conn = self.conn.lock().unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO metadata (pk, key, value_type, value)
|
||||
VALUES (?1, ?2, ?3, ?4)
|
||||
ON CONFLICT(pk, key) DO UPDATE SET
|
||||
value_type = excluded.value_type,
|
||||
value = excluded.value",
|
||||
params![pk, key, value_type, value_text.as_deref()],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Récupère une entrée de la base de données par sa clé
|
||||
/// Alias interne pour récupérer une métadonnée individuelle.
|
||||
///
|
||||
/// Préférer `get_metadata_value` pour les appels externes.
|
||||
pub fn get_a_metadata(&self, pk: &str, key: &str) -> rusqlite::Result<Option<Value>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
|
||||
conn.query_row(
|
||||
"SELECT value_type, value FROM metadata WHERE pk = ?1 AND key = ?2",
|
||||
params![pk, key],
|
||||
|row| {
|
||||
let value_type: String = row.get(0)?;
|
||||
let raw: Option<String> = row.get(1)?;
|
||||
decode_metadata_value(key, &value_type, raw)
|
||||
},
|
||||
)
|
||||
.optional()
|
||||
}
|
||||
|
||||
/// Récupère toutes les métadonnées d'une entrée sous forme d'objet JSON.
|
||||
///
|
||||
/// Retourne `Ok(None)` si aucune métadonnée n'est présente.
|
||||
pub fn get_metadata(&self, pk: &str) -> rusqlite::Result<Option<Value>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare("SELECT key, value_type, value FROM metadata WHERE pk = ?1")?;
|
||||
|
||||
let rows = stmt.query_map([pk], |row| {
|
||||
let key: String = row.get(0)?;
|
||||
let value_type: String = row.get(1)?;
|
||||
let value: Option<String> = row.get(2)?;
|
||||
Ok((key, value_type, value))
|
||||
})?;
|
||||
|
||||
let mut metadata = Map::new();
|
||||
let mut found = false;
|
||||
|
||||
for row in rows {
|
||||
let (key, value_type, raw) = row?;
|
||||
found = true;
|
||||
|
||||
let value = match value_type.as_str() {
|
||||
"null" => Value::Null,
|
||||
"boolean" => {
|
||||
let raw = raw.as_deref().ok_or_else(|| {
|
||||
Error::InvalidParameterName(format!(
|
||||
"missing boolean metadata for key '{key}'"
|
||||
))
|
||||
})?;
|
||||
let parsed = raw.parse::<bool>().map_err(|_| {
|
||||
Error::InvalidParameterName(format!(
|
||||
"invalid boolean metadata for key '{key}'"
|
||||
))
|
||||
})?;
|
||||
Value::Bool(parsed)
|
||||
}
|
||||
"number" => {
|
||||
let raw = raw.as_deref().ok_or_else(|| {
|
||||
Error::InvalidParameterName(format!(
|
||||
"missing number metadata for key '{key}'"
|
||||
))
|
||||
})?;
|
||||
let number = Number::from_str(raw).map_err(|_| {
|
||||
Error::InvalidParameterName(format!(
|
||||
"invalid number metadata for key '{key}'"
|
||||
))
|
||||
})?;
|
||||
Value::Number(number)
|
||||
}
|
||||
"string" => Value::String(raw.unwrap_or_default()),
|
||||
other => {
|
||||
return Err(Error::InvalidParameterName(format!(
|
||||
"unknown metadata type '{other}' for key '{key}'"
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
metadata.insert(key, value);
|
||||
}
|
||||
|
||||
if found {
|
||||
Ok(Some(Value::Object(metadata)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Enregistre l'URL d'origine liée à un élément du cache.
|
||||
pub fn set_origin_url(&self, pk: &str, origin_url: &str) -> rusqlite::Result<()> {
|
||||
self.set_a_metadata(pk, "origin_url", Value::String(origin_url.to_owned()))
|
||||
}
|
||||
|
||||
/// Récupère l'URL d'origine précédemment stockée pour un élément.
|
||||
///
|
||||
/// Retourne `Ok(None)` si aucune URL n'est définie.
|
||||
pub fn get_origin_url(&self, pk: &str) -> rusqlite::Result<Option<String>> {
|
||||
match self.get_metadata_value(pk, "origin_url")? {
|
||||
Some(Value::String(url)) => Ok(Some(url)),
|
||||
Some(Value::Null) | None => Ok(None),
|
||||
Some(other) => Err(Error::InvalidParameterName(format!(
|
||||
"metadata 'origin_url' must be a string, got {other}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère uniquement les métadonnées JSON d'une entrée
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pk` - Clé primaire de l'élément à récupérer
|
||||
pub fn get(&self, pk: &str) -> rusqlite::Result<CacheEntry> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
"SELECT pk, source_url, collection, hits, last_used, metadata_json FROM {} WHERE pk = ?1",
|
||||
self.table_name
|
||||
);
|
||||
/// * `pk` - Clé primaire de l'élément
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Les métadonnées JSON si présentes, None sinon
|
||||
pub fn get_metadata_json(&self, pk: &str) -> rusqlite::Result<Option<String>> {
|
||||
Ok(self.get_metadata(pk)?.map(|value| value.to_string()))
|
||||
}
|
||||
|
||||
conn.query_row(&sql, [pk], |row| {
|
||||
Ok(CacheEntry {
|
||||
pk: row.get(0)?,
|
||||
source_url: row.get(1)?,
|
||||
collection: row.get(2)?,
|
||||
hits: row.get(3)?,
|
||||
last_used: row.get(4)?,
|
||||
metadata_json: row.get(5)?,
|
||||
})
|
||||
})
|
||||
/// Récupère une métadonnée individuelle, si elle existe.
|
||||
pub fn get_metadata_value(&self, pk: &str, key: &str) -> rusqlite::Result<Option<Value>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
|
||||
conn.query_row(
|
||||
"SELECT value_type, value FROM metadata WHERE pk = ?1 AND key = ?2",
|
||||
params![pk, key],
|
||||
|row| {
|
||||
let value_type: String = row.get(0)?;
|
||||
let raw: Option<String> = row.get(1)?;
|
||||
decode_metadata_value(key, &value_type, raw)
|
||||
},
|
||||
)
|
||||
.optional()
|
||||
}
|
||||
|
||||
/// Récupère une entrée de la base de données par sa clé
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `pk` - Clé primaire de l'élément à récupérer.
|
||||
/// * `with_metadata` - Charge les métadonnées associées si `true`.
|
||||
pub fn get(&self, pk: &str, with_metadata: bool) -> rusqlite::Result<CacheEntry> {
|
||||
let mut entry = {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.query_row(
|
||||
"SELECT pk, id, collection, hits, last_used \
|
||||
FROM asset \
|
||||
WHERE pk = ?1",
|
||||
[pk],
|
||||
|row| {
|
||||
Ok(CacheEntry {
|
||||
pk: row.get(0)?,
|
||||
id: row.get(1)?,
|
||||
collection: row.get(2)?,
|
||||
hits: row.get(3)?,
|
||||
last_used: row.get(4)?,
|
||||
metadata: None,
|
||||
})
|
||||
},
|
||||
)?
|
||||
};
|
||||
|
||||
if with_metadata {
|
||||
entry.metadata = self.get_metadata(&entry.pk)?;
|
||||
}
|
||||
|
||||
Ok(entry)
|
||||
}
|
||||
|
||||
/// Récupère une entrée en utilisant la paire `(collection, id)`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `collection` - Collection dans laquelle chercher.
|
||||
/// * `id` - Identifiant logique de l'élément.
|
||||
/// * `with_metadata` - Charge les métadonnées associées si `true`.
|
||||
pub fn get_from_id(
|
||||
&self,
|
||||
collection: &str,
|
||||
id: &str,
|
||||
with_metadata: bool,
|
||||
) -> rusqlite::Result<CacheEntry> {
|
||||
let mut entry = {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.query_row(
|
||||
"SELECT pk, id, collection, hits, last_used \
|
||||
FROM asset \
|
||||
WHERE collection = ?1 AND id = ?2",
|
||||
params![collection, id],
|
||||
|row| {
|
||||
Ok(CacheEntry {
|
||||
pk: row.get(0)?,
|
||||
id: row.get(1)?,
|
||||
collection: row.get(2)?,
|
||||
hits: row.get(3)?,
|
||||
last_used: row.get(4)?,
|
||||
metadata: None,
|
||||
})
|
||||
},
|
||||
)?
|
||||
};
|
||||
|
||||
if with_metadata {
|
||||
entry.metadata = self.get_metadata(&entry.pk)?;
|
||||
}
|
||||
|
||||
Ok(entry)
|
||||
}
|
||||
|
||||
/// Définit ou remplace l'identifiant logique (`id`) d'une entrée.
|
||||
///
|
||||
/// Retourne `QueryReturnedNoRows` si la clé primaire est inconnue.
|
||||
pub fn set_id(&self, pk: &str, id: &str) -> rusqlite::Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let updated = conn.execute("UPDATE asset SET id = ?2 WHERE pk = ?1", params![pk, id])?;
|
||||
|
||||
if updated == 0 {
|
||||
return Err(Error::QueryReturnedNoRows);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Met à jour le compteur d'accès et la date du dernier accès
|
||||
@@ -183,47 +468,57 @@ impl DB {
|
||||
/// * `pk` - Clé primaire de l'élément
|
||||
pub fn update_hit(&self, pk: &str) -> rusqlite::Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
"UPDATE {} SET hits = hits + 1, last_used = ?1 WHERE pk = ?2",
|
||||
self.table_name
|
||||
);
|
||||
|
||||
conn.execute(&sql, params![Utc::now().to_rfc3339(), pk])?;
|
||||
conn.execute(
|
||||
&"UPDATE asset
|
||||
SET hits = hits + 1, last_used = ?1
|
||||
WHERE pk = ?2",
|
||||
params![Utc::now().to_rfc3339(), pk],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Purge toutes les entrées de la base de données
|
||||
/// Purge toutes les entrées de la base de données.
|
||||
pub fn purge(&self) -> rusqlite::Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!("DELETE FROM {}", self.table_name);
|
||||
conn.execute(&sql, [])?;
|
||||
conn.execute("DELETE FROM asset", [])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Récupère toutes les entrées, triées par nombre d'accès décroissant
|
||||
pub fn get_all(&self) -> rusqlite::Result<Vec<CacheEntry>> {
|
||||
/// Récupère toutes les entrées, triées par nombre d'accès décroissant.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `include_metadata` - Ajoute les métadonnées à chaque entrée si `true`.
|
||||
pub fn get_all(&self, include_metadata: bool) -> rusqlite::Result<Vec<CacheEntry>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
"SELECT pk, source_url, collection, hits, last_used, metadata_json FROM {} ORDER BY hits DESC",
|
||||
self.table_name
|
||||
);
|
||||
|
||||
let mut stmt = conn.prepare(&sql)?;
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT pk, id, collection, hits, last_used
|
||||
FROM asset
|
||||
ORDER BY hits DESC",
|
||||
)?;
|
||||
|
||||
let entries = stmt
|
||||
let mut entries = stmt
|
||||
.query_map([], |row| {
|
||||
Ok(CacheEntry {
|
||||
pk: row.get(0)?,
|
||||
source_url: row.get(1)?,
|
||||
id: row.get(1)?,
|
||||
collection: row.get(2)?,
|
||||
hits: row.get(3)?,
|
||||
last_used: row.get(4)?,
|
||||
metadata_json: row.get(5)?,
|
||||
metadata: None,
|
||||
})
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
|
||||
if include_metadata {
|
||||
for entry in entries.iter_mut() {
|
||||
entry.metadata = self.get_metadata(&entry.pk)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
@@ -231,53 +526,57 @@ impl DB {
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `collection` - Identifiant de la collection
|
||||
pub fn get_by_collection(&self, collection: &str) -> rusqlite::Result<Vec<CacheEntry>> {
|
||||
/// * `collection` - Identifiant de la collection.
|
||||
/// * `include_metadata` - Ajoute les métadonnées à chaque entrée si `true`.
|
||||
pub fn get_by_collection(
|
||||
&self,
|
||||
collection: &str,
|
||||
include_metadata: bool,
|
||||
) -> rusqlite::Result<Vec<CacheEntry>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
"SELECT pk, source_url, collection, hits, last_used, metadata_json FROM {} WHERE collection = ?1 ORDER BY hits DESC",
|
||||
self.table_name
|
||||
);
|
||||
|
||||
let mut stmt = conn.prepare(&sql)?;
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT pk, id, collection, hits, last_used
|
||||
FROM asset
|
||||
WHERE collection = ?1 ORDER BY hits DESC",
|
||||
)?;
|
||||
|
||||
let entries = stmt
|
||||
let mut entries = stmt
|
||||
.query_map([collection], |row| {
|
||||
Ok(CacheEntry {
|
||||
pk: row.get(0)?,
|
||||
source_url: row.get(1)?,
|
||||
id: row.get(1)?,
|
||||
collection: row.get(2)?,
|
||||
hits: row.get(3)?,
|
||||
last_used: row.get(4)?,
|
||||
metadata_json: row.get(5)?,
|
||||
metadata: None,
|
||||
})
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
|
||||
if include_metadata {
|
||||
for entry in entries.iter_mut() {
|
||||
entry.metadata = self.get_metadata(&entry.pk)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
/// Supprime toutes les entrées d'une collection
|
||||
/// Supprime toutes les entrées d'une collection.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `collection` - Identifiant de la collection à supprimer
|
||||
/// Les métadonnées associées sont supprimées automatiquement grâce à la
|
||||
/// contrainte `ON DELETE CASCADE`.
|
||||
pub fn delete_collection(&self, collection: &str) -> rusqlite::Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!("DELETE FROM {} WHERE collection = ?1", self.table_name);
|
||||
conn.execute(&sql, [collection])?;
|
||||
conn.execute("DELETE FROM asset WHERE collection = ?1", [collection])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Supprime une entrée de la base de données
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pk` - Clé primaire de l'élément à supprimer
|
||||
/// Supprime une entrée de la base de données ainsi que ses métadonnées.
|
||||
pub fn delete(&self, pk: &str) -> rusqlite::Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!("DELETE FROM {} WHERE pk = ?1", self.table_name);
|
||||
conn.execute(&sql, [pk])?;
|
||||
conn.execute("DELETE FROM asset WHERE pk = ?1", [pk])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -288,8 +587,7 @@ impl DB {
|
||||
/// Le nombre total d'entrées
|
||||
pub fn count(&self) -> rusqlite::Result<usize> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!("SELECT COUNT(*) FROM {}", self.table_name);
|
||||
let count: i64 = conn.query_row(&sql, [], |row| row.get(0))?;
|
||||
let count: i64 = conn.query_row("SELECT COUNT(*) FROM asset", [], |row| row.get(0))?;
|
||||
Ok(count as usize)
|
||||
}
|
||||
|
||||
@@ -307,65 +605,67 @@ impl DB {
|
||||
/// Liste des entrées les plus anciennes, triées par last_used ASC
|
||||
pub fn get_oldest(&self, limit: usize) -> rusqlite::Result<Vec<CacheEntry>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT pk, source_url, collection, hits, last_used, metadata_json
|
||||
FROM {}
|
||||
FROM asset
|
||||
ORDER BY last_used ASC, hits ASC
|
||||
LIMIT ?1",
|
||||
self.table_name
|
||||
);
|
||||
|
||||
let mut stmt = conn.prepare(&sql)?;
|
||||
)?;
|
||||
|
||||
let entries = stmt
|
||||
.query_map([limit], |row| {
|
||||
Ok(CacheEntry {
|
||||
pk: row.get(0)?,
|
||||
source_url: row.get(1)?,
|
||||
id: row.get(1)?,
|
||||
collection: row.get(2)?,
|
||||
hits: row.get(3)?,
|
||||
last_used: row.get(4)?,
|
||||
metadata_json: row.get(5)?,
|
||||
metadata: None,
|
||||
})
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
}
|
||||
|
||||
/// Récupère uniquement les métadonnées JSON d'une entrée
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pk` - Clé primaire de l'élément
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Les métadonnées JSON si présentes, None sinon
|
||||
pub fn get_metadata_json(&self, pk: &str) -> rusqlite::Result<Option<String>> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
"SELECT metadata_json FROM {} WHERE pk = ?1",
|
||||
self.table_name
|
||||
);
|
||||
|
||||
conn.query_row(&sql, [pk], |row| row.get(0))
|
||||
}
|
||||
|
||||
/// Met à jour uniquement les métadonnées JSON d'une entrée existante
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `pk` - Clé primaire de l'élément
|
||||
/// * `metadata_json` - Métadonnées JSON à stocker
|
||||
pub fn update_metadata(&self, pk: &str, metadata_json: &str) -> rusqlite::Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let sql = format!(
|
||||
"UPDATE {} SET metadata_json = ?1 WHERE pk = ?2",
|
||||
self.table_name
|
||||
);
|
||||
|
||||
conn.execute(&sql, params![metadata_json, pk])?;
|
||||
Ok(())
|
||||
/// Convertit une ligne de la table `metadata` en valeur JSON.
|
||||
fn decode_metadata_value(
|
||||
key: &str,
|
||||
value_type: &str,
|
||||
raw: Option<String>,
|
||||
) -> rusqlite::Result<Value> {
|
||||
match value_type {
|
||||
"null" => Ok(Value::Null),
|
||||
"boolean" => {
|
||||
let raw = raw.as_deref().ok_or_else(|| {
|
||||
Error::InvalidParameterName(format!("missing boolean metadata for '{key}'"))
|
||||
})?;
|
||||
raw.parse::<bool>().map(Value::Bool).map_err(|_| {
|
||||
Error::InvalidParameterName(format!("invalid boolean metadata for '{key}'"))
|
||||
})
|
||||
}
|
||||
"number" => {
|
||||
let raw = raw.as_deref().ok_or_else(|| {
|
||||
Error::InvalidParameterName(format!("missing number metadata for '{key}'"))
|
||||
})?;
|
||||
Number::from_str(raw).map(Value::Number).map_err(|_| {
|
||||
Error::InvalidParameterName(format!("invalid number metadata for '{key}'"))
|
||||
})
|
||||
}
|
||||
"string" => {
|
||||
let raw = raw.unwrap_or_default();
|
||||
let trimmed = raw.trim_start();
|
||||
if trimmed.starts_with('{') || trimmed.starts_with('[') {
|
||||
if let Ok(json) = serde_json::from_str::<Value>(&raw) {
|
||||
return Ok(json);
|
||||
}
|
||||
}
|
||||
Ok(Value::String(raw))
|
||||
}
|
||||
other => Err(Error::InvalidParameterName(format!(
|
||||
"unknown metadata type '{other}' for key '{key}'"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -501,7 +501,8 @@ pub async fn peek_header(url: &str, max_bytes: usize) -> Result<Vec<u8>, String>
|
||||
|
||||
// Si le serveur ne supporte pas Range (status 200 au lieu de 206),
|
||||
// on lit quand même mais on limite la lecture
|
||||
if !response.status().is_success() && response.status() != reqwest::StatusCode::PARTIAL_CONTENT {
|
||||
if !response.status().is_success() && response.status() != reqwest::StatusCode::PARTIAL_CONTENT
|
||||
{
|
||||
return Err(format!("HTTP error: {}", response.status()));
|
||||
}
|
||||
|
||||
|
||||
@@ -143,8 +143,8 @@ pub use cache::{Cache, CacheConfig};
|
||||
pub use cache_trait::{pk_from_content_header, pk_from_url, FileCache};
|
||||
pub use db::{CacheEntry, DB};
|
||||
pub use download::{
|
||||
download, download_with_transformer, ingest_with_transformer, peek_header,
|
||||
peek_reader_header, Download, StreamTransformer,
|
||||
download, download_with_transformer, ingest_with_transformer, peek_header, peek_reader_header,
|
||||
Download, StreamTransformer,
|
||||
};
|
||||
|
||||
#[cfg(feature = "pmoserver")]
|
||||
|
||||
Reference in New Issue
Block a user