• Stars
    star
    28
  • Rank 852,115 (Top 18 %)
  • Language
    Elixir
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Closure Table for Elixir - a simple solution for storing and manipulating complex hierarchies.

Closure Table

Hex.pm Hexdocs.pm

this library provides two adapters: an in-memory one, and one for using the closure-table solution with Ecto; for your testing and development convenience.

The Closure Table solution is a simple and elegant way of storing hierarchies. It involves storing all paths through a tree, not just those with a direct parent-child relationship. You may want to chose this model, over the Nested Sets model, should you need referential integrity and to assign nodes to multiple trees.

Throughout the various examples and tests, we will refer to the hierarchies depicted below, where we're modeling a hypothetical forum-like discussion between Rolie, Olie and Polie, and their debate around the usefulness of this implementation :)

Closure Table

Quick start

To start, you can simply use one Adapter from the ones provided, same way you'd use the Ecto's own Repo:

defmodule CTT do
  use CTE,
    otp_app: :cte,
    adapter: CTE.Adapter.Memory,
    nodes: %{
      1 => %{id: 1, author: "Olie", comment: "Is Closure Table better than the Nested Sets?"},
      2 => %{id: 2, author: "Rolie", comment: "It depends. Do you need referential integrity?"},
      3 => %{id: 3, author: "Olie", comment: "Yeah."}
    },
    paths: [[1, 1, 0], [1, 2, 1], [1, 3, 2], [2, 2, 0], [2, 3, 1], [3, 3, 0]]
end

With the configuration above, the :nodes attribute is a map containing the comments our interlocutors made; these are "nodes", in CTE's parlance. When using the CTE.Adapter.Ecto implementation, the :nodes attribute will be a Schema (or a table name! In our initial implementation, the nodes definitions must have at least the :id, as one of their properties. This caveat will be lifted in a later implementation. The :paths attribute represents the parent-child relationship between the comments.

Add the CTM module to your main supervision tree:

defmodule CTM.Application do
  @moduledoc false

  use Application

  def start(_type, _args) do
    opts = [strategy: :one_for_one, name: CTM.Supervisor]

    Supervisor.start_link([CTM], opts)
  end
end

And then using iex -S mix, for quickly experimenting with the CTE API, let's find the descendants of comment #1:

iex» CTM.descendants(1)
{:ok, [3, 2]}
iex> {:ok, tree} = CTT.tree(1)
...
iex> CTE.Utils.print_tree(tree, 1)
...
iex» CTE.Utils.print_tree(tree,1, callback: &({&2[&1].author <> ":", &2[&1].comment}))

Is Closure Table better than the Nested Sets?
└── It depends. Do you need referential integrity?
   └── Yeah.

Please check the docs for more details and return from more updates!

Oh and there is a simple utility for helping you drawing your paths, using graphviz! From Rolie's comments, excerpt:

dot

Maybe useful?! If yes, then we'll let you find this function by yourself ;)

hint: check the tests <3

Installation

If available in Hex, the package can be installed by adding closure_table to your list of dependencies in mix.exs:

def deps do
  [
    {:closure_table, "~> 1.1"}
  ]
end

Contributing

  • Fork this project
  • Create your feature branch (git checkout -b my-new-feature)
  • Setup database and test (mix test)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

License

Copyright 2023 Florin T.PATRASCU & the Contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

bolt_sips

Neo4j driver for Elixir
Elixir
256
star
2

micro

a modular micro MVC framework for Java web applications
Java
86
star
3

neo4j_sips

Elixir driver for the Neo4j graph database server
Elixir
81
star
4

movies_elixir_phoenix

Neo4j with Elixir, Phoenix and Neo4j.Sips - The Movies Example Application
Elixir
37
star
5

bolt_movies_elixir_phoenix

A very simple web application using Neo4j with Elixir, Phoenix, and: Bolt.Sips; an Elixir driver for Neo4j’s Bolt newest network protocol.
Elixir
20
star
6

vscode-elixir-snippets

Elixir code snippets for VS Code
18
star
7

elixir_grafana_loki_tempo

A very simple Elixir demo/project used for experimenting with Grafana Tempo and Loki #opentelemetry #observability #tracing
Elixir
12
star
8

libcluster_gig

A libcluster strategy for Google Managed Instance Groups
Elixir
10
star
9

jrack

a port of Rack to Java
Java
8
star
10

jpublish

JPublish provides a powerful system for managing your web site's content as well as your web site's application logic.
Java
7
star
11

neo4j_sips_models

Neo4j models, for Neo4j.Sips
Elixir
6
star
12

micro-examples

Examples of web applications or deployment solutions using the Micro MVC framework
Java
5
star
13

fixex

support for integrating ExUnit with external apps/cli, as external fixtures providers, or for mimicking external services
Elixir
4
star
14

Sinatra-HART

A Sinatra Haml, Activerecord, RSPEC and Twitter bootstrap boilerplate, with SimpleCov flavour :)
JavaScript
3
star
15

pomo-chrono

A simple yet versatile stateful jQuery Pomodori countdown timer plugin
JavaScript
2
star
16

silw

a simple ruby gem utility that allows one user to monitor several remote systems, provided he has the proper credentials and the authorization to execute remote commands
Ruby
2
star
17

bpl

A very simple gem to help you track your blood pressure
Ruby
1
star
18

mongo_fe

A simple Sinatra based web front-end that can be used for experimenting and learning MongoDB. It is also a Ruby gem :)
JavaScript
1
star
19

dnsimple_touch

a very simple DNSimple - jQTouch prototype
JavaScript
1
star
20

micro-docs

This is the Documentation web site for the Micro framework; using Micro for publishing.
CSS
1
star
21

google_playground

A repository containing small scripts that I use to manage some GCP infrastructure chores, notes, etc. Mostly Elixir of course =)
Elixir
1
star