summaryrefslogtreecommitdiffstats
path: root/pkg/icingadb/entitiesbyid.go
blob: b40050e10c24443526065acd7cc3b4f846afa58e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package icingadb

import (
	"context"
	"github.com/icinga/icingadb/pkg/contracts"
)

// EntitiesById is a map of key-contracts.Entity pairs.
type EntitiesById map[string]contracts.Entity

// Keys returns the keys.
func (ebi EntitiesById) Keys() []string {
	keys := make([]string, 0, len(ebi))
	for k := range ebi {
		keys = append(keys, k)
	}

	return keys
}

// IDs returns the contracts.ID of the entities.
func (ebi EntitiesById) IDs() []interface{} {
	ids := make([]interface{}, 0, len(ebi))
	for _, v := range ebi {
		ids = append(ids, v.(contracts.IDer).ID())
	}

	return ids
}

// Entities streams the entities on a returned channel.
func (ebi EntitiesById) Entities(ctx context.Context) <-chan contracts.Entity {
	entities := make(chan contracts.Entity)

	go func() {
		defer close(entities)

		for _, v := range ebi {
			select {
			case <-ctx.Done():
				return
			case entities <- v:
			}
		}
	}()

	return entities
}