forked from holochain-immersive/holochain-lesson-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
358 lines (301 loc) · 12 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, viewport-fit=cover"
/>
<meta name="Description" content="Put your description here." />
<base href="/" />
<style>
html,
body {
margin: 0;
padding: 0;
font-family: sans-serif;
background-color: #ededed;
--r-main-font-size: 24px;
--r-heading-margin: 20px 0 12px 0;
}
.slides {
width: 75% !important;
}
.container {
top: 0 !important;
display: flex !important;
flex-direction: row !important;
}
.column {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
section {
text-align: left;
}
.popover {
position: absolute;
background-color: #4d4d4d;
padding: 1rem 2rem;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
border-radius: 10px;
width: auto;
transform: translate(16px, -15px);
max-width: 600px;
white-space: break-spaces;
}
.popover:after {
content: "";
position: absolute;
top: 6px;
left: -8px;
border-style: solid;
border-width: 18px 12px 0;
border-color: #4d4d4d transparent;
display: block;
width: 0;
z-index: 1;
transform: translate(-50%, 50%) rotate(90deg);
}
.popover .hljs-meta,
.popover .hljs-class,
.popover .hljs-keyword,
.popover .hljs-string,
.popover .hljs-title {
color: rgb(221, 221, 221) !important;
font-weight: normal;
}
.reveal pre code {
max-height: 800px !important;
}
.dna {
background-color: green;
}
.zome {
background-color: blue;
}
.coordinator-zomes {
background-color: lightblue;
}
.dna,
.cell,
.zome,
.coordinator-zomes,
.source-chain,
.happ-bundle,
.box,
.dna-bundle,
.dht-shard,
.conductor,
.happ {
display: flex;
align-items: center;
justify-content: center;
padding: 12px;
border-width: 3px;
border-color: rgb(209, 209, 209);
border-style: solid;
}
</style>
<link rel="stylesheet" href="/node_modules/reveal.js/dist/reveal.css" />
<link
rel="stylesheet"
href="/node_modules/reveal.js/dist/theme/black.css"
/>
<link
rel="stylesheet"
href="/node_modules/reveal.js/plugin/highlight/monokai.css"
/>
<title>Holochain Lesson 4</title>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Anchors & Paths</h1>
</section>
<section language="markdown" animate="by-line with-ancestry">
### Anchors (I)
- Data in Holochain is best structured as a directed acyclic graph of linked actions, entries and public keys
- From a starting point ID (action, entry or agent), we typically want to retrieve certain data related to it
- For example, if our application will need to "get_comments_by_author(author: AgentPubKey)", then:
- When we create the comment,
- we also create a link
- from the public key of the author
- to the create action hash
- Inside "get_coments_by_author(author: AgentPubKey)"
- we get links from that agent public key
- to retrieve all the comments by that author
</section>
<section language="markdown" animate="by-line with-ancestry">
### Anchors (II)
- Problem: how do we build queries that don't have starting point?
- For example: "get_all_comments()", which gets all comments that any agent has created
- We can't go to all the cells and query all the comments that each cell has created as it's intractably expensive when the network grows
- Solution: hard code a deterministic entry in the source code
- Whenever we create a comment, we also create a link from the hash of that deterministic entry
- Then we can implement "get_all_comments()" by doing a get links from that hash that everyone knows
- These deterministic entries are called "anchors"
- Demo: https://holochain-gym.github.io/developers/intermediate/anchors/#try-it
</section>
<section>
<h3>Anchors (III)</h3>
<div class="row">
<pre
class="fragment fade-in" style="flex-basis: 800px;"
><code class="rust" data-noescape>
<fragment animate="by-line with-ancestry balanced separate-comments">
// Integrity
use hdi::prelude::*;
#[hdk_entry_helper]
struct Comment {
comment: String
}
#[hdk_entry_helper]
// We only need a string
struct Anchor(String);
#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
enum EntryTypes {
Comment(Comment),
Anchor(Anchor),
}
#[hdk_link_types]
enum LinkTypes {
AnchorToComment,
}
</fragment>
</code></pre>
<pre
class="fragment fade-in"
style="margin-left: 16px"
><code class="rust" data-noescape>
<fragment animate="by-line with-ancestry balanced separate-comments">
// Coordinator
use hdk::prelude::*;
#[hdk_extern]
fn create_comment(comment: Comment) -> ExternResult<ActionHash> {
let comment_action_hash = create_entry(EntryTypes::Comment(comment))?;
let anchor = Anchor(String::from("all_comments"));
// Create the anchor entry
let _anchor_action_hash<span class="fragment fade-in-then-out popover">This hash is not deterministic: it will be different every time this function is executed</span> = create_entry(EntryTypes::Anchor(anchor))?;
let anchor_entry_hash <span class="fragment fade-in-then-out popover">Hash is deterministic: will be the same independently of which cells executes this function</span>= hash_entry(anchor)?; // This hash is deterministic
create_link(
anchor_entry_hash, comment_action_hash, LinkTypes::AnchorToComment, ()
)?;
Ok(comment_action_hash)
}
#[hdk_extern]
fn get_all_comments_hashes(_: ()) -> ExternResult<Vec<ActionHash>> {
let anchor = Anchor(String::from("all_comments")); // Must use the same string as the create function
let anchor_hash = hash_entry(anchor)?;
let links = get_links(anchor_hash, LinkTypes::AnchorToComment, None)?; // Get all the links created above
let action_hashes: Vec<ActionHash> = links.into_iter()
.map(|link| ActionHash::from(link.target))
.collect(); // Extract the action hash from the links
Ok(action_hashes)
}
</fragment>
</code></pre>
</div>
</section>
<section language="markdown" animate="by-line with-ancestry">
### Paths (theory)
- Anchors are great, but they can introduce problems
- If a large number of links are attached to the same base, they become a burden on the agents responsible for those links
- This is know as a "DHT Hotspot"
- It's impractice to sort or filter an oversized result set
- For example, a query of the form "get_comment_by_date(start_date: Timestamp, end_date: Timestamp)"
- Paths solve these issues
- Paths are built-in to the HDK
- Paths are anchor trees
- Top level anchor acts as the root node
- This root node links to its children, which are also anchors
- This tree can be dynamically constructed and can have arbitrary depth
- Paths are composed of a vector of components
- Each component is an arbitrary byte array
- Normally constructed from a string
- Demo: https://holochain-gym.github.io/developers/intermediate/paths/#try-it
</section>
<section>
<h3>Paths (code example)</h3>
<fragment animate="by-line with-ancestry balanced separate-comments">
<pre
class="fragment fade-in"
><code class="rust" data-noescape>
use hdk::prelude::*;
use integrity_zome::{EntryTypes, Comment, LinkTypes}; // Import the types defined in our integrity zome
use chrono::*; // Import the chrono crate for time related utilites
#[hdk_extern]
fn create_comment(comment: Comment) -> ExternResult<ActionHash> {
let comment_action_hash = create_entry(EntryTypes::Comment(comment))?; // Create comment as always
let (secs, nsecs) = sys_time()?.as_seconds_and_nanos(); // Get the current timestamp in microseconds
let dt: DateTime<Utc> = DateTime::from_utc(NaiveDateTime::from_timestamp(secs, nsecs), Utc);
let path = Path::from(format!("all_comments.{}.{}"<span class="fragment fade-in-then-out popover">The '.' is the separator between components</span>, dt.year(), dt.month())); // Builds the path "all_comments.2022.7"
let typed_path = path.typed(LinkTypes::PathTimeIndex)?;<span class="fragment fade-in-then-out popover">"TypedPath" is a path with all the links of a certain †ype</span> // Add the link type to the path
typed_path.ensure()?;<span class="fragment fade-in-then-out popover">Creates the path tree structure, and all the necessary links</span> // Build the path's anchor tree
create_link(
typed_path.path_entry_hash()?, <span class="fragment fade-in-then-out popover">Entry hash of the path</span>
comment_action_hash,
LinkTypes::PathToComment,
()
)?;
Ok(comment_action_hash)
}
</code></pre></fragment></section>
<section>
<fragment animate="by-line with-ancestry balanced separate-comments">
<h3>Paths (roll-ups)</h3>
<pre
class="fragment fade-in"
><code class="rust" data-noescape>
#[hdk_extern]
fn get_comments_by_year(year: u32) -> ExternResult<Vec<ActionHash>> {
let year_path = Path::from(format!("all_comments.{}", year))<span class="fragment fade-in-then-out popover">Build the anchor in the intermediate node</span>.typed(LinkTypes::PathTimeIndex);
let month_paths: Vec<Path> = year_path.children_paths()?;<span class="fragment fade-in-then-out popover">Retrieves all the children node month paths</span>
let mut all_links: Vec<Link> = vec![];
for path in month_paths {
let last_component: Component = path.leaf()<span class="fragment fade-in-then-out popover">Returns an `Option<Component>` with the last component of this path</span> // `leaf()` gets the latest component
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from("The path is empty"))))?.clone();
let month = String::try_from(last_component).map_err(|err| wasm_error!(err))?;<span class="fragment fade-in-then-out popover">Converts the component's byte array to a string</span> // Converts the component to a string
let mut links = get_links(path.path_entry_hash()?, LinkTypes::PathToComment, None)?; // Get all the links created above
all_links.append(&mut links); // Collect the links
}
let all_links: Vec<ActionHash> = links.into_iter()
.map(|link| ActionHash::from(link.target))
.collect(); // Extract the action hash from the links
Ok(action_hashes)
}
</code></pre>
</fragment>
</section>
<section>
<h1>That's it!</h1>
</section>
</div>
</div>
<script type="module">
import Reveal from "reveal.js";
import Markdown from "reveal.js/plugin/markdown/markdown.esm.js";
import RevealHighlight from "reveal.js/plugin/highlight/highlight.esm.js";
import RevealNotes from "reveal.js/plugin/notes/notes.esm.js";
import RevealAnimateFragments from "reveal.js-animate-fragments";
import RevealEliminateEmptyLines from "reveal.js-eliminate-empty-lines";
let deck = new Reveal({
transition: "none",
plugins: [
Markdown,
RevealHighlight,
RevealNotes,
RevealAnimateFragments,
RevealEliminateEmptyLines
],
});
deck.initialize();
</script>
</body>
</html>