influxdb/tsdb/engine.go

151 lines
3.9 KiB
Go
Raw Normal View History

2015-07-22 14:53:20 +00:00
package tsdb
import (
"bytes"
2015-07-22 14:53:20 +00:00
"errors"
"fmt"
"io"
"os"
"sort"
2015-07-22 14:53:20 +00:00
"time"
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/models"
2015-07-22 14:53:20 +00:00
)
var (
// ErrFormatNotFound is returned when no format can be determined from a path.
ErrFormatNotFound = errors.New("format not found")
// ErrUnknownEngineFormat is returned when the engine format is
// unknown. ErrUnknownEngineFormat is currently returned if a format
// other than tsm1 is encountered.
ErrUnknownEngineFormat = errors.New("unknown engine format")
2015-07-22 14:53:20 +00:00
)
// Engine represents a swappable storage engine for the shard.
type Engine interface {
Open() error
Close() error
SetLogOutput(io.Writer)
LoadMetadataIndex(shard *Shard, index *DatabaseIndex) error
2015-07-22 14:53:20 +00:00
2015-11-04 21:06:06 +00:00
CreateIterator(opt influxql.IteratorOptions) (influxql.Iterator, error)
SeriesKeys(opt influxql.IteratorOptions) (influxql.SeriesList, error)
WritePoints(points []models.Point) error
2016-04-27 19:01:07 +00:00
DeleteSeries(keys []string) error
DeleteSeriesRange(keys []string, min, max int64) error
2015-07-22 14:53:20 +00:00
DeleteMeasurement(name string, seriesKeys []string) error
SeriesCount() (n int, err error)
MeasurementFields(measurement string) *MeasurementFields
// Format will return the format for the engine
Format() EngineFormat
io.WriterTo
Backup(w io.Writer, basePath string, since time.Time) error
2015-07-22 14:53:20 +00:00
}
2016-02-10 20:04:18 +00:00
// EngineFormat represents the format for an engine.
type EngineFormat int
const (
2016-02-10 20:04:18 +00:00
// TSM1Format is the format used by the tsm1 engine.
2015-11-04 21:06:06 +00:00
TSM1Format EngineFormat = 2
)
2015-07-22 14:53:20 +00:00
// NewEngineFunc creates a new engine.
type NewEngineFunc func(path string, walPath string, options EngineOptions) Engine
2015-07-22 14:53:20 +00:00
// newEngineFuncs is a lookup of engine constructors by name.
var newEngineFuncs = make(map[string]NewEngineFunc)
// RegisterEngine registers a storage engine initializer by name.
func RegisterEngine(name string, fn NewEngineFunc) {
if _, ok := newEngineFuncs[name]; ok {
panic("engine already registered: " + name)
}
newEngineFuncs[name] = fn
}
// RegisteredEngines returns the slice of currently registered engines.
func RegisteredEngines() []string {
a := make([]string, 0, len(newEngineFuncs))
2016-02-10 20:04:18 +00:00
for k := range newEngineFuncs {
a = append(a, k)
}
sort.Strings(a)
return a
}
2015-07-22 14:53:20 +00:00
// NewEngine returns an instance of an engine based on its format.
// If the path does not exist then the DefaultFormat is used.
func NewEngine(path string, walPath string, options EngineOptions) (Engine, error) {
2015-07-22 14:53:20 +00:00
// Create a new engine
if _, err := os.Stat(path); os.IsNotExist(err) {
return newEngineFuncs[options.EngineVersion](path, walPath, options), nil
2015-07-22 14:53:20 +00:00
}
2015-11-04 21:06:06 +00:00
// If it's a dir then it's a tsm1 engine
2015-11-04 21:06:06 +00:00
format := "tsm1"
2015-11-04 21:06:06 +00:00
if fi, err := os.Stat(path); err != nil {
2015-07-22 14:53:20 +00:00
return nil, err
2015-11-04 21:06:06 +00:00
} else if !fi.Mode().IsDir() {
return nil, ErrUnknownEngineFormat
2015-11-04 21:06:06 +00:00
} else {
format = "tsm1"
2015-07-22 14:53:20 +00:00
}
// Lookup engine by format.
fn := newEngineFuncs[format]
if fn == nil {
return nil, fmt.Errorf("invalid engine format: %q", format)
}
return fn(path, walPath, options), nil
2015-07-22 14:53:20 +00:00
}
// EngineOptions represents the options used to initialize the engine.
type EngineOptions struct {
EngineVersion string
2015-08-18 20:59:54 +00:00
Config Config
2015-07-22 14:53:20 +00:00
}
// NewEngineOptions returns the default options.
func NewEngineOptions() EngineOptions {
return EngineOptions{
EngineVersion: DefaultEngine,
Config: NewConfig(),
2015-07-22 14:53:20 +00:00
}
}
// DedupeEntries returns slices with unique keys (the first 8 bytes).
func DedupeEntries(a [][]byte) [][]byte {
// Convert to a map where the last slice is used.
m := make(map[string][]byte)
for _, b := range a {
m[string(b[0:8])] = b
}
// Convert map back to a slice of byte slices.
other := make([][]byte, 0, len(m))
for _, v := range m {
other = append(other, v)
}
// Sort entries.
sort.Sort(ByteSlices(other))
return other
}
2016-02-10 20:04:18 +00:00
// ByteSlices wraps a list of byte-slices for sorting.
type ByteSlices [][]byte
func (a ByteSlices) Len() int { return len(a) }
func (a ByteSlices) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByteSlices) Less(i, j int) bool { return bytes.Compare(a[i], a[j]) == -1 }