mpath

package module
v0.0.0-...-1dd64a8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2020 License: MIT Imports: 1 Imported by: 0

README

mpath-go

Go Report Card CircleCI codecov

Golang realisation of MPTT (or modified preorder tree traversal) in materialized path way.

About

It provides interfaces which yor database object should implement.

Your database object should store:

  • path property as slice of uint64 IDs of materialized path to this object in traversal tree;
  • position property as integer for determine the order of leafs in tree

Usage

Implementation example and tests are in test file.

package main

import (
    "fmt"

    "github.com/spacetab-io/mpath"
)

type TestItems []*TestItem

type TestItem struct {
    ID       uint64
    Path     []uint64
    Position int
    Siblings TestItems
    Name     string
}

// Leaf interface implementation for TestItem
// ...
// Leafs interface implementation for TestItems
// ...

func main() {
    flatItemsSlice := getTestItems()

    var parent = TestItem{}
    if err := mpath.InitTree(&parent, flatItemsSlice); err != nil {
        panic("error tree init")
    }
    
    fmt.Print(parent)
}

func getTestItems() *TestItems {
    return &TestItems{
        {ID: 1, Position: 0, Name: "item 1", Path: []uint64{1}},
        {ID: 2, Position: 0, Name: "item 2", Path: []uint64{1, 2}},
        {ID: 3, Position: 1, Name: "item 3", Path: []uint64{1, 3}},
        {ID: 4, Position: 0, Name: "item 4", Path: []uint64{1, 2, 4}},
        {ID: 5, Position: 1, Name: "item 5", Path: []uint64{1, 2, 5}},
        {ID: 6, Position: 0, Name: "item 6", Path: []uint64{1, 3, 6}},
    }
}

Tests

go test ./... -v -race

Documentation

Overview

Package mpath is golang realisation of MPTT (modified preorder tree traversal) in materialized path way. It includes interfaces with methods that objects should implement to make an ordered tree from a flat slice of them.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitTree

func InitTree(tree Leaf, leafs Leafs) error

InitTree creates Leaf tree (index) from flat Leafs slice

Example
itemsFull = &TestItems{
	{ID: 1, Position: 0, Name: "item 1", Path: []uint64{1}},
	{ID: 2, Position: 0, Name: "item 2", Path: []uint64{1, 2}},
	{ID: 3, Position: 1, Name: "item 3", Path: []uint64{1, 3}},
	{ID: 4, Position: 0, Name: "item 4", Path: []uint64{1, 2, 4}},
	{ID: 5, Position: 1, Name: "item 5", Path: []uint64{1, 2, 5}},
	{ID: 6, Position: 0, Name: "item 6", Path: []uint64{1, 3, 6}},
}

var parent = TestItem{}
if err := InitTree(&parent, itemsFull); err != nil {
	log.Fatalln(err)
}

fmt.Println("Tree view:")
showResult(parent, "")
Output:

Tree view:
 id: 1, name: item 1, position: 0
 siblings:
  id: 2, name: item 2, position: 0
  siblings:
   id: 4, name: item 4, position: 0
   siblings:
   id: 5, name: item 5, position: 1
   siblings:
  id: 3, name: item 3, position: 1
  siblings:
   id: 6, name: item 6, position: 0
   siblings:

Types

type Leaf

type Leaf interface {

	//GetID returns an uint64 ID of Leaf object
	GetID() uint64

	//SetID sets ID to an empty Leaf object
	SetID(id uint64)

	//GetPosition returns position of Leafs branch
	GetPosition() int

	//SetPosition sets position of Leaf in its branch
	SetPosition(pos int)

	//GetPath returns Leafs path as Leafs IDs slice
	GetPath() []uint64

	//GetPathFromIdx returns Leaf path chunk started from passed index of element in path slice
	GetPathFromIdx(index *int) []uint64

	//GetLeafByID returns Leaf from Leafs by its ID
	GetLeafByID(leafs Leafs, id uint64) Leaf

	//GetLeafOrMakeNew returns Leaf from Leafs by its id or makes new Leaf object with ID and Position property
	GetLeafOrMakeNew(leafs Leafs, id uint64, position int) Leaf

	//GetSiblings return Leaf siblings as Leafs
	GetSiblings() Leafs

	//AppendSiblings append Leafs siblings to current Leaf
	AppendSiblings(interface{})

	//MakeRoot creates root Leaf to a Leafs tree and return path index of this root Leaf in Leafs path
	MakeRoot(leafs Leafs, leaf Leaf) (pathIndex *int)

	//GetRootPathIndex returns index of root Leaf id in Leaf path slice
	GetRootPathIndex() *int
}

Leaf interface for making tree from flat Leafs slice

type Leafs

type Leafs interface {

	//GetLeafs return Leafs slice
	GetLeafs() []Leaf
}

Leafs is Leaf collection interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL