-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync_trie.go
96 lines (73 loc) · 2.15 KB
/
sync_trie.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package trie
import (
"github.com/golang-infrastructure/go-tuple"
"sync"
)
type SyncTrie[T any] struct {
lock sync.RWMutex
trie *Trie[T]
}
func NewSync[T any](pathSplitFunc ...PathSplitFunc) *SyncTrie[T] {
return &SyncTrie[T]{
lock: sync.RWMutex{},
trie: New[T](pathSplitFunc...),
}
}
// Add 仅当不存在时插入到树上,已经存在的话则忽略
func (x *SyncTrie[T]) Add(path string, value T) error {
x.lock.Lock()
defer x.lock.Unlock()
return x.trie.Add(path, value)
}
// Upsert 如果树上已经存在则更新,不存在则插入
func (x *SyncTrie[T]) Upsert(path string, value T) error {
x.lock.Lock()
defer x.lock.Unlock()
return x.trie.Upsert(path, value)
}
// Remove 从树上移除给定路径
func (x *SyncTrie[T]) Remove(path string) error {
x.lock.Lock()
defer x.lock.Unlock()
return x.trie.Remove(path)
}
// Query 查询路径的值
func (x *SyncTrie[T]) Query(path string) (value T, err error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.trie.Query(path)
}
// QueryOrDefault 查询给定的路径的负载,如果不存在的话则返回默认值
func (x *SyncTrie[T]) QueryOrDefault(path string, defaultValue T) (value T, err error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.trie.QueryOrDefault(path, defaultValue)
}
// FindTrieNode 寻找路径绑定的节点
func (x *SyncTrie[T]) FindTrieNode(path string) ([]string, *TrieNode[T], error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.trie.FindTrieNode(path)
}
// ToSlice 把字典树转为字典列表
func (x *SyncTrie[T]) ToSlice(delimiter ...string) []*tuple.Tuple2[string, T] {
x.lock.RLock()
defer x.lock.RUnlock()
return x.trie.ToSlice(delimiter...)
}
// QueryByPrefix 根据前缀查询
func (x *SyncTrie[T]) QueryByPrefix(prefix string, delimiter ...string) []*tuple.Tuple2[string, T] {
x.lock.RLock()
defer x.lock.RUnlock()
return x.trie.QueryByPrefix(prefix, delimiter...)
}
func (x *SyncTrie[T]) Contains(path string) (bool, error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.trie.Contains(path)
}
func (x *SyncTrie[T]) ExportToDotLanguage() string {
x.lock.RLock()
defer x.lock.RUnlock()
return x.trie.ExportToDotLanguage()
}