This commit is contained in:
despiegk 2025-04-09 08:11:28 +02:00
parent b9df692a54
commit 1f155d1bfb
5 changed files with 96 additions and 5 deletions

View File

@ -64,6 +64,10 @@ impl Collection {
println!("DEBUG: Deleting existing collection data from Redis key 'collections:{}'", self.name);
self.storage.delete_collection(&self.name)?;
// Store the collection's path in Redis
println!("DEBUG: Storing collection path in Redis key 'collections:{}:path'", self.name);
self.storage.store_collection_path(&self.name, &self.path.to_string_lossy())?;
// Walk through the directory
let walker = WalkDir::new(&self.path);
for entry_result in walker {

View File

@ -207,9 +207,21 @@ impl DocTree {
return Err(DocTreeError::CollectionNotFound(name.to_string()));
}
// Try to get the collection's path from Redis
let path = match self.storage.get_collection_path(name) {
Ok(path_str) => {
println!("DEBUG: Found collection path in Redis: {}", path_str);
PathBuf::from(path_str)
},
Err(e) => {
println!("DEBUG: Could not retrieve collection path from Redis: {}", e);
PathBuf::new() // Fallback to empty path if not found
}
};
// Create a new collection
let collection = Collection {
path: PathBuf::new(), // We don't have the path, but it's not needed for Redis operations
path,
name: name.to_string(),
storage: self.storage.clone(),
};
@ -236,9 +248,21 @@ impl DocTree {
continue;
}
// Try to get the collection's path from Redis
let path = match self.storage.get_collection_path(&name) {
Ok(path_str) => {
println!("DEBUG: Found collection path in Redis: {}", path_str);
PathBuf::from(path_str)
},
Err(e) => {
println!("DEBUG: Could not retrieve collection path from Redis: {}", e);
PathBuf::new() // Fallback to empty path if not found
}
};
// Create a new collection
let collection = Collection {
path: PathBuf::new(), // We don't have the path, but it's not needed for Redis operations
path,
name: name.clone(),
storage: self.storage.clone(),
};

View File

@ -4,7 +4,6 @@
//! and processing includes between documents.
// Import lazy_static for global state
#[macro_use]
extern crate lazy_static;
mod error;

View File

@ -1,4 +1,4 @@
use redis::{Client, Commands, Connection};
use redis::{Client, Connection};
use std::sync::{Arc, Mutex};
use crate::error::{DocTreeError, Result};
@ -298,6 +298,71 @@ impl RedisStorage {
Ok(())
}
/// Store a collection's path
///
/// # Arguments
///
/// * `collection` - Collection name
/// * `path` - Collection path
///
/// # Returns
///
/// Ok(()) on success or an error
pub fn store_collection_path(&self, collection: &str, path: &str) -> Result<()> {
let redis_key = format!("collections:{}:path", collection);
println!("DEBUG: Redis operation - SET {} {}", redis_key, path);
// Get a connection from the pool
let mut conn = self.connection.lock().unwrap();
// Store the path using SET
redis::cmd("SET")
.arg(&redis_key)
.arg(path)
.execute(&mut *conn);
println!("DEBUG: Stored collection path in Redis - collection: '{}', path: '{}'",
collection, path);
Ok(())
}
/// Get a collection's path
///
/// # Arguments
///
/// * `collection` - Collection name
///
/// # Returns
///
/// The collection path or an error
pub fn get_collection_path(&self, collection: &str) -> Result<String> {
let redis_key = format!("collections:{}:path", collection);
println!("DEBUG: Redis operation - GET {}", redis_key);
// Get a connection from the pool
let mut conn = self.connection.lock().unwrap();
// Get the path using GET
let result: Option<String> = redis::cmd("GET")
.arg(&redis_key)
.query(&mut *conn)
.map_err(|e| DocTreeError::RedisError(format!("Redis error: {}", e)))?;
// Check if the path exists
match result {
Some(path) => {
println!("DEBUG: Retrieved collection path from Redis - collection: '{}', path: '{}'",
collection, path);
Ok(path)
},
None => {
println!("DEBUG: Collection path not found in Redis - collection: '{}'",
collection);
Err(DocTreeError::CollectionNotFound(collection.to_string()))
}
}
}
}
// Implement Clone for RedisStorage

View File

@ -1,5 +1,4 @@
use pulldown_cmark::{Parser, Options, html};
use std::path::Path;
use sal::text;
/// Fix a name to be used as a key