-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.go
233 lines (181 loc) · 4.12 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package mandodb
import (
"strings"
"sync"
"github.com/RoaringBitmap/roaring"
)
// Memory Index 负责管理内存索引的存储和搜索
type memorySidSet struct {
set map[string]struct{}
mut sync.Mutex
}
func newMemorySidSet() *memorySidSet {
return &memorySidSet{set: make(map[string]struct{})}
}
func (mss *memorySidSet) Add(a string) {
mss.mut.Lock()
defer mss.mut.Unlock()
mss.set[a] = struct{}{}
}
func (mss *memorySidSet) Size() int {
mss.mut.Lock()
defer mss.mut.Unlock()
return len(mss.set)
}
func (mss *memorySidSet) Copy() *memorySidSet {
mss.mut.Lock()
defer mss.mut.Unlock()
newset := newMemorySidSet()
for k := range mss.set {
newset.set[k] = struct{}{}
}
return newset
}
func (mss *memorySidSet) Intersection(other *memorySidSet) {
mss.mut.Lock()
defer mss.mut.Unlock()
for k := range mss.set {
_, ok := other.set[k]
if !ok {
delete(mss.set, k)
}
}
}
func (mss *memorySidSet) Union(other *memorySidSet) {
mss.mut.Lock()
defer mss.mut.Unlock()
for k := range other.set {
mss.set[k] = struct{}{}
}
}
func (mss *memorySidSet) List() []string {
mss.mut.Lock()
defer mss.mut.Unlock()
keys := make([]string, 0, len(mss.set))
for k := range mss.set {
keys = append(keys, k)
}
return keys
}
type memoryIndexMap struct {
idx map[string]*memorySidSet
mut sync.Mutex
}
func newMemoryIndexMap() *memoryIndexMap {
return &memoryIndexMap{idx: make(map[string]*memorySidSet)}
}
func (mim *memoryIndexMap) Range(f func(k string, v *memorySidSet)) {
mim.mut.Lock()
defer mim.mut.Unlock()
for k, sids := range mim.idx {
f(k, sids)
}
}
func (mim *memoryIndexMap) UpdateIndex(sid string, labels LabelSet) {
mim.mut.Lock()
defer mim.mut.Unlock()
for _, label := range labels {
key := label.MarshalName()
if _, ok := mim.idx[key]; !ok {
mim.idx[key] = newMemorySidSet()
}
mim.idx[key].Add(sid)
}
}
func (mim *memoryIndexMap) MatchSids(lvs *labelValueSet, lms LabelMatcherSet) []string {
mim.mut.Lock()
defer mim.mut.Unlock()
sids := newMemorySidSet()
var got bool
for i := len(lms) - 1; i >= 0; i-- {
tmp := newMemorySidSet()
vs := lvs.Match(lms[i])
for _, v := range vs {
midx := mim.idx[joinSeparator(lms[i].Name, v)]
if midx == nil || midx.Size() <= 0 {
continue
}
tmp.Union(midx.Copy())
}
if tmp == nil || tmp.Size() <= 0 {
return nil
}
if !got {
sids = tmp
got = true
continue
}
sids.Intersection(tmp.Copy())
}
return sids.List()
}
// Disk Index 负责管理磁盘的索引存储和搜索
type diskSidSet struct {
set *roaring.Bitmap
mut sync.Mutex
}
func newDiskSidSet() *diskSidSet {
return &diskSidSet{set: roaring.New()}
}
func (dss *diskSidSet) Add(a uint32) {
dss.mut.Lock()
defer dss.mut.Unlock()
dss.set.Add(a)
}
type diskIndexMap struct {
label2sids map[string]*diskSidSet
labelOrdered map[int]string
mut sync.Mutex
}
func newDiskIndexMap(swl []seriesWithLabel) *diskIndexMap {
dim := &diskIndexMap{
label2sids: make(map[string]*diskSidSet),
labelOrdered: make(map[int]string),
}
for i := range swl {
row := swl[i]
dim.label2sids[row.Name] = newDiskSidSet()
for _, sid := range swl[i].Sids {
dim.label2sids[row.Name].Add(sid)
}
dim.labelOrdered[i] = row.Name
}
return dim
}
func (dim *diskIndexMap) MatchLabels(lids ...uint32) []Label {
ret := make([]Label, 0, len(lids))
for _, lid := range lids {
labelPair := dim.labelOrdered[int(lid)]
kv := strings.SplitN(labelPair, separator, 2)
if len(kv) != 2 {
continue
}
ret = append(ret, Label{
Name: kv[0],
Value: kv[1],
})
}
return ret
}
func (dim *diskIndexMap) MatchSids(lvs *labelValueSet, lms LabelMatcherSet) []uint32 {
dim.mut.Lock()
defer dim.mut.Unlock()
lst := make([]*roaring.Bitmap, 0)
for i := len(lms) - 1; i >= 0; i-- {
tmp := make([]*roaring.Bitmap, 0)
vs := lvs.Match(lms[i])
for _, v := range vs {
didx := dim.label2sids[joinSeparator(lms[i].Name, v)]
if didx == nil || didx.set.IsEmpty() {
continue
}
tmp = append(tmp, didx.set)
}
union := roaring.ParOr(4, tmp...)
if union.IsEmpty() {
return nil
}
lst = append(lst, union)
}
return roaring.ParAnd(4, lst...).ToArray()
}