2017-05-26 08:56:31 -07:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
package tombstones
|
2017-05-16 20:06:56 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2023-11-11 10:19:50 -08:00
|
|
|
"errors"
|
2017-05-23 04:57:45 -07:00
|
|
|
"fmt"
|
2019-09-19 02:15:41 -07:00
|
|
|
"hash"
|
|
|
|
"hash/crc32"
|
2023-05-17 06:15:12 -07:00
|
|
|
"math"
|
2017-05-16 20:06:56 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-06-29 09:00:22 -07:00
|
|
|
"sort"
|
2018-06-08 04:52:01 -07:00
|
|
|
"sync"
|
2018-09-20 01:33:52 -07:00
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2019-08-13 01:34:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/encoding"
|
|
|
|
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/fileutil"
|
2017-05-16 20:06:56 -07:00
|
|
|
)
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
const TombstonesFilename = "tombstones"
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2017-05-23 04:57:45 -07:00
|
|
|
const (
|
|
|
|
// MagicTombstone is 4 bytes at the head of a tombstone file.
|
2018-11-09 03:37:02 -08:00
|
|
|
MagicTombstone = 0x0130BA30
|
2017-05-23 04:57:45 -07:00
|
|
|
|
2021-11-01 03:35:19 -07:00
|
|
|
tombstoneFormatV1 = 1
|
|
|
|
tombstoneFormatVersionSize = 1
|
|
|
|
tombstonesHeaderSize = 5
|
|
|
|
tombstonesCRCSize = 4
|
2017-05-23 04:57:45 -07:00
|
|
|
)
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
// The table gets initialized with sync.Once but may still cause a race
|
|
|
|
// with any other use of the crc32 package anywhere. Thus we initialize it
|
|
|
|
// before.
|
|
|
|
var castagnoliTable *crc32.Table
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newCRC32 initializes a CRC32 hash with a preconfigured polynomial, so the
|
|
|
|
// polynomial may be easily changed in one location at a later time, if necessary.
|
|
|
|
func newCRC32() hash.Hash32 {
|
|
|
|
return crc32.New(castagnoliTable)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader gives access to tombstone intervals by series reference.
|
|
|
|
type Reader interface {
|
2017-11-13 04:32:24 -08:00
|
|
|
// Get returns deletion intervals for the series with the given reference.
|
2021-11-06 03:10:04 -07:00
|
|
|
Get(ref storage.SeriesRef) (Intervals, error)
|
2017-10-09 06:21:46 -07:00
|
|
|
|
2017-11-13 04:32:24 -08:00
|
|
|
// Iter calls the given function for each encountered interval.
|
2021-11-06 03:10:04 -07:00
|
|
|
Iter(func(storage.SeriesRef, Intervals) error) error
|
2017-11-13 04:32:24 -08:00
|
|
|
|
2018-09-27 04:43:22 -07:00
|
|
|
// Total returns the total count of tombstones.
|
|
|
|
Total() uint64
|
|
|
|
|
2017-11-13 04:32:24 -08:00
|
|
|
// Close any underlying resources
|
2017-10-09 06:21:46 -07:00
|
|
|
Close() error
|
2017-09-04 07:08:38 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func WriteFile(logger log.Logger, dir string, tr Reader) (int64, error) {
|
|
|
|
path := filepath.Join(dir, TombstonesFilename)
|
2017-05-16 20:06:56 -07:00
|
|
|
tmp := path + ".tmp"
|
2017-08-26 09:04:00 -07:00
|
|
|
hash := newCRC32()
|
2019-06-24 08:42:29 -07:00
|
|
|
var size int
|
2017-05-16 20:06:56 -07:00
|
|
|
|
|
|
|
f, err := os.Create(tmp)
|
|
|
|
if err != nil {
|
2019-06-24 08:42:29 -07:00
|
|
|
return 0, err
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
2017-10-31 07:37:41 -07:00
|
|
|
defer func() {
|
|
|
|
if f != nil {
|
2019-04-26 01:27:36 -07:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
level.Error(logger).Log("msg", "close tmp file", "err", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(tmp); err != nil {
|
|
|
|
level.Error(logger).Log("msg", "remove tmp file", "err", err.Error())
|
2017-10-31 07:37:41 -07:00
|
|
|
}
|
|
|
|
}()
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
buf := encoding.Encbuf{B: make([]byte, 3*binary.MaxVarintLen64)}
|
|
|
|
buf.Reset()
|
2017-05-23 04:57:45 -07:00
|
|
|
// Write the meta.
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutBE32(MagicTombstone)
|
2019-06-24 08:42:29 -07:00
|
|
|
n, err := f.Write(buf.Get())
|
2017-05-23 04:57:45 -07:00
|
|
|
if err != nil {
|
2019-06-24 08:42:29 -07:00
|
|
|
return 0, err
|
2017-05-23 04:57:45 -07:00
|
|
|
}
|
2019-06-24 08:42:29 -07:00
|
|
|
size += n
|
2017-05-23 04:57:45 -07:00
|
|
|
|
2020-09-29 21:55:09 -07:00
|
|
|
bytes, err := Encode(tr)
|
|
|
|
if err != nil {
|
2023-11-11 10:19:50 -08:00
|
|
|
return 0, fmt.Errorf("encoding tombstones: %w", err)
|
2020-09-29 21:55:09 -07:00
|
|
|
}
|
2017-11-13 04:32:24 -08:00
|
|
|
|
2020-09-29 21:55:09 -07:00
|
|
|
// Ignore first byte which is the format type. We do this for compatibility.
|
2021-11-01 03:35:19 -07:00
|
|
|
if _, err := hash.Write(bytes[tombstoneFormatVersionSize:]); err != nil {
|
2023-11-11 10:19:50 -08:00
|
|
|
return 0, fmt.Errorf("calculating hash for tombstones: %w", err)
|
2020-09-29 21:55:09 -07:00
|
|
|
}
|
2017-05-24 01:12:56 -07:00
|
|
|
|
2020-09-29 21:55:09 -07:00
|
|
|
n, err = f.Write(bytes)
|
|
|
|
if err != nil {
|
2023-11-11 10:19:50 -08:00
|
|
|
return 0, fmt.Errorf("writing tombstones: %w", err)
|
2018-09-20 01:33:52 -07:00
|
|
|
}
|
2020-09-29 21:55:09 -07:00
|
|
|
size += n
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2019-06-24 08:42:29 -07:00
|
|
|
n, err = f.Write(hash.Sum(nil))
|
2017-05-16 20:06:56 -07:00
|
|
|
if err != nil {
|
2019-06-24 08:42:29 -07:00
|
|
|
return 0, err
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
2019-06-24 08:42:29 -07:00
|
|
|
size += n
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2020-10-28 08:24:58 -07:00
|
|
|
if err := f.Sync(); err != nil {
|
|
|
|
return 0, tsdb_errors.NewMulti(err, f.Close()).Err()
|
2019-04-08 05:06:40 -07:00
|
|
|
}
|
|
|
|
|
2017-10-31 07:37:41 -07:00
|
|
|
if err = f.Close(); err != nil {
|
2019-06-24 08:42:29 -07:00
|
|
|
return 0, err
|
2017-10-31 07:37:41 -07:00
|
|
|
}
|
|
|
|
f = nil
|
2019-06-24 08:42:29 -07:00
|
|
|
return int64(size), fileutil.Replace(tmp, path)
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:55:09 -07:00
|
|
|
// Encode encodes the tombstones from the reader.
|
|
|
|
// It does not attach any magic number or checksum.
|
|
|
|
func Encode(tr Reader) ([]byte, error) {
|
|
|
|
buf := encoding.Encbuf{}
|
|
|
|
buf.PutByte(tombstoneFormatV1)
|
2021-11-06 03:10:04 -07:00
|
|
|
err := tr.Iter(func(ref storage.SeriesRef, ivs Intervals) error {
|
2020-09-29 21:55:09 -07:00
|
|
|
for _, iv := range ivs {
|
2021-11-06 03:10:04 -07:00
|
|
|
buf.PutUvarint64(uint64(ref))
|
2020-09-29 21:55:09 -07:00
|
|
|
buf.PutVarint64(iv.Mint)
|
|
|
|
buf.PutVarint64(iv.Maxt)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return buf.Get(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode decodes the tombstones from the bytes
|
|
|
|
// which was encoded using the Encode method.
|
|
|
|
func Decode(b []byte) (Reader, error) {
|
|
|
|
d := &encoding.Decbuf{B: b}
|
|
|
|
if flag := d.Byte(); flag != tombstoneFormatV1 {
|
2023-11-11 10:19:50 -08:00
|
|
|
return nil, fmt.Errorf("invalid tombstone format %x", flag)
|
2020-09-29 21:55:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.Err() != nil {
|
|
|
|
return nil, d.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
stonesMap := NewMemTombstones()
|
|
|
|
for d.Len() > 0 {
|
2021-11-06 03:10:04 -07:00
|
|
|
k := storage.SeriesRef(d.Uvarint64())
|
2020-09-29 21:55:09 -07:00
|
|
|
mint := d.Varint64()
|
|
|
|
maxt := d.Varint64()
|
|
|
|
if d.Err() != nil {
|
|
|
|
return nil, d.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
stonesMap.AddInterval(k, Interval{mint, maxt})
|
|
|
|
}
|
|
|
|
return stonesMap, nil
|
|
|
|
}
|
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
// Stone holds the information on the posting and time-range
|
2017-05-16 20:06:56 -07:00
|
|
|
// that is deleted.
|
2017-05-26 08:56:31 -07:00
|
|
|
type Stone struct {
|
2021-11-06 03:10:04 -07:00
|
|
|
Ref storage.SeriesRef
|
2019-09-19 02:15:41 -07:00
|
|
|
Intervals Intervals
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func ReadTombstones(dir string) (Reader, int64, error) {
|
2022-04-27 02:24:36 -07:00
|
|
|
b, err := os.ReadFile(filepath.Join(dir, TombstonesFilename))
|
style: Replace `else if` cascades with `switch`
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.
The exceptions that I have found in our codebase are just these two:
* The `if else` is followed by an additional statement before the next
condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
used. In this case, using `switch` would require tagging the `for`
loop, which probably tips the balance.
Why are `switch` statements more readable?
For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.
I'm sure the aforemention wise coders can list even more reasons.
In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-12 07:14:31 -07:00
|
|
|
switch {
|
|
|
|
case os.IsNotExist(err):
|
2019-09-19 02:15:41 -07:00
|
|
|
return NewMemTombstones(), 0, nil
|
style: Replace `else if` cascades with `switch`
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.
The exceptions that I have found in our codebase are just these two:
* The `if else` is followed by an additional statement before the next
condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
used. In this case, using `switch` would require tagging the `for`
loop, which probably tips the balance.
Why are `switch` statements more readable?
For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.
I'm sure the aforemention wise coders can list even more reasons.
In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-12 07:14:31 -07:00
|
|
|
case err != nil:
|
2019-06-24 08:42:29 -07:00
|
|
|
return nil, 0, err
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2020-03-22 00:29:35 -07:00
|
|
|
if len(b) < tombstonesHeaderSize {
|
2023-11-11 10:22:06 -08:00
|
|
|
return nil, 0, fmt.Errorf("tombstones header: %w", encoding.ErrInvalidSize)
|
2017-05-26 08:56:31 -07:00
|
|
|
}
|
|
|
|
|
2021-11-01 03:35:19 -07:00
|
|
|
d := &encoding.Decbuf{B: b[:len(b)-tombstonesCRCSize]}
|
2019-02-22 09:11:11 -08:00
|
|
|
if mg := d.Be32(); mg != MagicTombstone {
|
2019-06-24 08:42:29 -07:00
|
|
|
return nil, 0, fmt.Errorf("invalid magic number %x", mg)
|
2017-05-23 04:57:45 -07:00
|
|
|
}
|
2017-05-26 08:56:31 -07:00
|
|
|
|
2018-01-11 04:50:04 -08:00
|
|
|
// Verify checksum.
|
2017-08-26 09:04:00 -07:00
|
|
|
hash := newCRC32()
|
2020-09-29 21:55:09 -07:00
|
|
|
// Ignore first byte which is the format type.
|
2021-11-01 03:35:19 -07:00
|
|
|
if _, err := hash.Write(d.Get()[tombstoneFormatVersionSize:]); err != nil {
|
2023-11-11 10:19:50 -08:00
|
|
|
return nil, 0, fmt.Errorf("write to hash: %w", err)
|
2017-05-24 01:12:56 -07:00
|
|
|
}
|
2021-11-01 03:35:19 -07:00
|
|
|
if binary.BigEndian.Uint32(b[len(b)-tombstonesCRCSize:]) != hash.Sum32() {
|
2019-06-24 08:42:29 -07:00
|
|
|
return nil, 0, errors.New("checksum did not match")
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:55:09 -07:00
|
|
|
if d.Err() != nil {
|
|
|
|
return nil, 0, d.Err()
|
|
|
|
}
|
2017-05-21 22:01:50 -07:00
|
|
|
|
2020-09-29 21:55:09 -07:00
|
|
|
stonesMap, err := Decode(d.Get())
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2019-06-24 08:42:29 -07:00
|
|
|
return stonesMap, int64(len(b)), nil
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2020-01-20 07:38:00 -08:00
|
|
|
type MemTombstones struct {
|
2021-11-06 03:10:04 -07:00
|
|
|
intvlGroups map[storage.SeriesRef]Intervals
|
2018-07-10 06:24:13 -07:00
|
|
|
mtx sync.RWMutex
|
2018-06-08 04:52:01 -07:00
|
|
|
}
|
2017-11-13 04:32:24 -08:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
// NewMemTombstones creates new in memory Tombstone Reader
|
2018-11-14 08:40:01 -08:00
|
|
|
// that allows adding new intervals.
|
2020-01-20 07:38:00 -08:00
|
|
|
func NewMemTombstones() *MemTombstones {
|
2021-11-06 03:10:04 -07:00
|
|
|
return &MemTombstones{intvlGroups: make(map[storage.SeriesRef]Intervals)}
|
2017-05-23 22:54:24 -07:00
|
|
|
}
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2020-01-20 07:38:00 -08:00
|
|
|
func NewTestMemTombstones(intervals []Intervals) *MemTombstones {
|
2019-09-19 02:15:41 -07:00
|
|
|
ret := NewMemTombstones()
|
|
|
|
for i, intervalsGroup := range intervals {
|
|
|
|
for _, interval := range intervalsGroup {
|
2021-11-06 03:10:04 -07:00
|
|
|
ret.AddInterval(storage.SeriesRef(i+1), interval)
|
2019-09-19 02:15:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
func (t *MemTombstones) Get(ref storage.SeriesRef) (Intervals, error) {
|
2018-06-08 04:52:01 -07:00
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
2023-05-17 06:15:12 -07:00
|
|
|
intervals, ok := t.intvlGroups[ref]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// Make a copy to avoid race.
|
|
|
|
res := make(Intervals, len(intervals))
|
|
|
|
copy(res, intervals)
|
|
|
|
return res, nil
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
func (t *MemTombstones) DeleteTombstones(refs map[storage.SeriesRef]struct{}) {
|
2021-09-15 23:50:03 -07:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
for ref := range refs {
|
|
|
|
delete(t.intvlGroups, ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *MemTombstones) TruncateBefore(beforeT int64) {
|
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
for ref, ivs := range t.intvlGroups {
|
|
|
|
i := len(ivs) - 1
|
|
|
|
for ; i >= 0; i-- {
|
|
|
|
if beforeT > ivs[i].Maxt {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ivs[i+1:]) == 0 {
|
|
|
|
delete(t.intvlGroups, ref)
|
|
|
|
} else {
|
|
|
|
newIvs := make(Intervals, len(ivs[i+1:]))
|
|
|
|
copy(newIvs, ivs[i+1:])
|
|
|
|
t.intvlGroups[ref] = newIvs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
func (t *MemTombstones) Iter(f func(storage.SeriesRef, Intervals) error) error {
|
2018-06-25 06:52:11 -07:00
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
2018-07-10 06:24:13 -07:00
|
|
|
for ref, ivs := range t.intvlGroups {
|
2017-11-13 04:32:24 -08:00
|
|
|
if err := f(ref, ivs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2020-01-20 07:38:00 -08:00
|
|
|
func (t *MemTombstones) Total() uint64 {
|
2018-09-27 04:43:22 -07:00
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
|
|
|
|
|
|
|
total := uint64(0)
|
|
|
|
for _, ivs := range t.intvlGroups {
|
|
|
|
total += uint64(len(ivs))
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
// AddInterval to an existing memTombstones.
|
2021-11-06 03:10:04 -07:00
|
|
|
func (t *MemTombstones) AddInterval(ref storage.SeriesRef, itvs ...Interval) {
|
2018-06-08 04:52:01 -07:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
2018-06-25 06:52:11 -07:00
|
|
|
for _, itv := range itvs {
|
2019-09-19 02:15:41 -07:00
|
|
|
t.intvlGroups[ref] = t.intvlGroups[ref].Add(itv)
|
2018-06-25 06:52:11 -07:00
|
|
|
}
|
2017-05-26 08:56:31 -07:00
|
|
|
}
|
|
|
|
|
2020-01-20 07:38:00 -08:00
|
|
|
func (*MemTombstones) Close() error {
|
2017-10-09 06:21:46 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
// Interval represents a single time-interval.
|
|
|
|
type Interval struct {
|
|
|
|
Mint, Maxt int64
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (tr Interval) InBounds(t int64) bool {
|
2017-08-25 01:11:46 -07:00
|
|
|
return t >= tr.Mint && t <= tr.Maxt
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (tr Interval) IsSubrange(dranges Intervals) bool {
|
2017-05-22 04:12:36 -07:00
|
|
|
for _, r := range dranges {
|
2019-09-19 02:15:41 -07:00
|
|
|
if r.InBounds(tr.Mint) && r.InBounds(tr.Maxt) {
|
2017-05-17 02:19:42 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
// Intervals represents a set of increasing and non-overlapping time-intervals.
|
|
|
|
type Intervals []Interval
|
2017-05-22 04:12:36 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
// Add the new time-range to the existing ones.
|
2017-05-21 10:50:05 -07:00
|
|
|
// The existing ones must be sorted.
|
2020-06-29 09:00:22 -07:00
|
|
|
func (in Intervals) Add(n Interval) Intervals {
|
|
|
|
if len(in) == 0 {
|
|
|
|
return append(in, n)
|
|
|
|
}
|
|
|
|
// Find min and max indexes of intervals that overlap with the new interval.
|
|
|
|
// Intervals are closed [t1, t2] and t is discreet, so if neighbour intervals are 1 step difference
|
|
|
|
// to the new one, we can merge those together.
|
2023-05-17 06:15:12 -07:00
|
|
|
mini := 0
|
|
|
|
if n.Mint != math.MinInt64 { // Avoid overflow.
|
|
|
|
mini = sort.Search(len(in), func(i int) bool { return in[i].Maxt >= n.Mint-1 })
|
|
|
|
if mini == len(in) {
|
|
|
|
return append(in, n)
|
|
|
|
}
|
2020-06-29 09:00:22 -07:00
|
|
|
}
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2023-05-17 06:15:12 -07:00
|
|
|
maxi := len(in)
|
|
|
|
if n.Maxt != math.MaxInt64 { // Avoid overflow.
|
|
|
|
maxi = sort.Search(len(in)-mini, func(i int) bool { return in[mini+i].Mint > n.Maxt+1 })
|
|
|
|
if maxi == 0 {
|
|
|
|
if mini == 0 {
|
|
|
|
return append(Intervals{n}, in...)
|
|
|
|
}
|
|
|
|
return append(in[:mini], append(Intervals{n}, in[mini:]...)...)
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 09:00:22 -07:00
|
|
|
if n.Mint < in[mini].Mint {
|
|
|
|
in[mini].Mint = n.Mint
|
|
|
|
}
|
|
|
|
in[mini].Maxt = in[maxi+mini-1].Maxt
|
|
|
|
if n.Maxt > in[mini].Maxt {
|
|
|
|
in[mini].Maxt = n.Maxt
|
|
|
|
}
|
|
|
|
return append(in[:mini+1], in[maxi+mini:]...)
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|