1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! # Filesystem Interface
//!
//! This module provides a clean, high-level filesystem abstraction for Heroforge repositories.
//! It hides the complexity of SQLite-backed storage and provides a familiar filesystem-like API.
//!
//! ## Architecture Overview
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ FsInterface (sync API) │
//! │ - RwLock<StagingState> │
//! │ - Author name (set at initialization) │
//! │ - All read/write operations acquire appropriate locks │
//! └─────────────────────────────────────────────────────────────────┘
//! │
//! ┌───────────────────┴───────────────────┐
//! │ │
//! ▼ ▼
//! ┌─────────────────────┐ ┌─────────────────────────┐
//! │ Staging Directory │ │ Commit Thread │
//! │ │ │ (background) │
//! │ - All writes go │ │ │
//! │ here first │ │ - Runs every 1 minute │
//! │ - Files < 2MB │ │ - Acquires write lock │
//! │ - Frequent updates │ │ - Blocks all I/O │
//! │ allowed │ │ - Flushes to .forge DB │
//! │ │ │ - Clears staging dir │
//! └─────────────────────┘ └─────────────────────────┘
//! │ │
//! └───────────────────┬───────────────────┘
//! │
//! ▼
//! ┌─────────────────────┐
//! │ .forge Database │
//! │ (SQLite) │
//! └─────────────────────┘
//! ```
//!
//! ## Quick Start with FsInterface (Recommended)
//!
//! The `FsInterface` provides staging-based writes with automatic background commits:
//!
//! ```no_run
//! use heroforge_core::Repository;
//! use std::sync::Arc;
//!
//! fn main() -> heroforge_core::Result<()> {
//! let repo = Arc::new(Repository::open_rw("project.forge")?);
//! let fs = heroforge_core::fs::FsInterface::new(repo, "developer@example.com")?;
//!
//! // Writes go to staging (fast)
//! fs.write_file("config.json", b"{}")?;
//!
//! // Reads check staging first, then database
//! let content = fs.read_file("config.json")?;
//!
//! // Partial updates use read-modify-write pattern
//! fs.write_at("data.bin", 100, b"updated")?;
//!
//! // Force immediate commit (normally auto-commits every 1 minute)
//! fs.commit()?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Legacy FileSystem API
//!
//! The original `FileSystem` API is still available for direct database operations:
//!
//! ```no_run
//! use heroforge_core::Repository;
//! use heroforge_core::fs::FileSystem;
//! use std::sync::Arc;
//!
//! fn main() -> heroforge_core::Result<()> {
//! let repo = Arc::new(Repository::open_rw("project.forge")?);
//! let fs = FileSystem::new(repo);
//!
//! // Direct database operations (no staging)
//! fs.write_file("config.json", b"{}", "developer", "Add config")?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Staging Directory
//!
//! All write operations go to a **staging directory** first, not directly to SQLite:
//!
//! 1. **Fast writes**: Writing to filesystem is faster than SQLite transactions
//! 2. **Frequent updates**: Files can be modified many times before commit
//! 3. **Atomic commits**: All staged changes are committed together
//! 4. **Crash recovery**: Uncommitted work is recoverable from staging
//!
//! **Current Limitations:**
//! - Files larger than **2 MB** are not supported
//! - Staging directory is local to the repository
//!
//! ## Read Path (Layered Lookup)
//!
//! When reading a file, the interface checks locations in this order:
//!
//! 1. **Staging Directory** → If file exists here, return it (most recent)
//! 2. **`.forge` Database** → Query SQLite for committed version
//! 3. **Return Error** → File does not exist
//!
//! ## Auto-Commit Behavior
//!
//! - Commits happen automatically every **1 minute**
//! - During commit, an **exclusive write lock** is held
//! - All operations are blocked during commit (typically milliseconds)
//! - Forced commits happen on: branch change, tag creation, shutdown
//!
//! ## Thread Safety
//!
//! Uses `RwLock<StagingState>` for concurrent access:
//!
//! | Operation | Lock Type | Blocks |
//! |-----------|-----------|--------|
//! | `exists()` | Read | Nothing |
//! | `read_file()` | Read | Nothing |
//! | `write_file()` | Write | Other writes |
//! | `delete_file()` | Write | Other writes |
//! | **Commit** | **Exclusive** | **Everything** |
//!
//! ## Error Handling
//!
//! All operations return `FsResult<T>` which is `Result<T, FsError>`:
//!
//! ```no_run
//! # use heroforge_core::Repository;
//! # use std::sync::Arc;
//! let repo = Arc::new(Repository::open_rw("repo.forge")?);
//! let fs = heroforge_core::fs::FsInterface::new(repo, "user")?;
//!
//! match fs.read_file("missing.txt") {
//! Ok(content) => println!("Read {} bytes", content.len()),
//! Err(err) => eprintln!("Error: {}", err),
//! }
//! # Ok::<(), heroforge_core::FossilError>(())
//! ```
pub use ;
pub use ;
pub use ;
pub use ;
// New staging-based interface
pub use ;
pub use ;
pub use ;
// Find and high-level operations
pub use ;
pub use ;
// Upload/Download (OS filesystem <-> .forge database)
pub use ;
/// Filesystem interface version
pub const VERSION: &str = "1.0.0";