Skip to content

Commit

Permalink
Merge pull request #73 from n0-computer/doc_list
Browse files Browse the repository at this point in the history
fill out all `doc_list` examples
  • Loading branch information
b5 authored Nov 4, 2023
2 parents efc9f63 + c9767db commit cd189bc
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 64 deletions.
23 changes: 4 additions & 19 deletions api-code-examples/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const doc = [
tiqpal5qnrb3idy7g4n7hnh5esex7zu6jtqyuwt6hr4iq2nnlpua
3ogcanavjfehmoeuf3jkel5pmbv2bpdwybvzt7xzk5sgbub72mia
njszszvgpziwnxqnsi32nmc7j2czs2rnj3m7czavudurqxld3nbq`,
cli: `$ iroh doc list
tiqpal5qnrb3idy7g4n7hnh5esex7zu6jtqyuwt6hr4iq2nnlpua
3ogcanavjfehmoeuf3jkel5pmbv2bpdwybvzt7xzk5sgbub72mia
njszszvgpziwnxqnsi32nmc7j2czs2rnj3m7czavudurqxld3nbq`
}
},
{
Expand Down Expand Up @@ -230,25 +234,6 @@ Total: 328 B
Collection: bafkr4ie3xsx3vdsbflainnk6p4xs4h2hq3hdmuasuoflkgybvnsbljb3ke`,
}
},
{
name: 'blob share',
description: 'Download data to the running provider\'s database and provide it.',
slug: 'blob-share',
arguments: [
{ name: 'hash', required: '', description: 'Hash to get, required unless ticket is specified.' },
{ name: 'recursive', required: '', description: 'Treat as collection, required unless ticket is specified [possible values: true, false].'},
{ name: 'peer', required: '', description: 'PublicKey of the provider.' },
{ name: 'addr', required: '', description: 'Addresses of the provider.' },
{ name: 'derp-region', required: '', description: 'DERP region ID of the provider.' },
{ name: 'token', required: '', description: 'Base32-encoded request token to use for authentication, if any.' },
{ name: 'ticket', required: '', description: 'Base32-encoded ticket to use for fetching.' },
{ name: 'out', required: '', description: 'Directory in which to save the file(s).' },
{ name: 'stable', required: '', description: 'If this is set to true, the data will be moved to the output directory, and iroh will assume that it will not change.'}
],
examples: {
console: `> `,
}
},
{
name: 'blob list blobs',
description: 'List the available blobs on the running provider.',
Expand Down
40 changes: 40 additions & 0 deletions api-code-examples/go/doc-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"

"github.com/n0-computer/iroh-ffi/iroh"
)

func main() {
node, err := iroh.NewIrohNode()
if err != nil {
// real programs handle errors!
panic(err)
}

// create one document
doc, err := node.DocNew()
if err != nil {
panic(err)
}
fmt.Printf("Created document %s\n", doc.Id())

// create a second document
doc, err := node.DocNew()
if err != nil {
panic(err)
}
fmt.Printf("Created document %s\n", doc.Id())

// list all your documents
docs, err := node.DocList()
if err != nil {
panic(err)
}

fmt.Printf("Listing all %d documents:\n", len(docs))
for _, doc_id := range docs {
fmt.Printf("\t%s\n", doc_id.ToString())
}
}
20 changes: 20 additions & 0 deletions api-code-examples/python/doc-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import iroh

IROH_DATA_DIR = "./iroh-data"

node = iroh.IrohNode(IROH_DATA_DIR)
print("Started Iroh node: {}".format(node.node_id()))

# create a document
doc = node.doc_new()
print("Created doc: {}".format(doc.id()))

# create a second document
doc = node.doc_new()
print("Created doc: {}".format(doc.id()))

# list all your documents
docs = node.doc_list();
print("List all {} docs:".format(len(docs)))
for doc in docs:
print("\t{}".format(doc.to_string()))
34 changes: 11 additions & 23 deletions api-code-examples/rust/doc-get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
use anyhow::{anyhow, Result};
use futures::StreamExt;

