Debian Codegraph @

pdata/xpdata/entity/entity_map.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package entity // import "go.opentelemetry.io/collector/pdata/xpdata/entity"
5
6 import (
7 "iter"
8
9 "go.opentelemetry.io/collector/pdata/pcommon"
10 )
11
12 // EntityMap logically represents a map of Entity keyed by entity type.
13 //
14 // This is a reference type. If passed by value and callee modifies it, the
15 // caller will see the modification.
16 //
17 // Must use NewEntityMap function to create new instances.
18 // Important: zero-initialized instance is not valid for use.
19 type EntityMap struct {
20 refs EntityRefSlice
21 attributes pcommon.Map
22 }
23
24 // NewEntityMap creates a EntityMap with 0 elements.
25 // Can use "EnsureCapacity" to initialize with a given capacity.
26 func NewEntityMap() EntityMap {
27 return EntityMap{
28 refs: NewEntityRefSlice(),
29 attributes: pcommon.NewMap(),
30 }
31 }
32
33 // Len returns the number of elements in the map.
34 //
35 // Returns "0" for a newly instance created with "NewEntityMap()".
36 func (em EntityMap) Len() int {
37 return em.refs.Len()
38 }
39
40 // EnsureCapacity is an operation that ensures the map has at least the specified capacity.
41 // 1. If the newCap <= cap then no change in capacity.
42 // 2. If the newCap > cap then the map capacity will be expanded to equal newCap.
43 func (em EntityMap) EnsureCapacity(newCap int) {
44 em.refs.EnsureCapacity(newCap)
45 }
46
47 // Get returns the Entity associated with the entityType and true. The returned
48 // Entity is not a copy, it is a reference to the entity stored in this map.
49 // It is allowed to modify the returned entity.
50 // Such modification will be applied to the entity stored in this map.
51 //
52 // If the entityType does not exist, returns a zero-initialized Entity and false.
53 // Calling any functions on the returned invalid instance may cause a panic.
54 func (em EntityMap) Get(entityType string) (Entity, bool) {
55 if entityType == "" {
56 return Entity{}, false
57 }
58 for i := 0; i < em.Len(); i++ {
59 if em.refs.At(i).Type() == entityType {
60 return Entity{
61 ref: em.refs.At(i),
62 attributes: em.attributes,
63 }, true
64 }
65 }
66 return Entity{}, false
67 }
68
69 // All returns an iterator over entity type-Entity pairs in the EntityMap.
70 //
71 // for entityType, entity := range em.All() {
72 // ... // Do something with entity type and entity
73 // }
74 func (em EntityMap) All() iter.Seq2[string, Entity] {
75 return func(yield func(string, Entity) bool) {
76 for i := 0; i < em.Len(); i++ {
77 ref := em.refs.At(i)
78 entity := Entity{
79 ref: ref,
80 attributes: em.attributes,
81 }
82 if !yield(ref.Type(), entity) {
83 return
84 }
85 }
86 }
87 }
88
89 // Remove removes the entity associated with the entityType and returns true if the entity
90 // was present in the map, otherwise returns false. All attributes associated with the entity
91 // are also removed.
92 func (em EntityMap) Remove(entityType string) bool {
93 for i := 0; i < em.refs.Len(); i++ {
94 ref := em.refs.At(i)
95 if ref.Type() == entityType {
96 for _, k := range ref.IdKeys().All() {
97 em.attributes.Remove(k)
98 }
99 for _, k := range ref.DescriptionKeys().All() {
100 em.attributes.Remove(k)
101 }
102 em.refs.RemoveIf(func(er EntityRef) bool {
103 return er.Type() == entityType
104 })
105 return true
106 }
107 }
108 return false
109 }
110
111 // PutEmpty inserts or replaces an empty Entity with the specified type and returns it.
112 // If an entity with the given type already exists, it replaces it and removes all attributes associated with it.
113 func (em EntityMap) PutEmpty(entityType string) Entity {
114 em.Remove(entityType)
115 ref := em.refs.AppendEmpty()
116 ref.SetType(entityType)
117 return Entity{
118 ref: ref,
119 attributes: em.attributes,
120 }
121 }