A gym tool Holochain Gym Concepts Developers Core Concepts Toggle darkmode Glossary of Terms Give us Feedback!

Intermediate: Paths

Paths are the replacement of anchors in RSM. They fill the same role but add a lot more flexibility and dimensionality, and allow you to create complex indexes to query faster the DHT very easily.

The content of each path is a string with segments separated by a dot, for example: all_tasks.project1.finished. This path will create these entries:

  • all_tasks
  • all_tasks.project1
  • all_tasks.project1.finished

Here, you can see that the root parent of the path is all_tasks, which has all_tasks.project1 as child. Each of these entries has a hash in the DHT like any other entry. Also, every parent will have a link pointing to all its children.

Try it!

Here you can create paths yourself, and see which entries and links are created.

The basic mechanism for which these entries are useful is to attach links to them. If you attach a link to the all_tasks.project1.finished that points to all tasks related with project1 that have finished, now you can do a get_links on that path to get only those.

If, on the contrary, you want to get all tasks within the project regardless of status, you can get all the children paths from all_tasks.project1, which will give you for example all_tasks.project1.todo, all_tasks.project1.doing and all_tasks.project1.finished, and then do a get_links to tasks on those.

You can imagine different types of indexes built on top of paths, with multidimensional properties.

Keep in mind that paths are already incorporated in the core hdk, so you don't need to import them from an external library. Although it is necessary to define them as an entry definition in your zome like this:
entry_defs![
    Path::entry_def(),
    ...
];

Exercise

Problem statement

We need to code a small zome that satisfies these capabilities:

  • Create a new post, passing a content and some tags
  • Get all posts within a day or an hour, examples:
    • "get me all posts posted on 21st February, 2021"
    • "get me all posts posted between 21:00 and 22:00 of 21st February, 2021"
  • Get all the tags that have been created
  • Get all posts that have been created with a certain tag
    • "get me all posts that have been posted with the tag "nature""

You can follow this entry design to accomplish it:

  1. Go to the developer-exercises.
  2. Enter the nix-shell: nix-shell
    you should run this in the folder containing the default.nix file
    starting the nix-shell for the very first time might take a long time, somewhere between 20 to 80 minutes, after that I will take just a few seconds
  3. Go to folder with the exercise intermediate/1.paths
  4. Inside zome/exercise/src/lib.rs
    • Implement all unimplemented!() functions
  5. Compile your code: ./run_build.sh.
  6. Run the test: ./run_tests.sh
  7. Don't stop until the test runs green

Relevant HDK documentation:

Solution

If you get stuck implementing this exercise, you can always look at its solution.