Rename iterator value getters to At()

This commit is contained in:
Fabian Reinartz 2017-01-02 13:27:52 +01:00
parent 5c45a1cc6f
commit 62b8ded0a5
10 changed files with 64 additions and 67 deletions

View file

@ -51,7 +51,7 @@ type Appender interface {
// Iterator is a simple iterator that can only get the next value. // Iterator is a simple iterator that can only get the next value.
type Iterator interface { type Iterator interface {
Values() (int64, float64) At() (int64, float64)
Err() error Err() error
Next() bool Next() bool
} }

View file

@ -67,7 +67,7 @@ func testChunk(c Chunk) error {
it := c.Iterator() it := c.Iterator()
var res []pair var res []pair
for it.Next() { for it.Next() {
ts, v := it.Values() ts, v := it.At()
res = append(res, pair{t: ts, v: v}) res = append(res, pair{t: ts, v: v})
} }
if it.Err() != nil { if it.Err() != nil {
@ -125,7 +125,7 @@ func benchmarkIterator(b *testing.B, newChunk func() Chunk) {
it := c.Iterator() it := c.Iterator()
for it.Next() { for it.Next() {
_, v := it.Values() _, v := it.At()
res = append(res, v) res = append(res, v)
} }
if it.Err() != io.EOF { if it.Err() != io.EOF {

View file

@ -190,7 +190,7 @@ type xorIterator struct {
err error err error
} }
func (it *xorIterator) Values() (int64, float64) { func (it *xorIterator) At() (int64, float64) {
return it.t, it.val return it.t, it.val
} }

View file

@ -196,7 +196,7 @@ func (c *compactionSeriesSet) Next() bool {
return false return false
} }
c.l, c.c, c.err = c.index.Series(c.p.Value()) c.l, c.c, c.err = c.index.Series(c.p.At())
if c.err != nil { if c.err != nil {
return false return false
} }

2
db.go
View file

@ -95,8 +95,6 @@ func (db *DB) Close() error {
var g errgroup.Group var g errgroup.Group
for _, shard := range db.shards { for _, shard := range db.shards {
// Fix closure argument to goroutine.
shard := shard
g.Go(shard.Close) g.Go(shard.Close)
} }

View file

@ -39,8 +39,8 @@ type Postings interface {
// true if a value was found. // true if a value was found.
Seek(v uint32) bool Seek(v uint32) bool
// Value returns the value at the current iterator position. // At returns the value at the current iterator position.
Value() uint32 At() uint32
// Err returns the last error of the iterator. // Err returns the last error of the iterator.
Err() error Err() error
@ -53,12 +53,12 @@ type errPostings struct {
func (e errPostings) Next() bool { return false } func (e errPostings) Next() bool { return false }
func (e errPostings) Seek(uint32) bool { return false } func (e errPostings) Seek(uint32) bool { return false }
func (e errPostings) Value() uint32 { return 0 } func (e errPostings) At() uint32 { return 0 }
func (e errPostings) Err() error { return e.err } func (e errPostings) Err() error { return e.err }
func expandPostings(p Postings) (res []uint32, err error) { func expandPostings(p Postings) (res []uint32, err error) {
for p.Next() { for p.Next() {
res = append(res, p.Value()) res = append(res, p.At())
} }
return res, p.Err() return res, p.Err()
} }
@ -93,7 +93,7 @@ func newIntersectPostings(a, b Postings) *intersectPostings {
return it return it
} }
func (it *intersectPostings) Value() uint32 { func (it *intersectPostings) At() uint32 {
return it.cur return it.cur
} }
@ -102,7 +102,7 @@ func (it *intersectPostings) Next() bool {
if !it.aok || !it.bok { if !it.aok || !it.bok {
return false return false
} }
av, bv := it.a.Value(), it.b.Value() av, bv := it.a.At(), it.b.At()
if av < bv { if av < bv {
it.aok = it.a.Seek(bv) it.aok = it.a.Seek(bv)
@ -157,7 +157,7 @@ func newMergePostings(a, b Postings) *mergePostings {
return it return it
} }
func (it *mergePostings) Value() uint32 { func (it *mergePostings) At() uint32 {
return it.cur return it.cur
} }
@ -167,17 +167,17 @@ func (it *mergePostings) Next() bool {
} }
if !it.aok { if !it.aok {
it.cur = it.b.Value() it.cur = it.b.At()
it.bok = it.b.Next() it.bok = it.b.Next()
return true return true
} }
if !it.bok { if !it.bok {
it.cur = it.a.Value() it.cur = it.a.At()
it.aok = it.a.Next() it.aok = it.a.Next()
return true return true
} }
acur, bcur := it.a.Value(), it.b.Value() acur, bcur := it.a.At(), it.b.At()
if acur < bcur { if acur < bcur {
it.cur = acur it.cur = acur
@ -219,7 +219,7 @@ func newListPostings(list []uint32) *listPostings {
return &listPostings{list: list, idx: -1} return &listPostings{list: list, idx: -1}
} }
func (it *listPostings) Value() uint32 { func (it *listPostings) At() uint32 {
return it.list[it.idx] return it.list[it.idx]
} }

View file

@ -33,8 +33,6 @@ type Series interface {
// Iterator returns a new iterator of the data of the series. // Iterator returns a new iterator of the data of the series.
Iterator() SeriesIterator Iterator() SeriesIterator
// Ref() uint32
} }
// querier merges query results from a set of shard querieres. // querier merges query results from a set of shard querieres.
@ -313,14 +311,14 @@ func (q *blockQuerier) Close() error {
// SeriesSet contains a set of series. // SeriesSet contains a set of series.
type SeriesSet interface { type SeriesSet interface {
Next() bool Next() bool
Series() Series At() Series
Err() error Err() error
} }
type nopSeriesSet struct{} type nopSeriesSet struct{}
func (nopSeriesSet) Next() bool { return false } func (nopSeriesSet) Next() bool { return false }
func (nopSeriesSet) Series() Series { return nil } func (nopSeriesSet) At() Series { return nil }
func (nopSeriesSet) Err() error { return nil } func (nopSeriesSet) Err() error { return nil }
type mergedSeriesSet struct { type mergedSeriesSet struct {
@ -330,7 +328,7 @@ type mergedSeriesSet struct {
err error err error
} }
func (s *mergedSeriesSet) Series() Series { return s.sets[s.cur].Series() } func (s *mergedSeriesSet) At() Series { return s.sets[s.cur].At() }
func (s *mergedSeriesSet) Err() error { return s.sets[s.cur].Err() } func (s *mergedSeriesSet) Err() error { return s.sets[s.cur].Err() }
func (s *mergedSeriesSet) Next() bool { func (s *mergedSeriesSet) Next() bool {
@ -365,7 +363,7 @@ func newShardSeriesSet(a, b SeriesSet) *shardSeriesSet {
return s return s
} }
func (s *shardSeriesSet) Series() Series { func (s *shardSeriesSet) At() Series {
return s.cur return s.cur
} }
@ -383,7 +381,7 @@ func (s *shardSeriesSet) compare() int {
if s.bdone { if s.bdone {
return -1 return -1
} }
return labels.Compare(s.a.Series().Labels(), s.a.Series().Labels()) return labels.Compare(s.a.At().Labels(), s.a.At().Labels())
} }
func (s *shardSeriesSet) Next() bool { func (s *shardSeriesSet) Next() bool {
@ -394,15 +392,15 @@ func (s *shardSeriesSet) Next() bool {
d := s.compare() d := s.compare()
// Both sets contain the current series. Chain them into a single one. // Both sets contain the current series. Chain them into a single one.
if d > 0 { if d > 0 {
s.cur = s.b.Series() s.cur = s.b.At()
s.bdone = !s.b.Next() s.bdone = !s.b.Next()
} else if d < 0 { } else if d < 0 {
s.cur = s.a.Series() s.cur = s.a.At()
s.adone = !s.a.Next() s.adone = !s.a.Next()
} else { } else {
s.cur = &chainedSeries{series: []Series{s.a.Series(), s.b.Series()}} s.cur = &chainedSeries{series: []Series{s.a.At(), s.b.At()}}
s.adone = !s.a.Next() s.adone = !s.a.Next()
s.bdone = !s.b.Next() s.bdone = !s.b.Next()
} }
@ -425,7 +423,7 @@ func (s *blockSeriesSet) Next() bool {
// Step through the postings iterator to find potential series. // Step through the postings iterator to find potential series.
outer: outer:
for s.it.Next() { for s.it.Next() {
lset, chunks, err := s.index.Series(s.it.Value()) lset, chunks, err := s.index.Series(s.it.At())
if err != nil { if err != nil {
s.err = err s.err = err
return false return false
@ -467,7 +465,7 @@ outer:
return false return false
} }
func (s *blockSeriesSet) Series() Series { return s.cur } func (s *blockSeriesSet) At() Series { return s.cur }
func (s *blockSeriesSet) Err() error { return s.err } func (s *blockSeriesSet) Err() error { return s.err }
// chunkSeries is a series that is backed by a sequence of chunks holding // chunkSeries is a series that is backed by a sequence of chunks holding
@ -510,7 +508,7 @@ type SeriesIterator interface {
// before tt. // before tt.
Seek(t int64) bool Seek(t int64) bool
// Values returns the current timestamp/value pair. // Values returns the current timestamp/value pair.
Values() (t int64, v float64) At() (t int64, v float64)
// Next advances the iterator by one. // Next advances the iterator by one.
Next() bool Next() bool
// Err returns the current error. // Err returns the current error.
@ -575,8 +573,8 @@ func (it *chainedSeriesIterator) Next() bool {
return it.Next() return it.Next()
} }
func (it *chainedSeriesIterator) Values() (t int64, v float64) { func (it *chainedSeriesIterator) At() (t int64, v float64) {
return it.cur.Values() return it.cur.At()
} }
func (it *chainedSeriesIterator) Err() error { func (it *chainedSeriesIterator) Err() error {
@ -625,7 +623,7 @@ func (it *chunkSeriesIterator) Seek(t int64) (ok bool) {
it.cur = it.chunks[x].Iterator() it.cur = it.chunks[x].Iterator()
for it.cur.Next() { for it.cur.Next() {
t0, _ := it.cur.Values() t0, _ := it.cur.At()
if t0 >= t { if t0 >= t {
return true return true
} }
@ -633,8 +631,8 @@ func (it *chunkSeriesIterator) Seek(t int64) (ok bool) {
return false return false
} }
func (it *chunkSeriesIterator) Values() (t int64, v float64) { func (it *chunkSeriesIterator) At() (t int64, v float64) {
return it.cur.Values() return it.cur.At()
} }
func (it *chunkSeriesIterator) Next() bool { func (it *chunkSeriesIterator) Next() bool {
@ -700,7 +698,7 @@ func (b *BufferedSeriesIterator) Seek(t int64) bool {
if !ok { if !ok {
return false return false
} }
b.lastTime, _ = b.Values() b.lastTime, _ = b.At()
} }
if b.lastTime >= t { if b.lastTime >= t {
@ -718,18 +716,18 @@ func (b *BufferedSeriesIterator) Seek(t int64) bool {
// Next advances the iterator to the next element. // Next advances the iterator to the next element.
func (b *BufferedSeriesIterator) Next() bool { func (b *BufferedSeriesIterator) Next() bool {
// Add current element to buffer before advancing. // Add current element to buffer before advancing.
b.buf.add(b.it.Values()) b.buf.add(b.it.At())
ok := b.it.Next() ok := b.it.Next()
if ok { if ok {
b.lastTime, _ = b.Values() b.lastTime, _ = b.At()
} }
return ok return ok
} }
// Values returns the current element of the iterator. // Values returns the current element of the iterator.
func (b *BufferedSeriesIterator) Values() (int64, float64) { func (b *BufferedSeriesIterator) At() (int64, float64) {
return b.it.Values() return b.it.At()
} }
// Err returns the last encountered error. // Err returns the last encountered error.
@ -786,7 +784,7 @@ func (it *sampleRingIterator) Err() error {
return nil return nil
} }
func (it *sampleRingIterator) Values() (int64, float64) { func (it *sampleRingIterator) At() (int64, float64) {
return it.r.at(it.i) return it.r.at(it.i)
} }

View file

@ -11,13 +11,13 @@ import (
type mockSeriesIterator struct { type mockSeriesIterator struct {
seek func(int64) bool seek func(int64) bool
values func() (int64, float64) at func() (int64, float64)
next func() bool next func() bool
err func() error err func() error
} }
func (m *mockSeriesIterator) Seek(t int64) bool { return m.seek(t) } func (m *mockSeriesIterator) Seek(t int64) bool { return m.seek(t) }
func (m *mockSeriesIterator) Values() (int64, float64) { return m.values() } func (m *mockSeriesIterator) At() (int64, float64) { return m.at() }
func (m *mockSeriesIterator) Next() bool { return m.next() } func (m *mockSeriesIterator) Next() bool { return m.next() }
func (m *mockSeriesIterator) Err() error { return m.err() } func (m *mockSeriesIterator) Err() error { return m.err() }
@ -38,7 +38,7 @@ func newListSeriesIterator(list []sample) *listSeriesIterator {
return &listSeriesIterator{list: list, idx: -1} return &listSeriesIterator{list: list, idx: -1}
} }
func (it *listSeriesIterator) Values() (int64, float64) { func (it *listSeriesIterator) At() (int64, float64) {
s := it.list[it.idx] s := it.list[it.idx]
return s.t, s.v return s.t, s.v
} }
@ -72,7 +72,7 @@ type mockSeriesSet struct {
} }
func (m *mockSeriesSet) Next() bool { return m.next() } func (m *mockSeriesSet) Next() bool { return m.next() }
func (m *mockSeriesSet) Series() Series { return m.series() } func (m *mockSeriesSet) At() Series { return m.series() }
func (m *mockSeriesSet) Err() error { return m.err() } func (m *mockSeriesSet) Err() error { return m.err() }
func newListSeriesSet(list []Series) *mockSeriesSet { func newListSeriesSet(list []Series) *mockSeriesSet {
@ -152,8 +152,8 @@ Outer:
if !eok { if !eok {
continue Outer continue Outer
} }
sexp := c.exp.Series() sexp := c.exp.At()
sres := res.Series() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
@ -168,7 +168,7 @@ Outer:
func expandSeriesIterator(it SeriesIterator) (r []sample, err error) { func expandSeriesIterator(it SeriesIterator) (r []sample, err error) {
for it.Next() { for it.Next() {
t, v := it.Values() t, v := it.At()
r = append(r, sample{t: t, v: v}) r = append(r, sample{t: t, v: v})
} }
@ -243,13 +243,13 @@ func TestBufferedSeriesIterator(t *testing.T) {
var b []sample var b []sample
bit := it.Buffer() bit := it.Buffer()
for bit.Next() { for bit.Next() {
t, v := bit.Values() t, v := bit.At()
b = append(b, sample{t: t, v: v}) b = append(b, sample{t: t, v: v})
} }
require.Equal(t, exp, b, "buffer mismatch") require.Equal(t, exp, b, "buffer mismatch")
} }
sampleEq := func(ets int64, ev float64) { sampleEq := func(ets int64, ev float64) {
ts, v := it.Values() ts, v := it.At()
require.Equal(t, ets, ts, "timestamp mismatch") require.Equal(t, ets, ts, "timestamp mismatch")
require.Equal(t, ev, v, "value mismatch") require.Equal(t, ev, v, "value mismatch")
} }

5
wal.go
View file

@ -136,10 +136,11 @@ func newWALEncoder(f *os.File) (*walEncoder, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &walEncoder{ enc := &walEncoder{
w: ioutil.NewPageWriter(f, walPageBytes, int(offset)), w: ioutil.NewPageWriter(f, walPageBytes, int(offset)),
buf: make([]byte, 0, 1024*1024), buf: make([]byte, 0, 1024*1024),
}, nil }
return enc, nil
} }
func (e *walEncoder) flush() error { func (e *walEncoder) flush() error {

View file

@ -424,9 +424,9 @@ func (w *indexWriter) WritePostings(name, value string, it Postings) error {
var refs []uint32 var refs []uint32
for it.Next() { for it.Next() {
s, ok := w.series[it.Value()] s, ok := w.series[it.At()]
if !ok { if !ok {
return errors.Errorf("series for reference %d not found", it.Value()) return errors.Errorf("series for reference %d not found", it.At())
} }
refs = append(refs, s.offset) refs = append(refs, s.offset)
} }