florinpatrascu / closure_table

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

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

License:Apache License 2.0


Languages

Language:Elixir 100.0%