mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
3.2 KiB
172 lines
3.2 KiB
8 years ago
|
package tsdb
|
||
|
|
||
8 years ago
|
import (
|
||
|
"sort"
|
||
|
"strings"
|
||
|
)
|
||
8 years ago
|
|
||
8 years ago
|
type memPostings struct {
|
||
|
m map[term][]uint32
|
||
8 years ago
|
}
|
||
|
|
||
|
type term struct {
|
||
|
name, value string
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Postings returns an iterator over the postings list for s.
|
||
8 years ago
|
func (p *memPostings) get(t term) Postings {
|
||
8 years ago
|
return &listPostings{list: p.m[t], idx: -1}
|
||
8 years ago
|
}
|
||
|
|
||
|
// add adds a document to the index. The caller has to ensure that no
|
||
|
// term argument appears twice.
|
||
8 years ago
|
func (p *memPostings) add(id uint32, terms ...term) {
|
||
8 years ago
|
for _, t := range terms {
|
||
|
p.m[t] = append(p.m[t], id)
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Postings provides iterative access over a postings list.
|
||
|
type Postings interface {
|
||
8 years ago
|
// Next advances the iterator and returns true if another value was found.
|
||
8 years ago
|
Next() bool
|
||
8 years ago
|
|
||
8 years ago
|
// Seek advances the iterator to value v or greater and returns
|
||
|
// true if a value was found.
|
||
|
Seek(v uint32) bool
|
||
8 years ago
|
|
||
8 years ago
|
// Value returns the value at the current iterator position.
|
||
|
Value() uint32
|
||
8 years ago
|
|
||
|
// Err returns the last error of the iterator.
|
||
|
Err() error
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// errPostings is an empty iterator that always errors.
|
||
|
type errPostings struct {
|
||
8 years ago
|
err error
|
||
|
}
|
||
|
|
||
8 years ago
|
func (e errPostings) Next() bool { return false }
|
||
|
func (e errPostings) Seek(uint32) bool { return false }
|
||
|
func (e errPostings) Value() uint32 { return 0 }
|
||
|
func (e errPostings) Err() error { return e.err }
|
||
8 years ago
|
|
||
8 years ago
|
// Intersect returns a new postings list over the intersection of the
|
||
|
// input postings.
|
||
|
func Intersect(its ...Postings) Postings {
|
||
8 years ago
|
if len(its) == 0 {
|
||
8 years ago
|
return errPostings{err: nil}
|
||
8 years ago
|
}
|
||
|
a := its[0]
|
||
|
|
||
|
for _, b := range its[1:] {
|
||
8 years ago
|
a = &intersectPostings{a: a, b: b}
|
||
8 years ago
|
}
|
||
|
return a
|
||
|
}
|
||
|
|
||
8 years ago
|
type intersectPostings struct {
|
||
|
a, b Postings
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func (it *intersectPostings) Value() uint32 {
|
||
8 years ago
|
return 0
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *intersectPostings) Next() bool {
|
||
8 years ago
|
return false
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *intersectPostings) Seek(id uint32) bool {
|
||
8 years ago
|
return false
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *intersectPostings) Err() error {
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// Merge returns a new iterator over the union of the input iterators.
|
||
8 years ago
|
func Merge(its ...Postings) Postings {
|
||
8 years ago
|
if len(its) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
a := its[0]
|
||
|
|
||
|
for _, b := range its[1:] {
|
||
8 years ago
|
a = &mergePostings{a: a, b: b}
|
||
8 years ago
|
}
|
||
|
return a
|
||
|
}
|
||
|
|
||
8 years ago
|
type mergePostings struct {
|
||
|
a, b Postings
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func (it *mergePostings) Value() uint32 {
|
||
8 years ago
|
return 0
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *mergePostings) Next() bool {
|
||
8 years ago
|
return false
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *mergePostings) Seek(id uint32) bool {
|
||
8 years ago
|
return false
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *mergePostings) Err() error {
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// listPostings implements the Postings interface over a plain list.
|
||
|
type listPostings struct {
|
||
8 years ago
|
list []uint32
|
||
|
idx int
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *listPostings) Value() uint32 {
|
||
8 years ago
|
return it.list[it.idx]
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *listPostings) Next() bool {
|
||
8 years ago
|
it.idx++
|
||
|
return it.idx < len(it.list)
|
||
|
}
|
||
|
|
||
8 years ago
|
func (it *listPostings) Seek(x uint32) bool {
|
||
8 years ago
|
// Do binary search between current position and end.
|
||
|
it.idx = sort.Search(len(it.list)-it.idx, func(i int) bool {
|
||
|
return it.list[i+it.idx] >= x
|
||
|
})
|
||
|
return it.idx < len(it.list)
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
func (it *listPostings) Err() error {
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
type stringset map[string]struct{}
|
||
|
|
||
|
func (ss stringset) set(s string) {
|
||
|
ss[s] = struct{}{}
|
||
|
}
|
||
|
|
||
|
func (ss stringset) has(s string) bool {
|
||
|
_, ok := ss[s]
|
||
|
return ok
|
||
|
}
|
||
|
|
||
|
func (ss stringset) String() string {
|
||
|
return strings.Join(ss.slice(), ",")
|
||
|
}
|
||
|
|
||
|
func (ss stringset) slice() []string {
|
||
|
slice := make([]string, 0, len(ss))
|
||
|
for k := range ss {
|
||
|
slice = append(slice, k)
|
||
|
}
|
||
|
sort.Strings(slice)
|
||
|
return slice
|
||
|
}
|