Implement Postings Iterator Over Bytes

Closes fabxc/tsdb#18
This commit is contained in:
Goutham Veeramachaneni 2017-03-26 23:40:12 +05:30
parent f85d89abc0
commit efb0dfe1be
No known key found for this signature in database
GPG Key ID: F1C217E8E9023CAD
2 changed files with 39 additions and 13 deletions

View File

@ -698,20 +698,11 @@ func (r *indexReader) Postings(name, value string) (Postings, error) {
return nil, errors.Wrapf(errInvalidFlag, "section at %d", off)
}
// TODO(fabxc): just read into memory as an intermediate solution.
// Add iterator over serialized data.
var l []uint32
for len(b) > 0 {
if len(b) < 4 {
return nil, errors.Wrap(errInvalidSize, "plain postings entry")
}
l = append(l, binary.BigEndian.Uint32(b[:4]))
b = b[4:]
// Add iterator over the bytes.
if len(b)%4 != 0 {
return nil, errors.Wrap(errInvalidSize, "plain postings entry")
}
return &listPostings{list: l, idx: -1}, nil
return newBytePostings(b), nil
}
type stringTuples struct {

View File

@ -1,6 +1,7 @@
package tsdb
import (
"encoding/binary"
"sort"
"strings"
)
@ -240,6 +241,40 @@ func (it *listPostings) Err() error {
return nil
}
type bytePostings struct {
list []byte
idx int
}
func newBytePostings(list []byte) *bytePostings {
return &bytePostings{list: list, idx: -1}
}
func (it *bytePostings) At() uint32 {
idx := 4 * it.idx
return binary.BigEndian.Uint32(it.list[idx : idx+4])
}
func (it *bytePostings) Next() bool {
it.idx++
return it.idx*4 < len(it.list)
}
func (it *bytePostings) Seek(x uint32) bool {
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)
}
func (it *bytePostings) Err() error {
return nil
}
type stringset map[string]struct{}
func (ss stringset) set(s string) {