influxdb/tsdb/index.go

111 lines
2.9 KiB
Go
Raw Normal View History

2016-09-14 14:15:23 +00:00
package tsdb
import (
2016-11-15 16:20:00 +00:00
"fmt"
"os"
2016-09-14 14:15:23 +00:00
"regexp"
2016-11-15 16:20:00 +00:00
"sort"
2016-09-14 14:15:23 +00:00
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/models"
2016-09-21 15:04:37 +00:00
"github.com/influxdata/influxdb/pkg/estimator"
2016-09-14 14:15:23 +00:00
)
type Index interface {
2016-10-21 15:48:00 +00:00
Open() error
Close() error
2016-09-29 09:39:13 +00:00
Measurement(name []byte) (*Measurement, error)
2016-09-14 14:15:23 +00:00
Measurements() (Measurements, error)
MeasurementsByExpr(expr influxql.Expr) (Measurements, bool, error)
2016-11-08 21:07:01 +00:00
MeasurementsByName(names [][]byte) ([]*Measurement, error)
2016-11-11 16:25:53 +00:00
MeasurementNamesByRegex(re *regexp.Regexp) ([][]byte, error)
2016-09-29 09:39:13 +00:00
DropMeasurement(name []byte) error
2016-09-14 14:15:23 +00:00
2016-11-10 15:45:27 +00:00
CreateSeriesIfNotExists(name []byte, tags models.Tags) error
2016-11-08 21:07:01 +00:00
DropSeries(keys [][]byte) error
2016-09-14 14:15:23 +00:00
2016-09-21 15:04:37 +00:00
SeriesN() (uint64, error)
2016-09-23 13:33:47 +00:00
SeriesSketches() (estimator.Sketch, estimator.Sketch, error)
MeasurementsSketches() (estimator.Sketch, estimator.Sketch, error)
2016-09-14 14:15:23 +00:00
2016-10-21 15:48:00 +00:00
Dereference(b []byte)
2016-11-10 15:45:27 +00:00
2016-11-15 16:20:00 +00:00
TagSets(name []byte, dimensions []string, condition influxql.Expr) ([]*influxql.TagSet, error)
2016-09-14 14:15:23 +00:00
}
2016-11-15 16:20:00 +00:00
// IndexFormat represents the format for an index.
type IndexFormat int
const (
// InMemFormat is the format used by the original in-memory shared index.
InMemFormat IndexFormat = 1
// TSI1Format is the format used by the tsi1 index.
TSI1Format IndexFormat = 2
)
// NewIndexFunc creates a new index.
type NewIndexFunc func(id uint64, path string, options IndexOptions) Index
// newIndexFuncs is a lookup of index constructors by name.
var newIndexFuncs = make(map[string]NewIndexFunc)
// RegisterIndex registers a storage index initializer by name.
func RegisterIndex(name string, fn NewIndexFunc) {
if _, ok := newIndexFuncs[name]; ok {
panic("index already registered: " + name)
}
newIndexFuncs[name] = fn
}
// RegisteredIndexs returns the slice of currently registered indexes.
func RegisteredIndexes() []string {
a := make([]string, 0, len(newIndexFuncs))
for k := range newIndexFuncs {
a = append(a, k)
}
sort.Strings(a)
return a
}
// NewIndex returns an instance of an index based on its format.
// If the path does not exist then the DefaultFormat is used.
func NewIndex(id uint64, path string, options IndexOptions) (Index, error) {
// Create a new index.
if _, err := os.Stat(path); os.IsNotExist(err) {
return newIndexFuncs[options.IndexVersion](id, path, options), nil
}
// Use default format.
format := DefaultIndex
// Lookup index by format.
fn := newIndexFuncs[format]
if fn == nil {
return nil, fmt.Errorf("invalid index format: %q", format)
}
return fn(id, path, options), nil
}
// IndexOptions represents the options used to initialize the index.
type IndexOptions struct {
IndexVersion string
ShardID uint64
InmemIndex interface{} // shared in-memory index
Config Config
}
// NewIndexOptions returns the default options.
func NewIndexOptions() IndexOptions {
return IndexOptions{
IndexVersion: DefaultIndex,
Config: NewConfig(),
}
}
// NewInmemIndex returns a new "inmem" index type.
var NewInmemIndex func(name string) (interface{}, error)