Skip to content

Commit

Permalink
Some renaming:
Browse files Browse the repository at this point in the history
PartialMap -> MapMut
PartialEntry -> EntryMut
  • Loading branch information
rklaehn committed Feb 19, 2024
1 parent 12db8c2 commit d386636
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 60 deletions.
8 changes: 4 additions & 4 deletions iroh-bytes/src/get/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
Stats,
},
protocol::{GetRequest, RangeSpecSeq},
store::{MapEntry, PartialMap, PartialMapEntry, Store as BaoStore},
store::{MapEntry, MapEntryMut, MapMut, Store as BaoStore},
util::progress::{IdGenerator, ProgressSender},
BlobFormat, HashAndFormat,
};
Expand Down Expand Up @@ -143,7 +143,7 @@ async fn get_blob<
}

/// Given a partial entry, get the valid ranges.
pub async fn valid_ranges<D: PartialMap>(entry: &D::PartialEntry) -> anyhow::Result<ChunkRanges> {
pub async fn valid_ranges<D: MapMut>(entry: &D::EntryMut) -> anyhow::Result<ChunkRanges> {
use tracing::trace as log;
// compute the valid range from just looking at the data file
let mut data_reader = entry.data_reader().await?;
Expand Down Expand Up @@ -217,7 +217,7 @@ async fn get_blob_inner<D: BaoStore>(
async fn get_blob_inner_partial<D: BaoStore>(
db: &D,
at_header: AtBlobHeader,
entry: D::PartialEntry,
entry: D::EntryMut,
sender: impl ProgressSender<Msg = DownloadProgress> + IdGenerator,
) -> Result<AtEndBlob, GetError> {
// read the size. The size we get here is not verified, but since we use
Expand Down Expand Up @@ -469,7 +469,7 @@ pub enum BlobInfo<D: BaoStore> {
/// we have the blob partially
Partial {
/// The partial entry.
entry: D::PartialEntry,
entry: D::EntryMut,
/// The ranges that are available locally.
valid_ranges: ChunkRanges,
},
Expand Down
26 changes: 13 additions & 13 deletions iroh-bytes/src/store/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ use std::time::SystemTime;

use super::{
BaoBatchWriter, CombinedBatchWriter, EntryStatus, ExportMode, ImportMode, ImportProgress, Map,
MapEntry, PartialMap, PartialMapEntry, PossiblyPartialEntry, ReadableStore, ValidateProgress,
MapEntry, MapEntryMut, MapMut, PossiblyPartialEntry, ReadableStore, ValidateProgress,
};
use crate::util::progress::{IdGenerator, IgnoreProgressSender, ProgressSender};
use crate::util::{LivenessTracker, Tag};
Expand Down Expand Up @@ -240,7 +240,7 @@ impl PartialEntryData {
}
}

impl MapEntry for PartialEntry {
impl MapEntry for EntryMut {
fn hash(&self) -> Hash {
self.hash
}
Expand Down Expand Up @@ -272,7 +272,7 @@ impl MapEntry for PartialEntry {
}
}

impl PartialEntry {
impl EntryMut {
async fn outboard_mut(&self) -> io::Result<PreOrderOutboard<File>> {
let hash = self.hash;
let size = self.size;
Expand Down Expand Up @@ -305,16 +305,16 @@ impl PartialEntry {
}
}

impl PartialMapEntry for PartialEntry {
impl MapEntryMut for EntryMut {
async fn batch_writer(&self) -> io::Result<impl BaoBatchWriter> {
let data = self.data_writer().await?;
let outboard = self.outboard_mut().await?;
Ok(CombinedBatchWriter { data, outboard })
}
}

impl PartialMap for Store {
type PartialEntry = PartialEntry;
impl MapMut for Store {
type EntryMut = EntryMut;

fn entry_status(&self, hash: &Hash) -> io::Result<EntryStatus> {
let state = self.0.state.read().unwrap();
Expand All @@ -330,7 +330,7 @@ impl PartialMap for Store {
fn get_possibly_partial(&self, hash: &Hash) -> io::Result<PossiblyPartialEntry<Self>> {
let state = self.0.state.read().unwrap();
Ok(if let Some(entry) = state.partial.get(hash) {
PossiblyPartialEntry::Partial(PartialEntry {
PossiblyPartialEntry::Partial(EntryMut {
hash: *hash,
size: entry.size,
data_path: self.0.options.partial_data_path(*hash, &entry.uuid),
Expand All @@ -346,7 +346,7 @@ impl PartialMap for Store {
})
}

fn get_or_create_partial(&self, hash: Hash, size: u64) -> io::Result<Self::PartialEntry> {
fn get_or_create_partial(&self, hash: Hash, size: u64) -> io::Result<Self::EntryMut> {
let mut state = self.0.state.write().unwrap();
// this protects the entry from being deleted until the next mark phase
//
Expand All @@ -367,15 +367,15 @@ impl PartialMap for Store {
.or_insert_with(|| PartialEntryData::new(size, new_uuid()));
let data_path = self.0.options.partial_data_path(hash, &entry.uuid);
let outboard_path = self.0.options.partial_outboard_path(hash, &entry.uuid);
Ok(PartialEntry {
Ok(EntryMut {
hash,
size: entry.size,
data_path,
outboard_path,
})
}

async fn insert_complete(&self, entry: Self::PartialEntry) -> io::Result<()> {
async fn insert_complete(&self, entry: Self::EntryMut) -> io::Result<()> {
let this = self.clone();
tokio::task::spawn_blocking(move || this.insert_complete_sync(entry))
.map(flatten_to_io)
Expand Down Expand Up @@ -560,9 +560,9 @@ fn needs_outboard(size: u64) -> bool {
size > (IROH_BLOCK_SIZE.bytes() as u64)
}

/// The [PartialMapEntry] implementation for [Store].
/// The [MapEntryMut] implementation for [Store].
#[derive(Debug, Clone)]
pub struct PartialEntry {
pub struct EntryMut {
hash: Hash,
size: u64,
data_path: PathBuf,
Expand Down Expand Up @@ -1065,7 +1065,7 @@ impl Store {
Ok(())
}

fn insert_complete_sync(&self, entry: PartialEntry) -> io::Result<()> {
fn insert_complete_sync(&self, entry: EntryMut) -> io::Result<()> {
let hash = entry.hash;
let data_path = self.0.options.owned_data_path(&hash);
let size = entry.size;
Expand Down
24 changes: 12 additions & 12 deletions iroh-bytes/src/store/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use super::PossiblyPartialEntry;
use super::TempCounterMap;
use crate::{
store::{
EntryStatus, ExportMode, ImportMode, ImportProgress, Map, MapEntry, PartialMap,
PartialMapEntry, ReadableStore, ValidateProgress,
EntryStatus, ExportMode, ImportMode, ImportProgress, Map, MapEntry, MapEntryMut,
MapMut, ReadableStore, ValidateProgress,
},
util::{
progress::{IdGenerator, IgnoreProgressSender, ProgressSender},
Expand Down Expand Up @@ -231,13 +231,13 @@ impl MapEntry for Entry {

/// The [MapEntry] implementation for [Store].
#[derive(Debug, Clone)]
pub struct PartialEntry {
pub struct EntryMut {
hash: Hash,
outboard: PreOrderOutboard<MutableMemFile>,
data: MutableMemFile,
}

impl MapEntry for PartialEntry {
impl MapEntry for EntryMut {
fn hash(&self) -> Hash {
self.hash
}
Expand Down Expand Up @@ -359,8 +359,8 @@ impl ReadableStore for Store {
}
}

impl PartialMap for Store {
type PartialEntry = PartialEntry;
impl MapMut for Store {
type EntryMut = EntryMut;

fn entry_status(&self, hash: &Hash) -> io::Result<EntryStatus> {
let state = self.0.state.read().unwrap();
Expand All @@ -376,7 +376,7 @@ impl PartialMap for Store {
fn get_possibly_partial(&self, hash: &Hash) -> io::Result<PossiblyPartialEntry<Self>> {
let state = self.0.state.read().unwrap();
Ok(match state.partial.get(hash) {
Some((data, outboard)) => PossiblyPartialEntry::Partial(PartialEntry {
Some((data, outboard)) => PossiblyPartialEntry::Partial(EntryMut {
hash: *hash,
outboard: outboard.clone(),
data: data.clone(),
Expand All @@ -385,7 +385,7 @@ impl PartialMap for Store {
})
}

fn get_or_create_partial(&self, hash: Hash, size: u64) -> io::Result<PartialEntry> {
fn get_or_create_partial(&self, hash: Hash, size: u64) -> io::Result<EntryMut> {
let tree = BaoTree::new(ByteNum(size), IROH_BLOCK_SIZE);
let outboard_size =
usize::try_from(outboard_size(size, IROH_BLOCK_SIZE)).map_err(data_too_large)?;
Expand All @@ -404,7 +404,7 @@ impl PartialMap for Store {
.unwrap()
.partial
.insert(hash, (data.clone(), ob2));
Ok(PartialEntry {
Ok(EntryMut {
hash,
outboard: PreOrderOutboard {
root: hash.into(),
Expand All @@ -415,7 +415,7 @@ impl PartialMap for Store {
})
}

async fn insert_complete(&self, entry: PartialEntry) -> io::Result<()> {
async fn insert_complete(&self, entry: EntryMut) -> io::Result<()> {
tracing::debug!("insert_complete_entry {:#}", entry.hash());
let hash = entry.hash;
let data = entry.data.freeze();
Expand Down Expand Up @@ -636,7 +636,7 @@ impl Store {
}
}

impl PartialEntry {
impl EntryMut {
fn outboard_mut(&self) -> PreOrderOutboard<MutableMemFile> {
self.outboard.clone()
}
Expand All @@ -646,7 +646,7 @@ impl PartialEntry {
}
}

impl PartialMapEntry for PartialEntry {
impl MapEntryMut for EntryMut {
async fn batch_writer(&self) -> io::Result<impl BaoBatchWriter> {
let data = self.data_writer();
let outboard = self.outboard_mut();
Expand Down
32 changes: 16 additions & 16 deletions iroh-bytes/src/store/readonly_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::{

use crate::{
store::{
EntryStatus, ExportMode, ImportMode, ImportProgress, Map, MapEntry, PartialMap,
PartialMapEntry, ReadableStore, ValidateProgress,
EntryStatus, ExportMode, ImportMode, ImportProgress, Map, MapEntry, MapEntryMut,
MapMut, ReadableStore, ValidateProgress,
},
util::{
progress::{IdGenerator, ProgressSender},
Expand Down Expand Up @@ -160,11 +160,11 @@ pub struct Entry {
data: Bytes,
}

/// The [PartialMapEntry] implementation for [Store].
/// The [MapEntryMut] implementation for [Store].
///
/// This is an unoccupied type, since [Store] is does not allow creating partial entries.
#[derive(Debug, Clone)]
pub enum PartialEntry {}
pub enum EntryMut {}

impl MapEntry for Entry {
fn hash(&self) -> Hash {
Expand Down Expand Up @@ -203,10 +203,10 @@ impl Map for Store {
}
}

impl PartialMap for Store {
type PartialEntry = PartialEntry;
impl MapMut for Store {
type EntryMut = EntryMut;

fn get_or_create_partial(&self, _hash: Hash, _size: u64) -> io::Result<PartialEntry> {
fn get_or_create_partial(&self, _hash: Hash, _size: u64) -> io::Result<EntryMut> {
Err(io::Error::new(
io::ErrorKind::Other,
"cannot create temp entry in readonly database",
Expand All @@ -232,7 +232,7 @@ impl PartialMap for Store {
})
}

async fn insert_complete(&self, _entry: PartialEntry) -> io::Result<()> {
async fn insert_complete(&self, _entry: EntryMut) -> io::Result<()> {
// this is unreachable, since we cannot create partial entries
unreachable!()
}
Expand Down Expand Up @@ -277,41 +277,41 @@ impl ReadableStore for Store {
}
}

impl MapEntry for PartialEntry {
impl MapEntry for EntryMut {
fn hash(&self) -> Hash {
// this is unreachable, since PartialEntry can not be created
// this is unreachable, since EntryMut can not be created
unreachable!()
}

async fn available_ranges(&self) -> io::Result<ChunkRanges> {
// this is unreachable, since PartialEntry can not be created
// this is unreachable, since EntryMut can not be created
unreachable!()
}

fn size(&self) -> u64 {
// this is unreachable, since PartialEntry can not be created
// this is unreachable, since EntryMut can not be created
unreachable!()
}

#[allow(refining_impl_trait)]
async fn outboard(&self) -> io::Result<PreOrderMemOutboard> {
// this is unreachable, since PartialEntry can not be created
// this is unreachable, since EntryMut can not be created
unreachable!()
}

#[allow(refining_impl_trait)]
async fn data_reader(&self) -> io::Result<Bytes> {
// this is unreachable, since PartialEntry can not be created
// this is unreachable, since EntryMut can not be created
unreachable!()
}

fn is_complete(&self) -> bool {
// this is unreachable, since PartialEntry can not be created
// this is unreachable, since EntryMut can not be created
unreachable!()
}
}

impl PartialMapEntry for PartialEntry {
impl MapEntryMut for EntryMut {
async fn batch_writer(&self) -> io::Result<impl BaoBatchWriter> {
enum Bar {}
impl BaoBatchWriter for Bar {
Expand Down
20 changes: 9 additions & 11 deletions iroh-bytes/src/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ pub enum EntryStatus {
///
/// This correspnds to [`EntryStatus`], but also includes the entry itself.
#[derive(Debug)]
pub enum PossiblyPartialEntry<D: PartialMap> {
pub enum PossiblyPartialEntry<D: MapMut> {
/// A complete entry.
Complete(D::Entry),
/// A partial entry.
Partial(D::PartialEntry),
Partial(D::EntryMut),
/// We got nothing.
NotFound,
}
Expand Down Expand Up @@ -100,7 +100,7 @@ pub trait Map: Clone + Send + Sync + 'static {
}

/// A partial entry
pub trait PartialMapEntry: MapEntry {
pub trait MapEntryMut: MapEntry {
/// Get a batch writer
fn batch_writer(&self) -> impl Future<Output = io::Result<impl BaoBatchWriter>> + Send;
}
Expand Down Expand Up @@ -242,17 +242,15 @@ where
}

/// A mutable bao map
pub trait PartialMap: Map {
/// A partial entry. This is an entry that is writeable and possibly incomplete.
///
/// It must also be readable.
type PartialEntry: PartialMapEntry;
pub trait MapMut: Map {
/// An entry that is possibly writable
type EntryMut: MapEntryMut;

/// Get an existing partial entry, or create a new one.
///
/// We need to know the size of the partial entry. This might produce an
/// error e.g. if there is not enough space on disk.
fn get_or_create_partial(&self, hash: Hash, size: u64) -> io::Result<Self::PartialEntry>;
fn get_or_create_partial(&self, hash: Hash, size: u64) -> io::Result<Self::EntryMut>;

/// Find out if the data behind a `hash` is complete, partial, or not present.
///
Expand All @@ -269,7 +267,7 @@ pub trait PartialMap: Map {
fn get_possibly_partial(&self, hash: &Hash) -> io::Result<PossiblyPartialEntry<Self>>;

/// Upgrade a partial entry to a complete entry.
fn insert_complete(&self, entry: Self::PartialEntry) -> impl Future<Output = io::Result<()>>;
fn insert_complete(&self, entry: Self::EntryMut) -> impl Future<Output = io::Result<()>>;
}

/// Extension of BaoMap to add misc methods used by the rpc calls.
Expand Down Expand Up @@ -308,7 +306,7 @@ pub trait ReadableStore: Map {
}

/// The mutable part of a BaoDb
pub trait Store: ReadableStore + PartialMap {
pub trait Store: ReadableStore + MapMut {
/// This trait method imports a file from a local path.
///
/// `data` is the path to the file.
Expand Down
Loading

0 comments on commit d386636

Please sign in to comment.