-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So users are forced to use the correct methods. Also adds lots of documentation
- Loading branch information
1 parent
f165342
commit 2d75b1f
Showing
4 changed files
with
100 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package staleness | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" | ||
) | ||
|
||
// Map is an abstraction over a map | ||
type Map[T any] interface { | ||
// Load the value at key. If it does not exist, the boolean will be false and the value returned will be the zero value | ||
Load(key identity.Stream) (T, bool) | ||
// Store the given key value pair in the map | ||
Store(key identity.Stream, value T) | ||
// Remove the value at key from the map | ||
Delete(key identity.Stream) | ||
// Items returns an iterator function that in future go version can be used with range | ||
// See: https://go.dev/wiki/RangefuncExperiment | ||
Items() func(yield func(identity.Stream, T) bool) bool | ||
} | ||
|
||
// RawMap implementation | ||
|
||
var _ Map[time.Time] = (*RawMap[identity.Stream, time.Time])(nil) | ||
|
||
// RawMap is an implementation of the Map interface using a standard golang map | ||
type RawMap[K comparable, V any] map[K]V | ||
|
||
func (rm *RawMap[K, V]) Load(key K) (V, bool) { | ||
value, ok := (*rm)[key] | ||
return value, ok | ||
} | ||
|
||
func (rm *RawMap[K, V]) Store(key K, value V) { | ||
(*rm)[key] = value | ||
} | ||
|
||
func (rm *RawMap[K, V]) Delete(key K) { | ||
delete(*rm, key) | ||
} | ||
|
||
func (rm *RawMap[K, V]) Items() func(yield func(K, V) bool) bool { | ||
return func(yield func(K, V) bool) bool { | ||
for k, v := range *rm { | ||
if !yield(k, v) { | ||
break | ||
} | ||
} | ||
return false | ||
} | ||
} |
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