2020-01-25 17:43:02 +00:00
|
|
|
package resolver
|
2015-02-03 14:53:57 +00:00
|
|
|
|
|
|
|
type suffixTreeNode struct {
|
|
|
|
key string
|
2021-04-15 05:04:58 +00:00
|
|
|
value *Nameserver
|
2015-02-03 14:53:57 +00:00
|
|
|
children map[string]*suffixTreeNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSuffixTreeRoot() *suffixTreeNode {
|
2021-04-15 05:04:58 +00:00
|
|
|
return newSuffixTree("", nil)
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 05:04:58 +00:00
|
|
|
func newSuffixTree(key string, value *Nameserver) *suffixTreeNode {
|
2015-02-03 14:53:57 +00:00
|
|
|
root := &suffixTreeNode{
|
|
|
|
key: key,
|
|
|
|
value: value,
|
|
|
|
children: map[string]*suffixTreeNode{},
|
|
|
|
}
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *suffixTreeNode) ensureSubTree(key string) {
|
|
|
|
if _, ok := node.children[key]; !ok {
|
2021-04-15 05:04:58 +00:00
|
|
|
node.children[key] = newSuffixTree(key, nil)
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 05:04:58 +00:00
|
|
|
func (node *suffixTreeNode) insert(key string, value *Nameserver) {
|
2015-02-03 14:53:57 +00:00
|
|
|
if c, ok := node.children[key]; ok {
|
|
|
|
c.value = value
|
|
|
|
} else {
|
|
|
|
node.children[key] = newSuffixTree(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 05:04:58 +00:00
|
|
|
func (node *suffixTreeNode) sinsert(keys []string, value *Nameserver) {
|
2015-02-03 14:53:57 +00:00
|
|
|
if len(keys) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
key := keys[len(keys)-1]
|
|
|
|
if len(keys) > 1 {
|
|
|
|
node.ensureSubTree(key)
|
|
|
|
node.children[key].sinsert(keys[:len(keys)-1], value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node.insert(key, value)
|
|
|
|
}
|
|
|
|
|
2021-04-15 05:04:58 +00:00
|
|
|
func (node *suffixTreeNode) search(keys []string) (*Nameserver, bool) {
|
2015-02-03 14:53:57 +00:00
|
|
|
if len(keys) == 0 {
|
2021-04-15 05:04:58 +00:00
|
|
|
return nil, false
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key := keys[len(keys)-1]
|
|
|
|
if n, ok := node.children[key]; ok {
|
|
|
|
if nextValue, found := n.search(keys[:len(keys)-1]); found {
|
|
|
|
return nextValue, found
|
|
|
|
}
|
2021-04-15 05:04:58 +00:00
|
|
|
return n.value, n.value != nil
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 05:04:58 +00:00
|
|
|
return nil, false
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|