Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Links, paths, and anchors #453

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d1bbb0a
add glossary entry for addressable content
pdaoust May 24, 2024
a8870fd
new links content
pdaoust Jun 6, 2024
34847bc
add links page to nav
pdaoust Jun 6, 2024
c404706
modifications to entries page to make the format fit better with links
pdaoust Jun 6, 2024
047e9ac
add pubkey to dictionary
pdaoust Jun 6, 2024
9b29a8c
fix broken links
pdaoust Jun 6, 2024
7a81f50
small text edit on links page
pdaoust Jun 7, 2024
7123b93
small content edits
pdaoust Sep 27, 2024
41470c5
refine warnings about depending on hashes
pdaoust Sep 27, 2024
5397386
fix code samples for anchors
pdaoust Sep 27, 2024
abcfc67
fix erroneous link tag size
pdaoust Sep 27, 2024
7296c0f
add identifiers to nav
pdaoust Sep 30, 2024
866ba85
small edits to links build guide
pdaoust Nov 12, 2024
b12d049
large changes to identifiers build guide
pdaoust Nov 12, 2024
8064b6c
add words to dictionary
pdaoust Nov 12, 2024
e9820c9
add links to community path libraries
pdaoust Nov 13, 2024
40cc54a
improve/consistent-ify navigation in build guide
pdaoust Nov 13, 2024
adebfaf
Merge branch 'main' into feat/guide/links
pdaoust Nov 22, 2024
9f79f3e
fix broken link
pdaoust Nov 22, 2024
a885aaf
fix language re: 'dead' and 'entry creation' actions
pdaoust Nov 22, 2024
6441164
nailing down some challenging language re: action hashes and graph DHT
pdaoust Nov 28, 2024
4803bb1
suppress spelling messages on hashes
pdaoust Nov 28, 2024
cd3451c
fix spelling mistake
pdaoust Nov 28, 2024
9792f1c
table styling
pdaoust Nov 28, 2024
3e6a315
final sweep for incorrect or very confusing things
pdaoust Nov 28, 2024
e3be354
fix broken link
pdaoust Nov 28, 2024
f6d07d4
experiment: remove cspell directives from code blocks
pdaoust Nov 28, 2024
b4366e8
change count_links bandwidth note
pdaoust Dec 16, 2024
fd08910
gitignore a local hApp folder for testing build guide code
pdaoust Dec 19, 2024
c1dda73
add Director entry type
pdaoust Dec 19, 2024
00b92d3
prose/code improvements
pdaoust Dec 19, 2024
e13d99b
unbreak/simplify code in build guide, update for 0.4
pdaoust Dec 20, 2024
cf17a98
Merge branch 'feat/guide/links' of github.com:holochain/docs-pages in…
pdaoust Dec 20, 2024
95a8f1c
Apply suggestions from code review
pdaoust Jan 2, 2025
322b58e
fix inconsistent indentation in code
pdaoust Jan 2, 2025
f2378c8
more nicer timestamp construction
pdaoust Jan 2, 2025
e7d6cc6
Merge branch 'feat/guide/links' of github.com:holochain/docs-pages in…
pdaoust Jan 2, 2025
8fa57d5
private data is encrypted
pdaoust Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ dist
# My 11ty specifics
_site/
client-side-compiled/
styles-compiled/
styles-compiled/

