https://github.com/spacetab-io/mpath-go
Golang package for MPTT (Modified Preorder Tree Traversal) - materialized path realisation.
https://github.com/spacetab-io/mpath-go
database go golang materialized-path mpath mptt tree tree-structure
Last synced: 11 months ago
JSON representation
Golang package for MPTT (Modified Preorder Tree Traversal) - materialized path realisation.
- Host: GitHub
- URL: https://github.com/spacetab-io/mpath-go
- Owner: spacetab-io
- License: mit
- Created: 2020-01-09T15:04:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-13T06:49:07.000Z (over 6 years ago)
- Last Synced: 2025-03-20T21:19:26.088Z (about 1 year ago)
- Topics: database, go, golang, materialized-path, mpath, mptt, tree, tree-structure
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 13
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-Char - mpath - MPTT (Modified Preorder Tree Traversal) package for SQL records - materialized path realisation. (Database / Advanced Console UIs)
- awesome-go-cn - mpath - 实物化路径的实现 (数据库 / SQL 查询语句构建库)
- awesome-go-info - mpath-go - materialized path realisation. | (Uncategorized)
- go-awesome-with-star-updatetime - mpath - MPTT (Modified Preorder Tree Traversal) package for SQL records - materialized path realisation. (Database / Advanced Console UIs)
README
# mpath-go
[](https://goreportcard.com/report/github.com/spacetab-io/mpath-go)
[](https://circleci.com/gh/spacetab-io/mpath-go)
[](https://codecov.io/gh/spacetab-io/mpath-go)
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](/mpath_test.go).
```go
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