pub struct FileStore { /* private fields */ }Expand description
Content-addressed store holding one immutable file per chunk.
The filesystem is the sole authority. The in-memory index is a cache of what the directory tree already contains, rebuilt from directory entries at every open, and every mutation of it mirrors a filesystem operation that has already completed. Bitcask’s issue #114 is the cautionary tale for the opposite order: an index that is rebuilt at startup and then mutated in anticipation drifts, and the drift is silent.
Implementations§
Source§impl FileStore
impl FileStore
Sourcepub async fn new(config: FileStoreConfig) -> Result<Self>
pub async fn new(config: FileStoreConfig) -> Result<Self>
Open (or create) the store at {root_dir}/chunks/.
Sweeps orphaned temp files, then rebuilds the index from directory entries.
The scan reads names only: it never stats an entry and never reads a chunk.
§Errors
Returns Error::Storage if the directory cannot be created, the layout marker
is unreadable or describes a layout this build does not implement, or the scan
fails.
Sourcepub async fn put(&self, address: &XorName, content: &[u8]) -> Result<bool>
pub async fn put(&self, address: &XorName, content: &[u8]) -> Result<bool>
Store a chunk.
On Unix, publishing is a rename within the destination directory, so the final name
can never appear on partial content: the name is the hash, and the content is
fully written and flushed before the name exists. Off Unix there is no rename, for
the reason publish_in_place gives (it is compiled only on those platforms, so this
is not a link), and a partial file can wear a real name; that
is why a duplicate is read and compared rather than trusted.
§Returns
true if the chunk was newly stored, false if it was already present.
§Errors
Returns Error::Storage if the content does not hash to address, the disk
is too full, or the write fails.
Sourcepub fn flush_namespace(&self) -> Result<()>
pub fn flush_namespace(&self) -> Result<()>
Flush every directory a chunk can live in, so the names in them are durable.
Byte integrity is not the whole of what the pre-retirement proof has to establish. A chunk whose contents are on the platter but whose name is not is still lost to a power loss, and a publish whose rename landed and whose directory flush failed leaves exactly that: the next attempt sees the name, the next verification reads the right bytes, and nothing goes back to retry the flush. So the proof flushes them itself rather than trusting that each publish did.
Cheap: at most 257 directory flushes for a store of any size, and nothing off Unix, where directories cannot be flushed and the retirement marker covers the same ground instead.
§Errors
Returns Error::Storage on the first directory that cannot be flushed. The
caller must treat that as a proof it did not get.
Sourcepub fn stored_len(&self, address: &XorName) -> Option<usize>
pub fn stored_len(&self, address: &XorName) -> Option<usize>
The size of the file behind address, if there is one.
One metadata call, no read. Used where an indexed name has to be checked against
what a caller is offering before that offer is turned away.
Sourcepub async fn repair(&self, address: &XorName, content: &[u8]) -> Result<()>
pub async fn repair(&self, address: &XorName, content: &[u8]) -> Result<()>
Replace the file behind an address with known-good bytes, atomically.
Unlike Self::put, this deliberately publishes over an existing name. It
exists for one caller: repairing a file whose bytes no longer hash to their own
name, from a copy held elsewhere, before that copy is destroyed. Doing it as
delete-then-put would leave a window where the only remaining copy is the one
about to be deleted, and any failure in that window is unrecoverable.
§Errors
Returns Error::Storage if content does not hash to address, or the write
fails. The old file is left untouched on every error path.
Sourcepub async fn get(&self, address: &XorName) -> Result<Option<Vec<u8>>>
pub async fn get(&self, address: &XorName) -> Result<Option<Vec<u8>>>
Retrieve a chunk, verifying it against its address when configured to.
§Errors
Returns Error::Storage on an I/O failure, or when verification fails. A
chunk whose bytes do not hash to its name is removed and dropped from the index
before the error is returned, so it leaves all_keys() and ordinary replication
repairs it.
Sourcepub async fn get_raw(&self, address: &XorName) -> Result<Option<Vec<u8>>>
pub async fn get_raw(&self, address: &XorName) -> Result<Option<Vec<u8>>>
Retrieve raw chunk bytes without content-address verification.
§Errors
Returns Error::Storage on an I/O failure.
Sourcepub fn exists(&self, address: &XorName) -> Result<bool>
pub fn exists(&self, address: &XorName) -> Result<bool>
Check whether a chunk is stored.
An in-memory lookup: no syscall, no I/O.
§Errors
Never fails. The signature keeps the shape the LMDB store had, because callers treat the error as “assume absent”.
Sourcepub fn is_indexed(&self, address: &XorName) -> bool
pub fn is_indexed(&self, address: &XorName) -> bool
Is this chunk in the index, whether or not it can currently be read?
The physical question, as against Self::exists’s question about what the node
is willing to claim. The migration must ask this one: a suspect chunk is still a
file this store has, and treating it as absent would put the key in the legacy-only
set, from where the union view advertises it again — a key the node claims through
one view and cannot serve through either.
Sourcepub async fn delete(&self, address: &XorName) -> Result<bool>
pub async fn delete(&self, address: &XorName) -> Result<bool>
Delete a chunk, returning whether it was present.
unlink returns the blocks to the filesystem immediately. That is the whole
point of this store: no free list, no compaction, no free space required to
reclaim space.
§Errors
Returns Error::Storage if the file exists but cannot be removed. The index
keeps the key in that case, because the bytes are still on disk.
Sourcepub async fn all_keys(&self) -> Result<Vec<XorName>>
pub async fn all_keys(&self) -> Result<Vec<XorName>>
Return every stored key, in ascending order.
The order is a correctness requirement, not a convenience: the commitment
builder truncates the responsible subset with take(cap) before the Merkle tree
sorts it, so an unstable order would make the node’s published commitment depend
on iteration luck.
§Errors
Never fails. The signature matches the LMDB store’s.
Sourcepub fn health_generation(&self) -> u64
pub fn health_generation(&self) -> u64
What the store’s health looked like at this moment.
Compare a value taken before a long-running check with one taken after, or after taking a lock: different means a chunk stopped being servable in between and any conclusion drawn from that check is out of date.
Sourcepub fn note_known_wrong(&self, address: &XorName)
pub fn note_known_wrong(&self, address: &XorName)
Stop answering for a chunk a read has proven wrong.
Unlike a chunk that merely could not be read, a later read does not clear this. The bytes are wrong, and reading them again says the same thing; only replacing them or removing them settles it. Bumping health matters as much as the suppression: a chunk that has become unservable since the last pre-retirement pass must invalidate that pass, or a repair that fails leaves the node deleting the copy it would have repaired from.
For callers outside this module that have proven it themselves.
Sourcepub fn note_bytes_proven_good(&self, address: &XorName)
pub fn note_bytes_proven_good(&self, address: &XorName)
A caller outside this module has proven the stored bytes are right.
Sourcepub fn current_chunks(&self) -> Result<u64>
pub fn current_chunks(&self) -> Result<u64>
Number of chunks currently stored.
The physical count: every name in the index, including chunks the node has stopped
answering for because a read found them wrong or could not read them at all. It is
deliberately not the same number as all_keys().len(), which is what the node is
willing to claim and so leaves those out.
Anything asking “how much is on this disk” wants this one, and that is what its
callers ask: the migration’s progress, the storage stats, and the size an audit is
built for. Anything asking “what will this node answer for” wants all_keys.
Quietly filtering this one would move all three of those without saying so, which
is why the difference is written down here rather than removed.
§Errors
Never fails. The signature matches the LMDB store’s.
Sourcepub fn stats(&self) -> StorageStats
pub fn stats(&self) -> StorageStats
Operation statistics, with the live chunk count filled in.
Sourcepub fn chunks_dir(&self) -> &Path
pub fn chunks_dir(&self) -> &Path
The directory holding the shard tree.
Sourcepub fn check_capacity(&self) -> Result<()>
pub fn check_capacity(&self) -> Result<()>
Reject work early when the disk cannot take another chunk at all.
§Errors
Returns Error::Storage when free space is below the configured reserve.
Sourcepub fn capacity_verdict(&self) -> CapacityVerdict
pub fn capacity_verdict(&self) -> CapacityVerdict
Three-way answer to “can this store take a write right now”.
Kept distinct from Self::check_capacity because a failed free-space query and a
genuinely full disk are not the same thing, and the replication verification cycle
depends on the difference: a full disk is a standing condition worth minutes of
backoff, while a statvfs that failed says nothing about available space and may
well succeed on the next pass.
Sourcepub fn check_capacity_for(&self, bytes: u64) -> Result<()>
pub fn check_capacity_for(&self, bytes: u64) -> Result<()>
Reject work early when the disk cannot take bytes more.
§Errors
Returns Error::Storage when the write would not fit above the reserve.
Sourcepub fn invalidate_capacity_cache(&self)
pub fn invalidate_capacity_cache(&self)
Force the next capacity question to re-measure the filesystem.
Called after the legacy environment is removed, because that is a step change in free space that the short-lived cache would otherwise hide for a few seconds.
Sourcepub async fn wait_for_write(&self, address: &XorName)
pub async fn wait_for_write(&self, address: &XorName)
Wait until nothing is part-way through writing address.
For callers that must be last: a delete whose key still has a write in flight would be undone by that write landing afterwards.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for FileStore
impl !RefUnwindSafe for FileStore
impl !UnwindSafe for FileStore
impl Send for FileStore
impl Sync for FileStore
impl Unpin for FileStore
impl UnsafeUnpin for FileStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more