You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/search/tree.go

240 lines
4.2 KiB
Go

4 years ago
package search
import (
"errors"
"fmt"
)
4 years ago
const (
colon = ':'
slash = '/'
)
var (
// errDupItem means adding duplicated item.
errDupItem = errors.New("duplicated item")
// errDupSlash means item is started with more than one slash.
errDupSlash = errors.New("duplicated slash")
// errEmptyItem means adding empty item.
errEmptyItem = errors.New("empty item")
// errInvalidState means search tree is in an invalid state.
errInvalidState = errors.New("search tree is in an invalid state")
// errNotFromRoot means path is not starting with slash.
errNotFromRoot = errors.New("path should start with /")
4 years ago
// NotFound is used to hold the not found result.
4 years ago
NotFound Result
)
type (
innerResult struct {
key string
value string
named bool
found bool
}
node struct {
item any
4 years ago
children [2]map[string]*node
}
// A Tree is a search tree.
4 years ago
Tree struct {
root *node
}
// A Result is a search result from tree.
4 years ago
Result struct {
Item any
4 years ago
Params map[string]string
}
)
// NewTree returns a Tree.
4 years ago
func NewTree() *Tree {
return &Tree{
root: newNode(nil),
}
}
// Add adds item to associate with route.
func (t *Tree) Add(route string, item any) error {
4 years ago
if len(route) == 0 || route[0] != slash {
return errNotFromRoot
4 years ago
}
if item == nil {
return errEmptyItem
4 years ago
}
err := add(t.root, route[1:], item)
switch err {
case errDupItem:
return duplicatedItem(route)
case errDupSlash:
return duplicatedSlash(route)
default:
return err
}
4 years ago
}
// Search searches item that associates with given route.
4 years ago
func (t *Tree) Search(route string) (Result, bool) {
if len(route) == 0 || route[0] != slash {
return NotFound, false
}
var result Result
ok := t.next(t.root, route[1:], &result)
return result, ok
}
func (t *Tree) next(n *node, route string, result *Result) bool {
if len(route) == 0 && n.item != nil {
result.Item = n.item
return true
}
for i := range route {
if route[i] != slash {
continue
4 years ago
}
token := route[:i]
return n.forEach(func(k string, v *node) bool {
r := match(k, token)
if !r.found || !t.next(v, route[i+1:], result) {
return false
}
if r.named {
addParam(result, r.key, r.value)
}
return true
})
4 years ago
}
return n.forEach(func(k string, v *node) bool {
if r := match(k, route); r.found && v.item != nil {
result.Item = v.item
if r.named {
addParam(result, r.key, r.value)
}
4 years ago
return true
}
return false
})
}
func (nd *node) forEach(fn func(string, *node) bool) bool {
for _, children := range nd.children {
for k, v := range children {
if fn(k, v) {
4 years ago
return true
}
}
}
return false
}
func (nd *node) getChildren(route string) map[string]*node {
if len(route) > 0 && route[0] == colon {
return nd.children[1]
}
return nd.children[0]
4 years ago
}
func add(nd *node, route string, item any) error {
4 years ago
if len(route) == 0 {
if nd.item != nil {
return errDupItem
4 years ago
}
nd.item = item
return nil
}
if route[0] == slash {
return errDupSlash
4 years ago
}
for i := range route {
if route[i] != slash {
continue
}
token := route[:i]
children := nd.getChildren(token)
if child, ok := children[token]; ok {
if child != nil {
return add(child, route[i+1:], item)
4 years ago
}
return errInvalidState
4 years ago
}
child := newNode(nil)
children[token] = child
return add(child, route[i+1:], item)
4 years ago
}
children := nd.getChildren(route)
if child, ok := children[route]; ok {
if child.item != nil {
return errDupItem
4 years ago
}
child.item = item
} else {
children[route] = newNode(item)
}
return nil
}
func addParam(result *Result, k, v string) {
if result.Params == nil {
result.Params = make(map[string]string)
}
result.Params[k] = v
}
func duplicatedItem(item string) error {
return fmt.Errorf("duplicated item for %s", item)
}
func duplicatedSlash(item string) error {
return fmt.Errorf("duplicated slash for %s", item)
}
4 years ago
func match(pat, token string) innerResult {
if pat[0] == colon {
return innerResult{
key: pat[1:],
value: token,
named: true,
found: true,
}
}
return innerResult{
found: pat == token,
}
}
func newNode(item any) *node {
4 years ago
return &node{
item: item,
children: [2]map[string]*node{
make(map[string]*node),
make(map[string]*node),
},
}
}