Refactor cache API and expand installation docs
- Remove CacheCheckResult and simplify Cache::check_cache to return Option<OrganizationPlan> - Replace cache_response_with_metadata with cache_response that takes a base path; update Gemini client and tests to use new API - Improve load_or_create error handling and early-return when cache file missing - Expand README/docs/INSTALLATION.md with detailed per-OS install and PATH instructions
This commit is contained in:
47
README.md
47
README.md
@@ -26,26 +26,53 @@ NoEntropy is a smart command-line tool that organizes your cluttered Downloads f
|
||||
|
||||
### Installation
|
||||
|
||||
**Option 1: Download Pre-built Binary**
|
||||
### Download Pre-built Binary
|
||||
|
||||
Download the binary for your operating system from [releases](https://github.com/glitchySid/noentropy/releases):
|
||||
Download the latest release for your operating system from [releases](https://github.com/glitchySid/noentropy/releases):
|
||||
|
||||
| OS | Download |
|
||||
|----|----------|
|
||||
| Linux x86_64 | `noentropy-x86_64-unknown-linux-gnu.tar.gz` |
|
||||
| macOS x86_64 | `noentropy-x86_64-apple-darwin.tar.gz` |
|
||||
| macOS arm64 | `noentropy-aarch64-apple-darwin.tar.gz` |
|
||||
| Windows x86_64 | `noentropy-x86_64-pc-windows-msvc.zip` |
|
||||
|
||||
**Linux/macOS:**
|
||||
```bash
|
||||
# Linux/macOS: Give execute permissions
|
||||
chmod +x noentropy
|
||||
# Download and extract
|
||||
curl -LO https://github.com/glitchySid/noentropy/releases/latest/download/noentropy-x86_64-unknown-linux-gnu.tar.gz
|
||||
tar -xzf noentropy-x86_64-unknown-linux-gnu.tar.gz
|
||||
|
||||
# Run NoEntropy
|
||||
./noentropy
|
||||
# Add to PATH (user-level)
|
||||
mkdir -p ~/.local/bin
|
||||
mv noentropy ~/.local/bin/
|
||||
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc # or ~/.zshrc
|
||||
source ~/.bashrc
|
||||
|
||||
# Verify
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
**Option 2: Build from Source**
|
||||
**Windows:**
|
||||
```powershell
|
||||
# Download and extract
|
||||
Invoke-WebRequest -Uri "https://github.com/glitchySid/noentropy/releases/latest/download/noentropy-x86_64-pc-windows-msvc.zip" -OutFile "noentropy.zip"
|
||||
Expand-Archive -Path "noentropy.zip" -DestinationPath "noentropy"
|
||||
|
||||
# Add to PATH (User-level)
|
||||
$env:PATH += ";$env:USERPROFILE\AppData\Local\NoEntropy"
|
||||
|
||||
# Or add via System Properties:
|
||||
# Win + R → sysdm.cpl → Environment Variables → Edit PATH
|
||||
```
|
||||
|
||||
See the [Installation Guide](docs/INSTALLATION.md) for detailed instructions.
|
||||
|
||||
### Build from Source
|
||||
|
||||
```bash
|
||||
# Clone repository
|
||||
git clone https://github.com/glitchySid/noentropy.git
|
||||
cd noentropy
|
||||
|
||||
# Build and run
|
||||
cargo build --release
|
||||
./target/release/noentropy
|
||||
```
|
||||
|
||||
@@ -6,7 +6,6 @@ This guide covers different ways to install and set up NoEntropy on your system.
|
||||
|
||||
Before installing NoEntropy, ensure you have:
|
||||
|
||||
- **Rust 2024 Edition** or later (if building from source)
|
||||
- **Google Gemini API Key** - Get one at [https://ai.google.dev/](https://ai.google.dev/)
|
||||
- A folder full of unorganized files to clean up!
|
||||
|
||||
@@ -14,48 +13,207 @@ Before installing NoEntropy, ensure you have:
|
||||
|
||||
The easiest way to get started is to download a pre-built binary for your operating system.
|
||||
|
||||
1. **Download Binary**
|
||||
|
||||
Visit the releases page and download the binary for your operating system (Windows, Linux, or macOS):
|
||||
```bash
|
||||
https://github.com/glitchySid/noentropy/releases
|
||||
### Step 1: Download the Binary
|
||||
|
||||
Visit the [releases page](https://github.com/glitchySid/noentropy/releases) and download the appropriate archive for your system:
|
||||
|
||||
| Operating System | Architecture | File to Download |
|
||||
|------------------|--------------|------------------|
|
||||
| Linux | x86_64 | `noentropy-x86_64-unknown-linux-gnu.tar.gz` |
|
||||
| macOS | x86_64 (Intel) | `noentropy-x86_64-apple-darwin.tar.gz` |
|
||||
| macOS | arm64 (Apple Silicon) | `noentropy-aarch64-apple-darwin.tar.gz` |
|
||||
| Windows | x86_64 | `noentropy-x86_64-pc-windows-msvc.zip` |
|
||||
|
||||
Or download directly from the command line:
|
||||
|
||||
**Linux:**
|
||||
```bash
|
||||
curl -LO https://github.com/glitchySid/noentropy/releases/latest/download/noentropy-x86_64-unknown-linux-gnu.tar.gz
|
||||
```
|
||||
|
||||
**macOS (Intel):**
|
||||
```bash
|
||||
curl -LO https://github.com/glitchySid/noentropy/releases/latest/download/noentropy-x86_64-apple-darwin.tar.gz
|
||||
```
|
||||
|
||||
**macOS (Apple Silicon):**
|
||||
```bash
|
||||
curl -LO https://github.com/glitchySid/noentropy/releases/latest/download/noentropy-aarch64-apple-darwin.tar.gz
|
||||
```
|
||||
|
||||
**Windows (PowerShell):**
|
||||
```powershell
|
||||
Invoke-WebRequest -Uri "https://github.com/glitchySid/noentropy/releases/latest/download/noentropy-x86_64-pc-windows-msvc.zip" -OutFile "noentropy.zip"
|
||||
```
|
||||
|
||||
### Step 2: Extract the Archive
|
||||
|
||||
**Linux/macOS:**
|
||||
```bash
|
||||
tar -xzf noentropy-x86_64-unknown-linux-gnu.tar.gz
|
||||
```
|
||||
|
||||
**Windows:**
|
||||
Right-click the downloaded zip file and select "Extract All..." or use PowerShell:
|
||||
```powershell
|
||||
Expand-Archive -Path "noentropy.zip" -DestinationPath "noentropy"
|
||||
```
|
||||
|
||||
### Step 3: Add to PATH
|
||||
|
||||
You need to add the folder containing `noentropy` to your system's PATH so you can run it from anywhere.
|
||||
|
||||
#### Linux/macOS
|
||||
|
||||
**Option A: User-level (recommended, no sudo required)**
|
||||
|
||||
```bash
|
||||
# Create local bin directory if it doesn't exist
|
||||
mkdir -p ~/.local/bin
|
||||
|
||||
# Move the binary to a location in your PATH
|
||||
mv noentropy ~/.local/bin/
|
||||
|
||||
# Add to PATH temporarily for this session
|
||||
export PATH="$HOME/.local/bin:$PATH"
|
||||
|
||||
# Verify it works
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
To make this permanent, add this line to your shell configuration file:
|
||||
|
||||
**For bash (`~/.bashrc` or `~/.bash_profile`):**
|
||||
```bash
|
||||
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
|
||||
source ~/.bashrc
|
||||
```
|
||||
|
||||
**For zsh (`~/.zshrc`):**
|
||||
```bash
|
||||
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
|
||||
source ~/.zshrc
|
||||
```
|
||||
|
||||
**Option B: System-wide (requires sudo)**
|
||||
|
||||
```bash
|
||||
# Move to system bin (requires sudo on most systems)
|
||||
sudo mv noentropy /usr/local/bin/
|
||||
|
||||
# Verify it works
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
#### Windows
|
||||
|
||||
**Option A: User-level (recommended)**
|
||||
|
||||
1. Move the extracted `noentropy.exe` to a folder, for example:
|
||||
```
|
||||
C:\Users\<YourUsername>\AppData\Local\NoEntropy
|
||||
```
|
||||
|
||||
2. **Give Permission (Linux/macOS only)**
|
||||
|
||||
Make the binary executable:
|
||||
```bash
|
||||
chmod +x noentropy
|
||||
2. Add to User PATH:
|
||||
- Press `Win + R`, type `sysdm.cpl`, press Enter
|
||||
- Click "Environment Variables"
|
||||
- Under "User variables", select "Path", click "Edit"
|
||||
- Click "New" and add:
|
||||
```
|
||||
C:\Users\<YourUsername>\AppData\Local\NoEntropy
|
||||
```
|
||||
- Click "OK" on all dialogs
|
||||
|
||||
3. **Alternative using PowerShell (Admin):**
|
||||
```powershell
|
||||
# Create installation directory
|
||||
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\AppData\Local\NoEntropy"
|
||||
|
||||
# Move the binary
|
||||
Move-Item -Path ".\noentropy.exe" -Destination "$env:USERPROFILE\AppData\Local\NoEntropy\"
|
||||
|
||||
# Add to PATH (User level)
|
||||
$path = [Environment]::GetEnvironmentVariable("PATH", "User")
|
||||
$newPath = "$path;$env:USERPROFILE\AppData\Local\NoEntropy"
|
||||
[Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
|
||||
|
||||
# Verify
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
3. **Run NoEntropy**
|
||||
|
||||
```bash
|
||||
./noentropy
|
||||
```
|
||||
4. **Restart your terminal** or start a new Command Prompt/PowerShell window for the PATH changes to take effect.
|
||||
|
||||
**Option B: System-wide (requires Administrator)**
|
||||
|
||||
```powershell
|
||||
# Run PowerShell as Administrator
|
||||
Move-Item -Path ".\noentropy.exe" -Destination "C:\Program Files\NoEntropy\noentropy.exe"
|
||||
|
||||
# Add to system PATH
|
||||
$path = [Environment]::GetEnvironmentVariable("PATH", "Machine")
|
||||
$newPath = "$path;C:\Program Files\NoEntropy"
|
||||
[Environment]::SetEnvironmentVariable("PATH", $newPath, "Machine")
|
||||
|
||||
# Verify
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
### Step 4: Verify Installation
|
||||
|
||||
```bash
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
You should see the help message with available options.
|
||||
|
||||
---
|
||||
|
||||
## Option 2: Build from Source
|
||||
|
||||
If you prefer to build from source or want the latest development version:
|
||||
|
||||
1. **Clone the Repository**
|
||||
|
||||
```bash
|
||||
git clone https://github.com/glitchySid/noentropy.git
|
||||
cd noentropy
|
||||
```
|
||||
### Prerequisites
|
||||
|
||||
2. **Build the Application**
|
||||
|
||||
```bash
|
||||
cargo build --release
|
||||
```
|
||||
- **Rust 2024 Edition** or later - Install from [rustup.rs](https://rustup.rs/)
|
||||
- **Git** - For cloning the repository
|
||||
|
||||
3. **Run the Application**
|
||||
|
||||
```bash
|
||||
./target/release/noentropy
|
||||
```
|
||||
### Step 1: Clone the Repository
|
||||
|
||||
```bash
|
||||
git clone https://github.com/glitchySid/noentropy.git
|
||||
cd noentropy
|
||||
```
|
||||
|
||||
### Step 2: Build the Application
|
||||
|
||||
```bash
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
The binary will be located at `target/release/noentropy`.
|
||||
|
||||
### Step 3: Install Globally (Optional)
|
||||
|
||||
**Linux/macOS:**
|
||||
```bash
|
||||
# User-level installation
|
||||
mkdir -p ~/.local/bin
|
||||
cp target/release/noentropy ~/.local/bin/
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
**Windows:**
|
||||
```powershell
|
||||
# Create installation directory
|
||||
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\AppData\Local\NoEntropy"
|
||||
|
||||
# Copy the binary
|
||||
Copy-Item -Path ".\target\release\noentropy.exe" -Destination "$env:USERPROFILE\AppData\Local\NoEntropy\"
|
||||
|
||||
# Add to PATH (see Windows instructions above)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## First-Run Setup
|
||||
|
||||
@@ -69,19 +227,40 @@ NoEntropy provides an interactive setup if configuration is missing:
|
||||
- **Missing download folder?** → You'll be prompted to specify it (with default suggestion)
|
||||
- **Both missing?** → You'll be guided through complete setup
|
||||
|
||||
Configuration is automatically saved to `~/.config/noentropy/config.toml` after interactive setup.
|
||||
Configuration is automatically saved to:
|
||||
|
||||
| OS | Path |
|
||||
|----|------|
|
||||
| Linux/macOS | `~/.config/noentropy/config.toml` |
|
||||
| Windows | `%APPDATA%\NoEntropy\config.toml` |
|
||||
|
||||
### Manual Configuration
|
||||
|
||||
Alternatively, you can manually create the configuration file:
|
||||
|
||||
**Linux/macOS:**
|
||||
```bash
|
||||
mkdir -p ~/.config/noentropy
|
||||
cp config.example.toml ~/.config/noentropy/config.toml
|
||||
nano ~/.config/noentropy/config.toml
|
||||
```
|
||||
|
||||
**Windows:**
|
||||
```powershell
|
||||
# Create config directory
|
||||
New-Item -ItemType Directory -Force -Path "$env:APPDATA\NoEntropy"
|
||||
|
||||
# Copy example config
|
||||
Copy-Item -Path ".\config.example.toml" -Destination "$env:APPDATA\NoEntropy\config.toml"
|
||||
|
||||
# Edit with Notepad
|
||||
notepad "$env:APPDATA\NoEntropy\config.toml"
|
||||
```
|
||||
|
||||
See the [Configuration Guide](CONFIGURATION.md) for detailed configuration options.
|
||||
|
||||
---
|
||||
|
||||
## Getting Your Gemini API Key
|
||||
|
||||
1. Visit [Google AI Studio](https://ai.google.dev/)
|
||||
@@ -89,16 +268,28 @@ See the [Configuration Guide](CONFIGURATION.md) for detailed configuration optio
|
||||
3. Create a new API key
|
||||
4. Copy the key to your configuration file or enter it during interactive setup
|
||||
|
||||
---
|
||||
|
||||
## Verification
|
||||
|
||||
To verify your installation works correctly:
|
||||
|
||||
1. Run NoEntropy with the `--dry-run` flag:
|
||||
```bash
|
||||
./noentropy --dry-run
|
||||
```
|
||||
```bash
|
||||
noentropy --help
|
||||
```
|
||||
|
||||
2. You should see NoEntropy scan your downloads folder and display an organization plan without moving any files.
|
||||
If you see the help output, installation was successful!
|
||||
|
||||
To test file organization:
|
||||
|
||||
```bash
|
||||
# Organize your downloads folder (or configured folder)
|
||||
noentropy --dry-run
|
||||
```
|
||||
|
||||
You should see NoEntropy scan your folder and display an organization plan without moving any files.
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
@@ -106,15 +297,19 @@ To verify your installation works correctly:
|
||||
- [Usage Guide](USAGE.md) - Learn how to use NoEntropy effectively
|
||||
- [How It Works](HOW_IT_WORKS.md) - Understand the organization process
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
If you encounter issues during installation, check the [Troubleshooting Guide](TROUBLESHOOTING.md).
|
||||
|
||||
Common installation issues:
|
||||
|
||||
- **"noentropy: command not found"**: The folder is not in your PATH. Restart your terminal or run `source ~/.bashrc` (or `source ~/.zshrc`).
|
||||
- **Permission denied (Linux/macOS)**: Make sure the binary has execute permissions: `chmod +x noentropy`
|
||||
- **Windows PATH not updating**: Restart your terminal or computer after adding to PATH.
|
||||
- **Rust not installed**: Install Rust from [rustup.rs](https://rustup.rs/)
|
||||
- **Build errors**: Ensure you have the latest Rust toolchain: `rustup update`
|
||||
- **Permission denied**: Make sure the binary has execute permissions (Linux/macOS)
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -93,17 +93,13 @@ impl GeminiClient {
|
||||
) -> Result<OrganizationPlan, GeminiError> {
|
||||
let url = self.build_url();
|
||||
|
||||
// Check cache and get pre-fetched metadata in one pass
|
||||
let cache_result = match (cache.as_ref(), base_path) {
|
||||
(Some(c), Some(bp)) => Some(c.check_cache(&filenames, bp)),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// Return cached response if valid
|
||||
if let Some(ref result) = cache_result
|
||||
&& let Some(ref cached_response) = result.cached_response
|
||||
{
|
||||
return Ok(cached_response.clone());
|
||||
// Check cache first
|
||||
if let Some(ref mut c) = cache {
|
||||
if let Some(bp) = base_path {
|
||||
if let Some(cached) = c.check_cache(&filenames, bp) {
|
||||
return Ok(cached);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let prompt = PromptBuilder::new(&filenames).build_categorization_prompt(&self.categories);
|
||||
@@ -112,9 +108,9 @@ impl GeminiClient {
|
||||
let res = self.send_request_with_retry(&url, &request_body).await?;
|
||||
let plan = self.parse_categorization_response(res).await?;
|
||||
|
||||
// Cache response using pre-fetched metadata (no second metadata lookup)
|
||||
if let (Some(cache), Some(result)) = (cache.as_mut(), cache_result) {
|
||||
cache.cache_response_with_metadata(&filenames, plan.clone(), result.file_metadata);
|
||||
// Cache the response
|
||||
if let (Some(cache), Some(base_path)) = (cache, base_path) {
|
||||
cache.cache_response(&filenames, plan.clone(), base_path);
|
||||
}
|
||||
|
||||
Ok(plan)
|
||||
|
||||
@@ -6,12 +6,6 @@ use std::fs;
|
||||
use std::path::Path;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// Result of checking the cache - includes pre-fetched metadata to avoid double lookups
|
||||
pub struct CacheCheckResult {
|
||||
pub cached_response: Option<OrganizationPlan>,
|
||||
pub file_metadata: HashMap<String, FileMetadata>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Cache {
|
||||
entries: HashMap<String, CacheEntry>,
|
||||
@@ -37,26 +31,25 @@ impl Cache {
|
||||
}
|
||||
|
||||
pub fn load_or_create(cache_path: &Path) -> Self {
|
||||
if cache_path.exists() {
|
||||
match fs::read_to_string(cache_path) {
|
||||
Ok(content) => match serde_json::from_str::<Cache>(&content) {
|
||||
Ok(cache) => {
|
||||
println!("Loaded cache with {} entries", cache.entries.len());
|
||||
cache
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Cache corrupted, creating new cache");
|
||||
Self::new()
|
||||
}
|
||||
},
|
||||
if !cache_path.exists() {
|
||||
return Self::new();
|
||||
}
|
||||
|
||||
match fs::read_to_string(cache_path) {
|
||||
Ok(content) => match serde_json::from_str::<Cache>(&content) {
|
||||
Ok(cache) => {
|
||||
println!("Loaded cache with {} entries", cache.entries.len());
|
||||
cache
|
||||
}
|
||||
Err(_) => {
|
||||
println!("Failed to read cache, creating new cache");
|
||||
println!("Cache corrupted, creating new cache");
|
||||
Self::new()
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
println!("Failed to read cache, creating new cache");
|
||||
Self::new()
|
||||
}
|
||||
} else {
|
||||
println!("Creating new cache file");
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,107 +63,40 @@ impl Cache {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Checks cache and returns pre-fetched metadata to avoid double lookups.
|
||||
/// The returned metadata can be passed to `cache_response_with_metadata` on cache miss.
|
||||
pub fn check_cache(&self, filenames: &[String], base_path: &Path) -> CacheCheckResult {
|
||||
// Fetch metadata once for all files
|
||||
let file_metadata: HashMap<String, FileMetadata> = filenames
|
||||
.iter()
|
||||
.filter_map(|filename| {
|
||||
let file_path = base_path.join(filename);
|
||||
Self::get_file_metadata(&file_path)
|
||||
.ok()
|
||||
.map(|m| (filename.clone(), m))
|
||||
})
|
||||
.collect();
|
||||
pub fn check_cache(&self, filenames: &[String], base_path: &Path) -> Option<OrganizationPlan> {
|
||||
let cache_key = Self::generate_cache_key(filenames);
|
||||
let entry = self.entries.get(&cache_key)?;
|
||||
|
||||
let cache_key = self.generate_cache_key(filenames);
|
||||
|
||||
let cached_response = self.entries.get(&cache_key).and_then(|entry| {
|
||||
// Validate all files are unchanged using pre-fetched metadata
|
||||
let all_unchanged = filenames.iter().all(|filename| {
|
||||
match (
|
||||
file_metadata.get(filename),
|
||||
entry.file_metadata.get(filename),
|
||||
) {
|
||||
(Some(current), Some(cached)) => current == cached,
|
||||
_ => false,
|
||||
}
|
||||
});
|
||||
|
||||
if all_unchanged {
|
||||
println!("Using cached response (timestamp: {})", entry.timestamp);
|
||||
Some(entry.response.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
let all_unchanged = filenames.iter().all(|filename| {
|
||||
let file_path = base_path.join(filename);
|
||||
FileMetadata::from_path(&file_path).ok().as_ref() == entry.file_metadata.get(filename)
|
||||
});
|
||||
|
||||
CacheCheckResult {
|
||||
cached_response,
|
||||
file_metadata,
|
||||
if all_unchanged {
|
||||
println!("Using cached response (timestamp: {})", entry.timestamp);
|
||||
Some(entry.response.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Cache response using pre-fetched metadata (avoids double metadata lookup)
|
||||
pub fn cache_response_with_metadata(
|
||||
&mut self,
|
||||
filenames: &[String],
|
||||
response: OrganizationPlan,
|
||||
file_metadata: HashMap<String, FileMetadata>,
|
||||
) {
|
||||
let cache_key = self.generate_cache_key(filenames);
|
||||
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
|
||||
let entry = CacheEntry {
|
||||
response,
|
||||
timestamp,
|
||||
file_metadata,
|
||||
};
|
||||
|
||||
self.entries.insert(cache_key, entry);
|
||||
|
||||
if self.entries.len() > self.max_entries {
|
||||
self.evict_oldest();
|
||||
}
|
||||
|
||||
println!("Cached response for {} files", filenames.len());
|
||||
}
|
||||
|
||||
/// Legacy method - checks cache for a response (fetches metadata internally)
|
||||
#[deprecated(
|
||||
note = "Use check_cache() + cache_response_with_metadata() to avoid double metadata lookups"
|
||||
)]
|
||||
pub fn get_cached_response(
|
||||
&self,
|
||||
filenames: &[String],
|
||||
base_path: &Path,
|
||||
) -> Option<OrganizationPlan> {
|
||||
let result = self.check_cache(filenames, base_path);
|
||||
result.cached_response
|
||||
}
|
||||
|
||||
/// Legacy method - caches a response (fetches metadata internally)
|
||||
#[deprecated(note = "Use cache_response_with_metadata() with pre-fetched metadata")]
|
||||
pub fn cache_response(
|
||||
&mut self,
|
||||
filenames: &[String],
|
||||
response: OrganizationPlan,
|
||||
base_path: &Path,
|
||||
) {
|
||||
let cache_key = self.generate_cache_key(filenames);
|
||||
let mut file_metadata = HashMap::new();
|
||||
let cache_key = Self::generate_cache_key(filenames);
|
||||
|
||||
for filename in filenames {
|
||||
let file_path = base_path.join(filename);
|
||||
if let Ok(metadata) = Self::get_file_metadata(&file_path) {
|
||||
file_metadata.insert(filename.clone(), metadata);
|
||||
}
|
||||
}
|
||||
let file_metadata: HashMap<String, FileMetadata> = filenames
|
||||
.iter()
|
||||
.filter_map(|filename| {
|
||||
let file_path = base_path.join(filename);
|
||||
FileMetadata::from_path(&file_path)
|
||||
.ok()
|
||||
.map(|m| (filename.clone(), m))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
@@ -192,12 +118,12 @@ impl Cache {
|
||||
println!("Cached response for {} files", filenames.len());
|
||||
}
|
||||
|
||||
fn generate_cache_key(&self, filenames: &[String]) -> String {
|
||||
let mut sorted_filenames = filenames.to_vec();
|
||||
sorted_filenames.sort();
|
||||
|
||||
fn generate_cache_key(filenames: &[String]) -> String {
|
||||
let mut hasher = Hasher::new();
|
||||
for filename in &sorted_filenames {
|
||||
let mut sorted: Vec<_> = filenames.iter().collect();
|
||||
sorted.sort();
|
||||
|
||||
for filename in sorted {
|
||||
hasher.update(filename.as_bytes());
|
||||
hasher.update(b"|");
|
||||
}
|
||||
@@ -205,16 +131,6 @@ impl Cache {
|
||||
hasher.finalize().to_hex().to_string()
|
||||
}
|
||||
|
||||
fn get_file_metadata(file_path: &Path) -> Result<FileMetadata, Box<dyn std::error::Error>> {
|
||||
let metadata = fs::metadata(file_path)?;
|
||||
let modified = metadata.modified()?.duration_since(UNIX_EPOCH)?.as_secs();
|
||||
|
||||
Ok(FileMetadata {
|
||||
size: metadata.len(),
|
||||
modified,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn cleanup_old_entries(&mut self, max_age_seconds: u64) {
|
||||
let current_time = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
@@ -231,8 +147,8 @@ impl Cache {
|
||||
println!("Cleaned up {} old cache entries", removed_count);
|
||||
}
|
||||
|
||||
if self.entries.len() > self.max_entries {
|
||||
self.compact_cache();
|
||||
while self.entries.len() > self.max_entries {
|
||||
self.evict_oldest();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,9 +164,11 @@ impl Cache {
|
||||
}
|
||||
}
|
||||
|
||||
fn compact_cache(&mut self) {
|
||||
while self.entries.len() > self.max_entries {
|
||||
self.evict_oldest();
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.entries.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.entries.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
pub mod cache;
|
||||
pub mod undo_log;
|
||||
|
||||
pub use cache::{Cache, CacheCheckResult};
|
||||
pub use cache::Cache;
|
||||
pub use undo_log::UndoLog;
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
//! - File reading for deep inspection
|
||||
//! - Integration between components
|
||||
|
||||
use noentropy::files::{FileBatch, is_text_file, read_file_sample};
|
||||
use noentropy::files::{is_text_file, read_file_sample, FileBatch};
|
||||
use noentropy::models::{FileCategory, OrganizationPlan};
|
||||
use noentropy::storage::{Cache, UndoLog};
|
||||
use std::collections::HashMap;
|
||||
@@ -53,18 +53,18 @@ fn test_cache_stores_and_retrieves_organization_plans() {
|
||||
}],
|
||||
};
|
||||
|
||||
// Check cache (will also fetch metadata)
|
||||
let check_result = cache.check_cache(&filenames, temp_dir.path());
|
||||
assert!(check_result.cached_response.is_none());
|
||||
// Check cache (returns None on miss)
|
||||
let cached = cache.check_cache(&filenames, temp_dir.path());
|
||||
assert!(cached.is_none());
|
||||
|
||||
// Store in cache with metadata
|
||||
cache.cache_response_with_metadata(&filenames, plan.clone(), check_result.file_metadata);
|
||||
// Store in cache
|
||||
cache.cache_response(&filenames, plan.clone(), temp_dir.path());
|
||||
|
||||
// Retrieve from cache
|
||||
let check_result2 = cache.check_cache(&filenames, temp_dir.path());
|
||||
assert!(check_result2.cached_response.is_some());
|
||||
let cached2 = cache.check_cache(&filenames, temp_dir.path());
|
||||
assert!(cached2.is_some());
|
||||
|
||||
let cached = check_result2.cached_response.unwrap();
|
||||
let cached = cached2.unwrap();
|
||||
assert_eq!(cached.files.len(), 1);
|
||||
assert_eq!(cached.files[0].filename, "test.txt");
|
||||
}
|
||||
@@ -84,8 +84,7 @@ fn test_cache_invalidates_on_file_modification() {
|
||||
};
|
||||
|
||||
// Cache the response
|
||||
let check_result = cache.check_cache(&filenames, temp_dir.path());
|
||||
cache.cache_response_with_metadata(&filenames, plan, check_result.file_metadata);
|
||||
cache.cache_response(&filenames, plan, temp_dir.path());
|
||||
|
||||
// Wait longer to ensure filesystem timestamp changes (at least 1 second for most filesystems)
|
||||
std::thread::sleep(std::time::Duration::from_secs(2));
|
||||
@@ -101,14 +100,14 @@ fn test_cache_invalidates_on_file_modification() {
|
||||
let _ = fs::metadata(temp_dir.path().join("test.txt"));
|
||||
|
||||
// Cache should be invalidated due to modification time change
|
||||
let check_result2 = cache.check_cache(&filenames, temp_dir.path());
|
||||
let cached = cache.check_cache(&filenames, temp_dir.path());
|
||||
|
||||
// Note: Cache invalidation depends on file metadata (size/mtime) changing.
|
||||
// If the filesystem has coarse timestamp granularity, this test may be flaky.
|
||||
// The important behavior is that the cache CAN detect file changes.
|
||||
// For a more robust test, we check that the cache at least loads without error.
|
||||
// In production, files are typically modified minutes/hours apart.
|
||||
if check_result2.cached_response.is_some() {
|
||||
if cached.is_some() {
|
||||
// If cache wasn't invalidated, it means the filesystem timestamp
|
||||
// didn't change within our sleep window - this is acceptable
|
||||
// as long as the mechanism works for real-world use cases
|
||||
@@ -151,12 +150,11 @@ fn test_cache_handles_multiple_files() {
|
||||
],
|
||||
};
|
||||
|
||||
let check_result = cache.check_cache(&filenames, temp_dir.path());
|
||||
cache.cache_response_with_metadata(&filenames, plan.clone(), check_result.file_metadata);
|
||||
cache.cache_response(&filenames, plan.clone(), temp_dir.path());
|
||||
|
||||
let check_result2 = cache.check_cache(&filenames, temp_dir.path());
|
||||
assert!(check_result2.cached_response.is_some());
|
||||
assert_eq!(check_result2.cached_response.unwrap().files.len(), 3);
|
||||
let cached = cache.check_cache(&filenames, temp_dir.path());
|
||||
assert!(cached.is_some());
|
||||
assert_eq!(cached.unwrap().files.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user