项目作者: spacetab-io

项目描述 :
Golang package for MPTT (Modified Preorder Tree Traversal) - materialized path realisation.
高级语言: Go
项目地址: git://github.com/spacetab-io/mpath-go.git
创建时间: 2020-01-09T15:04:45Z
项目社区:https://github.com/spacetab-io/mpath-go

开源协议:MIT License

下载


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.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spacetab-io/mpath"
  5. )
  6. type TestItems []*TestItem
  7. type TestItem struct {
  8. ID uint64
  9. Path []uint64
  10. Position int
  11. Siblings TestItems
  12. Name string
  13. }
  14. // Leaf interface implementation for TestItem
  15. // ...
  16. // Leafs interface implementation for TestItems
  17. // ...
  18. func main() {
  19. flatItemsSlice := getTestItems()
  20. var parent = TestItem{}
  21. if err := mpath.InitTree(&parent, flatItemsSlice); err != nil {
  22. panic("error tree init")
  23. }
  24. fmt.Print(parent)
  25. }
  26. func getTestItems() *TestItems {
  27. return &TestItems{
  28. {ID: 1, Position: 0, Name: "item 1", Path: []uint64{1}},
  29. {ID: 2, Position: 0, Name: "item 2", Path: []uint64{1, 2}},
  30. {ID: 3, Position: 1, Name: "item 3", Path: []uint64{1, 3}},
  31. {ID: 4, Position: 0, Name: "item 4", Path: []uint64{1, 2, 4}},
  32. {ID: 5, Position: 1, Name: "item 5", Path: []uint64{1, 2, 5}},
  33. {ID: 6, Position: 0, Name: "item 6", Path: []uint64{1, 3, 6}},
  34. }
  35. }

Tests

  1. go test ./... -v -race