Skip to content

Commit

Permalink
Implement Postings Iterator Over Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
gouthamve committed Mar 26, 2017
1 parent f85d89a commit efb0dfe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
17 changes: 4 additions & 13 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions postings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tsdb

import (
"encoding/binary"
"sort"
"strings"
)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit efb0dfe

Please sign in to comment.