From 1f155d1bfbf002969e0345fc502581d2c15dafbb Mon Sep 17 00:00:00 2001 From: despiegk Date: Wed, 9 Apr 2025 08:11:28 +0200 Subject: [PATCH] ... --- doctree/src/collection.rs | 4 +++ doctree/src/doctree.rs | 28 ++++++++++++++-- doctree/src/lib.rs | 1 - doctree/src/storage.rs | 67 ++++++++++++++++++++++++++++++++++++++- doctree/src/utils.rs | 1 - 5 files changed, 96 insertions(+), 5 deletions(-) diff --git a/doctree/src/collection.rs b/doctree/src/collection.rs index 7e5874a..cfc197e 100644 --- a/doctree/src/collection.rs +++ b/doctree/src/collection.rs @@ -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 { diff --git a/doctree/src/doctree.rs b/doctree/src/doctree.rs index 086e3a2..910f160 100644 --- a/doctree/src/doctree.rs +++ b/doctree/src/doctree.rs @@ -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(), }; diff --git a/doctree/src/lib.rs b/doctree/src/lib.rs index 7acdc78..dd8638f 100644 --- a/doctree/src/lib.rs +++ b/doctree/src/lib.rs @@ -4,7 +4,6 @@ //! and processing includes between documents. // Import lazy_static for global state -#[macro_use] extern crate lazy_static; mod error; diff --git a/doctree/src/storage.rs b/doctree/src/storage.rs index 71493b1..d8fa6cb 100644 --- a/doctree/src/storage.rs +++ b/doctree/src/storage.rs @@ -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 { + 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 = 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 diff --git a/doctree/src/utils.rs b/doctree/src/utils.rs index 327a4ae..31a27c7 100644 --- a/doctree/src/utils.rs +++ b/doctree/src/utils.rs @@ -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