forked from kent/consciousness
store: internal locking, remove Arc<Mutex<Store>> wrapper
Store now has internal Mutex for capnp appends and AtomicU64 for size tracking. All methods take &self. The external Arc<Mutex<Store>> is replaced with Arc<Store>. - Store::append_lock protects file appends - local.rs functions take &Store (not &mut Store) - access_local() returns Arc<Store> - All .lock().await calls removed from callers Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
4696bb8b7d
commit
b3d0a3ab25
13 changed files with 86 additions and 70 deletions
|
|
@ -269,8 +269,15 @@ impl Store {
|
|||
}
|
||||
|
||||
// Record log sizes
|
||||
store.loaded_nodes_size = fs::metadata(&nodes_p).map(|m| m.len()).unwrap_or(0);
|
||||
store.loaded_rels_size = fs::metadata(&rels_p).map(|m| m.len()).unwrap_or(0);
|
||||
use std::sync::atomic::Ordering;
|
||||
store.loaded_nodes_size.store(
|
||||
fs::metadata(&nodes_p).map(|m| m.len()).unwrap_or(0),
|
||||
Ordering::Relaxed
|
||||
);
|
||||
store.loaded_rels_size.store(
|
||||
fs::metadata(&rels_p).map(|m| m.len()).unwrap_or(0),
|
||||
Ordering::Relaxed
|
||||
);
|
||||
|
||||
// Orphan edges filtered naturally during for_each_relation (unresolvable UUIDs skipped)
|
||||
|
||||
|
|
@ -408,7 +415,9 @@ impl Store {
|
|||
}
|
||||
|
||||
/// Append nodes to the log file. Returns the offset where the message was written.
|
||||
pub fn append_nodes(&mut self, nodes: &[Node]) -> Result<u64> {
|
||||
pub fn append_nodes(&self, nodes: &[Node]) -> Result<u64> {
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
let mut msg = message::Builder::new_default();
|
||||
{
|
||||
let log = msg.init_root::<memory_capnp::node_log::Builder>();
|
||||
|
|
@ -421,6 +430,9 @@ impl Store {
|
|||
serialize::write_message(&mut buf, &msg)
|
||||
.with_context(|| format!("serialize nodes"))?;
|
||||
|
||||
// Lock for file append
|
||||
let _guard = self.append_lock.lock().unwrap();
|
||||
|
||||
let path = nodes_path();
|
||||
let file = fs::OpenOptions::new()
|
||||
.create(true).append(true).open(&path)
|
||||
|
|
@ -433,12 +445,17 @@ impl Store {
|
|||
(&file).write_all(&buf)
|
||||
.with_context(|| format!("write nodes"))?;
|
||||
|
||||
self.loaded_nodes_size = file.metadata().map(|m| m.len()).unwrap_or(0);
|
||||
self.loaded_nodes_size.store(
|
||||
file.metadata().map(|m| m.len()).unwrap_or(0),
|
||||
Ordering::Relaxed
|
||||
);
|
||||
Ok(offset)
|
||||
}
|
||||
|
||||
/// Append relations to the log file.
|
||||
pub fn append_relations(&mut self, relations: &[Relation]) -> Result<()> {
|
||||
pub fn append_relations(&self, relations: &[Relation]) -> Result<()> {
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
let mut msg = message::Builder::new_default();
|
||||
{
|
||||
let log = msg.init_root::<memory_capnp::relation_log::Builder>();
|
||||
|
|
@ -451,6 +468,9 @@ impl Store {
|
|||
serialize::write_message(&mut buf, &msg)
|
||||
.with_context(|| format!("serialize relations"))?;
|
||||
|
||||
// Lock for file append
|
||||
let _guard = self.append_lock.lock().unwrap();
|
||||
|
||||
let path = relations_path();
|
||||
let file = fs::OpenOptions::new()
|
||||
.create(true).append(true).open(&path)
|
||||
|
|
@ -459,7 +479,10 @@ impl Store {
|
|||
(&file).write_all(&buf)
|
||||
.with_context(|| format!("write relations"))?;
|
||||
|
||||
self.loaded_rels_size = file.metadata().map(|m| m.len()).unwrap_or(0);
|
||||
self.loaded_rels_size.store(
|
||||
file.metadata().map(|m| m.len()).unwrap_or(0),
|
||||
Ordering::Relaxed
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue