2016-09-02 14:52:11 +00:00
|
|
|
package tsi1
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
import (
|
2016-12-22 18:50:56 +00:00
|
|
|
"bufio"
|
2016-10-21 15:31:40 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2016-11-11 16:25:53 +00:00
|
|
|
"errors"
|
2016-12-15 15:31:18 +00:00
|
|
|
"fmt"
|
2016-10-21 15:31:40 +00:00
|
|
|
"hash/crc32"
|
2016-10-25 14:36:58 +00:00
|
|
|
"io"
|
2016-10-21 15:31:40 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
2016-11-21 16:59:23 +00:00
|
|
|
"sync"
|
2017-01-03 17:27:25 +00:00
|
|
|
"time"
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-11-28 22:12:22 +00:00
|
|
|
"github.com/influxdata/influxdb/pkg/estimator/hll"
|
|
|
|
|
2016-11-10 15:45:27 +00:00
|
|
|
"github.com/influxdata/influxdb/influxql"
|
2016-10-21 15:31:40 +00:00
|
|
|
"github.com/influxdata/influxdb/models"
|
2016-11-28 22:12:22 +00:00
|
|
|
"github.com/influxdata/influxdb/pkg/estimator"
|
2016-10-21 15:31:40 +00:00
|
|
|
"github.com/influxdata/influxdb/pkg/mmap"
|
2016-11-11 16:25:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Log errors.
|
|
|
|
var (
|
|
|
|
ErrLogEntryChecksumMismatch = errors.New("log entry checksum mismatch")
|
2016-10-21 15:31:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Log entry flag constants.
|
|
|
|
const (
|
|
|
|
LogEntrySeriesTombstoneFlag = 0x01
|
|
|
|
LogEntryMeasurementTombstoneFlag = 0x02
|
|
|
|
LogEntryTagKeyTombstoneFlag = 0x04
|
|
|
|
LogEntryTagValueTombstoneFlag = 0x08
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogFile represents an on-disk write-ahead log file.
|
|
|
|
type LogFile struct {
|
2016-12-15 15:31:18 +00:00
|
|
|
mu sync.RWMutex
|
2016-12-28 19:59:09 +00:00
|
|
|
wg sync.WaitGroup // ref count
|
|
|
|
data []byte // mmap
|
|
|
|
file *os.File // writer
|
|
|
|
w *bufio.Writer // buffered writer
|
|
|
|
buf []byte // marshaling buffer
|
2017-01-03 17:27:25 +00:00
|
|
|
|
|
|
|
size int64 // tracks current file size
|
|
|
|
modTime time.Time // tracks last time write occurred
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-11-28 22:12:22 +00:00
|
|
|
mSketch, mTSketch estimator.Sketch // Measurement sketches
|
|
|
|
sSketch, sTSketch estimator.Sketch // Series sketche
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// In-memory index.
|
2016-11-11 16:25:53 +00:00
|
|
|
mms logMeasurements
|
2016-10-21 15:31:40 +00:00
|
|
|
|
|
|
|
// Filepath to the log file.
|
2017-02-01 21:19:24 +00:00
|
|
|
path string
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogFile returns a new instance of LogFile.
|
2017-02-06 18:14:13 +00:00
|
|
|
func NewLogFile(path string) *LogFile {
|
2016-10-21 15:31:40 +00:00
|
|
|
return &LogFile{
|
2017-02-06 18:14:13 +00:00
|
|
|
path: path,
|
2016-11-28 22:12:22 +00:00
|
|
|
mms: make(logMeasurements),
|
|
|
|
mSketch: hll.NewDefaultPlus(),
|
|
|
|
mTSketch: hll.NewDefaultPlus(),
|
|
|
|
sSketch: hll.NewDefaultPlus(),
|
|
|
|
sTSketch: hll.NewDefaultPlus(),
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open reads the log from a file and validates all the checksums.
|
|
|
|
func (f *LogFile) Open() error {
|
|
|
|
if err := f.open(); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *LogFile) open() error {
|
|
|
|
// Open file for appending.
|
2017-02-01 21:19:24 +00:00
|
|
|
file, err := os.OpenFile(f.Path(), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
2016-10-21 15:31:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.file = file
|
2016-12-22 18:50:56 +00:00
|
|
|
f.w = bufio.NewWriter(f.file)
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-11-10 15:45:27 +00:00
|
|
|
// Finish opening if file is empty.
|
|
|
|
fi, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if fi.Size() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-15 15:31:18 +00:00
|
|
|
f.size = fi.Size()
|
2017-01-03 17:27:25 +00:00
|
|
|
f.modTime = fi.ModTime()
|
2016-11-10 15:45:27 +00:00
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// Open a read-only memory map of the existing data.
|
2017-02-01 21:19:24 +00:00
|
|
|
data, err := mmap.Map(f.Path())
|
2016-10-21 15:31:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.data = data
|
|
|
|
|
|
|
|
// Read log entries from mmap.
|
2017-02-06 23:02:12 +00:00
|
|
|
var n int64
|
2016-10-21 15:31:40 +00:00
|
|
|
for buf := f.data; len(buf) > 0; {
|
2017-02-06 23:02:12 +00:00
|
|
|
// Read next entry. Truncate partial writes.
|
2016-10-21 15:31:40 +00:00
|
|
|
var e LogEntry
|
2017-02-06 23:02:12 +00:00
|
|
|
if err := e.UnmarshalBinary(buf); err == io.ErrShortBuffer {
|
|
|
|
if err := file.Truncate(n); err != nil {
|
|
|
|
return err
|
|
|
|
} else if _, err := file.Seek(0, io.SeekEnd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2016-10-21 15:31:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// Execute entry against in-memory index.
|
|
|
|
f.execEntry(&e)
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// Move buffer forward.
|
2017-02-06 23:02:12 +00:00
|
|
|
n += int64(e.Size)
|
2016-10-21 15:31:40 +00:00
|
|
|
buf = buf[e.Size:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts down the file handle and mmap.
|
|
|
|
func (f *LogFile) Close() error {
|
2017-01-02 16:29:18 +00:00
|
|
|
// Wait until the file has no more references.
|
|
|
|
f.wg.Wait()
|
|
|
|
|
2016-12-22 18:50:56 +00:00
|
|
|
if f.w != nil {
|
|
|
|
f.w.Flush()
|
|
|
|
f.w = nil
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
if f.file != nil {
|
|
|
|
f.file.Close()
|
2016-10-25 14:36:58 +00:00
|
|
|
f.file = nil
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
2016-12-22 18:50:56 +00:00
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
if f.data != nil {
|
|
|
|
mmap.Unmap(f.data)
|
|
|
|
}
|
2016-11-11 16:25:53 +00:00
|
|
|
|
|
|
|
f.mms = make(logMeasurements)
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-15 17:23:58 +00:00
|
|
|
// Flush flushes buffered data to disk.
|
|
|
|
func (f *LogFile) Flush() error {
|
|
|
|
if f.w != nil {
|
|
|
|
return f.w.Flush()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-01 21:19:24 +00:00
|
|
|
// Path returns the file path.
|
|
|
|
func (f *LogFile) Path() string { return f.path }
|
|
|
|
|
|
|
|
// SetPath sets the log file's path.
|
|
|
|
func (f *LogFile) SetPath(path string) { f.path = path }
|
|
|
|
|
2016-12-28 19:59:09 +00:00
|
|
|
// Retain adds a reference count to the file.
|
|
|
|
func (f *LogFile) Retain() { f.wg.Add(1) }
|
|
|
|
|
|
|
|
// Release removes a reference count from the file.
|
|
|
|
func (f *LogFile) Release() { f.wg.Done() }
|
|
|
|
|
2017-01-03 17:27:25 +00:00
|
|
|
// Stat returns size and last modification time of the file.
|
|
|
|
func (f *LogFile) Stat() (int64, time.Time) {
|
2016-12-15 15:31:18 +00:00
|
|
|
f.mu.Lock()
|
2017-01-03 17:27:25 +00:00
|
|
|
size, modTime := f.size, f.modTime
|
2016-12-15 15:31:18 +00:00
|
|
|
f.mu.Unlock()
|
2017-01-03 17:27:25 +00:00
|
|
|
return size, modTime
|
2016-12-15 15:31:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 15:44:11 +00:00
|
|
|
// Size returns the size of the file, in bytes.
|
|
|
|
func (f *LogFile) Size() int64 {
|
|
|
|
f.mu.Lock()
|
|
|
|
v := f.size
|
|
|
|
f.mu.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2016-11-27 16:34:03 +00:00
|
|
|
// Measurement returns a measurement element.
|
|
|
|
func (f *LogFile) Measurement(name []byte) MeasurementElem {
|
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
mm, ok := f.mms[string(name)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-12 16:29:40 +00:00
|
|
|
|
2017-01-02 16:29:18 +00:00
|
|
|
return mm
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// MeasurementNames returns an ordered list of measurement names.
|
|
|
|
func (f *LogFile) MeasurementNames() []string {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
2016-12-15 15:31:18 +00:00
|
|
|
return f.measurementNames()
|
|
|
|
}
|
2016-11-21 16:59:23 +00:00
|
|
|
|
2016-12-15 15:31:18 +00:00
|
|
|
func (f *LogFile) measurementNames() []string {
|
2016-11-11 16:25:53 +00:00
|
|
|
a := make([]string, 0, len(f.mms))
|
|
|
|
for name := range f.mms {
|
|
|
|
a = append(a, name)
|
|
|
|
}
|
2017-02-16 16:39:51 +00:00
|
|
|
sort.Strings(a)
|
2016-11-11 16:25:53 +00:00
|
|
|
return a
|
2016-11-08 21:07:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// DeleteMeasurement adds a tombstone for a measurement to the log file.
|
|
|
|
func (f *LogFile) DeleteMeasurement(name []byte) error {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
e := LogEntry{Flag: LogEntryMeasurementTombstoneFlag, Name: name}
|
|
|
|
if err := f.appendEntry(&e); err != nil {
|
2016-10-21 15:31:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-11-11 16:25:53 +00:00
|
|
|
f.execEntry(&e)
|
2016-10-21 15:31:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
// TagKeySeriesIterator returns a series iterator for a tag key.
|
|
|
|
func (f *LogFile) TagKeySeriesIterator(name, key []byte) SeriesIterator {
|
2016-11-27 16:34:03 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
mm, ok := f.mms[string(name)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tk, ok := mm.tagSet[string(key)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Combine iterators across all tag keys.
|
|
|
|
itrs := make([]SeriesIterator, 0, len(tk.tagValues))
|
|
|
|
for _, tv := range tk.tagValues {
|
2016-12-15 15:31:18 +00:00
|
|
|
if len(tv.series) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-27 16:34:03 +00:00
|
|
|
itrs = append(itrs, newLogSeriesIterator(tv.series))
|
|
|
|
}
|
2016-12-15 15:31:18 +00:00
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
return MergeSeriesIterators(itrs...)
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
// TagKeyIterator returns a value iterator for a measurement.
|
|
|
|
func (f *LogFile) TagKeyIterator(name []byte) TagKeyIterator {
|
2016-11-27 20:15:32 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
mm, ok := f.mms[string(name)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-11-27 20:15:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
a := make([]logTagKey, 0, len(mm.tagSet))
|
|
|
|
for _, k := range mm.tagSet {
|
|
|
|
a = append(a, k)
|
|
|
|
}
|
|
|
|
return newLogTagKeyIterator(a)
|
|
|
|
}
|
|
|
|
|
2017-01-24 16:27:47 +00:00
|
|
|
// TagKey returns a tag key element.
|
|
|
|
func (f *LogFile) TagKey(name, key []byte) TagKeyElem {
|
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
mm, ok := f.mms[string(name)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tk, ok := mm.tagSet[string(key)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tk
|
|
|
|
}
|
|
|
|
|
2016-12-05 17:51:06 +00:00
|
|
|
// TagValue returns a tag value element.
|
|
|
|
func (f *LogFile) TagValue(name, key, value []byte) TagValueElem {
|
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
mm, ok := f.mms[string(name)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tk, ok := mm.tagSet[string(key)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tv, ok := tk.tagValues[string(value)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tv
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
// TagValueIterator returns a value iterator for a tag key.
|
|
|
|
func (f *LogFile) TagValueIterator(name, key []byte) TagValueIterator {
|
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
mm, ok := f.mms[string(name)]
|
2016-11-27 20:15:32 +00:00
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-11-27 20:15:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
tk, ok := mm.tagSet[string(key)]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2016-11-27 20:15:32 +00:00
|
|
|
}
|
2016-11-29 18:09:33 +00:00
|
|
|
return tk.TagValueIterator()
|
2016-11-27 20:15:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// DeleteTagKey adds a tombstone for a tag key to the log file.
|
|
|
|
func (f *LogFile) DeleteTagKey(name, key []byte) error {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
e := LogEntry{Flag: LogEntryTagKeyTombstoneFlag, Name: name, Tags: models.Tags{{Key: key}}}
|
|
|
|
if err := f.appendEntry(&e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.execEntry(&e)
|
|
|
|
return nil
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
// TagValueSeriesIterator returns a series iterator for a tag value.
|
|
|
|
func (f *LogFile) TagValueSeriesIterator(name, key, value []byte) SeriesIterator {
|
2016-11-27 16:34:03 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
mm, ok := f.mms[string(name)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tk, ok := mm.tagSet[string(key)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tv, ok := tk.tagValues[string(value)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-12-15 15:31:18 +00:00
|
|
|
} else if len(tv.series) == 0 {
|
|
|
|
return nil
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
2016-12-15 15:31:18 +00:00
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
return newLogSeriesIterator(tv.series)
|
2016-11-27 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// DeleteTagValue adds a tombstone for a tag value to the log file.
|
|
|
|
func (f *LogFile) DeleteTagValue(name, key, value []byte) error {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
e := LogEntry{Flag: LogEntryTagValueTombstoneFlag, Name: name, Tags: models.Tags{{Key: key, Value: value}}}
|
|
|
|
if err := f.appendEntry(&e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.execEntry(&e)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:50:56 +00:00
|
|
|
// AddSeriesList adds a list of series to the log file in bulk.
|
|
|
|
func (f *LogFile) AddSeriesList(names [][]byte, tagsSlice []models.Tags) error {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
for i := range names {
|
2017-01-25 03:13:48 +00:00
|
|
|
// The name and tags are clone to prevent a memory leak
|
|
|
|
e := LogEntry{Name: []byte(string(names[i])), Tags: tagsSlice[i].Clone()}
|
2016-12-22 18:50:56 +00:00
|
|
|
if err := f.appendEntry(&e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.execEntry(&e)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// AddSeries adds a series to the log file.
|
|
|
|
func (f *LogFile) AddSeries(name []byte, tags models.Tags) error {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2017-01-25 03:13:48 +00:00
|
|
|
// The name and tags are clone to prevent a memory leak
|
2017-02-08 18:49:03 +00:00
|
|
|
newName := make([]byte, len(name))
|
|
|
|
copy(newName, name)
|
|
|
|
|
|
|
|
e := LogEntry{Name: newName, Tags: tags.Clone()}
|
2016-11-11 16:25:53 +00:00
|
|
|
if err := f.appendEntry(&e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.execEntry(&e)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSeries adds a tombstone for a series to the log file.
|
|
|
|
func (f *LogFile) DeleteSeries(name []byte, tags models.Tags) error {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
e := LogEntry{Flag: LogEntrySeriesTombstoneFlag, Name: name, Tags: tags}
|
|
|
|
if err := f.appendEntry(&e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.execEntry(&e)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeriesN returns the total number of series in the file.
|
|
|
|
func (f *LogFile) SeriesN() (n uint64) {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
for _, mm := range f.mms {
|
|
|
|
n += uint64(len(mm.series))
|
|
|
|
}
|
|
|
|
return n
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 18:50:56 +00:00
|
|
|
// HasSeries returns flags indicating if the series exists and if it is tombstoned.
|
2017-03-21 14:44:35 +00:00
|
|
|
func (f *LogFile) HasSeries(name []byte, tags models.Tags, buf []byte) (exists, tombstoned bool) {
|
|
|
|
e := f.SeriesWithBuffer(name, tags, buf)
|
2016-12-22 18:50:56 +00:00
|
|
|
if e == nil {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
return true, e.Deleted()
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
// Series returns a series by name/tags.
|
|
|
|
func (f *LogFile) Series(name []byte, tags models.Tags) SeriesElem {
|
2017-03-21 14:44:35 +00:00
|
|
|
return f.SeriesWithBuffer(name, tags, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeriesWithBuffer returns a series by name/tags.
|
|
|
|
func (f *LogFile) SeriesWithBuffer(name []byte, tags models.Tags, buf []byte) SeriesElem {
|
|
|
|
key := AppendSeriesKey(buf[:0], name, tags)
|
|
|
|
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
2016-11-10 15:45:27 +00:00
|
|
|
mm, ok := f.mms[string(name)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
return nil
|
2016-11-10 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 14:44:35 +00:00
|
|
|
s := mm.series[string(key)]
|
|
|
|
if s == nil {
|
|
|
|
return nil
|
2016-11-10 15:45:27 +00:00
|
|
|
}
|
2017-03-21 14:44:35 +00:00
|
|
|
return s
|
2016-11-10 15:45:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// appendEntry adds a log entry to the end of the file.
|
|
|
|
func (f *LogFile) appendEntry(e *LogEntry) error {
|
|
|
|
// Marshal entry to the local buffer.
|
2016-11-21 16:59:58 +00:00
|
|
|
f.buf = appendLogEntry(f.buf[:0], e)
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// Save the size of the record.
|
|
|
|
e.Size = len(f.buf)
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// Write record to file.
|
2016-12-22 18:50:56 +00:00
|
|
|
n, err := f.w.Write(f.buf)
|
2016-12-15 15:31:18 +00:00
|
|
|
if err != nil {
|
2016-11-11 16:25:53 +00:00
|
|
|
// Move position backwards over partial entry.
|
|
|
|
// Log should be reopened if seeking cannot be completed.
|
|
|
|
if n > 0 {
|
2016-12-22 18:50:56 +00:00
|
|
|
f.w.Reset(f.file)
|
2016-11-11 16:25:53 +00:00
|
|
|
if _, err := f.file.Seek(int64(-n), os.SEEK_CUR); err != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-03 17:27:25 +00:00
|
|
|
// Update in-memory file size & modification time.
|
2016-12-15 15:31:18 +00:00
|
|
|
f.size += int64(n)
|
2017-01-03 17:27:25 +00:00
|
|
|
f.modTime = time.Now()
|
2016-11-11 16:25:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// execEntry executes a log entry against the in-memory index.
|
|
|
|
// This is done after appending and on replay of the log.
|
|
|
|
func (f *LogFile) execEntry(e *LogEntry) {
|
|
|
|
switch e.Flag {
|
|
|
|
case LogEntryMeasurementTombstoneFlag:
|
|
|
|
f.execDeleteMeasurementEntry(e)
|
|
|
|
case LogEntryTagKeyTombstoneFlag:
|
|
|
|
f.execDeleteTagKeyEntry(e)
|
|
|
|
case LogEntryTagValueTombstoneFlag:
|
|
|
|
f.execDeleteTagValueEntry(e)
|
|
|
|
default:
|
|
|
|
f.execSeriesEntry(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *LogFile) execDeleteMeasurementEntry(e *LogEntry) {
|
2017-02-08 16:00:08 +00:00
|
|
|
mm := f.createMeasurementIfNotExists(e.Name)
|
2016-11-11 16:25:53 +00:00
|
|
|
mm.deleted = true
|
2016-11-29 18:09:33 +00:00
|
|
|
mm.tagSet = make(map[string]logTagKey)
|
2017-03-21 14:44:35 +00:00
|
|
|
mm.series = make(map[string]*logSerie)
|
2016-11-28 22:12:22 +00:00
|
|
|
|
|
|
|
// Update measurement tombstone sketch.
|
|
|
|
f.mTSketch.Add(e.Name)
|
2016-11-11 16:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *LogFile) execDeleteTagKeyEntry(e *LogEntry) {
|
|
|
|
key := e.Tags[0].Key
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
mm := f.createMeasurementIfNotExists(e.Name)
|
2016-11-11 16:25:53 +00:00
|
|
|
ts := mm.createTagSetIfNotExists(key)
|
|
|
|
|
|
|
|
ts.deleted = true
|
|
|
|
|
|
|
|
mm.tagSet[string(key)] = ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *LogFile) execDeleteTagValueEntry(e *LogEntry) {
|
|
|
|
key, value := e.Tags[0].Key, e.Tags[0].Value
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
mm := f.createMeasurementIfNotExists(e.Name)
|
2016-11-11 16:25:53 +00:00
|
|
|
ts := mm.createTagSetIfNotExists(key)
|
|
|
|
tv := ts.createTagValueIfNotExists(value)
|
|
|
|
|
|
|
|
tv.deleted = true
|
|
|
|
|
|
|
|
ts.tagValues[string(value)] = tv
|
|
|
|
mm.tagSet[string(key)] = ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *LogFile) execSeriesEntry(e *LogEntry) {
|
2016-10-21 15:31:40 +00:00
|
|
|
// Check if series is deleted.
|
|
|
|
deleted := (e.Flag & LogEntrySeriesTombstoneFlag) != 0
|
|
|
|
|
|
|
|
// Fetch measurement.
|
2017-02-08 16:00:08 +00:00
|
|
|
mm := f.createMeasurementIfNotExists(e.Name)
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-12-05 17:51:06 +00:00
|
|
|
// Undelete measurement if it's been tombstoned previously.
|
|
|
|
if !deleted && mm.deleted {
|
|
|
|
mm.deleted = false
|
|
|
|
}
|
|
|
|
|
2016-12-26 17:17:14 +00:00
|
|
|
// Generate key & series, if not exists.
|
2017-03-21 14:44:35 +00:00
|
|
|
key := AppendSeriesKey(nil, e.Name, e.Tags)
|
|
|
|
serie := mm.createSeriesIfNotExists(key, e.Name, e.Tags, deleted)
|
2016-12-26 17:17:14 +00:00
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// Save tags.
|
|
|
|
for _, t := range e.Tags {
|
2016-11-11 16:25:53 +00:00
|
|
|
ts := mm.createTagSetIfNotExists(t.Key)
|
|
|
|
tv := ts.createTagValueIfNotExists(t.Value)
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2017-02-10 18:49:03 +00:00
|
|
|
// Add a reference to the series on the tag value.
|
2017-03-21 14:44:35 +00:00
|
|
|
tv.series[string(key)] = serie
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
ts.tagValues[string(t.Value)] = tv
|
2016-10-21 15:31:40 +00:00
|
|
|
mm.tagSet[string(t.Key)] = ts
|
|
|
|
}
|
|
|
|
|
2017-02-01 13:43:37 +00:00
|
|
|
// Update the sketches.
|
2016-11-28 22:12:22 +00:00
|
|
|
if deleted {
|
2016-11-29 12:26:52 +00:00
|
|
|
// TODO(edd) decrement series count...
|
2016-12-27 16:22:15 +00:00
|
|
|
f.sTSketch.Add(key) // Deleting series so update tombstone sketch.
|
2016-11-28 22:12:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-29 12:26:52 +00:00
|
|
|
// TODO(edd) increment series count....
|
2016-12-27 16:22:15 +00:00
|
|
|
f.sSketch.Add(key) // Add series to sketch.
|
|
|
|
f.mSketch.Add(e.Name) // Add measurement to sketch as this may be the fist series for the measurement.
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 16:59:36 +00:00
|
|
|
// SeriesIterator returns an iterator over all series in the log file.
|
|
|
|
func (f *LogFile) SeriesIterator() SeriesIterator {
|
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
2017-02-10 18:49:03 +00:00
|
|
|
// Determine total series count across all measurements.
|
2016-11-28 16:59:36 +00:00
|
|
|
var n int
|
2017-02-10 18:49:03 +00:00
|
|
|
mSeriesIdx := make([]int, len(f.mms))
|
2017-03-21 14:44:35 +00:00
|
|
|
mSeries := make([][]logSerie, 0, len(f.mms))
|
2016-11-28 16:59:36 +00:00
|
|
|
for _, mm := range f.mms {
|
|
|
|
n += len(mm.series)
|
2017-03-21 14:44:35 +00:00
|
|
|
a := make([]logSerie, 0, len(mm.series))
|
|
|
|
for _, s := range mm.series {
|
|
|
|
a = append(a, *s)
|
|
|
|
}
|
|
|
|
sort.Sort(logSeries(a))
|
|
|
|
mSeries = append(mSeries, a)
|
2016-11-28 16:59:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 18:49:03 +00:00
|
|
|
// Combine series across all measurements by merging the already sorted
|
|
|
|
// series lists.
|
|
|
|
sBuffer := make([]*logSerie, len(f.mms))
|
2016-11-28 16:59:36 +00:00
|
|
|
series := make(logSeries, 0, n)
|
2017-02-10 18:49:03 +00:00
|
|
|
var (
|
|
|
|
minSerie *logSerie
|
|
|
|
minSerieIdx int
|
|
|
|
)
|
|
|
|
|
|
|
|
for s := 0; s < cap(series); s++ {
|
|
|
|
for i := 0; i < len(sBuffer); i++ {
|
|
|
|
// Are there still serie to pull from this measurement?
|
|
|
|
if mSeriesIdx[i] < len(mSeries[i]) && sBuffer[i] == nil {
|
|
|
|
// Fill the buffer slot for this measurement.
|
2017-03-21 14:44:35 +00:00
|
|
|
sBuffer[i] = &mSeries[i][mSeriesIdx[i]]
|
2017-02-10 18:49:03 +00:00
|
|
|
mSeriesIdx[i]++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does this measurement have the smallest current serie out of
|
|
|
|
// all those in the buffer?
|
|
|
|
if minSerie == nil || (sBuffer[i] != nil && sBuffer[i].Compare(minSerie.name, minSerie.tags) < 0) {
|
|
|
|
minSerie, minSerieIdx = sBuffer[i], i
|
|
|
|
}
|
2016-12-26 17:17:14 +00:00
|
|
|
}
|
2017-02-10 18:49:03 +00:00
|
|
|
series, minSerie, sBuffer[minSerieIdx] = append(series, *minSerie), nil, nil
|
2016-11-28 16:59:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 15:31:18 +00:00
|
|
|
if len(series) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-26 17:17:14 +00:00
|
|
|
return &logSeriesIterator{series: series}
|
2016-11-28 16:59:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
// createMeasurementIfNotExists returns a measurement by name.
|
|
|
|
func (f *LogFile) createMeasurementIfNotExists(name []byte) *logMeasurement {
|
2017-01-02 16:29:18 +00:00
|
|
|
mm := f.mms[string(name)]
|
|
|
|
if mm == nil {
|
|
|
|
mm = &logMeasurement{
|
2016-12-26 17:17:14 +00:00
|
|
|
name: name,
|
|
|
|
tagSet: make(map[string]logTagKey),
|
2017-03-21 14:44:35 +00:00
|
|
|
series: make(map[string]*logSerie),
|
2016-12-26 17:17:14 +00:00
|
|
|
}
|
2017-01-02 16:29:18 +00:00
|
|
|
f.mms[string(name)] = mm
|
2017-01-06 16:31:25 +00:00
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
return mm
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeasurementIterator returns an iterator over all the measurements in the file.
|
|
|
|
func (f *LogFile) MeasurementIterator() MeasurementIterator {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
2016-10-31 14:46:07 +00:00
|
|
|
var itr logMeasurementIterator
|
2016-10-21 15:31:40 +00:00
|
|
|
for _, mm := range f.mms {
|
2017-01-02 16:29:18 +00:00
|
|
|
itr.mms = append(itr.mms, *mm)
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
2016-10-31 14:46:07 +00:00
|
|
|
sort.Sort(logMeasurementSlice(itr.mms))
|
2016-10-21 15:31:40 +00:00
|
|
|
return &itr
|
|
|
|
}
|
|
|
|
|
2016-11-28 16:59:36 +00:00
|
|
|
// MeasurementSeriesIterator returns an iterator over all series for a measurement.
|
2016-11-08 21:07:01 +00:00
|
|
|
func (f *LogFile) MeasurementSeriesIterator(name []byte) SeriesIterator {
|
2016-11-21 16:59:23 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
2016-11-10 15:45:27 +00:00
|
|
|
mm := f.mms[string(name)]
|
2017-01-02 16:29:18 +00:00
|
|
|
if mm == nil || len(mm.series) == 0 {
|
2016-12-15 15:31:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-27 16:34:03 +00:00
|
|
|
return newLogSeriesIterator(mm.series)
|
2016-11-08 21:07:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 15:31:18 +00:00
|
|
|
// WriteTo compacts the log file and writes it to w.
|
|
|
|
func (f *LogFile) WriteTo(w io.Writer) (n int64, err error) {
|
2016-12-27 16:22:15 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
2016-12-15 15:31:18 +00:00
|
|
|
|
2016-12-26 17:17:14 +00:00
|
|
|
// Wrap in bufferred writer.
|
|
|
|
bw := bufio.NewWriter(w)
|
2016-10-25 14:36:58 +00:00
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
// Setup compaction offset tracking data.
|
2016-12-26 17:17:14 +00:00
|
|
|
var t IndexFileTrailer
|
2017-02-08 16:00:08 +00:00
|
|
|
info := newLogFileCompactInfo()
|
2016-10-25 14:36:58 +00:00
|
|
|
|
|
|
|
// Write magic number.
|
2016-12-26 17:17:14 +00:00
|
|
|
if err := writeTo(bw, []byte(FileSignature), &n); err != nil {
|
2016-10-25 14:36:58 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write series list.
|
2016-11-02 16:09:49 +00:00
|
|
|
t.SeriesBlock.Offset = n
|
2017-02-08 16:00:08 +00:00
|
|
|
if err := f.writeSeriesBlockTo(bw, info, &n); err != nil {
|
2016-10-25 14:36:58 +00:00
|
|
|
return n, err
|
|
|
|
}
|
2016-11-02 16:09:49 +00:00
|
|
|
t.SeriesBlock.Size = n - t.SeriesBlock.Offset
|
2016-10-25 14:36:58 +00:00
|
|
|
|
|
|
|
// Sort measurement names.
|
|
|
|
names := f.mms.names()
|
|
|
|
|
|
|
|
// Write tagset blocks in measurement order.
|
2017-02-08 16:00:08 +00:00
|
|
|
if err := f.writeTagsetsTo(bw, names, info, &n); err != nil {
|
2016-10-25 14:36:58 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write measurement block.
|
|
|
|
t.MeasurementBlock.Offset = n
|
2017-02-08 16:00:08 +00:00
|
|
|
if err := f.writeMeasurementBlockTo(bw, names, info, &n); err != nil {
|
2016-10-25 14:36:58 +00:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
t.MeasurementBlock.Size = n - t.MeasurementBlock.Offset
|
|
|
|
|
|
|
|
// Write trailer.
|
2016-12-26 17:17:14 +00:00
|
|
|
nn, err := t.WriteTo(bw)
|
2016-10-25 14:36:58 +00:00
|
|
|
n += nn
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2016-12-26 17:17:14 +00:00
|
|
|
// Flush buffer.
|
|
|
|
if err := bw.Flush(); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2016-10-25 14:36:58 +00:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
func (f *LogFile) writeSeriesBlockTo(w io.Writer, info *logFileCompactInfo, n *int64) error {
|
2016-10-25 14:36:58 +00:00
|
|
|
// Write all series.
|
2017-02-16 16:39:51 +00:00
|
|
|
enc := NewSeriesBlockEncoder(w)
|
2016-11-11 16:25:53 +00:00
|
|
|
|
|
|
|
// Retreve measurement names in order.
|
2016-12-15 15:31:18 +00:00
|
|
|
names := f.measurementNames()
|
2016-11-11 16:25:53 +00:00
|
|
|
|
2017-01-02 16:29:18 +00:00
|
|
|
// Add series from measurements.
|
2016-11-11 16:25:53 +00:00
|
|
|
for _, name := range names {
|
|
|
|
mm := f.mms[name]
|
2017-03-21 14:44:35 +00:00
|
|
|
|
|
|
|
// Sort series.
|
2017-03-21 18:33:05 +00:00
|
|
|
keys := make([][]byte, 0, len(mm.series))
|
2017-03-21 14:44:35 +00:00
|
|
|
for k := range mm.series {
|
2017-03-21 18:33:05 +00:00
|
|
|
keys = append(keys, []byte(k))
|
2017-03-21 14:44:35 +00:00
|
|
|
}
|
2017-03-21 18:33:05 +00:00
|
|
|
sort.Sort(seriesKeys(keys))
|
2017-03-21 14:44:35 +00:00
|
|
|
|
|
|
|
for _, key := range keys {
|
2017-03-21 18:33:05 +00:00
|
|
|
serie := mm.series[string(key)]
|
2017-02-16 16:39:51 +00:00
|
|
|
if err := enc.Encode(serie.name, serie.tags, serie.deleted); err != nil {
|
2016-11-11 16:25:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 16:39:51 +00:00
|
|
|
// Close and flush series list.
|
|
|
|
err := enc.Close()
|
|
|
|
*n += enc.N()
|
2016-10-25 14:36:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add series to each measurement and key/value.
|
2017-02-16 16:39:51 +00:00
|
|
|
var seriesKey []byte
|
2016-11-11 16:25:53 +00:00
|
|
|
for _, name := range names {
|
|
|
|
mm := f.mms[name]
|
2017-02-08 16:00:08 +00:00
|
|
|
mmInfo := info.createMeasurementInfoIfNotExists(name)
|
|
|
|
mmInfo.seriesIDs = make([]uint64, 0, len(mm.series))
|
2016-10-25 14:36:58 +00:00
|
|
|
|
2017-03-21 14:44:35 +00:00
|
|
|
for _, serie := range mm.series {
|
|
|
|
seriesKey = AppendSeriesKey(seriesKey[:0], serie.name, serie.tags)
|
2016-11-11 16:25:53 +00:00
|
|
|
|
|
|
|
// Lookup series offset.
|
2017-02-16 16:39:51 +00:00
|
|
|
offset := enc.Offset(seriesKey)
|
2017-02-08 16:00:08 +00:00
|
|
|
if offset == 0 {
|
2016-12-15 15:31:18 +00:00
|
|
|
panic("series not found: " + string(serie.name) + " " + serie.tags.String())
|
2016-11-11 16:25:53 +00:00
|
|
|
}
|
2016-10-25 14:36:58 +00:00
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// Add series id to measurement, tag key, and tag value.
|
2017-02-08 16:00:08 +00:00
|
|
|
mmInfo.seriesIDs = append(mmInfo.seriesIDs, offset)
|
2016-10-25 14:36:58 +00:00
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// Add series id to each tag value.
|
|
|
|
for _, tag := range serie.tags {
|
2017-02-08 16:00:08 +00:00
|
|
|
tagSetInfo := mmInfo.createTagSetInfoIfNotExists(tag.Key)
|
|
|
|
tagValueInfo := tagSetInfo.createTagValueInfoIfNotExists(tag.Value)
|
|
|
|
tagValueInfo.seriesIDs = append(tagValueInfo.seriesIDs, offset)
|
2016-11-11 16:25:53 +00:00
|
|
|
}
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
func (f *LogFile) writeTagsetsTo(w io.Writer, names []string, info *logFileCompactInfo, n *int64) error {
|
2016-10-25 14:36:58 +00:00
|
|
|
for _, name := range names {
|
2017-02-08 16:00:08 +00:00
|
|
|
if err := f.writeTagsetTo(w, name, info, n); err != nil {
|
2016-10-25 14:36:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeTagsetTo writes a single tagset to w and saves the tagset offset.
|
2017-02-08 16:00:08 +00:00
|
|
|
func (f *LogFile) writeTagsetTo(w io.Writer, name string, info *logFileCompactInfo, n *int64) error {
|
2016-10-25 14:36:58 +00:00
|
|
|
mm := f.mms[name]
|
2017-02-08 16:00:08 +00:00
|
|
|
mmInfo := info.mms[name]
|
2016-10-25 14:36:58 +00:00
|
|
|
|
2017-03-10 17:08:16 +00:00
|
|
|
enc := NewTagBlockEncoder(w)
|
|
|
|
for _, k := range mm.keys() {
|
|
|
|
tag := mm.tagSet[k]
|
|
|
|
|
|
|
|
// Encode tag. Skip values if tag is deleted.
|
|
|
|
if err := enc.EncodeKey(tag.name, tag.deleted); err != nil {
|
|
|
|
return err
|
|
|
|
} else if tag.deleted {
|
2016-10-25 14:36:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
// Lookup compaction info.
|
|
|
|
tagSetInfo := mmInfo.tagSet[k]
|
|
|
|
assert(tagSetInfo != nil, "tag set info not found")
|
|
|
|
|
2016-10-25 14:36:58 +00:00
|
|
|
// Add each value.
|
2017-02-08 16:00:08 +00:00
|
|
|
for v, value := range tag.tagValues {
|
|
|
|
tagValueInfo := tagSetInfo.tagValues[v]
|
|
|
|
sort.Sort(uint64Slice(tagValueInfo.seriesIDs))
|
2017-03-10 17:08:16 +00:00
|
|
|
|
|
|
|
if err := enc.EncodeValue(value.name, value.deleted, tagValueInfo.seriesIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save tagset offset to measurement.
|
2017-02-08 16:00:08 +00:00
|
|
|
mmInfo.offset = *n
|
2016-10-25 14:36:58 +00:00
|
|
|
|
2017-03-10 17:08:16 +00:00
|
|
|
// Flush tag block.
|
|
|
|
err := enc.Close()
|
|
|
|
*n += enc.N()
|
2016-10-25 14:36:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save tagset offset to measurement.
|
2017-02-08 16:00:08 +00:00
|
|
|
mmInfo.size = *n - mmInfo.offset
|
2016-10-25 14:36:58 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
func (f *LogFile) writeMeasurementBlockTo(w io.Writer, names []string, info *logFileCompactInfo, n *int64) error {
|
2016-10-25 14:36:58 +00:00
|
|
|
mw := NewMeasurementBlockWriter()
|
|
|
|
|
|
|
|
// Add measurement data.
|
2017-01-09 17:10:12 +00:00
|
|
|
for _, name := range names {
|
|
|
|
mm := f.mms[name]
|
2017-02-08 16:00:08 +00:00
|
|
|
mmInfo := info.mms[name]
|
|
|
|
assert(mmInfo != nil, "measurement info not found")
|
|
|
|
|
|
|
|
sort.Sort(uint64Slice(mmInfo.seriesIDs))
|
|
|
|
mw.Add(mm.name, mm.deleted, mmInfo.offset, mmInfo.size, mmInfo.seriesIDs)
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:43:37 +00:00
|
|
|
// Flush data to writer.
|
2016-10-25 14:36:58 +00:00
|
|
|
nn, err := mw.WriteTo(w)
|
|
|
|
*n += nn
|
2017-02-01 13:43:37 +00:00
|
|
|
return err
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
// logFileCompactInfo is a context object to track compaction position info.
|
|
|
|
type logFileCompactInfo struct {
|
|
|
|
mms map[string]*logFileMeasurementCompactInfo
|
|
|
|
}
|
2016-11-11 16:25:53 +00:00
|
|
|
|
2017-02-08 16:00:08 +00:00
|
|
|
// newLogFileCompactInfo returns a new instance of logFileCompactInfo.
|
|
|
|
func newLogFileCompactInfo() *logFileCompactInfo {
|
|
|
|
return &logFileCompactInfo{
|
|
|
|
mms: make(map[string]*logFileMeasurementCompactInfo),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *logFileCompactInfo) createMeasurementInfoIfNotExists(name string) *logFileMeasurementCompactInfo {
|
|
|
|
mmInfo := info.mms[name]
|
|
|
|
if mmInfo == nil {
|
|
|
|
mmInfo = &logFileMeasurementCompactInfo{
|
|
|
|
tagSet: make(map[string]*logFileTagSetCompactInfo),
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
2017-02-08 16:00:08 +00:00
|
|
|
info.mms[name] = mmInfo
|
|
|
|
}
|
|
|
|
return mmInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type logFileMeasurementCompactInfo struct {
|
|
|
|
offset int64
|
|
|
|
size int64
|
|
|
|
seriesIDs []uint64
|
|
|
|
|
|
|
|
tagSet map[string]*logFileTagSetCompactInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *logFileMeasurementCompactInfo) createTagSetInfoIfNotExists(key []byte) *logFileTagSetCompactInfo {
|
|
|
|
tagSetInfo := info.tagSet[string(key)]
|
|
|
|
if tagSetInfo == nil {
|
|
|
|
tagSetInfo = &logFileTagSetCompactInfo{tagValues: make(map[string]*logFileTagValueCompactInfo)}
|
|
|
|
info.tagSet[string(key)] = tagSetInfo
|
|
|
|
}
|
|
|
|
return tagSetInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type logFileTagSetCompactInfo struct {
|
|
|
|
tagValues map[string]*logFileTagValueCompactInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *logFileTagSetCompactInfo) createTagValueInfoIfNotExists(value []byte) *logFileTagValueCompactInfo {
|
|
|
|
tagValueInfo := info.tagValues[string(value)]
|
|
|
|
if tagValueInfo == nil {
|
|
|
|
tagValueInfo = &logFileTagValueCompactInfo{}
|
|
|
|
info.tagValues[string(value)] = tagValueInfo
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
2017-02-08 16:00:08 +00:00
|
|
|
return tagValueInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type logFileTagValueCompactInfo struct {
|
|
|
|
seriesIDs []uint64
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 15:33:30 +00:00
|
|
|
// MergeSeriesSketches merges the series sketches belonging to this LogFile
|
|
|
|
// into the provided sketches.
|
|
|
|
//
|
|
|
|
// MergeSeriesSketches is safe for concurrent use by multiple goroutines.
|
2016-12-20 15:58:06 +00:00
|
|
|
func (f *LogFile) MergeSeriesSketches(sketch, tsketch estimator.Sketch) error {
|
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
if err := sketch.Merge(f.sSketch); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-01 15:33:30 +00:00
|
|
|
return tsketch.Merge(f.sTSketch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeMeasurementsSketches merges the measurement sketches belonging to this
|
|
|
|
// LogFile into the provided sketches.
|
|
|
|
//
|
|
|
|
// MergeMeasurementsSketches is safe for concurrent use by multiple goroutines.
|
|
|
|
func (f *LogFile) MergeMeasurementsSketches(sketch, tsketch estimator.Sketch) error {
|
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
|
|
|
|
if err := sketch.Merge(f.mSketch); err != nil {
|
2016-12-20 15:58:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-01 15:33:30 +00:00
|
|
|
return tsketch.Merge(f.mTSketch)
|
2016-12-20 15:58:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// LogEntry represents a single log entry in the write-ahead log.
|
|
|
|
type LogEntry struct {
|
|
|
|
Flag byte // flag
|
|
|
|
Name []byte // measurement name
|
|
|
|
Tags models.Tags // tagset
|
|
|
|
Checksum uint32 // checksum of flag/name/tags.
|
|
|
|
Size int // total size of record, in bytes.
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary unmarshals data into e.
|
|
|
|
func (e *LogEntry) UnmarshalBinary(data []byte) error {
|
2016-11-11 16:25:53 +00:00
|
|
|
orig := data
|
2016-10-21 15:31:40 +00:00
|
|
|
start := len(data)
|
|
|
|
|
|
|
|
// Parse flag data.
|
2017-02-06 23:02:12 +00:00
|
|
|
if len(data) < 1 {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
e.Flag, data = data[0], data[1:]
|
|
|
|
|
2017-02-06 23:02:12 +00:00
|
|
|
// Parse name length.
|
|
|
|
if len(data) < 1 {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
sz, n := binary.Uvarint(data)
|
2017-02-06 23:02:12 +00:00
|
|
|
|
|
|
|
// Read name data.
|
|
|
|
if len(data) < n+int(sz) {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
e.Name, data = data[n:n+int(sz)], data[n+int(sz):]
|
|
|
|
|
|
|
|
// Parse tag count.
|
2017-02-06 23:02:12 +00:00
|
|
|
if len(data) < 1 {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
tagN, n := binary.Uvarint(data)
|
|
|
|
data = data[n:]
|
|
|
|
|
|
|
|
// Parse tags.
|
|
|
|
tags := make(models.Tags, tagN)
|
|
|
|
for i := range tags {
|
|
|
|
tag := &tags[i]
|
|
|
|
|
2017-02-06 23:02:12 +00:00
|
|
|
// Parse key length.
|
|
|
|
if len(data) < 1 {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
sz, n := binary.Uvarint(data)
|
2017-02-06 23:02:12 +00:00
|
|
|
|
|
|
|
// Read key data.
|
|
|
|
if len(data) < n+int(sz) {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
tag.Key, data = data[n:n+int(sz)], data[n+int(sz):]
|
|
|
|
|
|
|
|
// Parse value.
|
2017-02-06 23:02:12 +00:00
|
|
|
if len(data) < 1 {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
sz, n = binary.Uvarint(data)
|
2017-02-06 23:02:12 +00:00
|
|
|
|
|
|
|
// Read value data.
|
|
|
|
if len(data) < n+int(sz) {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
tag.Value, data = data[n:n+int(sz)], data[n+int(sz):]
|
|
|
|
}
|
2016-11-21 17:00:41 +00:00
|
|
|
e.Tags = tags
|
2016-10-21 15:31:40 +00:00
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// Compute checksum.
|
|
|
|
chk := crc32.ChecksumIEEE(orig[:start-len(data)])
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// Parse checksum.
|
2017-02-06 23:02:12 +00:00
|
|
|
if len(data) < 4 {
|
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2016-10-21 15:31:40 +00:00
|
|
|
e.Checksum, data = binary.BigEndian.Uint32(data[:4]), data[4:]
|
|
|
|
|
2016-11-11 16:25:53 +00:00
|
|
|
// Verify checksum.
|
|
|
|
if chk != e.Checksum {
|
|
|
|
return ErrLogEntryChecksumMismatch
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
// Save length of elem.
|
|
|
|
e.Size = start - len(data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendLogEntry appends to dst and returns the new buffer.
|
|
|
|
// This updates the checksum on the entry.
|
|
|
|
func appendLogEntry(dst []byte, e *LogEntry) []byte {
|
|
|
|
var buf [binary.MaxVarintLen64]byte
|
|
|
|
start := len(dst)
|
|
|
|
|
|
|
|
// Append flag.
|
|
|
|
dst = append(dst, e.Flag)
|
|
|
|
|
|
|
|
// Append name.
|
|
|
|
n := binary.PutUvarint(buf[:], uint64(len(e.Name)))
|
|
|
|
dst = append(dst, buf[:n]...)
|
|
|
|
dst = append(dst, e.Name...)
|
|
|
|
|
|
|
|
// Append tag count.
|
|
|
|
n = binary.PutUvarint(buf[:], uint64(len(e.Tags)))
|
|
|
|
dst = append(dst, buf[:n]...)
|
|
|
|
|
|
|
|
// Append key/value pairs.
|
|
|
|
for i := range e.Tags {
|
|
|
|
t := &e.Tags[i]
|
|
|
|
|
|
|
|
// Append key.
|
|
|
|
n := binary.PutUvarint(buf[:], uint64(len(t.Key)))
|
|
|
|
dst = append(dst, buf[:n]...)
|
|
|
|
dst = append(dst, t.Key...)
|
|
|
|
|
|
|
|
// Append value.
|
|
|
|
n = binary.PutUvarint(buf[:], uint64(len(t.Value)))
|
|
|
|
dst = append(dst, buf[:n]...)
|
|
|
|
dst = append(dst, t.Value...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate checksum.
|
|
|
|
e.Checksum = crc32.ChecksumIEEE(dst[start:])
|
|
|
|
|
|
|
|
// Append checksum.
|
|
|
|
binary.BigEndian.PutUint32(buf[:4], e.Checksum)
|
|
|
|
dst = append(dst, buf[:4]...)
|
|
|
|
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2016-10-25 14:36:58 +00:00
|
|
|
type logSerie struct {
|
|
|
|
name []byte
|
|
|
|
tags models.Tags
|
|
|
|
deleted bool
|
|
|
|
}
|
|
|
|
|
2017-02-10 18:49:03 +00:00
|
|
|
func (s *logSerie) String() string {
|
|
|
|
return fmt.Sprintf("key: %s tags: %v", s.name, s.tags)
|
|
|
|
}
|
|
|
|
|
2016-11-10 15:45:27 +00:00
|
|
|
func (s *logSerie) Name() []byte { return s.name }
|
|
|
|
func (s *logSerie) Tags() models.Tags { return s.tags }
|
|
|
|
func (s *logSerie) Deleted() bool { return s.deleted }
|
|
|
|
func (s *logSerie) Expr() influxql.Expr { return nil }
|
2017-02-10 18:49:03 +00:00
|
|
|
func (s *logSerie) Compare(name []byte, tags models.Tags) int {
|
|
|
|
if cmp := bytes.Compare(s.name, name); cmp != 0 {
|
|
|
|
return cmp
|
|
|
|
}
|
|
|
|
return models.CompareTags(s.tags, tags)
|
|
|
|
}
|
2016-11-10 15:45:27 +00:00
|
|
|
|
2016-10-25 14:36:58 +00:00
|
|
|
type logSeries []logSerie
|
|
|
|
|
|
|
|
func (a logSeries) Len() int { return len(a) }
|
|
|
|
func (a logSeries) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a logSeries) Less(i, j int) bool {
|
2017-02-10 18:49:03 +00:00
|
|
|
return a[i].Compare(a[j].name, a[j].tags) == -1
|
2016-10-25 14:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// logMeasurements represents a map of measurement names to measurements.
|
2017-01-02 16:29:18 +00:00
|
|
|
type logMeasurements map[string]*logMeasurement
|
2016-10-25 14:36:58 +00:00
|
|
|
|
|
|
|
// names returns a sorted list of measurement names.
|
|
|
|
func (m logMeasurements) names() []string {
|
|
|
|
a := make([]string, 0, len(m))
|
|
|
|
for name := range m {
|
|
|
|
a = append(a, name)
|
|
|
|
}
|
|
|
|
sort.Strings(a)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
type logMeasurement struct {
|
|
|
|
name []byte
|
2016-11-29 18:09:33 +00:00
|
|
|
tagSet map[string]logTagKey
|
2016-10-21 15:31:40 +00:00
|
|
|
deleted bool
|
2017-03-21 14:44:35 +00:00
|
|
|
series map[string]*logSerie
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
func (m *logMeasurement) Name() []byte { return m.name }
|
|
|
|
func (m *logMeasurement) Deleted() bool { return m.deleted }
|
2016-10-31 14:46:07 +00:00
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
func (m *logMeasurement) createTagSetIfNotExists(key []byte) logTagKey {
|
2016-11-11 16:25:53 +00:00
|
|
|
ts, ok := m.tagSet[string(key)]
|
|
|
|
if !ok {
|
2016-11-29 18:09:33 +00:00
|
|
|
ts = logTagKey{name: key, tagValues: make(map[string]logTagValue)}
|
2016-11-11 16:25:53 +00:00
|
|
|
}
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2017-03-21 14:44:35 +00:00
|
|
|
// createSeriesIfNotExists creates or returns an existing series on the measurement.
|
|
|
|
func (m *logMeasurement) createSeriesIfNotExists(key []byte, name []byte, tags models.Tags, deleted bool) *logSerie {
|
|
|
|
s := m.series[string(key)]
|
|
|
|
if s == nil {
|
|
|
|
s = &logSerie{name: name, tags: tags, deleted: deleted}
|
|
|
|
m.series[string(key)] = s
|
|
|
|
} else {
|
|
|
|
s.deleted = deleted
|
2017-02-10 18:49:03 +00:00
|
|
|
}
|
2017-03-21 14:44:35 +00:00
|
|
|
return s
|
2017-02-10 18:49:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 17:08:16 +00:00
|
|
|
// keys returns a sorted list of tag keys.
|
|
|
|
func (m *logMeasurement) keys() []string {
|
|
|
|
a := make([]string, 0, len(m.tagSet))
|
|
|
|
for k := range m.tagSet {
|
|
|
|
a = append(a, k)
|
|
|
|
}
|
|
|
|
sort.Strings(a)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2016-10-31 14:46:07 +00:00
|
|
|
// logMeasurementSlice is a sortable list of log measurements.
|
|
|
|
type logMeasurementSlice []logMeasurement
|
|
|
|
|
|
|
|
func (a logMeasurementSlice) Len() int { return len(a) }
|
|
|
|
func (a logMeasurementSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a logMeasurementSlice) Less(i, j int) bool { return bytes.Compare(a[i].name, a[j].name) == -1 }
|
|
|
|
|
|
|
|
// logMeasurementIterator represents an iterator over a slice of measurements.
|
|
|
|
type logMeasurementIterator struct {
|
|
|
|
mms []logMeasurement
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next element in the iterator.
|
|
|
|
func (itr *logMeasurementIterator) Next() (e MeasurementElem) {
|
|
|
|
if len(itr.mms) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e, itr.mms = &itr.mms[0], itr.mms[1:]
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
type logTagKey struct {
|
2016-10-21 15:31:40 +00:00
|
|
|
name []byte
|
|
|
|
deleted bool
|
2016-11-11 16:25:53 +00:00
|
|
|
tagValues map[string]logTagValue
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
func (tk *logTagKey) Key() []byte { return tk.name }
|
|
|
|
func (tk *logTagKey) Deleted() bool { return tk.deleted }
|
|
|
|
|
|
|
|
func (tk *logTagKey) TagValueIterator() TagValueIterator {
|
|
|
|
a := make([]logTagValue, 0, len(tk.tagValues))
|
|
|
|
for _, v := range tk.tagValues {
|
|
|
|
a = append(a, v)
|
|
|
|
}
|
|
|
|
return newLogTagValueIterator(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tk *logTagKey) createTagValueIfNotExists(value []byte) logTagValue {
|
|
|
|
tv, ok := tk.tagValues[string(value)]
|
2016-11-11 16:25:53 +00:00
|
|
|
if !ok {
|
2017-03-21 14:44:35 +00:00
|
|
|
tv = logTagValue{name: value, series: make(map[string]*logSerie)}
|
2016-11-11 16:25:53 +00:00
|
|
|
}
|
|
|
|
return tv
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
// logTagKey is a sortable list of log tag keys.
|
|
|
|
type logTagKeySlice []logTagKey
|
|
|
|
|
|
|
|
func (a logTagKeySlice) Len() int { return len(a) }
|
|
|
|
func (a logTagKeySlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a logTagKeySlice) Less(i, j int) bool { return bytes.Compare(a[i].name, a[j].name) == -1 }
|
|
|
|
|
2016-10-21 15:31:40 +00:00
|
|
|
type logTagValue struct {
|
|
|
|
name []byte
|
|
|
|
deleted bool
|
2017-03-21 14:44:35 +00:00
|
|
|
series map[string]*logSerie
|
2016-10-21 15:31:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
func (tv *logTagValue) Value() []byte { return tv.name }
|
|
|
|
func (tv *logTagValue) Deleted() bool { return tv.deleted }
|
2016-11-27 20:15:32 +00:00
|
|
|
|
|
|
|
// logTagValue is a sortable list of log tag values.
|
|
|
|
type logTagValueSlice []logTagValue
|
|
|
|
|
|
|
|
func (a logTagValueSlice) Len() int { return len(a) }
|
|
|
|
func (a logTagValueSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a logTagValueSlice) Less(i, j int) bool { return bytes.Compare(a[i].name, a[j].name) == -1 }
|
|
|
|
|
2016-11-29 18:09:33 +00:00
|
|
|
// logTagKeyIterator represents an iterator over a slice of tag keys.
|
|
|
|
type logTagKeyIterator struct {
|
|
|
|
a []logTagKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLogTagKeyIterator returns a new instance of logTagKeyIterator.
|
|
|
|
func newLogTagKeyIterator(a []logTagKey) *logTagKeyIterator {
|
|
|
|
sort.Sort(logTagKeySlice(a))
|
|
|
|
return &logTagKeyIterator{a: a}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next element in the iterator.
|
|
|
|
func (itr *logTagKeyIterator) Next() (e TagKeyElem) {
|
|
|
|
if len(itr.a) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e, itr.a = &itr.a[0], itr.a[1:]
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:15:32 +00:00
|
|
|
// logTagValueIterator represents an iterator over a slice of tag values.
|
|
|
|
type logTagValueIterator struct {
|
|
|
|
a []logTagValue
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLogTagValueIterator returns a new instance of logTagValueIterator.
|
|
|
|
func newLogTagValueIterator(a []logTagValue) *logTagValueIterator {
|
|
|
|
sort.Sort(logTagValueSlice(a))
|
|
|
|
return &logTagValueIterator{a: a}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next element in the iterator.
|
|
|
|
func (itr *logTagValueIterator) Next() (e TagValueElem) {
|
|
|
|
if len(itr.a) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e, itr.a = &itr.a[0], itr.a[1:]
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-11-10 15:45:27 +00:00
|
|
|
// logSeriesIterator represents an iterator over a slice of series.
|
|
|
|
type logSeriesIterator struct {
|
|
|
|
series logSeries
|
|
|
|
}
|
|
|
|
|
2016-11-27 16:34:03 +00:00
|
|
|
// newLogSeriesIterator returns a new instance of logSeriesIterator.
|
|
|
|
// All series are copied to the iterator.
|
2017-03-21 14:44:35 +00:00
|
|
|
func newLogSeriesIterator(m map[string]*logSerie) *logSeriesIterator {
|
|
|
|
if len(m) == 0 {
|
2016-12-15 15:31:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-21 14:44:35 +00:00
|
|
|
itr := logSeriesIterator{series: make(logSeries, 0, len(m))}
|
|
|
|
for _, s := range m {
|
|
|
|
itr.series = append(itr.series, *s)
|
2016-12-26 17:17:14 +00:00
|
|
|
}
|
2017-03-21 14:44:35 +00:00
|
|
|
sort.Sort(itr.series)
|
|
|
|
|
2016-11-27 16:34:03 +00:00
|
|
|
return &itr
|
|
|
|
}
|
|
|
|
|
2016-11-10 15:45:27 +00:00
|
|
|
// Next returns the next element in the iterator.
|
|
|
|
func (itr *logSeriesIterator) Next() (e SeriesElem) {
|
|
|
|
if len(itr.series) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e, itr.series = &itr.series[0], itr.series[1:]
|
|
|
|
return e
|
|
|
|
}
|
2016-12-15 15:31:18 +00:00
|
|
|
|
|
|
|
// FormatLogFileName generates a log filename for the given index.
|
|
|
|
func FormatLogFileName(i int) string {
|
|
|
|
return fmt.Sprintf("%08d%s", i, LogFileExt)
|
|
|
|
}
|