loonfs_core/lib.rs
1//! Core LoonFS namespace operations.
2//!
3//! `loonfs-core` is the low-level API for building directly on the LoonFS
4//! metadata protocol. Most callers should start with [`NamespaceEngine`].
5//!
6//! A namespace is one durable filesystem history. File bytes are written to
7//! object storage first, then metadata is published as a committed namespace
8//! mutation. Reads rebuild or reuse a verified view of the namespace before
9//! walking paths.
10//!
11//! # Example
12//!
13//! Commits are published as candidate batches through
14//! [`publish::NamespaceCommitEngine`]; day-to-day reads and writes should go
15//! through the `loonfs` crate's `FsReader`/`FsWriter` handles, which wrap
16//! this crate with caching and batching.
17//!
18//! ```no_run
19//! use loonfs_api::{AbsolutePath, CommitId, NamespaceId};
20//! use loonfs_core::publish::{
21//! FilesystemOperation, CommitRequest, NamespaceCommitEngine, CommitCandidate,
22//! PublishTailOptions,
23//! };
24//! use loonfs_core::{BootstrapOptions, MutationContext, NamespaceEngine};
25//! use loonfs_objectstore::local_fs_store::LocalFsStore;
26//!
27//! let store = LocalFsStore::new(std::env::temp_dir()).expect("store");
28//! let namespace = NamespaceId::parse("docs").expect("valid namespace id");
29//!
30//! let engine = NamespaceEngine::builder(store)
31//! .namespace_id(namespace.clone())
32//! .writer_id("example-writer")
33//! .build()
34//! .expect("engine");
35//! let _ = engine.bootstrap_namespace(BootstrapOptions::default());
36//!
37//! let publish_store = LocalFsStore::new(std::env::temp_dir()).expect("store");
38//! let context = MutationContext {
39//! writer_id: "example-writer".to_owned(),
40//! now_ms: 0,
41//! };
42//! let mut publisher = NamespaceCommitEngine::new(namespace);
43//! let _ = publisher.publish_batch(
44//! &publish_store,
45//! vec![CommitCandidate::new(CommitRequest::single(
46//! CommitId::generate(),
47//! None,
48//! FilesystemOperation::CreateDirectory {
49//! path: AbsolutePath::parse("/plans").expect("path"),
50//! parents: false,
51//! },
52//! ))],
53//! &context,
54//! &PublishTailOptions::default(),
55//! );
56//! ```
57
58// Sanctioned consumers of this crate's public surface, in full:
59//
60// - **`loonfs`** — the embedded runtime, the only production consumer. It
61// wraps everything below with caching, batching, and handles, and re-exports
62// what applications need. Application code depends on `loonfs`, never on
63// this crate.
64// - **`loonfs-core`'s own integration tests** (`tests/it`) — a white-box
65// consumer that asserts on durable layout and replay directly. It is why
66// `metadata` and parts of `commit` are public at all.
67//
68// Nothing else depends on this crate. `loonfs-grep` was extracted and reads
69// filesystem state through `loonfs`; `loonfs-sim`, `loonfs-model`, and
70// `loonfs-test-support` never depended on it; `loonfs-server` and `loonfs-cli`
71// reach the durable control plane through `loonfs::control`.
72//
73// The module list below is grouped by that intent: private modules are engine
74// internals, and each public one names why it is public.
75
76// --- engine internals: private, reachable only through the seams below ---
77mod checkpoint;
78mod commit_engine;
79mod context;
80mod control_update;
81mod engine;
82mod error;
83mod gc;
84mod namespace;
85mod options;
86mod protocol;
87mod recency;
88mod storage;
89mod timing;
90mod wal;
91
92// --- public seams ---
93/// Commit planning, validation, and materialization. Consumed by the `loonfs`
94/// publisher and by this crate's commit-validation integration tests.
95pub mod commit;
96/// Content staging and preparation-token minting. Consumed by `loonfs`'s
97/// write path and its server-integration `content_tokens` seam.
98pub mod content;
99/// Protocol and resource ceilings. Consumed by `loonfs` (re-exported to the
100/// server for request validation) and by layout tests.
101pub mod limits;
102/// Durable metadata state and its row codecs. Public for this crate's
103/// white-box integration tests, which compare projected state against the
104/// reference model; `loonfs` reaches metadata only through the seams above.
105pub mod metadata;
106/// Path parsing and current-state resolution. Consumed by `loonfs`'s write
107/// path (`parse_mutation_path`).
108pub mod path;
109/// The wall-clock boundary durable timestamps are stamped at. Consumed by
110/// `loonfs`, whose mutation contexts and maintenance clock stamp from the
111/// same boundary this crate's own commits do.
112pub mod time;
113
114/// Cache types and configuration for runtime read paths. Consumed by
115/// `loonfs`, which owns the runtime's cache configuration and stats, and
116/// which re-exports [`cache::Recency`] for the grep index's own block cache.
117pub mod cache {
118 pub use crate::recency::Recency;
119
120 pub use crate::checkpoint::{
121 ManifestLoadError, ManifestLoadFailureClass, MetadataTableCache, MetadataTableCacheConfig,
122 MetadataTableCacheStats, WalTailProjectionCache, WalTailProjectionCacheConfig,
123 WalTailProjectionCacheKey, WalTailProjectionCacheStats,
124 DEFAULT_METADATA_TABLE_CACHE_DECODED_BYTES, DEFAULT_WAL_TAIL_PROJECTION_DECODED_BYTES,
125 DEFAULT_WAL_TAIL_PROJECTION_ROWS,
126 };
127 pub use crate::namespace::status::{
128 load_deleted_namespace_head_summary, load_namespace_head_summary, NamespaceHeadSummary,
129 };
130}
131
132/// Typed namespace control-object loaders and verified catalog state.
133/// Consumed by `loonfs`'s cache and write paths, and re-exported as
134/// `loonfs::control` for the white-box layout assertions the server and this
135/// crate's own tests make.
136pub mod control {
137 pub use crate::namespace::catalog::{
138 load_namespace_catalog_entry, NamespaceCatalogLoadError, VerifiedNamespaceCatalogEntry,
139 };
140 pub use crate::namespace::control::{
141 load_namespace_checkpoint_record_control, load_namespace_head_control,
142 load_namespace_metadata_root_control, load_namespace_read_anchor,
143 load_namespace_wal_floor_control, ControlObjectIdentity, ControlObjectLoadError,
144 LoadedHeadControl, LoadedMetadataRootControl, LoadedWalFloorControl,
145 };
146 pub use crate::namespace::{BasisManifest, MetadataBasis};
147}
148
149/// Commit publication types for runtime integrations. Consumed by `loonfs`'s
150/// publisher, and re-exported as `loonfs::publish` for the server's
151/// filesystem handlers.
152pub mod publish {
153 pub use crate::commit::{CommitFingerprint, CommitHeadPublishError};
154 pub use crate::commit_engine::{
155 CommitCandidate, ContentPreparation, ContentPreparationError, NamespaceCommitEngine,
156 NamespaceCommitEnginePublishResult, ResultingReadState, SharedWriterSessionState,
157 WalTailPolicy, WriterSessionState,
158 };
159 pub use crate::path::write::{CommitRequest, FilesystemOperation};
160 pub use crate::protocol::{PublishTailOptions, PublishTailWeight};
161 pub use crate::storage::content_admission::PreparedContent;
162}
163
164// Crate-root re-exports. Every name below has a named consumer: `loonfs`
165// unless the comment says otherwise, or reachability through a public
166// signature where noted.
167// `MetadataReorganizeReport` has no caller that names it; it stays public
168// because it is the return type of `NamespaceEngine::reorganize_metadata`.
169pub use checkpoint::{
170 CheckpointFile, CheckpointFilesPage, CheckpointFilesPageCursor, MetadataReorganizeOutcome,
171 MetadataReorganizeReport,
172};
173pub use context::MutationContext;
174pub use engine::RuntimeReadContext;
175pub use engine::{
176 BeginDirectMultipartUploadTargetResponse, BeginDirectPutUploadTargetResponse,
177 DirectMultipartUploadTarget, DirectPutUploadTarget, MultipartPartTarget, MultipartPartTargets,
178};
179// The builder pair is reachable through `NamespaceEngine::builder()` and its
180// `build()`, so both stay public even though no caller names them directly.
181pub use engine::{NamespaceEngine, NamespaceEngineBuildError, NamespaceEngineBuilder};
182pub use error::{
183 Error, ErrorCode, ErrorKind, MetadataProjectionLoadError, MetadataViewError, StoreFailureClass,
184 WriterFence,
185};
186pub use gc::{delete_if_aged, gc_namespace, AgedSweep, GcConfig, PassBudget};
187pub use namespace::BootstrapNamespaceError;
188pub use options::{BootstrapOptions, DeleteNamespaceOptions};
189pub use path::read::{CurrentFileState, DirectDownloadTarget, MAX_RESOLVE_CURRENT_FILES};
190// The streaming read `loonfs`'s reader handle returns, and the chunk size it
191// reads in.
192pub use storage::content::{FileContentStream, CONTENT_READ_CHUNK_BYTES};