Skip to content

Commit

Permalink
refactor(iroh)!: replace public fields in iroh client with accessors …
Browse files Browse the repository at this point in the history
…and use ref-cast to eliminate them entirely (#2350)

## Description

With 4 different clients, the current approach might be OK. But we are
going to have more. E.g. gossip, see
#2258 .

And in any case it feels weird to store the same thing multiple times.

So this replaces public fields in the iroh client with accessors and use
ref-cast to eliminate them entirely. There is now only one rpc field,
and you can switch from that to the different subsystem client views
without runtime overhead, not even an arc clone.

## Breaking Changes

Everything that uses the iroh client will have to switch from field
accesses to accessor fns.

## Notes & open questions

<!-- Any notes, remarks or open questions you have to make about the PR.
-->

## Change checklist

- [ ] Self-review.
- [ ] Documentation updates if relevant.
- [ ] Tests if relevant.
- [ ] All breaking changes documented.
  • Loading branch information
rklaehn authored Jun 7, 2024
1 parent 8d91b10 commit 35ce780
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 153 deletions.
12 changes: 6 additions & 6 deletions iroh-cli/src/commands/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl AuthorCommands {
println!("Active author is now {}", fmt_short(author.as_bytes()));
}
Self::List => {
let mut stream = iroh.authors.list().await?;
let mut stream = iroh.authors().list().await?;
while let Some(author_id) = stream.try_next().await? {
println!("{}", author_id);
}
Expand All @@ -57,7 +57,7 @@ impl AuthorCommands {
if switch && !env.is_console() {
bail!("The --switch flag is only supported within the Iroh console.");
}
let author_id = iroh.authors.default().await?;
let author_id = iroh.authors().default().await?;
println!("{}", author_id);
if switch {
env.set_author(author_id)?;
Expand All @@ -69,7 +69,7 @@ impl AuthorCommands {
bail!("The --switch flag is only supported within the Iroh console.");
}

let author_id = iroh.authors.create().await?;
let author_id = iroh.authors().create().await?;
println!("{}", author_id);

if switch {
Expand All @@ -78,10 +78,10 @@ impl AuthorCommands {
}
}
Self::Delete { author } => {
iroh.authors.delete(author).await?;
iroh.authors().delete(author).await?;
println!("Deleted author {}", fmt_short(author.as_bytes()));
}
Self::Export { author } => match iroh.authors.export(author).await? {
Self::Export { author } => match iroh.authors().export(author).await? {
Some(author) => {
println!("{}", author);
}
Expand All @@ -92,7 +92,7 @@ impl AuthorCommands {
Self::Import { author } => match Author::from_str(&author) {
Ok(author) => {
let id = author.id();
iroh.authors.import(author).await?;
iroh.authors().import(author).await?;
println!("Imported {}", fmt_short(id));
}
Err(err) => {
Expand Down
30 changes: 15 additions & 15 deletions iroh-cli/src/commands/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl BlobCommands {
};

let mut stream = iroh
.blobs
.blobs()
.download_with_opts(
hash,
DownloadOptions {
Expand All @@ -281,7 +281,7 @@ impl BlobCommands {
Some(OutputTarget::Stdout) => {
// we asserted above that `OutputTarget::Stdout` is only permitted if getting a
// single hash and not a hashseq.
let mut blob_read = iroh.blobs.read(hash).await?;
let mut blob_read = iroh.blobs().read(hash).await?;
tokio::io::copy(&mut blob_read, &mut tokio::io::stdout()).await?;
}
Some(OutputTarget::Path(path)) => {
Expand All @@ -299,7 +299,7 @@ impl BlobCommands {
false => ExportFormat::Blob,
};
tracing::info!("exporting to {} -> {}", path.display(), absolute.display());
let stream = iroh.blobs.export(hash, absolute, format, mode).await?;
let stream = iroh.blobs().export(hash, absolute, format, mode).await?;

// TODO: report export progress
stream.await?;
Expand All @@ -320,7 +320,7 @@ impl BlobCommands {
!recursive,
"Recursive option is not supported when exporting to STDOUT"
);
let mut blob_read = iroh.blobs.read(hash).await?;
let mut blob_read = iroh.blobs().read(hash).await?;
tokio::io::copy(&mut blob_read, &mut tokio::io::stdout()).await?;
}
OutputTarget::Path(path) => {
Expand All @@ -341,7 +341,7 @@ impl BlobCommands {
path.display(),
absolute.display()
);
let stream = iroh.blobs.export(hash, absolute, format, mode).await?;
let stream = iroh.blobs().export(hash, absolute, format, mode).await?;
// TODO: report export progress
stream.await?;
}
Expand Down Expand Up @@ -369,8 +369,8 @@ impl BlobCommands {
} else {
BlobFormat::Raw
};
let status = iroh.blobs.status(hash).await?;
let ticket = iroh.blobs.share(hash, format, addr_options).await?;
let status = iroh.blobs().status(hash).await?;
let ticket = iroh.blobs().share(hash, format, addr_options).await?;

let (blob_status, size) = match (status, format) {
(BlobStatus::Complete { size }, BlobFormat::Raw) => ("blob", size),
Expand Down Expand Up @@ -453,21 +453,21 @@ impl ListCommands {
{
match self {
Self::Blobs => {
let mut response = iroh.blobs.list().await?;
let mut response = iroh.blobs().list().await?;
while let Some(item) = response.next().await {
let BlobInfo { path, hash, size } = item?;
println!("{} {} ({})", path, hash, HumanBytes(size));
}
}
Self::IncompleteBlobs => {
let mut response = iroh.blobs.list_incomplete().await?;
let mut response = iroh.blobs().list_incomplete().await?;
while let Some(item) = response.next().await {
let IncompleteBlobInfo { hash, size, .. } = item?;
println!("{} ({})", hash, HumanBytes(size));
}
}
Self::Collections => {
let mut response = iroh.blobs.list_collections()?;
let mut response = iroh.blobs().list_collections()?;
while let Some(item) = response.next().await {
let CollectionInfo {
tag,
Expand Down Expand Up @@ -513,7 +513,7 @@ impl DeleteCommands {
{
match self {
Self::Blob { hash } => {
let response = iroh.blobs.delete_blob(hash).await;
let response = iroh.blobs().delete_blob(hash).await;
if let Err(e) = response {
eprintln!("Error: {}", e);
}
Expand Down Expand Up @@ -544,7 +544,7 @@ pub async fn consistency_check<C>(iroh: &Iroh<C>, verbose: u8, repair: bool) ->
where
C: ServiceConnection<RpcService>,
{
let mut response = iroh.blobs.consistency_check(repair).await?;
let mut response = iroh.blobs().consistency_check(repair).await?;
let verbosity = get_report_level(verbose);
let print = |level: ReportLevel, entry: Option<Hash>, message: String| {
if level < verbosity {
Expand Down Expand Up @@ -589,7 +589,7 @@ where
C: ServiceConnection<RpcService>,
{
let mut state = ValidateProgressState::new();
let mut response = iroh.blobs.validate(repair).await?;
let mut response = iroh.blobs().validate(repair).await?;
let verbosity = get_report_level(verbose);
let print = |level: ReportLevel, entry: Option<Hash>, message: String| {
if level < verbosity {
Expand Down Expand Up @@ -854,7 +854,7 @@ pub async fn add<C: ServiceConnection<RpcService>>(

// tell the node to add the data
let stream = client
.blobs
.blobs()
.add_from_path(absolute, in_place, tag, wrap)
.await?;
aggregate_add_response(stream).await?
Expand All @@ -872,7 +872,7 @@ pub async fn add<C: ServiceConnection<RpcService>>(

// tell the node to add the data
let stream = client
.blobs
.blobs()
.add_from_path(path_buf, false, tag, wrap)
.await?;
aggregate_add_response(stream).await?
Expand Down
16 changes: 8 additions & 8 deletions iroh-cli/src/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl DocCommands {
bail!("The --switch flag is only supported within the Iroh console.");
}

let doc = iroh.docs.create().await?;
let doc = iroh.docs().create().await?;
println!("{}", doc.id());

if switch {
Expand All @@ -330,7 +330,7 @@ impl DocCommands {
bail!("The --switch flag is only supported within the Iroh console.");
}

let doc = iroh.docs.import(ticket).await?;
let doc = iroh.docs().import(ticket).await?;
println!("{}", doc.id());

if switch {
Expand All @@ -339,7 +339,7 @@ impl DocCommands {
}
}
Self::List => {
let mut stream = iroh.docs.list().await?;
let mut stream = iroh.docs().list().await?;
while let Some((id, kind)) = stream.try_next().await? {
println!("{id} {kind}")
}
Expand Down Expand Up @@ -483,7 +483,7 @@ impl DocCommands {
}

let stream = iroh
.blobs
.blobs()
.add_from_path(
root.clone(),
in_place,
Expand Down Expand Up @@ -627,7 +627,7 @@ impl DocCommands {
.interact()
.unwrap_or(false)
{
iroh.docs.drop_doc(doc.id()).await?;
iroh.docs().drop_doc(doc.id()).await?;
println!("Doc {} has been deleted.", fmt_short(doc.id()));
} else {
println!("Aborted.")
Expand Down Expand Up @@ -681,7 +681,7 @@ async fn get_doc<C>(
where
C: ServiceConnection<RpcService>,
{
iroh.docs
iroh.docs()
.open(env.doc(id)?)
.await?
.context("Document not found")
Expand Down Expand Up @@ -975,8 +975,8 @@ mod tests {

let node = crate::commands::start::start_node(data_dir.path(), None).await?;
let client = node.client();
let doc = client.docs.create().await.context("doc create")?;
let author = client.authors.create().await.context("author create")?;
let doc = client.docs().create().await.context("doc create")?;
let author = client.authors().create().await.context("author create")?;

// set up command, getting iroh node
let cli = ConsoleEnv::for_console(data_dir.path().to_owned(), &node)
Expand Down
4 changes: 2 additions & 2 deletions iroh-cli/src/commands/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl TagCommands {
{
match self {
Self::List => {
let mut response = iroh.tags.list().await?;
let mut response = iroh.tags().list().await?;
while let Some(res) = response.next().await {
let res = res?;
println!("{}: {} ({:?})", res.name, res.hash, res.format);
Expand All @@ -38,7 +38,7 @@ impl TagCommands {
} else {
Tag::from(tag)
};
iroh.tags.delete(tag).await?;
iroh.tags().delete(tag).await?;
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion iroh-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ async fn env_author<C: ServiceConnection<RpcService>>(
{
Ok(author)
} else {
iroh.authors.default().await
iroh.authors().default().await
}
}

Expand Down
4 changes: 2 additions & 2 deletions iroh/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async fn main() -> anyhow::Result<()> {
// Could also use `node` directly, as it derefs to the client.
let client = node.client();

let doc = client.docs.create().await?;
let author = client.authors.default().await?;
let doc = client.docs().create().await?;
let author = client.authors().default().await?;

doc.set_bytes(author, "hello", "world").await?;

Expand Down
6 changes: 3 additions & 3 deletions iroh/examples/collection-fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn main() -> Result<()> {
// `download` returns a stream of `DownloadProgress` events. You can iterate through these updates to get progress
// on the state of your download.
let download_stream = node
.blobs
.blobs()
.download_hash_seq(ticket.hash(), ticket.node_addr().clone())
.await?;

Expand All @@ -76,7 +76,7 @@ async fn main() -> Result<()> {
// A `Collection` is a special `HashSeq`, where we preserve the names of any blobs added to the collection. (We do this by designating the first entry in the `Collection` as meta data.)
// To get the content of the collection, we first get the collection from the database using the `blobs` API
let collection = node
.blobs
.blobs()
.get_collection(ticket.hash())
.await
.context("expect hash with `BlobFormat::HashSeq` to be a collection")?;
Expand All @@ -85,7 +85,7 @@ async fn main() -> Result<()> {
for (name, hash) in collection.iter() {
println!("\nname: {name}, hash: {hash}");
// Use the hash of the blob to get the content.
let content = node.blobs.read_to_bytes(*hash).await?;
let content = node.blobs().read_to_bytes(*hash).await?;
let s = std::str::from_utf8(&content).context("unable to parse blob as as utf-8 string")?;
println!("{s}");
}
Expand Down
8 changes: 4 additions & 4 deletions iroh/examples/collection-provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async fn main() -> anyhow::Result<()> {
let node = iroh::node::Node::memory().spawn().await?;

// Add two blobs
let blob1 = node.blobs.add_bytes("the first blob of bytes").await?;
let blob2 = node.blobs.add_bytes("the second blob of bytes").await?;
let blob1 = node.blobs().add_bytes("the first blob of bytes").await?;
let blob2 = node.blobs().add_bytes("the second blob of bytes").await?;

// Create blobs from the data
let collection: Collection = [("blob1", blob1.hash), ("blob2", blob2.hash)]
Expand All @@ -37,14 +37,14 @@ async fn main() -> anyhow::Result<()> {

// Create a collection
let (hash, _) = node
.blobs
.blobs()
.create_collection(collection, SetTagOption::Auto, Default::default())
.await?;

// create a ticket
// tickets wrap all details needed to get a collection
let ticket = node
.blobs
.blobs()
.share(hash, BlobFormat::HashSeq, Default::default())
.await?;

Expand Down
4 changes: 2 additions & 2 deletions iroh/examples/hello-world-fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn main() -> Result<()> {
// `download` returns a stream of `DownloadProgress` events. You can iterate through these updates to get progress
// on the state of your download.
let download_stream = node
.blobs
.blobs()
.download(ticket.hash(), ticket.node_addr().clone())
.await?;

Expand All @@ -74,7 +74,7 @@ async fn main() -> Result<()> {

// Get the content we have just fetched from the iroh database.

let bytes = node.blobs.read_to_bytes(ticket.hash()).await?;
let bytes = node.blobs().read_to_bytes(ticket.hash()).await?;
let s = std::str::from_utf8(&bytes).context("unable to parse blob as as utf-8 string")?;
println!("{s}");

Expand Down
4 changes: 2 additions & 2 deletions iroh/examples/hello-world-provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ async fn main() -> anyhow::Result<()> {
let node = iroh::node::Node::memory().spawn().await?;

// add some data and remember the hash
let res = node.blobs.add_bytes("Hello, world!").await?;
let res = node.blobs().add_bytes("Hello, world!").await?;

// create a ticket
let ticket = node
.blobs
.blobs()
.share(res.hash, res.format, Default::default())
.await?;

Expand Down
Loading

0 comments on commit 35ce780

Please sign in to comment.