2017-04-10 11:59:45 -07:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-12-04 04:16:11 -08:00
|
|
|
package tsdb
|
|
|
|
|
2016-12-09 01:41:51 -08:00
|
|
|
import (
|
2017-03-26 11:10:12 -07:00
|
|
|
"encoding/binary"
|
2016-12-09 01:41:51 -08:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
2016-12-04 04:16:11 -08:00
|
|
|
|
2016-12-21 16:12:28 -08:00
|
|
|
type memPostings struct {
|
|
|
|
m map[term][]uint32
|
2016-12-10 00:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type term struct {
|
|
|
|
name, value string
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2016-12-09 01:41:51 -08:00
|
|
|
// Postings returns an iterator over the postings list for s.
|
2016-12-14 12:58:29 -08:00
|
|
|
func (p *memPostings) get(t term) Postings {
|
2016-12-31 01:19:02 -08:00
|
|
|
l := p.m[t]
|
|
|
|
if l == nil {
|
|
|
|
return emptyPostings
|
|
|
|
}
|
|
|
|
return &listPostings{list: l, idx: -1}
|
2016-12-09 01:41:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// add adds a document to the index. The caller has to ensure that no
|
|
|
|
// term argument appears twice.
|
2016-12-10 00:44:00 -08:00
|
|
|
func (p *memPostings) add(id uint32, terms ...term) {
|
2016-12-09 01:41:51 -08:00
|
|
|
for _, t := range terms {
|
2017-01-13 07:14:40 -08:00
|
|
|
p.m[t] = append(p.m[t], id)
|
2016-12-09 01:41:51 -08:00
|
|
|
}
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
// Postings provides iterative access over a postings list.
|
|
|
|
type Postings interface {
|
2016-12-13 06:26:58 -08:00
|
|
|
// Next advances the iterator and returns true if another value was found.
|
2016-12-04 04:16:11 -08:00
|
|
|
Next() bool
|
2016-12-13 06:26:58 -08:00
|
|
|
|
2016-12-04 04:16:11 -08:00
|
|
|
// Seek advances the iterator to value v or greater and returns
|
|
|
|
// true if a value was found.
|
|
|
|
Seek(v uint32) bool
|
2016-12-13 06:26:58 -08:00
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
// At returns the value at the current iterator position.
|
|
|
|
At() uint32
|
2016-12-13 06:26:58 -08:00
|
|
|
|
|
|
|
// Err returns the last error of the iterator.
|
|
|
|
Err() error
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
// errPostings is an empty iterator that always errors.
|
|
|
|
type errPostings struct {
|
2016-12-13 06:26:58 -08:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
func (e errPostings) Next() bool { return false }
|
|
|
|
func (e errPostings) Seek(uint32) bool { return false }
|
2017-01-02 04:27:52 -08:00
|
|
|
func (e errPostings) At() uint32 { return 0 }
|
2016-12-14 12:58:29 -08:00
|
|
|
func (e errPostings) Err() error { return e.err }
|
2016-12-13 06:26:58 -08:00
|
|
|
|
2016-12-27 02:32:10 -08:00
|
|
|
func expandPostings(p Postings) (res []uint32, err error) {
|
|
|
|
for p.Next() {
|
2017-01-02 04:27:52 -08:00
|
|
|
res = append(res, p.At())
|
2016-12-27 02:32:10 -08:00
|
|
|
}
|
|
|
|
return res, p.Err()
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
// Intersect returns a new postings list over the intersection of the
|
|
|
|
// input postings.
|
|
|
|
func Intersect(its ...Postings) Postings {
|
2016-12-04 04:16:11 -08:00
|
|
|
if len(its) == 0 {
|
2016-12-14 12:58:29 -08:00
|
|
|
return errPostings{err: nil}
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
a := its[0]
|
|
|
|
|
|
|
|
for _, b := range its[1:] {
|
2016-12-27 02:32:10 -08:00
|
|
|
a = newIntersectPostings(a, b)
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2016-12-28 02:02:19 -08:00
|
|
|
var emptyPostings = errPostings{}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
type intersectPostings struct {
|
2016-12-27 02:32:10 -08:00
|
|
|
a, b Postings
|
|
|
|
aok, bok bool
|
|
|
|
cur uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIntersectPostings(a, b Postings) *intersectPostings {
|
|
|
|
it := &intersectPostings{a: a, b: b}
|
|
|
|
it.aok = it.a.Next()
|
|
|
|
it.bok = it.b.Next()
|
|
|
|
|
|
|
|
return it
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (it *intersectPostings) At() uint32 {
|
2016-12-27 02:32:10 -08:00
|
|
|
return it.cur
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
func (it *intersectPostings) Next() bool {
|
2016-12-27 02:32:10 -08:00
|
|
|
for {
|
|
|
|
if !it.aok || !it.bok {
|
|
|
|
return false
|
|
|
|
}
|
2017-01-02 04:27:52 -08:00
|
|
|
av, bv := it.a.At(), it.b.At()
|
2016-12-27 02:32:10 -08:00
|
|
|
|
|
|
|
if av < bv {
|
|
|
|
it.aok = it.a.Seek(bv)
|
|
|
|
} else if bv < av {
|
|
|
|
it.bok = it.b.Seek(av)
|
|
|
|
} else {
|
|
|
|
it.cur = av
|
|
|
|
it.aok = it.a.Next()
|
|
|
|
it.bok = it.b.Next()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
func (it *intersectPostings) Seek(id uint32) bool {
|
2016-12-27 02:32:10 -08:00
|
|
|
it.aok = it.a.Seek(id)
|
|
|
|
it.bok = it.b.Seek(id)
|
|
|
|
return it.Next()
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2016-12-14 12:58:29 -08:00
|
|
|
func (it *intersectPostings) Err() error {
|
2016-12-27 02:32:10 -08:00
|
|
|
if it.a.Err() != nil {
|
|
|
|
return it.a.Err()
|
|
|
|
}
|
|
|
|
return it.b.Err()
|
2016-12-13 06:26:58 -08:00
|
|
|
}
|
|
|
|
|
2016-12-04 04:16:11 -08:00
|
|
|
// Merge returns a new iterator over the union of the input iterators.
|
2016-12-14 12:58:29 -08:00
|
|
|
func Merge(its ...Postings) Postings {
|
2016-12-04 04:16:11 -08:00
|
|
|
if len(its) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
a := its[0]
|
|
|
|
|
|
|
|
for _, b := range its[1:] {
|
2017-04-08 08:12:29 -07:00
|
|
|
a = newMergedPostings(a, b)
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-04-08 08:12:29 -07:00
|
|
|
type mergedPostings struct {
|
2016-12-28 02:02:19 -08:00
|
|
|
a, b Postings
|
|
|
|
aok, bok bool
|
|
|
|
cur uint32
|
|
|
|
}
|
|
|
|
|
2017-04-08 08:12:29 -07:00
|
|
|
func newMergedPostings(a, b Postings) *mergedPostings {
|
|
|
|
it := &mergedPostings{a: a, b: b}
|
2016-12-28 02:02:19 -08:00
|
|
|
it.aok = it.a.Next()
|
|
|
|
it.bok = it.b.Next()
|
|
|
|
|
|
|
|
return it
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2017-04-08 08:12:29 -07:00
|
|
|
func (it *mergedPostings) At() uint32 {
|
2016-12-28 02:02:19 -08:00
|
|
|
return it.cur
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2017-04-08 08:12:29 -07:00
|
|
|
func (it *mergedPostings) Next() bool {
|
2016-12-28 02:02:19 -08:00
|
|
|
if !it.aok && !it.bok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !it.aok {
|
2017-01-02 04:27:52 -08:00
|
|
|
it.cur = it.b.At()
|
2016-12-28 02:02:19 -08:00
|
|
|
it.bok = it.b.Next()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !it.bok {
|
2017-01-02 04:27:52 -08:00
|
|
|
it.cur = it.a.At()
|
2016-12-28 02:02:19 -08:00
|
|
|
it.aok = it.a.Next()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
acur, bcur := it.a.At(), it.b.At()
|
2016-12-28 02:02:19 -08:00
|
|
|
|
|
|
|
if acur < bcur {
|
|
|
|
it.cur = acur
|
|
|
|
it.aok = it.a.Next()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if bcur < acur {
|
|
|
|
it.cur = bcur
|
|
|
|
it.bok = it.b.Next()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
it.cur = acur
|
|
|
|
it.aok = it.a.Next()
|
|
|
|
it.bok = it.b.Next()
|
|
|
|
|
|
|
|
return true
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2017-04-08 08:12:29 -07:00
|
|
|
func (it *mergedPostings) Seek(id uint32) bool {
|
2016-12-28 02:02:19 -08:00
|
|
|
it.aok = it.a.Seek(id)
|
|
|
|
it.bok = it.b.Seek(id)
|
2017-04-08 08:12:29 -07:00
|
|
|
|
2016-12-28 02:02:19 -08:00
|
|
|
return it.Next()
|
2016-12-04 04:16:11 -08:00
|
|
|
}
|
|
|
|
|
2017-04-08 08:12:29 -07:00
|
|
|
func (it *mergedPostings) Err() error {
|
2016-12-28 02:02:19 -08:00
|
|
|
if it.a.Err() != nil {
|
|
|
|
return it.a.Err()
|
|
|
|
}
|
|
|
|
return it.b.Err()
|
2016-12-13 06:26:58 -08:00
|
|
|
}
|
|
|
|
|
2016-12-19 02:44:11 -08:00
|
|
|
// listPostings implements the Postings interface over a plain list.
|
|
|
|
type listPostings struct {
|
2016-12-04 04:16:11 -08:00
|
|
|
list []uint32
|
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
2016-12-27 23:50:20 -08:00
|
|
|
func newListPostings(list []uint32) *listPostings {
|
|
|
|
return &listPostings{list: list, idx: -1}
|
|
|
|
}
|
|
|
|
|
2017-01-02 04:27:52 -08:00
|
|
|
func (it *listPostings) At() uint32 {
|
2016-12-04 04:16:11 -08:00
|
|
|
return it.list[it.idx]
|
|
|
|
}
|
|
|
|
|
2016-12-19 02:44:11 -08:00
|
|
|
func (it *listPostings) Next() bool {
|
2016-12-04 04:16:11 -08:00
|
|
|
it.idx++
|
|
|
|
return it.idx < len(it.list)
|
|
|
|
}
|
|
|
|
|
2016-12-19 02:44:11 -08:00
|
|
|
func (it *listPostings) Seek(x uint32) bool {
|
2016-12-04 04:16:11 -08:00
|
|
|
// Do binary search between current position and end.
|
2016-12-27 23:50:20 -08:00
|
|
|
it.idx += sort.Search(len(it.list)-it.idx, func(i int) bool {
|
2016-12-04 04:16:11 -08:00
|
|
|
return it.list[i+it.idx] >= x
|
|
|
|
})
|
|
|
|
return it.idx < len(it.list)
|
|
|
|
}
|
2016-12-09 01:41:51 -08:00
|
|
|
|
2016-12-19 02:44:11 -08:00
|
|
|
func (it *listPostings) Err() error {
|
2016-12-13 06:26:58 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:34:42 -07:00
|
|
|
// bigEndianPostings implements the Postings interface over a byte stream of
|
|
|
|
// big endian numbers.
|
|
|
|
type bigEndianPostings struct {
|
2017-03-26 11:10:12 -07:00
|
|
|
list []byte
|
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:34:42 -07:00
|
|
|
func newBigEndianPostings(list []byte) *bigEndianPostings {
|
|
|
|
return &bigEndianPostings{list: list, idx: -1}
|
2017-03-26 11:10:12 -07:00
|
|
|
}
|
|
|
|
|
2017-03-27 01:34:42 -07:00
|
|
|
func (it *bigEndianPostings) At() uint32 {
|
2017-03-26 11:10:12 -07:00
|
|
|
idx := 4 * it.idx
|
|
|
|
return binary.BigEndian.Uint32(it.list[idx : idx+4])
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:34:42 -07:00
|
|
|
func (it *bigEndianPostings) Next() bool {
|
2017-03-26 11:10:12 -07:00
|
|
|
it.idx++
|
|
|
|
return it.idx*4 < len(it.list)
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:34:42 -07:00
|
|
|
func (it *bigEndianPostings) Seek(x uint32) bool {
|
2017-03-26 11:10:12 -07:00
|
|
|
num := len(it.list) / 4
|
|
|
|
// Do binary search between current position and end.
|
|
|
|
it.idx += sort.Search(num-it.idx, func(i int) bool {
|
|
|
|
idx := 4 * (it.idx + i)
|
|
|
|
val := binary.BigEndian.Uint32(it.list[idx : idx+4])
|
|
|
|
return val >= x
|
|
|
|
})
|
|
|
|
return it.idx*4 < len(it.list)
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:34:42 -07:00
|
|
|
func (it *bigEndianPostings) Err() error {
|
2017-03-26 11:10:12 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 01:41:51 -08:00
|
|
|
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
|
|
|
|
}
|