forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chore) Bloom shipper: Extend
Interval
struct with utility functions (
grafana#11841) Signed-off-by: Christian Haudum <[email protected]>
- Loading branch information
Showing
10 changed files
with
150 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package bloomshipper | ||
|
||
import ( | ||
"fmt" | ||
"hash" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/common/model" | ||
|
||
v1 "github.com/grafana/loki/pkg/storage/bloom/v1" | ||
"github.com/grafana/loki/pkg/util/encoding" | ||
) | ||
|
||
// Interval defines a time range with start end end time | ||
// where the start is inclusive, the end is non-inclusive. | ||
type Interval struct { | ||
Start, End model.Time | ||
} | ||
|
||
func NewInterval(start, end model.Time) Interval { | ||
return Interval{Start: start, End: end} | ||
} | ||
|
||
func (i Interval) Hash(h hash.Hash32) error { | ||
var enc encoding.Encbuf | ||
enc.PutBE64(uint64(i.Start)) | ||
enc.PutBE64(uint64(i.End)) | ||
_, err := h.Write(enc.Get()) | ||
return errors.Wrap(err, "writing Interval") | ||
} | ||
|
||
func (i Interval) String() string { | ||
// 13 digits are enough until Sat Nov 20 2286 17:46:39 UTC | ||
return fmt.Sprintf("%013d-%013d", i.Start, i.End) | ||
} | ||
|
||
func (i Interval) Repr() string { | ||
return fmt.Sprintf("[%s, %s)", i.Start.Time().UTC(), i.End.Time().UTC()) | ||
} | ||
|
||
// Cmp returns the position of a time relative to the interval | ||
func (i Interval) Cmp(ts model.Time) v1.BoundsCheck { | ||
if ts.Before(i.Start) { | ||
return v1.Before | ||
} else if ts.After(i.End) || ts.Equal(i.End) { | ||
return v1.After | ||
} | ||
return v1.Overlap | ||
} | ||
|
||
// Overlaps returns whether the interval overlaps (partially) with the target interval | ||
func (i Interval) Overlaps(target Interval) bool { | ||
return i.Cmp(target.Start) != v1.After && i.Cmp(target.End) != v1.Before | ||
} | ||
|
||
// Within returns whether the interval is fully within the target interval | ||
func (i Interval) Within(target Interval) bool { | ||
return i.Start >= target.Start && i.End <= target.End | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package bloomshipper | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/common/model" | ||
"github.com/stretchr/testify/assert" | ||
|
||
v1 "github.com/grafana/loki/pkg/storage/bloom/v1" | ||
) | ||
|
||
func Test_Interval_String(t *testing.T) { | ||
start := model.Time(0) | ||
end := model.TimeFromUnix(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC).Unix()) | ||
interval := NewInterval(start, end) | ||
assert.Equal(t, "0000000000000-1704067200000", interval.String()) | ||
assert.Equal(t, "[1970-01-01 00:00:00 +0000 UTC, 2024-01-01 00:00:00 +0000 UTC)", interval.Repr()) | ||
} | ||
|
||
func Test_Interval_Cmp(t *testing.T) { | ||
interval := NewInterval(10, 20) | ||
assert.Equal(t, v1.Before, interval.Cmp(0)) | ||
assert.Equal(t, v1.Overlap, interval.Cmp(10)) | ||
assert.Equal(t, v1.Overlap, interval.Cmp(15)) | ||
assert.Equal(t, v1.After, interval.Cmp(20)) // End is not inclusive | ||
assert.Equal(t, v1.After, interval.Cmp(21)) | ||
} | ||
|
||
func Test_Interval_Overlap(t *testing.T) { | ||
interval := NewInterval(10, 20) | ||
assert.True(t, interval.Overlaps(Interval{Start: 5, End: 15})) | ||
assert.True(t, interval.Overlaps(Interval{Start: 15, End: 25})) | ||
assert.True(t, interval.Overlaps(Interval{Start: 10, End: 20})) | ||
assert.True(t, interval.Overlaps(Interval{Start: 5, End: 25})) | ||
assert.False(t, interval.Overlaps(Interval{Start: 1, End: 9})) | ||
assert.False(t, interval.Overlaps(Interval{Start: 20, End: 30})) // End is not inclusive | ||
assert.False(t, interval.Overlaps(Interval{Start: 25, End: 30})) | ||
} | ||
|
||
func Test_Interval_Within(t *testing.T) { | ||
target := NewInterval(10, 20) | ||
assert.False(t, NewInterval(1, 9).Within(target)) | ||
assert.False(t, NewInterval(21, 30).Within(target)) | ||
assert.True(t, NewInterval(10, 20).Within(target)) | ||
assert.True(t, NewInterval(14, 15).Within(target)) | ||
assert.False(t, NewInterval(5, 15).Within(target)) | ||
assert.False(t, NewInterval(15, 25).Within(target)) | ||
assert.False(t, NewInterval(5, 25).Within(target)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.