-
Notifications
You must be signed in to change notification settings - Fork 2
/
lookup.go
61 lines (43 loc) · 1.27 KB
/
lookup.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
54
55
56
57
58
59
60
61
// Copyright 2016 David Lavieri. All rights reserved.
// Use of this source code is governed by a MIT License
// License that can be found in the LICENSE file.
package goradix
// ----------------------- Look Up ------------------------ //
// LookUp will return the node matching
func (r *Radix) LookUp(s string) (interface{}, error) {
return r.LookUpBytes([]byte(s))
}
// LookUpBytes will return the node matching
func (r *Radix) LookUpBytes(bs []byte) (interface{}, error) {
node, key, err := r.sLookUp(bs)
if err != nil {
return nil, err
}
if !key {
return nil, ErrNoMatchFound
}
return node.get(), err
}
func (r *Radix) sLookUp(bs []byte) (*Radix, bool, error) {
var traverseNode = r
traverseNode.rLock()
lbs, matches, _ := traverseNode.match(bs)
// && ((!r.master && matches > 0) || r.master)
if matches == len(traverseNode.Path) {
if matches < len(bs) {
for _, n := range traverseNode.nodes {
if tn, nkey, err := n.sLookUp(lbs); tn != nil {
traverseNode.rUnlock()
return tn, nkey, err
}
}
traverseNode.rUnlock()
// Do not jump back to parent node
return nil, false, ErrNoMatchFound
}
traverseNode.rUnlock()
return traverseNode, traverseNode.key, nil
}
traverseNode.rUnlock()
return nil, false, ErrNoMatchFound
}