-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
53 lines (45 loc) · 1.09 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package domiannameindex
import "sync"
const (
rootDomainName = "."
defaultPrefix = "├──> "
)
// Tree is a tree of domain names.
type Tree struct {
root *node
mutex sync.RWMutex
}
// New creates a new tree.
func New() Interface {
return &Tree{root: &node{children: make(map[string]*node), value: rootDomainName}}
}
// Insert inserts a domain name into the tree.
func (t *Tree) Insert(domainName ...string) {
t.mutex.Lock()
defer t.mutex.Unlock()
for _, name := range domainName {
t.root.insert(name)
}
}
// Find finds a domain name in the tree and returns fullPath with respect of wildcard.
func (t *Tree) Find(domainName string) (ok bool, fullPath string) {
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.root.find(domainName)
}
// Remove removes a domain name from the tree.
func (t *Tree) Remove(domainName string) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.root.remove(domainName)
}
// Print prints the tree.
func (t *Tree) Print(prefix string) {
t.mutex.RLock()
defer t.mutex.RUnlock()
if len(prefix) == 0 {
t.root.print(defaultPrefix)
return
}
t.root.print(prefix)
}