|
| 1 | +// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use |
| 2 | +// of this source code is governed by a BSD-style license that can be found in |
| 3 | +// the LICENSE file. |
| 4 | + |
| 5 | +package problemspans |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/RaduBerinde/axisds" |
| 13 | + "github.com/RaduBerinde/axisds/regiontree" |
| 14 | + "github.com/cockroachdb/crlib/crtime" |
| 15 | + "github.com/cockroachdb/pebble/internal/base" |
| 16 | +) |
| 17 | + |
| 18 | +// Set maintains a set of spans with expiration times and allows checking for |
| 19 | +// overlap against non-expired spans. |
| 20 | +// |
| 21 | +// When the spans added to the set are not overlapping, all operations are |
| 22 | +// logarithmic. |
| 23 | +// |
| 24 | +// Set is not safe for concurrent use. |
| 25 | +type Set struct { |
| 26 | + cmp base.Compare |
| 27 | + nowFn func() crtime.Mono |
| 28 | + |
| 29 | + now crtime.Mono |
| 30 | + |
| 31 | + // We use a region tree with key boundaries and the expirationTime as a |
| 32 | + // property. |
| 33 | + rt regiontree.T[axisds.Endpoint[[]byte], expirationTime] |
| 34 | +} |
| 35 | + |
| 36 | +// expirationTime of a problem span. 0 means that there is no problem span in a |
| 37 | +// region. Expiration times <= Set.now are equivalent to 0. |
| 38 | +type expirationTime crtime.Mono |
| 39 | + |
| 40 | +// Init must be called before a Set can be used. |
| 41 | +func (s *Set) Init(cmp base.Compare) { |
| 42 | + s.init(cmp, crtime.NowMono) |
| 43 | +} |
| 44 | + |
| 45 | +func (s *Set) init(cmp base.Compare, nowFn func() crtime.Mono) { |
| 46 | + *s = Set{} |
| 47 | + s.cmp = cmp |
| 48 | + s.nowFn = nowFn |
| 49 | + // The region tree supports a property equality function that "evolves" over |
| 50 | + // time, in that some properties that used to not be equal become equal. In |
| 51 | + // our case expired properties become equal to 0. |
| 52 | + // |
| 53 | + // Note that the region tree automatically removes boundaries between two |
| 54 | + // regions that have expired, even during enumeration. |
| 55 | + propEqFn := func(a, b expirationTime) bool { |
| 56 | + return a == b || |
| 57 | + crtime.Mono(a) <= s.now && crtime.Mono(b) <= s.now // Both are expired or 0. |
| 58 | + } |
| 59 | + endpointCmp := axisds.EndpointCompareFn(axisds.CompareFn[[]byte](cmp)) |
| 60 | + s.rt = regiontree.Make(endpointCmp, propEqFn) |
| 61 | +} |
| 62 | + |
| 63 | +func boundsToEndpoints(bounds base.UserKeyBounds) (start, end axisds.Endpoint[[]byte]) { |
| 64 | + start = axisds.MakeStartEndpoint(bounds.Start, axisds.Inclusive) |
| 65 | + end = axisds.MakeEndEndpoint(bounds.End.Key, axisds.InclusiveIf(bounds.End.Kind == base.Inclusive)) |
| 66 | + return start, end |
| 67 | +} |
| 68 | + |
| 69 | +// Add a span to the set. The span automatically expires after the given duration. |
| 70 | +func (s *Set) Add(bounds base.UserKeyBounds, expiration time.Duration) { |
| 71 | + s.now = s.nowFn() |
| 72 | + expTime := expirationTime(s.now + crtime.Mono(expiration)) |
| 73 | + start, end := boundsToEndpoints(bounds) |
| 74 | + s.rt.Update(start, end, func(p expirationTime) expirationTime { |
| 75 | + return max(p, expTime) |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +// Overlaps returns true if the bounds overlap with a non-expired span. |
| 80 | +func (s *Set) Overlaps(bounds base.UserKeyBounds) bool { |
| 81 | + s.now = s.nowFn() |
| 82 | + start, end := boundsToEndpoints(bounds) |
| 83 | + return s.rt.AnyWithGC(start, end, func(exp expirationTime) bool { |
| 84 | + return crtime.Mono(exp) > s.now |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +// Excise removes a span fragment from all spans in the set. Any overlapping |
| 89 | +// non-expired spans are cut accordingly. |
| 90 | +func (s *Set) Excise(bounds base.UserKeyBounds) { |
| 91 | + s.now = s.nowFn() |
| 92 | + start, end := boundsToEndpoints(bounds) |
| 93 | + s.rt.Update(start, end, func(p expirationTime) expirationTime { |
| 94 | + return 0 |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +// IsEmpty returns true if the set contains no non-expired spans. |
| 99 | +func (s *Set) IsEmpty() bool { |
| 100 | + s.now = s.nowFn() |
| 101 | + return s.rt.IsEmpty() |
| 102 | +} |
| 103 | + |
| 104 | +// String prints all active (non-expired) span fragments. |
| 105 | +func (s *Set) String() string { |
| 106 | + var buf strings.Builder |
| 107 | + s.now = s.nowFn() |
| 108 | + s.rt.EnumerateAll(func(start, end axisds.Endpoint[[]byte], prop expirationTime) bool { |
| 109 | + fmt.Fprintf(&buf, "%s expires in: %s\n", keyEndpointIntervalFormatter(start, end), time.Duration(prop)-time.Duration(s.now)) |
| 110 | + return true |
| 111 | + }) |
| 112 | + if buf.Len() == 0 { |
| 113 | + return "<empty>" |
| 114 | + } |
| 115 | + return buf.String() |
| 116 | +} |
| 117 | + |
| 118 | +var keyBoundaryFormatter axisds.BoundaryFormatter[[]byte] = func(b []byte) string { |
| 119 | + return string(b) |
| 120 | +} |
| 121 | + |
| 122 | +var keyEndpointIntervalFormatter = axisds.MakeEndpointIntervalFormatter(keyBoundaryFormatter) |
0 commit comments