Remove last vestiges of never used composite index code.

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
This commit is contained in:
Brian Brazil 2020-01-01 11:21:42 +00:00
parent f9fef91b59
commit e9ede51753
6 changed files with 70 additions and 109 deletions

View File

@ -63,7 +63,7 @@ type IndexReader interface {
Symbols() index.StringIter
// LabelValues returns sorted possible label values.
LabelValues(names ...string) (index.StringTuples, error)
LabelValues(name string) (index.StringTuples, error)
// Postings returns the postings list iterator for the label pairs.
// The Postings here contain the offsets to the series inside the index.
@ -433,8 +433,8 @@ func (r blockIndexReader) Symbols() index.StringIter {
return r.ir.Symbols()
}
func (r blockIndexReader) LabelValues(names ...string) (index.StringTuples, error) {
st, err := r.ir.LabelValues(names...)
func (r blockIndexReader) LabelValues(name string) (index.StringTuples, error) {
st, err := r.ir.LabelValues(name)
return st, errors.Wrapf(err, "block: %s", r.b.Meta().ULID)
}

View File

@ -31,7 +31,6 @@ import (
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/encoding"
"github.com/prometheus/prometheus/tsdb/index"
"github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/tsdb/tombstones"
@ -1353,20 +1352,16 @@ func (h *headIndexReader) Symbols() index.StringIter {
}
// LabelValues returns the possible label values
func (h *headIndexReader) LabelValues(names ...string) (index.StringTuples, error) {
if len(names) != 1 {
return nil, encoding.ErrInvalidSize
}
func (h *headIndexReader) LabelValues(name string) (index.StringTuples, error) {
h.head.symMtx.RLock()
sl := make([]string, 0, len(h.head.values[names[0]]))
for s := range h.head.values[names[0]] {
sl := make([]string, 0, len(h.head.values[name]))
for s := range h.head.values[name] {
sl = append(sl, s)
}
h.head.symMtx.RUnlock()
sort.Strings(sl)
return index.NewStringTuples(sl, len(names))
return index.NewStringTuples(sl)
}
// LabelNames returns all the unique label names present in the head.

View File

@ -26,7 +26,6 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"unsafe"
"github.com/pkg/errors"
@ -999,7 +998,7 @@ type StringTuples interface {
// Total number of tuples in the list.
Len() int
// At returns the tuple at position i.
At(i int) ([]string, error)
At(i int) (string, error)
}
// StringIter iterates over a sorted list of strings.
@ -1422,15 +1421,12 @@ func (r *Reader) SymbolTableSize() uint64 {
return uint64(r.symbols.Size())
}
// LabelValues returns value tuples that exist for the given label name tuples.
// LabelValues returns value tuples that exist for the given label name.
// It is not safe to use the return value beyond the lifetime of the byte slice
// passed into the Reader.
func (r *Reader) LabelValues(names ...string) (StringTuples, error) {
if len(names) != 1 {
return nil, errors.Errorf("only one label name supported")
}
func (r *Reader) LabelValues(name string) (StringTuples, error) {
if r.version == FormatV1 {
e, ok := r.postingsV1[names[0]]
e, ok := r.postingsV1[name]
if !ok {
return emptyStringTuples{}, nil
}
@ -1442,7 +1438,7 @@ func (r *Reader) LabelValues(names ...string) (StringTuples, error) {
return NewStringTuples(values, 1)
}
e, ok := r.postings[names[0]]
e, ok := r.postings[name]
if !ok {
return emptyStringTuples{}, nil
}
@ -1477,13 +1473,13 @@ func (r *Reader) LabelValues(names ...string) (StringTuples, error) {
if d.Err() != nil {
return nil, errors.Wrap(d.Err(), "get postings offset entry")
}
return NewStringTuples(values, 1)
return NewStringTuples(values)
}
type emptyStringTuples struct{}
func (emptyStringTuples) At(i int) ([]string, error) { return nil, nil }
func (emptyStringTuples) Len() int { return 0 }
func (emptyStringTuples) At(i int) (string, error) { return "", nil }
func (emptyStringTuples) Len() int { return 0 }
// Series reads the series with the given ID and writes its labels and chunks into lbls and chks.
func (r *Reader) Series(id uint64, lbls *labels.Labels, chks *[]chunks.Meta) error {
@ -1626,47 +1622,24 @@ func (r *Reader) LabelNames() ([]string, error) {
}
type stringTuples struct {
length int // tuple length
entries []string // flattened tuple entries
swapBuf []string
}
func NewStringTuples(entries []string, length int) (*stringTuples, error) {
if len(entries)%length != 0 {
return nil, errors.Wrap(encoding.ErrInvalidSize, "string tuple list")
}
func NewStringTuples(entries []string) (*stringTuples, error) {
return &stringTuples{
entries: entries,
length: length,
}, nil
}
func (t *stringTuples) Len() int { return len(t.entries) / t.length }
func (t *stringTuples) At(i int) ([]string, error) { return t.entries[i : i+t.length], nil }
func (t *stringTuples) Len() int { return len(t.entries) }
func (t *stringTuples) At(i int) (string, error) { return t.entries[i], nil }
func (t *stringTuples) Swap(i, j int) {
if t.swapBuf == nil {
t.swapBuf = make([]string, t.length)
}
copy(t.swapBuf, t.entries[i:i+t.length])
for k := 0; k < t.length; k++ {
t.entries[i+k] = t.entries[j+k]
t.entries[j+k] = t.swapBuf[k]
}
t.entries[i], t.entries[j] = t.entries[j], t.entries[i]
}
func (t *stringTuples) Less(i, j int) bool {
for k := 0; k < t.length; k++ {
d := strings.Compare(t.entries[i+k], t.entries[j+k])
if d < 0 {
return true
}
if d > 0 {
return false
}
}
return false
return t.entries[i] < t.entries[j]
}
// NewStringListIterator returns a StringIter for the given sorted list of strings.

View File

@ -37,18 +37,16 @@ type series struct {
}
type mockIndex struct {
series map[uint64]series
labelIndex map[string][]string
postings map[labels.Label][]uint64
symbols map[string]struct{}
series map[uint64]series
postings map[labels.Label][]uint64
symbols map[string]struct{}
}
func newMockIndex() mockIndex {
ix := mockIndex{
series: make(map[uint64]series),
labelIndex: make(map[string][]string),
postings: make(map[labels.Label][]uint64),
symbols: make(map[string]struct{}),
series: make(map[uint64]series),
postings: make(map[labels.Label][]uint64),
symbols: make(map[string]struct{}),
}
ix.postings[allPostingsKey] = []uint64{}
return ix
@ -87,13 +85,14 @@ func (m mockIndex) Close() error {
return nil
}
func (m mockIndex) LabelValues(names ...string) (StringTuples, error) {
// TODO support composite indexes
if len(names) != 1 {
return nil, errors.New("composite indexes not supported yet")
func (m mockIndex) LabelValues(name string) (StringTuples, error) {
values := []string{}
for l := range m.postings {
if l.Name == name {
values = append(values, l.Value)
}
}
return NewStringTuples(m.labelIndex[names[0]], 1)
return NewStringTuples(values)
}
func (m mockIndex) Postings(name string, values ...string) (Postings, error) {
@ -446,8 +445,13 @@ func TestPersistence_index_e2e(t *testing.T) {
testutil.Ok(t, gotp.Err())
}
for k, v := range mi.labelIndex {
tplsExp, err := NewStringTuples(v, 1)
labelPairs := map[string][]string{}
for l := range mi.postings {
labelPairs[l.Name] = append(labelPairs[l.Name], l.Value)
}
for k, v := range labelPairs {
sort.Strings(v)
tplsExp, err := NewStringTuples(v)
testutil.Ok(t, err)
tplsRes, err := ir.LabelValues(k)

View File

@ -225,11 +225,11 @@ func (q *blockQuerier) LabelValues(name string) ([]string, error) {
res := make([]string, 0, tpls.Len())
for i := 0; i < tpls.Len(); i++ {
vals, err := tpls.At(i)
val, err := tpls.At(i)
if err != nil {
return nil, err
}
res = append(res, vals[0])
res = append(res, val)
}
return res, nil
}
@ -419,12 +419,12 @@ func postingsForMatcher(ix IndexReader, m *labels.Matcher) (index.Postings, erro
var res []string
for i := 0; i < tpls.Len(); i++ {
vals, err := tpls.At(i)
val, err := tpls.At(i)
if err != nil {
return nil, err
}
if m.Matches(vals[0]) {
res = append(res, vals[0])
if m.Matches(val) {
res = append(res, val)
}
}
@ -444,13 +444,13 @@ func inversePostingsForMatcher(ix IndexReader, m *labels.Matcher) (index.Posting
var res []string
for i := 0; i < tpls.Len(); i++ {
vals, err := tpls.At(i)
val, err := tpls.At(i)
if err != nil {
return nil, err
}
if !m.Matches(vals[0]) {
res = append(res, vals[0])
if !m.Matches(val) {
res = append(res, val)
}
}

View File

@ -250,10 +250,6 @@ func createIdxChkReaders(t *testing.T, tc []seriesSamples) (IndexReader, ChunkRe
}
}
for l, vs := range lblIdx {
testutil.Ok(t, mi.WriteLabelIndex([]string{l}, vs.slice()))
}
testutil.Ok(t, postings.Iter(func(l labels.Label, p index.Postings) error {
return mi.WritePostings(l.Name, l.Value, p)
}))
@ -1300,18 +1296,16 @@ type series struct {
}
type mockIndex struct {
series map[uint64]series
labelIndex map[string][]string
postings map[labels.Label][]uint64
symbols map[string]struct{}
series map[uint64]series
postings map[labels.Label][]uint64
symbols map[string]struct{}
}
func newMockIndex() mockIndex {
ix := mockIndex{
series: make(map[uint64]series),
labelIndex: make(map[string][]string),
postings: make(map[labels.Label][]uint64),
symbols: make(map[string]struct{}),
series: make(map[uint64]series),
postings: make(map[labels.Label][]uint64),
symbols: make(map[string]struct{}),
}
return ix
}
@ -1345,16 +1339,6 @@ func (m *mockIndex) AddSeries(ref uint64, l labels.Labels, chunks ...chunks.Meta
return nil
}
func (m mockIndex) WriteLabelIndex(names []string, values []string) error {
// TODO support composite indexes
if len(names) != 1 {
return errors.New("composite indexes not supported yet")
}
sort.Strings(values)
m.labelIndex[names[0]] = values
return nil
}
func (m mockIndex) WritePostings(name, value string, it index.Postings) error {
l := labels.Label{Name: name, Value: value}
if _, ok := m.postings[l]; ok {
@ -1372,13 +1356,14 @@ func (m mockIndex) Close() error {
return nil
}
func (m mockIndex) LabelValues(names ...string) (index.StringTuples, error) {
// TODO support composite indexes
if len(names) != 1 {
return nil, errors.New("composite indexes not supported yet")
func (m mockIndex) LabelValues(name string) (index.StringTuples, error) {
values := []string{}
for l := range m.postings {
if l.Name == name {
values = append(values, l.Value)
}
}
return index.NewStringTuples(m.labelIndex[names[0]], 1)
return index.NewStringTuples(values)
}
func (m mockIndex) Postings(name string, values ...string) (index.Postings, error) {
@ -1414,12 +1399,16 @@ func (m mockIndex) Series(ref uint64, lset *labels.Labels, chks *[]chunks.Meta)
}
func (m mockIndex) LabelNames() ([]string, error) {
labelNames := make([]string, 0, len(m.labelIndex))
for name := range m.labelIndex {
labelNames = append(labelNames, name)
names := map[string]struct{}{}
for l := range m.postings {
names[l.Name] = struct{}{}
}
sort.Strings(labelNames)
return labelNames, nil
l := make([]string, 0, len(names))
for name := range names {
l = append(l, name)
}
sort.Strings(l)
return l, nil
}
type mockSeries struct {