Skip to content

Commit e81b1f3

Browse files
committed
change a few fns to take HashContext directly instead of 2 args
1 parent e65bd07 commit e81b1f3

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

src/store/fs.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl entity_manager::Params for EmParams {
226226
// have exact control over where it happens.
227227
if let Some(mut handle) = state.state.0.lock().await.take() {
228228
trace!("shutting down hash: {}, cause: {cause:?}", state.id);
229-
handle.persist(&state.id, &state.global.options);
229+
handle.persist(&state);
230230
}
231231
}
232232
}
@@ -924,11 +924,7 @@ async fn import_bao_impl(
924924
handle: BaoFileHandle,
925925
ctx: HashContext,
926926
) -> api::Result<()> {
927-
trace!(
928-
"importing bao: {} {} bytes",
929-
ctx.id.fmt_short(),
930-
size
931-
);
927+
trace!("importing bao: {} {} bytes", ctx.id.fmt_short(), size);
932928
let mut batch = Vec::<BaoContentItem>::new();
933929
let mut ranges = ChunkRanges::empty();
934930
while let Some(item) = rx.recv().await? {

src/store/fs/bao_file.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,25 @@ impl Default for BaoFileStorage {
335335
impl PartialMemStorage {
336336
/// Converts this storage into a complete storage, using the given hash for
337337
/// path names and the given options for decisions about inlining.
338-
fn into_complete(
339-
self,
340-
hash: &Hash,
341-
ctx: &TaskContext,
342-
) -> io::Result<(CompleteStorage, EntryState<Bytes>)> {
338+
fn into_complete(self, ctx: &HashContext) -> io::Result<(CompleteStorage, EntryState<Bytes>)> {
339+
let options = &ctx.global.options;
340+
let hash = &ctx.id;
343341
let size = self.current_size();
344342
let outboard_size = raw_outboard_size(size);
345-
let (data, data_location) = if ctx.options.is_inlined_data(size) {
343+
let (data, data_location) = if options.is_inlined_data(size) {
346344
let data: Bytes = self.data.to_vec().into();
347345
(MemOrFile::Mem(data.clone()), DataLocation::Inline(data))
348346
} else {
349-
let data_path = ctx.options.path.data_path(hash);
347+
let data_path = options.path.data_path(hash);
350348
let mut data_file = create_read_write(&data_path)?;
351349
self.data.persist(&mut data_file)?;
352350
(
353351
MemOrFile::File(FixedSize::new(data_file, size)),
354352
DataLocation::Owned(size),
355353
)
356354
};
357-
let (outboard, outboard_location) = if ctx.options.is_inlined_outboard(outboard_size) {
355+
let (outboard, outboard_location) = if ctx.global.options.is_inlined_outboard(outboard_size)
356+
{
358357
if outboard_size > 0 {
359358
let outboard: Bytes = self.outboard.to_vec().into();
360359
(
@@ -365,7 +364,7 @@ impl PartialMemStorage {
365364
(MemOrFile::empty(), OutboardLocation::NotNeeded)
366365
}
367366
} else {
368-
let outboard_path = ctx.options.path.outboard_path(hash);
367+
let outboard_path = ctx.global.options.path.outboard_path(hash);
369368
let mut outboard_file = create_read_write(&outboard_path)?;
370369
self.outboard.persist(&mut outboard_file)?;
371370
let outboard_location = if outboard_size == 0 {
@@ -411,10 +410,10 @@ impl BaoFileStorage {
411410
let changes = ms.bitfield.update(bitfield);
412411
let new = changes.new_state();
413412
if new.complete {
414-
let (cs, update) = ms.into_complete(&ctx.id, &ctx.global)?;
413+
let (cs, update) = ms.into_complete(ctx)?;
415414
(cs.into(), Some(update))
416415
} else {
417-
let fs = ms.persist(&ctx.global, &ctx.id)?;
416+
let fs = ms.persist(ctx)?;
418417
let update = EntryState::Partial {
419418
size: new.validated_size,
420419
};
@@ -427,7 +426,7 @@ impl BaoFileStorage {
427426
// a write at the end of a very large file.
428427
//
429428
// opt: we should check if we become complete to avoid going from mem to partial to complete
430-
let mut fs = ms.persist(&ctx.global, &ctx.id)?;
429+
let mut fs = ms.persist(ctx)?;
431430
fs.write_batch(bitfield.size(), batch)?;
432431
let changes = fs.bitfield.update(bitfield);
433432
let new = changes.new_state();
@@ -507,20 +506,17 @@ impl BaoFileStorage {
507506
pub(crate) struct BaoFileHandle(Arc<watch::Sender<BaoFileStorage>>);
508507

509508
impl BaoFileHandle {
510-
pub fn persist(&mut self, hash: &Hash, options: &Options) {
509+
pub(super) fn persist(&mut self, ctx: &HashContext) {
511510
self.send_if_modified(|guard| {
511+
let hash = &ctx.id;
512512
if Arc::strong_count(&self.0) > 1 {
513513
return false;
514514
}
515515
let BaoFileStorage::Partial(fs) = guard.take() else {
516516
return false;
517517
};
518-
let path = options.path.bitfield_path(hash);
519-
trace!(
520-
"writing bitfield for hash {} to {}",
521-
hash,
522-
path.display()
523-
);
518+
let path = ctx.global.options.path.bitfield_path(hash);
519+
trace!("writing bitfield for hash {} to {}", hash, path.display());
524520
if let Err(cause) = fs.sync_all(&path) {
525521
error!(
526522
"failed to write bitfield for {} at {}: {:?}",
@@ -695,8 +691,7 @@ impl BaoFileHandle {
695691
trace!("write_batch bitfield={:?} batch={}", bitfield, batch.len());
696692
let mut res = Ok(None);
697693
self.send_if_modified(|state| {
698-
let Ok((state1, update)) = state.take().write_batch(batch, bitfield, ctx)
699-
else {
694+
let Ok((state1, update)) = state.take().write_batch(batch, bitfield, ctx) else {
700695
res = Err(io::Error::other("write batch failed"));
701696
return false;
702697
};
@@ -713,9 +708,10 @@ impl BaoFileHandle {
713708

714709
impl PartialMemStorage {
715710
/// Persist the batch to disk.
716-
fn persist(self, ctx: &TaskContext, hash: &Hash) -> io::Result<PartialFileStorage> {
717-
let options = &ctx.options.path;
718-
ctx.protect.protect(
711+
fn persist(self, ctx: &HashContext) -> io::Result<PartialFileStorage> {
712+
let options = &ctx.global.options.path;
713+
let hash = &ctx.id;
714+
ctx.global.protect.protect(
719715
*hash,
720716
[
721717
BaoFilePart::Data,

0 commit comments

Comments
 (0)