use iroh::{
collection::IrohCollectionParser,
node::Node,
bytes::util::runtime,
};
use iroh_sync::store::GetFilter;
use iroh::{bytes::util::runtime, collection::IrohCollectionParser, node::Node};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -20,27 +15,20 @@ async fn main() -> Result<()> {
.collection_parser(IrohCollectionParser)
.runtime(&rt)
.bind_addr("127.0.0.1:0".parse()?);

// start the node & create a client
let node = node.spawn().await?;
let client = node.client();

// create a document & author
let author = client.create_author().await?;
let doc = client.create_doc().await?;

// set the key "key" to "value"
let key = b"key";
let value = b"value";
doc.set_bytes(author, key.to_vec(), value.to_vec()).await?;
println!("key is set!");
// create a document
let doc_0 = client.create_doc().await?;
let doc_1 = client.create_doc().await?;

// read the value back
let filter = GetFilter::latest().with_key(key);
let entry = doc.get(filter).await?.next().await.ok_or_else(|| anyhow!("entry not found"))??;
let content = doc.get_content_bytes(&entry).await?;

println!("value bytes: {:?}", content);
println!("List all docs:");
let doc_ids = client.doc_list().await?;
for doc_id in doc_ids.into_iter() {
println!("\t{doc_id}");
}

Ok(())
}
}
47 changes: 47 additions & 0 deletions api-code-examples/rust/doc-list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![cfg(feature = "mem-db")]

use anyhow::{anyhow, Result};
use futures::StreamExt;

use iroh::{bytes::util::runtime, collection::IrohCollectionParser, node::Node};
use iroh_sync::store::GetFilter;