# A place to put a local scaffolded DNA so you can test code for the build guide
# without having to have two projects/folderss open in your editor.
code_test/
95 changes: 49 additions & 46 deletions src/pages/build/entries.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ Entry types are defined in an [**integrity zome**](/resources/glossary/#integrit
```rust
use hdi::prelude::*;

#[hdk_entry_helper]
pub struct Director(pub String);

#[hdk_entry_helper]
pub struct Movie {
title: String,
director_hash: EntryHash,
imdb_id: Option<String>,
release_date: Timestamp,
box_office_revenue: u128,
pub title: String,
pub director_hash: EntryHash,
pub imdb_id: Option<String>,
pub release_date: Timestamp,
pub box_office_revenue: u128,
}
```

Expand All @@ -46,16 +49,16 @@ In order to dispatch validation to the proper integrity zome, Holochain needs to
use hdi::prelude::*;

#[hdk_entry_defs]
// This macro is required by hdk_entry_defs.
#[unit_enum(UnitEntryTypes)]
enum EntryTypes {
Director(Director),
Movie(Movie),
// other types...
}
```

This also gives you an enum that you can use later when you're storing app data. This is important because, under the hood, an entry type consists of two bytes --- an integrity zome index and an entry def index. These are required whenever you want to write an entry. Instead of having to remember those values every time you store something, your coordinator zome can just import and use this enum, which already knows how to convert each entry type to the right IDs.

The code sample above also uses a macro called `unit_enum`, which will generate an enum of all the entry types' names _without_ places to store values.
This also gives you an enum that you can use later when you're storing app data. Under the hood, an entry type consists of two bytes --- an integrity zome index and an entry def index. These are required whenever you want to write an entry. Instead of having to remember those values every time you store something, your coordinator zome can just import and use this enum, which already knows how to convert each entry type to the right IDs.

### Configure an entry type

Expand All @@ -70,14 +73,16 @@ use hdi::prelude::*;
#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
enum EntryTypes {
#[entry_def(required_validations = 7, )]
Director(Director),

#[entry_type(required_validations = 7, )]
Movie(Movie),

// You can reuse your Rust type in another entry type if you like. In this
// example, `HomeMovie` also (de)serializes to/from the `Movie` struct, but
// is actually a different entry type with different visibility, and can be
// subjected to different validation rules.
#[entry_def(visibility = "private", )]
#[entry_type(visibility = "private", )]
HomeMovie(Movie),
}
```
Expand All @@ -90,24 +95,23 @@ Create an entry by calling [`hdk::prelude::create_entry`](https://docs.rs/hdk/la

```rust
use hdk::prelude::*;
use chrono::Date;
// Import the entry types and the enum defined in the integrity zome.
use movie_integrity::*;

let movie = Movie {
title: "The Good, the Bad, and the Ugly",
title: "The Good, the Bad, and the Ugly".to_string(),
director_hash: EntryHash::from_raw_36(vec![ /* hash of 'Sergio Leone' entry */ ]),
mattyg marked this conversation as resolved.
Show resolved Hide resolved
imdb_id: Some("tt0060196"),
release_date: Timestamp::from(Date::Utc("1966-12-23")),
imdb_id: Some("tt0060196".to_string()),
release_date: Timestamp(-95_472_000_000_000), // 1966-12-23
pdaoust marked this conversation as resolved.
Show resolved Hide resolved
box_office_revenue: 389_000_000,
};

let create_action_hash: ActionHash = create_entry(
let create_action_hash = create_entry(
// The value you pass to `create_entry` needs a lot of traits to tell
// Holochain which entry type from which integrity zome you're trying to
// create. The `hdk_entry_defs` macro will have set this up for you, so all
// you need to do is wrap your movie in the corresponding enum variant.
&EntryTypes::Movie(movie.clone()),
&EntryTypes::Movie(movie),
)?;
```

Expand Down Expand Up @@ -143,20 +147,20 @@ Update an entry creation action by calling [`hdk::prelude::update_entry`](https:

```rust
use hdk::prelude::*;
use chrono::Date;
use movie_integrity::*;

let movie2 = Movie {
title: "The Good, the Bad, and the Ugly",
director: "Sergio Leone"
imdb_id: Some("tt0060196"),
release_date: Timestamp::from(Date::Utc("1966-12-23")),
title: "The Good, the Bad, and the Ugly".to_string(),
director_hash: EntryHash::from_raw_36(vec![ /* hash of 'Sergio Leone' entry */ ]),
imdb_id: Some("tt0060196".to_string()),
release_date: Timestamp(-95_472_000_000_000),
pdaoust marked this conversation as resolved.
Show resolved Hide resolved
// Corrected from 389_000_000
box_office_revenue: 400_000_000,
};

let update_action_hash: ActionHash = update_entry(
let update_action_hash = update_entry(
create_action_hash,
&EntryTypes::Movie(movie2.clone()),
&EntryTypes::Movie(movie2),
)?;
```

Expand Down Expand Up @@ -225,7 +229,7 @@ Delete an entry creation action by calling [`hdk::prelude::delete_entry`](https:
```rust
use hdk::prelude::*;

let delete_action_hash: ActionHash = delete_entry(
let delete_action_hash = delete_entry(
create_action_hash,
)?;
```
Expand Down Expand Up @@ -268,22 +272,23 @@ You can use any of these identifiers as a field in your entry types to model a m

## Retrieve an entry

### By record only
### As a single record

Get a record by calling [`hdk::prelude::get`](https://docs.rs/hdk/latest/hdk/prelude/fn.get.html) with the hash of its entry creation action. The return value is a <code>Result<[holochain_integrity_types::record::Record](https://docs.rs/holochain_integrity_types/latest/holochain_integrity_types/record/struct.Record.html)></code>.
Get a record by calling [`hdk::prelude::get`](https://docs.rs/hdk/latest/hdk/prelude/fn.get.html) with the hash of either its entry creation action. The return value is a <code>Result<[holochain_integrity_types::record::Record](https://docs.rs/holochain_integrity_types/latest/holochain_integrity_types/record/struct.Record.html)></code>.

You can also pass an entry hash to `get`, and the record returned will contain the _oldest live_ entry creation action that wrote it.
You can also pass an _entry hash_ to `get`, and the record returned will contain the _oldest live_ entry creation action that wrote it.

```rust
use hdk::prelude::*;
use movie_integrity::*;

let maybe_record: Option<Record> = get(
let maybe_record = get(
action_hash,
// Get the data and metadata directly from the DHT. You can also specify
// `GetOptions::content()`, which only accesses the DHT if the data at the
// supplied hash doesn't already exist locally.
GetOptions::latest()
// Get the data and metadata directly from the DHT, falling back to local
// storage if it can't access peers.
// You can also specify `GetOptions::local()`, which only accesses the local
// storage.
GetOptions::network()
)?;

match maybe_record {
Expand All @@ -296,7 +301,7 @@ match maybe_record {
// `Record` type, but in this simple example we'll skip that and assume
// that the action hash does reference an action with entry data
// attached to it.
let maybe_movie: Option<Movie> = record.entry().to_app_option();
let maybe_movie = record.entry().to_app_option()?;

match maybe_movie {
Some(movie) => debug!(
Expand All @@ -310,7 +315,7 @@ match maybe_record {
}
}
None => debug!("Movie record not found"),
}
};
```

### All data, actions, and links at an address
Expand All @@ -323,14 +328,14 @@ To get a record and all the updates, deletes, and outbound links associated with
use hdk::prelude::*;
use movie_integrity::*;

let maybe_details: Option<Details> = get_details(
let maybe_details = get_details(
action_hash,
GetOptions::latest()
GetOptions::network()
)?;

match maybe_details {
Some(Details::Record(record_details)) => {
let maybe_movie: Option<Movie> = record.entry().to_app_option();
let maybe_movie: Option<Movie> = record_details.record.entry().to_app_option()?;
match maybe_movie {
Some(movie) => debug!(
"Movie record {}, created on {}, was updated by {} agents and deleted by {} agents",
Expand All @@ -343,7 +348,7 @@ match maybe_details {
}
}
_ => debug!("Movie record not found"),
}
};
```

#### Entries
Expand All @@ -356,24 +361,22 @@ use movie_integrity::*;

let maybe_details: Option<Details> = get_details(
entry_hash,
GetOptions::latest()
GetOptions::network()
)?;

match maybe_details {
Some(Details::Entry(entry_details)) => {
let maybe_movie: Option<Movie> = entry_details.entry
let maybe_movie = entry_details.entry
.as_app_entry()
.clone()
.try_into()
.ok();
.map(|b| Movie::try_from(b.into_sb()))
.transpose()?;
match maybe_movie {
Some(movie) => debug!(
"Movie {} was written by {} agents, updated by {} agents, and deleted by {} agents. Its DHT status is currently {}.",
"Movie {} was written by {} agents, updated by {} agents, and deleted by {} agents.",
movie.title,
entry_details.actions.len(),
entry_details.updates.len(),
entry_details.deletes.len(),
entry_details.entry_dht_status
entry_details.deletes.len()
),
None => debug!("Movie entry couldn't be retrieved"),
}
Expand Down
Loading
Loading