|
|
|
@ -21,6 +21,8 @@ import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
@ -107,10 +109,10 @@ type Stone struct {
|
|
|
|
|
intervals Intervals
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readTombstones(dir string) (memTombstones, error) {
|
|
|
|
|
func readTombstones(dir string) (*memTombstones, error) {
|
|
|
|
|
b, err := ioutil.ReadFile(filepath.Join(dir, tombstoneFilename))
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return memTombstones{}, nil
|
|
|
|
|
return NewMemTombstones(), nil
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -140,7 +142,7 @@ func readTombstones(dir string) (memTombstones, error) {
|
|
|
|
|
return nil, errors.New("checksum did not match")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stonesMap := memTombstones{}
|
|
|
|
|
stonesMap := NewMemTombstones()
|
|
|
|
|
|
|
|
|
|
for d.len() > 0 {
|
|
|
|
|
k := d.uvarint64()
|
|
|
|
@ -150,27 +152,31 @@ func readTombstones(dir string) (memTombstones, error) {
|
|
|
|
|
return nil, d.err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stonesMap.add(k, Interval{mint, maxt})
|
|
|
|
|
stonesMap.addInterval(k, Interval{mint, maxt})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stonesMap, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type memTombstones map[uint64]Intervals
|
|
|
|
|
|
|
|
|
|
var emptyTombstoneReader = memTombstones{}
|
|
|
|
|
type memTombstones struct {
|
|
|
|
|
mts map[uint64]Intervals
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EmptyTombstoneReader returns a TombstoneReader that is always empty.
|
|
|
|
|
func EmptyTombstoneReader() TombstoneReader {
|
|
|
|
|
return emptyTombstoneReader
|
|
|
|
|
func NewMemTombstones() *memTombstones {
|
|
|
|
|
return &memTombstones{mts: make(map[uint64]Intervals)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t memTombstones) Get(ref uint64) (Intervals, error) {
|
|
|
|
|
return t[ref], nil
|
|
|
|
|
func (t *memTombstones) Get(ref uint64) (Intervals, error) {
|
|
|
|
|
t.mtx.RLock()
|
|
|
|
|
defer t.mtx.RUnlock()
|
|
|
|
|
return t.mts[ref], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t memTombstones) Iter(f func(uint64, Intervals) error) error {
|
|
|
|
|
for ref, ivs := range t {
|
|
|
|
|
func (t *memTombstones) Iter(f func(uint64, Intervals) error) error {
|
|
|
|
|
t.mtx.Lock()
|
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
|
for ref, ivs := range t.mts {
|
|
|
|
|
if err := f(ref, ivs); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -178,8 +184,18 @@ func (t memTombstones) Iter(f func(uint64, Intervals) error) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t memTombstones) add(ref uint64, itv Interval) {
|
|
|
|
|
t[ref] = t[ref].add(itv)
|
|
|
|
|
// addInterval to an existing memTombstones
|
|
|
|
|
func (t *memTombstones) addInterval(ref uint64, itv Interval) {
|
|
|
|
|
t.mtx.Lock()
|
|
|
|
|
t.mts[ref] = t.mts[ref].add(itv)
|
|
|
|
|
t.mtx.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *memTombstones) put(ref uint64, itvs Intervals) *memTombstones {
|
|
|
|
|
t.mtx.Lock()
|
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
|
t.mts[ref] = itvs
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (memTombstones) Close() error {
|
|
|
|
@ -208,7 +224,7 @@ func (tr Interval) isSubrange(dranges Intervals) bool {
|
|
|
|
|
// Intervals represents a set of increasing and non-overlapping time-intervals.
|
|
|
|
|
type Intervals []Interval
|
|
|
|
|
|
|
|
|
|
// This adds the new time-range to the existing ones.
|
|
|
|
|
// add the new time-range to the existing ones.
|
|
|
|
|
// The existing ones must be sorted.
|
|
|
|
|
func (itvs Intervals) add(n Interval) Intervals {
|
|
|
|
|
for i, r := range itvs {
|
|
|
|
|