#[tokio::main]
async fn main() -> Result<()> {
// build the node
let rt = runtime::Handle::from_currrent(1).unwrap();
let db = iroh::baomap::mem::Store::new(rt.clone());
let store = iroh_sync::store::memory::Store::default();
let node = Node::builder(db, store)
.collection_parser(IrohCollectionParser)
.runtime(&rt)
.bind_addr("127.0.0.1:0".parse()?);

// start the node & create a client
let node = node.spawn().await?;
let client = node.client();

// create a document & author
let author = client.create_author().await?;
let doc = client.create_doc().await?;

// set the key "key" to "value"
let key = b"key";
let value = b"value";
doc.set_bytes(author, key.to_vec(), value.to_vec()).await?;
println!("key is set!");

// read the value back
let filter = GetFilter::latest().with_key(key);
let entry = doc
.get(filter)
.await?
.next()
.await
.ok_or_else(|| anyhow!("entry not found"))??;
let content = doc.get_content_bytes(&entry).await?;

println!("value bytes: {:?}", content);

Ok(())
}
33 changes: 11 additions & 22 deletions src/app/docs/api/doc-get/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,7 @@ func main() {
use anyhow::{anyhow, Result};
use futures::StreamExt;

use iroh::{
collection::IrohCollectionParser,
node::Node,
bytes::util::runtime,
};
use iroh_sync::store::GetFilter;
use iroh::{bytes::util::runtime, collection::IrohCollectionParser, node::Node};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -150,30 +145,24 @@ async fn main() -> Result<()> {
.collection_parser(IrohCollectionParser)
.runtime(&rt)
.bind_addr("127.0.0.1:0".parse()?);

// start the node & create a client
let node = node.spawn().await?;
let client = node.client();

// create a document & author
let author = client.create_author().await?;
let doc = client.create_doc().await?;

// set the key "key" to "value"
let key = b"key";
let value = b"value";
doc.set_bytes(author, key.to_vec(), value.to_vec()).await?;
println!("key is set!");
// create a document
let doc_0 = client.create_doc().await?;
let doc_1 = client.create_doc().await?;

// read the value back
let filter = GetFilter::latest().with_key(key);
let entry = doc.get(filter).await?.next().await.ok_or_else(|| anyhow!("entry not found"))??;
let content = doc.get_content_bytes(&entry).await?;

println!("value bytes: {:?}", content);
println!("List all docs:");
let doc_ids = client.doc_list().await?;
for doc_id in doc_ids.into_iter() {
println!("\t{doc_id}");
}

Ok(())
}

```


Expand Down
126 changes: 126 additions & 0 deletions src/app/docs/api/doc-list/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,131 @@ List documents on this node. {{ className: 'lead' }}
njszszvgpziwnxqnsi32nmc7j2czs2rnj3m7czavudurqxld3nbq
```

```bash {{ title: 'CLI' }}
$ iroh doc list
tiqpal5qnrb3idy7g4n7hnh5esex7zu6jtqyuwt6hr4iq2nnlpua
3ogcanavjfehmoeuf3jkel5pmbv2bpdwybvzt7xzk5sgbub72mia
njszszvgpziwnxqnsi32nmc7j2czs2rnj3m7czavudurqxld3nbq
```

```python {{ title: 'python' }}
import iroh

IROH_DATA_DIR = "./iroh-data"

node = iroh.IrohNode(IROH_DATA_DIR)
print("Started Iroh node: {}".format(node.node_id()))

# create a document
doc = node.doc_new()
print("Created doc: {}".format(doc.id()))

# create a second document
doc = node.doc_new()
print("Created doc: {}".format(doc.id()))

# list all your documents
docs = node.doc_list();
print("List all {} docs:".format(len(docs)))
for doc in docs:
print("\t{}".format(doc.to_string()))

```

```swift {{ title: 'go' }}
package main

import (
"fmt"

"github.com/n0-computer/iroh-ffi/iroh"
)

func main() {
node, err := iroh.NewIrohNode()
if err != nil {
// real programs handle errors!
panic(err)
}

// create one document
doc, err := node.DocNew()
if err != nil {
panic(err)
}
fmt.Printf("Created document %s\n", doc.Id())

// create a second document
doc, err := node.DocNew()
if err != nil {
panic(err)
}
fmt.Printf("Created document %s\n", doc.Id())

// list all your documents
docs, err := node.DocList()
if err != nil {
panic(err)
}

fmt.Printf("Listing all %d documents:\n", len(docs))
for _, doc_id := range docs {
fmt.Printf("\t%s\n", doc_id.ToString())
}
}

```

```rust {{ title: 'rust' }}
#![cfg(feature = "mem-db")]

use anyhow::{anyhow, Result};
use futures::StreamExt;

use iroh::{bytes::util::runtime, collection::IrohCollectionParser, node::Node};
use iroh_sync::store::GetFilter;

#[tokio::main]
async fn main() -> Result<()> {
// build the node
let rt = runtime::Handle::from_currrent(1).unwrap();
let db = iroh::baomap::mem::Store::new(rt.clone());
let store = iroh_sync::store::memory::Store::default();
let node = Node::builder(db, store)
.collection_parser(IrohCollectionParser)
.runtime(&rt)
.bind_addr("127.0.0.1:0".parse()?);

// start the node & create a client
let node = node.spawn().await?;
let client = node.client();

// create a document & author
let author = client.create_author().await?;
let doc = client.create_doc().await?;

// set the key "key" to "value"
let key = b"key";
let value = b"value";
doc.set_bytes(author, key.to_vec(), value.to_vec()).await?;
println!("key is set!");

// read the value back
let filter = GetFilter::latest().with_key(key);
let entry = doc
.get(filter)
.await?
.next()
.await
.ok_or_else(|| anyhow!("entry not found"))??;
let content = doc.get_content_bytes(&entry).await?;

println!("value bytes: {:?}", content);

Ok(())
}

```


</CodeGroup>

0 comments on commit cd189bc

Please sign in to comment.